【问题标题】:Sending Stripe Webhooks to Controller in Grails在 Grails 中将 Stripe Webhook 发送到控制器
【发布时间】:2016-04-06 22:29:52
【问题描述】:

我正在尝试从 Stripe on Grails(2.5.1) 捕获测试网络挂钩。我在我的 URLmappings 中设置了一行,当我在运行 ngrok 的本地计算机上的浏览器上直接访问 URL 时,将执行控制器方法 ok。但是,我似乎无法将 Stripe 有效负载发送到控制器?谢谢!

 //UrlMappings.groovy
class UrlMappings {

  static mappings = {

    "/stripe-demo" (controller: 'charge', action: 'respond')

    "/$controller/$action?/$id?(.$format)?"{
        constraints {
            // apply constraints here
        }
    }


    "/"(view:"/index")
    "500"(view:'/error')


     }
 }



//Controller
package stripe.demo

class ChargeController {

def respond(String payload){

//capture the payload sent by Stripe and do something, also respond with 200.

 }

  }

【问题讨论】:

    标签: grails stripe-payments webhooks ngrok


    【解决方案1】:

    stripe webhook 数据来自request.JSON,因此将其数据绑定到这样的字符串是行不通的。这是一个例子:

    class ChargeController {
        def webHook() {
            if(request.JSON) {
                // do something with it, eg:
                switch(request.JSON.type) { }
            }
            render '' // sends nothing back but a http 200
        }
    }
    

    然后将 webhook 设置为 https://example.com/charge/webHook(注意,它必须可访问以进行条带化,这样 localhost 将无法工作)

    【讨论】:

    • 感谢 request.JSON 提示!我不太清楚您将 webhook 设置为 example.com 是什么意思? (我正在使用 ngrok 并转发到 localhost:8080 所以问题可能是 ngrok 没有正确转发。)
    • example.com 就是这样,一个示例(使用您的实际域),但听起来您已经解决了转发问题。你总是可以直接用 curl curl -H "Content-Type: application/json" -d '{webookdatahere}' 发布钩子
    • 嗯,好的。谢谢!我想我必须这样做。目前在 ngrok 屏幕上出现 302 错误,因此要尝试解决该问题,否则请使用 curl 选项。非常感谢!
    猜你喜欢
    • 2022-01-17
    • 2021-09-30
    • 2019-01-17
    • 2020-11-18
    • 2019-06-18
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多