Used mode to determine flow for checkout session (#184)

no-issue

This fixes a problem when subscribing to a Plan (Price) with a default
trial period. We also add logging to add a little more information about
which flow we're entering.

Subscriptions that are started with a trial have a `setup_intent`
present on the Checkout Session object, which was incorrectly causing us
to determine that we are in a "setup" flow and attempt to update a
customers card details.

We now use the `mode` property of the Checkout Session to determine
whether we are handling a new Subscription, or if we are in a "setup"
flow and should update the Customer's card details.
This commit is contained in:
Fabien 'egg' O'Carroll 2020-07-21 11:50:10 +02:00 committed by Fabien O'Carroll
parent 9dc22b2bb2
commit 9f1b9d6156

View File

@ -331,6 +331,7 @@ module.exports = function MembersApi({
res.writeHead(401);
return res.end();
}
common.logging.info(`Handling webhook ${event.type}`);
try {
if (event.type === 'customer.subscription.deleted') {
await stripe.handleCustomerSubscriptionDeletedWebhook(event.data.object);
@ -349,13 +350,15 @@ module.exports = function MembersApi({
}
if (event.type === 'checkout.session.completed') {
if (event.data.object.setup_intent) {
if (event.data.object.mode === 'setup') {
common.logging.info('Handling "setup" mode Checkout Session');
const setupIntent = await stripe.getSetupIntent(event.data.object.setup_intent);
const customer = await stripe.getCustomer(setupIntent.metadata.customer_id);
const member = await users.get({email: customer.email});
await stripe.handleCheckoutSetupSessionCompletedWebhook(setupIntent, member);
} else {
} else if (event.data.object.mode === 'subscription') {
common.logging.info('Handling "subscription" mode Checkout Session');
const customer = await stripe.getCustomer(event.data.object.customer, {
expand: ['subscriptions.data.default_payment_method']
});
@ -375,6 +378,8 @@ module.exports = function MembersApi({
const emailType = 'signup';
await sendEmailWithMagicLink({email: customer.email, requestedType: emailType, options: {forceEmailType: true}});
} else if (event.data.object.mode === 'payment') {
common.logging.info('Ignoring "payment" mode Checkout Session');
}
}