gpt4 book ai didi

scala - Play 框架的 Stripe Webhook 验证错误

转载 作者:行者123 更新时间:2023-12-03 19:47:56 24 4
gpt4 key购买 nike

我正在尝试使用 Play Framework 应用程序设置 strip 支付。我在设置 webhook 时遇到问题。

com.stripe.exception.SignatureVerificationException: No signatures found matching the expected signature for payload

这是我尝试构建从 strip 发送的事件时不断收到的错误。我打印出正文和签名的值,它们看起来应该是正确的。这是我用来收集 webhook 的代码。
 def webhook = Action { implicit request: Request[AnyContent] =>
println(request.headers.toMap)
val bodyOp:Option[JsValue] = request.body.asJson
val sigOp:Option[String] = request.headers.get("Stripe-Signature")
var event: Event = null
if (bodyOp.isEmpty || sigOp.isEmpty) {
WebhookController.logger.write("EMPTY BODY OR SIG body-"+bodyOp+" sig-"+sigOp,Logger.RED)
BadRequest
} else {
val body = bodyOp.get.toString
val sig = sigOp.get
println(body)
println(sig)
try {
event = Webhook.constructEvent(body, sig, "whsec_5XwS8yCNOcq1CKfhh2Dtvm8RaoaE3p7b")
val eventType: String = event.getType
eventType match {
case "customer.subscription.deleted" => deleteCustomer(event)
case "invoice.payment.succeeded" => successPayment(event)
case "invoice.payment.failed" => failedPayment(event)
case _ => WebhookController.logger.write("UNKNOWN " + event, Logger.RED)
}

Ok("")
} catch {
case e: JsonSyntaxException =>
e.printStackTrace()
WebhookController.logger.write("ERROR" + e.getMessage +" "+exceptionToString(e), Logger.RED)
BadRequest
case e: SignatureVerificationException =>
e.printStackTrace()
WebhookController.logger.write("ERROR" + e.getMessage + " "+exceptionToString(e), Logger.RED)
WebhookController.logger.write("SIG ERROR header-"+e.getSigHeader+" status code-"+e.getStatusCode,Logger.RED )
BadRequest
}
}
}

最佳答案

Karllekko 走在正确的轨道上。 Play 框架自动将其解析为导致错误的 json。 request.body.asText 不起作用,因为内容类型 header 值设置为 json。 Tolarant Text 可以工作,除了 stripe 使用 utf-8 发送他们的 webhooks 并且 tolarant 文本不使用 utf-8 解析。所以我最终不得不使用 RawBuffer 并将其转换为字符串( https://www.playframework.com/documentation/2.6.x/ScalaBodyParsers )

class WebhookController @Inject()(parsers: PlayBodyParsers) extends Controller() {

def webhook = Action(parsers.raw) { implicit request: Request[RawBuffer] =>
val bodyOp = request.body.asBytes()
val sigOp:Option[String] = request.headers.get("Stripe-Signature")
var event: Event = null
if (bodyOp.isEmpty || sigOp.isEmpty) {
BadRequest
} else {
val body = bodyOp.get.utf8String
val sig = sigOp.get
try {
event = Webhook.constructEvent(body, sig, secret)
...
...

关于scala - Play 框架的 Stripe Webhook 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com