Mira chalak
Mira chalak

Reputation: 387

stripe first time the user pays for a subscription

If this is the first time this specific customer pays for this specific subscription. I must set the cancel_at field of the subscription based on the chosen plan ID. (I don't want to set it upon the creation of the subscription). To tackle this first time , I have captured the invoice.payment_succeeded event and I did the following:

 $invoices = \Stripe\Invoice::all([
                    "limit" => 3,  //no need to get all of the invoices just checking if their number is = 1.
                    "status" => 'paid',
                    "customer" => $customer_id,
                    "subscription" => $subscription_id,
                    "expand" => ["data.charge"],
                  ]);
 if ( count($invoices->data) == 1 ) {
            //First time this user pays for this specific subscription;

Can You just tell me if it is right or if I missed anything as I am new to php as well as stripe. should I be doing count($invoices) instead of what i did ?

Upvotes: 0

Views: 421

Answers (1)

Paul Asjes
Paul Asjes

Reputation: 5847

Your code looks fine, but the only way to tell if it'll work or not is for you to run it and see if it does what you expect it to do. You can use your test mode API keys and the Stripe CLI to test the flow:

stripe trigger customer.subscription.created

This will create a test customer, a plan and a subscription. The latter will immediately create and pay an invoice, so you should get a invoice.payment_succeeded webhook event to test with.

Upvotes: 1

Related Questions