Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe: handle the first subscription and webhook event

  1. My customer join a our subscripton plan at the first time, I create a transaction (called START-transaction) and use Stripe's API to create new subscription and store subscription object return within above transaction.

  2. I also using webhook to catch Stripe's events. My bigest purpose is handle recurring payment (send notification on charge success or fail, and something for customer's interaction). I catch event invoice.payment_succeeded to detect it's a recuring billing. I create a transaction too (call RECURRENCE) and, of couse, I store event object within transaction. (base on subscription information in event invoice.payment_succceeded, I find out relative-START transaction and create a new transaction as long as RECURRENCE)

And the problem exposes, in the first time customer creates plan, there 6 event called back to my system: customer.created; charge.succeeded; invoice.created; invoice.payment_succeeded; customer.card.created; customer.subscription.created. So, my customer have 2-transactiona: 1-START and 1-RECURRENCE at the first time.

Do you suggest me any idea to remove RECURRENCE transaction?

Thank you.

like image 224
thienhaxanh2405 Avatar asked Oct 22 '16 08:10

thienhaxanh2405


People also ask

How do you handle webhook events?

Steps to receive webhooks Identify the events you want to monitor and the event payloads to parse. Create a webhook endpoint as an HTTP endpoint (URL) on your local server. Handle requests from Stripe by parsing each event object and returning 2xx response status codes.

Does Stripe retry webhooks?

In test mode, Stripe retries three times over a few hours. Webhooks can be manually retried after this time in the Dashboard, and you can also query for missed events to reconcile the data over any time period.


2 Answers

It sounds like you want to be able to tell if the invoice.payment_succeeded event you receive is for the first payment (which you already processed in your "START transaction") or not (in which case you want to process it in a "RECURRENCE transaction").

The simplest way to do this is to look at the event object's request attribute. Because the first invoice is a direct consequence of your subscription creation request, the first invoice.payment_succeeded event will have a non-null value for the request attribute. Subsequent invoices are created in the background by Stripe, and so the events will have a null value for the request attribute.

like image 149
Ywain Avatar answered Oct 27 '22 23:10

Ywain


You can use the Stripe Request's billing_reason to identify if it is a first-time subscription, a recurring payment, or an updated invoice payment. See https://stripe.com/docs/api/invoices/object#invoice_object-billing_reason

like image 22
Andrew McCombe Avatar answered Oct 27 '22 22:10

Andrew McCombe