mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 14:03:48 +03:00
Updated stripe module for the bulk importer (#196)
no-issue * Added LeakyBucket rate limiting for all Stripe requests * Added createCustomer method * Added createComplimentarySubscription method * Replaced getStripeCustomer with getCustomer * Exported createStripeCustomer & createComplimentarySubscription
This commit is contained in:
parent
8cc8cc7acc
commit
c7ea226d9e
@ -1,7 +1,16 @@
|
||||
const debug = require('ghost-ignition').debug('stripe-request');
|
||||
const LeakyBucket = require('leaky-bucket');
|
||||
const EXPECTED_API_EFFICIENCY = 0.95;
|
||||
const liveBucket = new LeakyBucket(EXPECTED_API_EFFICIENCY * 25, 1);
|
||||
const testBucket = new LeakyBucket(EXPECTED_API_EFFICIENCY * 100, 1);
|
||||
|
||||
module.exports = function createStripeRequest(makeRequest) {
|
||||
return function stripeRequest(...args) {
|
||||
return async function stripeRequest(stripe, ...args) {
|
||||
if (stripe.__TEST_MODE__) {
|
||||
await testBucket.throttle();
|
||||
} else {
|
||||
await liveBucket.throttle();
|
||||
}
|
||||
const errorHandler = (err) => {
|
||||
switch (err.type) {
|
||||
case 'StripeCardError':
|
||||
@ -39,7 +48,7 @@ module.exports = function createStripeRequest(makeRequest) {
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
return makeRequest(...args).catch(errorHandler);
|
||||
return makeRequest(stripe, ...args).catch(errorHandler);
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -13,6 +13,10 @@ module.exports = class StripePaymentProcessor {
|
||||
this._resolveReady = resolve;
|
||||
this._rejectReady = reject;
|
||||
});
|
||||
/**
|
||||
* @type Array<import('stripe').plans.IPlan>
|
||||
*/
|
||||
this._plans = [];
|
||||
this._configure(config);
|
||||
}
|
||||
|
||||
@ -39,10 +43,6 @@ module.exports = class StripePaymentProcessor {
|
||||
return this._rejectReady(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type Array<import('stripe').plans.IPlan>
|
||||
*/
|
||||
this._plans = [];
|
||||
for (const planSpec of config.plans) {
|
||||
try {
|
||||
const plan = await api.plans.ensure(this._stripe, planSpec, this._product);
|
||||
@ -223,10 +223,6 @@ module.exports = class StripePaymentProcessor {
|
||||
return customer;
|
||||
}
|
||||
|
||||
async getStripeCustomer(id) {
|
||||
return await retrieve(this._stripe, 'customers', id);
|
||||
}
|
||||
|
||||
async createCheckoutSetupSession(member, options) {
|
||||
const customer = await this._customerForMemberCheckoutSession(member);
|
||||
|
||||
@ -290,6 +286,26 @@ module.exports = class StripePaymentProcessor {
|
||||
return metadata.subscriptions;
|
||||
}
|
||||
|
||||
async createComplimentarySubscription(customer) {
|
||||
const monthlyPlan = this._plans.find(plan => plan.interval === 'month');
|
||||
if (!monthlyPlan) {
|
||||
throw new Error('Could not find monthly plan');
|
||||
}
|
||||
const complimentaryCurrency = monthlyPlan.currency.toLowerCase();
|
||||
const complimentaryPlan = this._plans.find(plan => plan.nickname === 'Complimentary' && plan.currency === complimentaryCurrency);
|
||||
|
||||
if (!complimentaryPlan) {
|
||||
throw new Error('Could not find complimentaryPlan');
|
||||
}
|
||||
|
||||
return create(this._stripe, 'subscriptions', {
|
||||
customer: customer.id,
|
||||
items: [{
|
||||
plan: complimentaryPlan.id
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
||||
async setComplimentarySubscription(member) {
|
||||
const subscriptions = await this.getActiveSubscriptions(member);
|
||||
|
||||
@ -464,7 +480,7 @@ module.exports = class StripePaymentProcessor {
|
||||
}
|
||||
|
||||
debug(`Creating customer for member ${member.get('email')}`);
|
||||
const customer = await create(this._stripe, 'customers', {
|
||||
const customer = await this.createCustomer({
|
||||
email: member.get('email')
|
||||
});
|
||||
|
||||
@ -477,6 +493,10 @@ module.exports = class StripePaymentProcessor {
|
||||
return retrieve(this._stripe, 'setupIntents', id, options);
|
||||
}
|
||||
|
||||
async createCustomer(options) {
|
||||
return create(this._stripe, 'customers', options);
|
||||
}
|
||||
|
||||
async getCustomer(id, options) {
|
||||
return retrieve(this._stripe, 'customers', id, options);
|
||||
}
|
||||
|
@ -96,7 +96,9 @@ module.exports = function ({
|
||||
setComplimentarySubscriptionById,
|
||||
cancelComplimentarySubscription: safeStripe('cancelComplimentarySubscription'),
|
||||
cancelStripeSubscriptions: safeStripe('cancelComplimentarySubscription'),
|
||||
getStripeCustomer: safeStripe('getStripeCustomer'),
|
||||
getStripeCustomer: safeStripe('getCustomer'),
|
||||
createStripeCustomer: safeStripe('createCustomer'),
|
||||
createComplimentarySubscription: safeStripe('createComplimentarySubscription'),
|
||||
linkStripeCustomer: safeStripe('linkStripeCustomer'),
|
||||
linkStripeCustomerById
|
||||
};
|
||||
|
@ -33,6 +33,7 @@
|
||||
"ghost-ignition": "4.2.2",
|
||||
"got": "^9.6.0",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"leaky-bucket": "2.2.0",
|
||||
"lodash": "^4.17.11",
|
||||
"node-jose": "^1.1.3",
|
||||
"stripe": "^7.4.0"
|
||||
|
Loading…
Reference in New Issue
Block a user