0
votes

My application structure is as follows:

orders table
order_id   subscription_id 
1          sub_AXXXXX
2          sub_BXXXXX

payments table
pid order_id   payment_status  date           charge_id
1    1        complete        1 May 2020      ch_XXXXX
2    1        pending         2 June 2020   
3    1        pending         3 July 2019     
4    2        complete        1 April 2020    ch_XXXXX
5    2        complete        1 May 2020       
6    2        pending         1 June 2020 
  • I know you create first create stripe customer(you get $customer->id)
  • Then create product(you get $product->id)
  • Then create plan

$plan = Plan::create(['currency' => 'usd', 'interval' => 'month', 'product' => $product->id,interval_count': 3, 'amount' => 10 ]);

  • Then I create subscription with

    "customer" => $customer->id,'plan' => $plan, 'metadata' => [ 'order_id' => $order_id, ],'prorate' => false

Question 1 How can I check from stripe for the order_id/subscription_id, how many payment charges were made success out of 3 for order_id 1.

Do I need to query all invoices for the subscription to do this. But how can you determine which month payment was made and failed ones other than looping through dates. Does stripe update interval_count for the plan anywhere if the payment is success for each month.

Question 2 How can I check if any payments failed case: If payment failed for pid 2, how can I show this.

I really do not want to use/rely web shook events for various reasons.

1

1 Answers

0
votes

I'm having some trouble fully understanding the relationship between the subscription ids and the charge ids in your tables, but I can try to answer one of the questions from an API point of view. If you have a Subscription sub_AXXX where a payment has failed i.e. the Subscription status is incomplete (failed after creation) or past_due (failed after recurring billing) you can can expand the latest_invoice [1], from there you expand the payment_intent [2] and inspect the last_payment_error [3]. That should give you information as to what happened.

[1] https://stripe.com/docs/api/subscriptions/object#subscription_object-latest_invoice

[2] https://stripe.com/docs/api/invoices/object#invoice_object-payment_intent

[3] https://stripe.com/docs/api/payment_intents/object#payment_intent_object-last_payment_error