#mikeybelike - subscription events

1 messages · Page 1 of 1 (latest)

whole agate
#

What do you mean by "inline" product?

#

The best event for recurring invoice payments is invoice.paid, which would include teh subscription id in the payload

weary haven
#

by inline, I mean a product that isn't predefined in Stripe. e,g

<?php

$checkout_session = \Stripe\Checkout\Session::create([
    'customer' => $stripeCustomer,
    'line_items' => [
        [
            'price_data' => [
                'currency' => $currency->code,
                'product_data' => [
                    'name' => "Payment for $name",
                ],
                'unit_amount' => $amount * 100,
                'recurring' => $subscription ? [
                    'interval' => 'month',
                    'interval_count' => 1,
                ] : []
            ],
            'quantity' => 1,
        ]
    ],
    'mode' => $subscription ? 'subscription' : 'payment',
    'expires_at' => time() + 3600,
    'metadata' => [
        'user_id' => $user->id,
        'anon' => $anon
    ]
], $intentObjOptions);
weary haven
#

also what about if the subscription invoice is NOT paid, what event should I listen out for here?

whole agate
#

Do you mean the first invoice when starting or any future invoice? When charging automatically, payment failures produce invoice.payment_failure events.

weary haven
#

okay that's good to know. is there a difference between the first and subsequent recurring ones? Currently i've been using checkout.session.completed for the initial one is this best practice?

whole agate
#

That will only exist for the initial subscription creation

#

but yes that will correspond to your customer completing checkout, which would also indicate successful payment of the first invoice (unless you use a trial period).

weary haven
#

Okay basically the idea is I want to give user roles based on the subscription, is this a good plan:

  • Use checkout.session.completed for the initial subscription payment - award role on receipt of this webhook event
  • Listen for invoice.paid or invoice.payment_failure to decide whether to keep the users role or to remove them
  • Maybe another event to listen to if they manually cancel the subscription to remove the role? Does one exist?
whole agate
#

The cancelation would only happen if you settings/integration enabled it, and you'd get the customer.subscription.deleted event in that case

#

Be careful of payment failures and your retry logic, you may want to wait longer than just one payment failure before revoking access

#

but thats up to you

weary haven
#

Thank you, that's solid advice^

whole agate
weary haven
#

perfect, thank you for your patience!

whole agate
#

NP! happy to help

weary haven
#

would you say the above plan is good practice though?

whole agate
#

Generally yes - i'd say following the successful setup you just default to keeping it active until a future failure/cancellation

#

You cna use the billing settings to auto-cancel following all payment retry failures if you prefer, and rely on that cancellation event.

weary haven
#

awesome thanks again!

#

now is there a way I can save this thread to view quickly in future?

whole agate
#

But it might be best to copy paste the text for reference into a separate document