diff --git a/ghost/members-api/lib/stripe/api/createStripeRequest.js b/ghost/members-api/lib/stripe/api/createStripeRequest.js index fbaf51c2b9..8eb5dda5a6 100644 --- a/ghost/members-api/lib/stripe/api/createStripeRequest.js +++ b/ghost/members-api/lib/stripe/api/createStripeRequest.js @@ -1,12 +1,16 @@ +const debug = require('ghost-ignition').debug('stripe-request'); + module.exports = function createStripeRequest(makeRequest) { return function stripeRequest(...args) { const errorHandler = (err) => { switch (err.type) { case 'StripeCardError': // Card declined + debug('StripeCardError'); throw err; case 'RateLimitError': // Ronseal + debug('RateLimitError'); return exponentiallyBackoff(makeRequest, ...args).catch((err) => { // We do not want to recurse further if we get RateLimitError // after running the exponential backoff @@ -16,15 +20,19 @@ module.exports = function createStripeRequest(makeRequest) { return errorHandler(err); }); case 'StripeInvalidRequestError': + debug('StripeInvalidRequestError'); // Invalid params to the request throw err; case 'StripeAPIError': + debug('StripeAPIError'); // Rare internal server error from stripe throw err; case 'StripeConnectionError': + debug('StripeConnectionError'); // Weird network/https issue throw err; case 'StripeAuthenticationError': + debug('StripeAuthenticationError'); // Invalid API Key (probably) throw err; default: diff --git a/ghost/members-api/lib/stripe/api/stripeRequests.js b/ghost/members-api/lib/stripe/api/stripeRequests.js index 290dec138a..426496be71 100644 --- a/ghost/members-api/lib/stripe/api/stripeRequests.js +++ b/ghost/members-api/lib/stripe/api/stripeRequests.js @@ -1,22 +1,28 @@ +const debug = require('ghost-ignition').debug('stripe-request'); const createStripeRequest = require('./createStripeRequest'); const retrieve = createStripeRequest(function (stripe, resource, id) { + debug(`retrieve ${resource} ${id}`); return stripe[resource].retrieve(id); }); const list = createStripeRequest(function (stripe, resource, options) { + debug(`list ${resource} ${JSON.stringify(options)}`); return stripe[resource].list(options); }); const create = createStripeRequest(function (stripe, resource, object) { + debug(`create ${resource} ${JSON.stringify(object)}`); return stripe[resource].create(object); }); const update = createStripeRequest(function (stripe, resource, id, object) { + debug(`update ${resource} ${id} ${JSON.stringify(object)}`); return stripe[resource].update(id, object); }); const del = createStripeRequest(function (stripe, resource, id) { + debug(`delete ${resource} ${id}`); return stripe[resource].del(id); }); diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index 841250c10c..7cdeb01e58 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -61,8 +61,9 @@ module.exports = class StripePaymentProcessor { this.logging.warn(err); this._webhookSecret = process.env.WEBHOOK_SECRET; } - this.logging.info(this._webhookSecret); + debug(`Webhook secret set to ${this._webhookSecret}`); } catch (err) { + debug(`Error configuring ${err.message}`); return this._rejectReady(err); } @@ -79,7 +80,12 @@ module.exports = class StripePaymentProcessor { async createCheckoutSession(member, planName) { let customer; if (member) { - customer = await this._customerForMember(member); + try { + customer = await this._customerForMember(member); + } catch (err) { + debug(`Ignoring Error getting customer for checkout ${err.message}`); + customer = null; + } } else { customer = null; } @@ -105,11 +111,20 @@ module.exports = class StripePaymentProcessor { async getActiveSubscriptions(member) { const metadata = await this.storage.get(member); - const customers = await Promise.all(metadata.map((data) => { - return this.getCustomer(data.customer_id); + const customers = await Promise.all(metadata.map(async (data) => { + try { + const customer = await this.getCustomer(data.customer_id); + return customer; + } catch (err) { + debug(`Ignoring Error getting customer for active subscriptions ${err.message}`); + return null; + } })); return customers.reduce(function (subscriptions, customer) { + if (!customer) { + return subscriptions; + } if (customer.deleted) { return subscriptions; } @@ -146,6 +161,7 @@ module.exports = class StripePaymentProcessor { if (metadata.some(data => data.customer_id === customer.id)) { return; } + debug(`Attaching customer to member ${member.email} ${customer.id}`); return this.storage.set(member, metadata.concat({ customer_id: customer.id })); @@ -160,11 +176,12 @@ module.exports = class StripePaymentProcessor { if (!customer.deleted) { return customer; } - } catch (e) { - console.log(e); + } catch (err) { + debug(`Ignoring Error getting customer for member ${err.message}`); } } + debug(`Creating customer for member ${member.email}`); const customer = await create(this._stripe, 'customers', { email: member.email }); diff --git a/ghost/members-api/lib/users.js b/ghost/members-api/lib/users.js index 0a78c32652..5805b6ce55 100644 --- a/ghost/members-api/lib/users.js +++ b/ghost/members-api/lib/users.js @@ -1,3 +1,4 @@ +const debug = require('ghost-ignition').debug('users'); module.exports = function ({ sendEmailWithMagicLink, stripe, @@ -8,6 +9,7 @@ module.exports = function ({ deleteMember }) { async function get(data, options) { + debug(`get id:${data.id} email:${data.email}`); const member = await getMember(data, options); if (!member) { return member; @@ -35,6 +37,7 @@ module.exports = function ({ } async function destroy(data, options) { + debug(`destroy id:${data.id} email:${data.email}`); const member = await getMember(data, options); if (!member) { return; @@ -46,6 +49,8 @@ module.exports = function ({ } async function update(data, options) { + debug(`update id:${data.id} email:${data.email}`); + await getMember(data, options); return updateMember(data, options); } @@ -54,8 +59,10 @@ module.exports = function ({ } async function create(data, options = {}) { + debug(`create email:${data.email}`); const member = await createMember(data); if (options.sendEmail) { + debug(`create sending email to ${member.email}`); await sendEmailWithMagicLink(member.email, options.emailType); } return member;