mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Added debugs and improved getCustomer handling
no-issue This adds more debugs so we can follow what's happening and also adds better handing for failures when getting a customer from stripe
This commit is contained in:
parent
1c3e563ad7
commit
561493bfb2
4 changed files with 44 additions and 6 deletions
|
@ -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:
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
@ -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
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue