stripe-paymentswebhooks

How to validate Stripe subscription is active and paid


First time using Stripe's API. What Stripe Webhook events should I use to ensure a user subscription is active and paid for and how should I store it in my database?

I was thinking to set up a Webhook for invoice.paid and then inserting the period_end datetime into my database and running something like

if ( $period_end < date('Y-m-d H:i:s') ) {
    //treat subscription as inactive
} else {
    //treat subscription as active
}

Is that an advisable way to ensure a subscription is active (paid for)?


Solution

  • You should use

    1. customer.subscription.updated
    2. invoice.payment_failed
    3. invoice.payment_succeeded

    You need to use customer.subscription.updated event to handle the case when first time user subscribes to your product.

    then use of invoice.payment_succeeded will inform you about the successful payment of auto payment for subscription and use of invoice.payment_failed will inform you that auto payment for your subscription is failed.

    If you want to check if your subscription is active then you can use stripe.checkout.sessions.retrieve(session_id);