stripe-java icon indicating copy to clipboard operation
stripe-java copied to clipboard

com.stripe.exception.SignatureVerificationException

Open georgemax99 opened this issue 6 years ago • 9 comments

My webserver is a apache tomcat java servlet webserver. I keep getting the error com.stripe.exception.SignatureVerificationException: No signatures found matching the expected signature for payload.

I have tried to get the request body with req.getReader().lines().collect(Collectors.joining()) but I still get the error. This is what the payload looks like { "id": "evt_1GxflCKnr9az9gTnSx6pa7KT", "object": "event", "api_version": "2020-03-02", "created": 1593032637, "data": { "object": { "id": "cs_test_rwrNI9q1Zw2ESPjNtuJPtTMvbcIdTfv3JBoUk1Er7h0wiSAzmuxUMeVf", "object": "checkout.session", "billing_address_collection": null, "cancel_url": "https://example.com/cancel", "client_reference_id": null, "customer": "cus_HWjDEWSjC9No1F", "customer_email": null, "livemode": false, "locale": null, "metadata": { "message": "m", "buyerName": "m", "email": "[email protected]" }, "mode": "subscription", "payment_intent": null, "payment_method_types": [ "card" ], "setup_intent": null, "shipping": null, "shipping_address_collection": null, "submit_type": null, "subscription": "sub_HWjDjtufcasHFy", "success_url": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}" } }, "livemode": false, "pending_webhooks": 1, "request": { "id": "req_BFygc4p4ANudUI", "idempotency_key": null }, "type": "checkout.session.completed"}. I am not sure how to get the rawbody the way that stripe wants me to with servlets.

I followed this documentation: https://stripe.com/docs/payments/checkout/set-up-a-subscription This error comes from the event = Webhook.constructEvent(payload, header, endpointSecret);

georgemax99 avatar Jun 24 '20 22:06 georgemax99

The body that you pass into Webhook.constructEvent must be the raw body. It looks like your body here has been parsed as JSON, which makes it impossible for stripe-java to verify its signature.

Check that your framework isn't automatically parsing the request's body, if it is there should be a setting to turn that off.

paulasjes-stripe avatar Jun 25 '20 01:06 paulasjes-stripe

@paulasjes-stripe How is the request body encoded? The webhook is sent as a post request?

georgemax99 avatar Jun 25 '20 02:06 georgemax99

The Stripe API returns JSON-encoded responses.

What framework are you using? How to get the raw body depends a lot on the framework. The Stripe docs show using the Spark framework, here's an example using the Play framework.

paulasjes-stripe avatar Jun 25 '20 03:06 paulasjes-stripe

@paulasjes-stripe I am using Apache Tomcat Java Servlets.

georgemax99 avatar Jun 25 '20 03:06 georgemax99

I'm not familiar with that framework, but your code req.getReader().lines().collect(Collectors.joining()) looks like you're manipulating the raw JSON, which won't work as stripe-java needs the raw request to verify the signature.

Here's a basic example: https://github.com/ob-stripe/example-stripe-java8-webhook/blob/master/src/main/java/com/stripe/ExampleStripeJava8Webhook.java#L38

Github issues are for bugs with the library so I'm going to close this, but please reach out to Stripe support directly if you're still having problems: https://support.stripe.com/contact

paulasjes-stripe avatar Jun 25 '20 03:06 paulasjes-stripe

@paulasjes-stripe Ok thank you.

georgemax99 avatar Jun 25 '20 04:06 georgemax99

If anyone comes here with my same problem here is how I got the raw body:

String payload = IOUtils.toString(req.getInputStream(), "UTF-8");

georgemax99 avatar Jun 25 '20 17:06 georgemax99

@georgemax99 Thanks for sharing your solution. I was encountering the same issue and your solution works perfectly!

fionnachan avatar Mar 29 '21 08:03 fionnachan

Thanks for sharing - very glad I found this bug-report.

mortensimon avatar Nov 16 '21 21:11 mortensimon