Search code examples
grailsstripe-paymentswebhooksngrok

Sending Stripe Webhooks to Controller in Grails


I am trying to capture a test web hook from Stripe on Grails(2.5.1). I setup a line in my URLmappings which when going directly to the URL on a browser on my local machine running ngrok executes the controller method ok. I cannot seem to get the Stripe payload to the controller however? Thanks!

 //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.

 }

  }

Solution

  • The stripe webhook data comes in request.JSON so databinding it to a String like that won't work. Here's an example:

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

    And then set the webhook to https://example.com/charge/webHook (note, it must be accessable to stripe so localhost wont work)