0

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.

 }

  }
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Jay S.
  • 15
  • 5

1 Answers1

1

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)

erichelgeson
  • 2,310
  • 1
  • 16
  • 24
  • Thanks for the request.JSON tip! Im not quite sure what you mean about setting the webhook to example.com? (I am using ngrok and forwarding to localhost:8080 so perhaps the issue is that ngrok is not forwarding correctly.) – Jay S. Apr 07 '16 at 19:58
  • example.com is just that, an example (use your actual domain) but sounds like you got forwarding figured out. You could always just post the hook directly with curl `curl -H "Content-Type: application/json" -d '{webookdatahere}'` – erichelgeson Apr 07 '16 at 21:22
  • Ahh ok. Thanks! I think I'll have to do that. Currently getting a 302 error on the ngrok screen so going to try to resolve that otherwise go with the curl option. Thank you so much! – Jay S. Apr 07 '16 at 21:36