Stripe: handle the first subscription and webhook event
Asked Answered
F

2

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

Freberg answered 22/10, 2016 at 8:38 Comment(0)
E
8

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.

Eanore answered 22/10, 2016 at 11:8 Comment(5)
OMG, is it so simple???? You give light for my life, ha ha. I'm testing. Thank you.Freberg
Glad I could help! :)Eanore
This can be dangerous, because the request attribute may also be set for recurring events. For example, if someone clicks the "Pay now" button from stripe's dashboard on a recurring invoice page, the request attribute will be set.Falstaffian
@Falstaffian Clicking the "Pay now" button is the same thing as calling stripe.com/docs/api#pay_invoice, i.e. you're manually triggering the payment for the invoice. In both cases, the request attribute would have a non-null value since it's a manual request. You can check the request attribute for the invoice.created event if you want to check whether the invoice was created (not paid) as part of an ongoing subscription.Eanore
So maybe billing_reason is the best to cover all cases?Mielke
S
8

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

Suet answered 11/12, 2020 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.