Solved

Help Sending Stripe Webhooks (REST API)

  • 19 September 2022
  • 7 replies
  • 95 views

Badge

I’ve been reading docs for hours and hours. I’ve read this post: https://www.revenuecat.com/docs/stripe

 

To accept stripe payments I can use a Stripe Checkout (https://stripe.com/payments/checkout) It’s a stripe payment page hosted by stripe which prevents me from having to set up my own payment server.

 

Here’s where I’m lost. After I receive a payment, I have to “Send Stripe tokens to RevenueCat”. How do I do that? I can’t figure out how to initiate a POST request from Stripe Checkout that sends the required info to Revenuecat. Here’s what I have to sent to Revenuecat…

 

curl -X POST \
https://api.revenuecat.com/v1/receipts \
-H 'Content-Type: application/json' \
-H 'X-Platform: stripe' \
-H 'Authorization: Bearer YOUR_REVENUECAT_STRIPE_APP_PUBLIC_API_KEY' \
-d '{ "app_user_id": "my_app_user_id",
"fetch_token": "sub_xxxxxxxxxx"
}'

 

Any help would be greatly appreciated. 

 

Chris

icon

Best answer by Jens 6 October 2022, 12:12

View original

7 replies

Userlevel 4
Badge +7

You can make this request to the REST API from your frontend as well. All you need is:

  • The app user ID you want to link the subscription to
  • The Stripe Checkout Session ID
  • Your Stripe public API key that you can find from the RevenueCat web application

E.g., if you’re using the fetch API, you could do the following:

const RevenueCatStripePublicAPIKey = ...
const StripeCheckoutSessionID = ...
const appUserID = ...

const options = {
method: 'POST',
headers: {
accept: 'application/json',
'Content-Type': 'application/json',
'X-Platform': 'stripe',
Authorization: 'Bearer ' + RevenueCatStripePublicAPIKey
},
body: JSON.stringify({app_user_id: appUserID, fetch_token: StripeCheckoutSessionID})
};

fetch('https://api.revenuecat.com/v1/receipts', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

 

Badge

@Jens Thank you! I’ll start working on this.

Have a great day.

Chris

You can make this request to the REST API from your frontend as well. All you need is:

  • The app user ID you want to link the subscription to
  • The Stripe Checkout Session ID
  • Your Stripe public API key that you can find from the RevenueCat web application

E.g., if you’re using the fetch API, you could do the following:

const RevenueCatStripePublicAPIKey = ...
const StripeCheckoutSessionID = ...
const appUserID = ...

const options = {
method: 'POST',
headers: {
accept: 'application/json',
'Content-Type': 'application/json',
'X-Platform': 'stripe',
Authorization: 'Bearer ' + RevenueCatStripePublicAPIKey
},
body: JSON.stringify({app_user_id: appUserID, fetch_token: StripeCheckoutSessionID})
};

fetch('https://api.revenuecat.com/v1/receipts', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

 

 

I have started to create a proof of concept with Stripe and Revenue cat as well.

@Jens , I’m also a bit stuck on the documentation here. After I’ve setup the webhook, enabling Revenue cat to receive information about Stripe subscription purchases like this. How does Revenue cat figure out the relationship between Revenue cat customer information and Stripe user/subscription?

One of (the only?) alternatives is to explicitly give Revenue cat this information. What I’ve found there is exactly what you mention above.


You can send your Stripe subscriptions to RevenueCat through the POST receipt endpoint.

 

“You can” sounds more like it is optional, but I’m not sure. Are there other alternatives? Is it possible to link them through using the Stripe customer id as Revenue cats app user id?

What worries me a bit is that the suggested solution of sending this from the client after the checkout seems a bit brittle. What happens if there is an error on the client? A network hiccup? Is that relation between Stripe and Revenue cat forever lost then?

To me it sounds like we need a more robust solution if we have to tell Revenue cat about the relationship (and I can’t really see how it could work without us giving that information to Revenue cat).

What alternatives do you see?

Userlevel 4
Badge +7

@developer Posting to the receipts endpoint is the only way to associate RevenueCat customer with Stripe subscription, currently. You can make the request from the frontend, but the better (and more reliable) solution is to make them from the backend if you have one.

@Jens

Thanks for you reply. I think we will opt in for listening on some Stripe webhook ourselves to post the a receipt to Revenue cat then.

Is there any race condition between Revenue cat receiving the first webhook event from Stripe (through the webhook integration we setup in Revenue cat) and us posting the receipt?

For example, if we listen to a Stripe event “something.happened.in.stripe” and we react on our server by posting a receipt to Revenue cat. What would happen if the Revenue cat webhook receives “customer.subscription.updated” before that? Do we end up with some inconsistent state or will Revenue cat catch up and build the correct information state once we post the receipt even if that happens later in time?

Userlevel 4
Badge +7

@developer that sounds like a feasible solution. I don’t think there’s a risk of a race condition there; the only thing that would be tricky is if the subscription renews before the receipt is posted (but even then, we would simply reflect the latest state of the subscription in RevenueCat, we might just not have the complete history. Also, that’s not going to happen as a race condition unless you have subscriptions that renew every few seconds)

@Jens , thank you very much for quick replies. I think we have all the pieces to proceed the implementation :)

 

Have a nice day.

Reply