From f2a7790cc9bf8a525706f5164f6c4b8d0edcb91b Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 10 Feb 2020 18:59:52 +0800 Subject: [PATCH] Added plan nickname fallback to empty string (#126) no issue - This solves a problem when connected Stripe plan doesn't have plan `nickname` filled out (possible with older versions of Stripe API) - Defaulting to empty string instead of creating a migration because SQLite doesn't support `ALTER ... MODIFY` syntax and thus knex can't altter the table that easy - "Marks the column as an alter / modify, instead of the default add. Note: This only works in .alterTable() and is not supported by SQlite or Amazon Redshift. Alter is not done incrementally over older column type so if you like to add notNull and keep the old default value, the alter statement must contain both .notNull().defaultTo(1).alter(). If one just tries to add .notNull().alter() the old default value will be dropped." (ref. https://knexjs.org/#Chainable) --- ghost/members-api/lib/stripe/index.js | 39 ++++++++++++++++----------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index 680c869b0b..3566e53b31 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -126,6 +126,8 @@ module.exports = class StripePaymentProcessor { await this._updateCustomer(member, customer); + debug(`Linking customer:${id} subscriptions`, JSON.stringify(customer.subscriptions)); + if (customer.subscriptions && customer.subscriptions.data) { for (const subscription of customer.subscriptions.data) { await this._updateSubscription(subscription); @@ -287,24 +289,29 @@ module.exports = class StripePaymentProcessor { }); return this._updateSubscription(subscriptionWithPayment); } + + const mappedSubscription = { + customer_id: subscription.customer, + + subscription_id: subscription.id, + status: subscription.status, + cancel_at_period_end: subscription.cancel_at_period_end, + current_period_end: new Date(subscription.current_period_end * 1000), + start_date: new Date(subscription.start_date * 1000), + default_payment_card_last4: payment && payment.card && payment.card.last4 || null, + + plan_id: subscription.plan.id, + plan_nickname: subscription.plan.nickname || '', // NOTE: defaulting to empty string here as migration to nullable field turned to be too much bigger problem + plan_interval: subscription.plan.interval, + plan_amount: subscription.plan.amount, + plan_currency: subscription.plan.currency + }; + debug(`Attaching subscription to customer ${subscription.customer} ${subscription.id}`); + debug(`Subscription details`, JSON.stringify(mappedSubscription)); + await this.storage.set({ - subscription: { - customer_id: subscription.customer, - - subscription_id: subscription.id, - status: subscription.status, - cancel_at_period_end: subscription.cancel_at_period_end, - current_period_end: new Date(subscription.current_period_end * 1000), - start_date: new Date(subscription.start_date * 1000), - default_payment_card_last4: payment && payment.card && payment.card.last4 || null, - - plan_id: subscription.plan.id, - plan_nickname: subscription.plan.nickname, - plan_interval: subscription.plan.interval, - plan_amount: subscription.plan.amount, - plan_currency: subscription.plan.currency - } + subscription: mappedSubscription }); }