2019-10-02 06:36:54 +03:00
|
|
|
const debug = require('ghost-ignition').debug('stripe');
|
|
|
|
const {retrieve, list, create, del} = require('./api/stripeRequests');
|
2019-09-06 08:13:35 +03:00
|
|
|
const api = require('./api');
|
|
|
|
|
2019-09-25 07:35:58 +03:00
|
|
|
const STRIPE_API_VERSION = '2019-09-09';
|
|
|
|
|
2019-09-06 08:13:35 +03:00
|
|
|
module.exports = class StripePaymentProcessor {
|
2019-10-02 06:57:23 +03:00
|
|
|
constructor(config, storage, logging) {
|
|
|
|
this.logging = logging;
|
2019-09-25 06:15:31 +03:00
|
|
|
this.storage = storage;
|
2019-09-06 08:13:35 +03:00
|
|
|
this._ready = new Promise((resolve, reject) => {
|
|
|
|
this._resolveReady = resolve;
|
|
|
|
this._rejectReady = reject;
|
|
|
|
});
|
|
|
|
this._configure(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
async ready() {
|
|
|
|
return this._ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _configure(config) {
|
|
|
|
this._stripe = require('stripe')(config.secretKey);
|
2019-09-25 07:35:58 +03:00
|
|
|
this._stripe.setAppInfo(config.appInfo);
|
|
|
|
this._stripe.setApiVersion(STRIPE_API_VERSION);
|
2019-09-06 08:13:35 +03:00
|
|
|
this._stripe.__TEST_MODE__ = config.secretKey.startsWith('sk_test_');
|
|
|
|
this._public_token = config.publicKey;
|
|
|
|
this._checkoutSuccessUrl = config.checkoutSuccessUrl;
|
|
|
|
this._checkoutCancelUrl = config.checkoutCancelUrl;
|
2019-09-24 13:20:59 +03:00
|
|
|
this._webhookHandlerUrl = config.webhookHandlerUrl;
|
2019-09-06 08:13:35 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
this._product = await api.products.ensure(this._stripe, config.product);
|
|
|
|
|
2019-09-24 13:20:59 +03:00
|
|
|
this._plans = [];
|
|
|
|
for (const planSpec of config.plans) {
|
2019-09-06 08:13:35 +03:00
|
|
|
const plan = await api.plans.ensure(this._stripe, planSpec, this._product);
|
|
|
|
this._plans.push(plan);
|
2019-09-24 13:20:59 +03:00
|
|
|
}
|
|
|
|
|
2019-10-02 06:36:54 +03:00
|
|
|
const webhooks = await list(this._stripe, 'webhookEndpoints', {
|
|
|
|
limit: 100
|
|
|
|
});
|
|
|
|
|
|
|
|
const webhookToDelete = webhooks.data.find((webhook) => {
|
|
|
|
return webhook.url === this._webhookHandlerUrl;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (webhookToDelete) {
|
|
|
|
await del(this._stripe, 'webhookEndpoints', webhookToDelete.id);
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:20:59 +03:00
|
|
|
try {
|
|
|
|
const webhook = await create(this._stripe, 'webhookEndpoints', {
|
|
|
|
url: this._webhookHandlerUrl,
|
2019-09-25 07:35:58 +03:00
|
|
|
api_version: STRIPE_API_VERSION,
|
2019-09-24 13:20:59 +03:00
|
|
|
enabled_events: ['checkout.session.completed']
|
|
|
|
});
|
|
|
|
this._webhookSecret = webhook.secret;
|
2019-09-06 08:13:35 +03:00
|
|
|
} catch (err) {
|
2019-10-02 06:57:23 +03:00
|
|
|
this.logging.warn(err);
|
2019-09-24 13:20:59 +03:00
|
|
|
this._webhookSecret = process.env.WEBHOOK_SECRET;
|
2019-09-06 08:13:35 +03:00
|
|
|
}
|
2019-10-02 07:14:21 +03:00
|
|
|
debug(`Webhook secret set to ${this._webhookSecret}`);
|
2019-09-24 13:20:59 +03:00
|
|
|
} catch (err) {
|
2019-10-02 07:14:21 +03:00
|
|
|
debug(`Error configuring ${err.message}`);
|
2019-09-24 13:20:59 +03:00
|
|
|
return this._rejectReady(err);
|
2019-09-06 08:13:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._resolveReady({
|
|
|
|
product: this._product,
|
|
|
|
plans: this._plans
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:24:28 +03:00
|
|
|
async parseWebhook(body, signature) {
|
|
|
|
return this._stripe.webhooks.constructEvent(body, signature, this._webhookSecret);
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:18:31 +03:00
|
|
|
async createCheckoutSession(member, planName, options) {
|
2019-09-25 06:41:58 +03:00
|
|
|
let customer;
|
|
|
|
if (member) {
|
2019-10-02 07:14:21 +03:00
|
|
|
try {
|
2019-10-08 09:31:59 +03:00
|
|
|
customer = await this._customerForMemberCheckoutSession(member);
|
2019-10-02 07:14:21 +03:00
|
|
|
} catch (err) {
|
|
|
|
debug(`Ignoring Error getting customer for checkout ${err.message}`);
|
|
|
|
customer = null;
|
|
|
|
}
|
2019-09-25 06:41:58 +03:00
|
|
|
} else {
|
|
|
|
customer = null;
|
|
|
|
}
|
2019-09-06 08:13:35 +03:00
|
|
|
const plan = this._plans.find(plan => plan.nickname === planName);
|
|
|
|
const session = await this._stripe.checkout.sessions.create({
|
|
|
|
payment_method_types: ['card'],
|
2019-10-03 13:18:31 +03:00
|
|
|
success_url: options.successUrl || this._checkoutSuccessUrl,
|
|
|
|
cancel_url: options.cancelUrl || this._checkoutCancelUrl,
|
2019-09-25 06:41:58 +03:00
|
|
|
customer: customer ? customer.id : undefined,
|
2019-09-06 08:13:35 +03:00
|
|
|
subscription_data: {
|
|
|
|
items: [{
|
|
|
|
plan: plan.id
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-25 06:41:58 +03:00
|
|
|
return {
|
|
|
|
sessionId: session.id,
|
|
|
|
publicKey: this._public_token
|
|
|
|
};
|
2019-09-06 08:13:35 +03:00
|
|
|
}
|
2019-09-24 13:27:53 +03:00
|
|
|
|
2019-10-02 10:40:00 +03:00
|
|
|
async cancelAllSubscriptions(member) {
|
|
|
|
const subscriptions = await this.getSubscriptions(member);
|
|
|
|
|
|
|
|
const activeSubscriptions = subscriptions.filter((subscription) => {
|
|
|
|
return subscription.status !== 'cancelled';
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(activeSubscriptions.map((subscription) => {
|
2019-10-08 10:09:59 +03:00
|
|
|
return del(this._stripe, 'subscriptions', subscription.id);
|
2019-10-02 10:40:00 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-02 10:37:56 +03:00
|
|
|
async getSubscriptions(member) {
|
2019-09-25 06:24:45 +03:00
|
|
|
const metadata = await this.storage.get(member);
|
|
|
|
|
2019-10-08 10:09:59 +03:00
|
|
|
const customers = metadata.customers.reduce((customers, customer) => {
|
|
|
|
return Object.assign(customers, {
|
|
|
|
[customer.customer_id]: {
|
|
|
|
id: customer.customer_id,
|
|
|
|
name: customer.name,
|
|
|
|
email: customer.email
|
2019-09-25 06:24:45 +03:00
|
|
|
}
|
2019-10-08 10:09:59 +03:00
|
|
|
});
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return metadata.subscriptions.map((subscription) => {
|
|
|
|
return {
|
|
|
|
id: subscription.subscription_id,
|
|
|
|
customer: customers[subscription.customer_id],
|
|
|
|
plan: {
|
|
|
|
id: subscription.plan_id,
|
|
|
|
nickname: subscription.plan_nickname,
|
|
|
|
interval: subscription.plan_interval,
|
|
|
|
amount: subscription.plan_amount,
|
|
|
|
currency: subscription.plan_currency
|
|
|
|
},
|
|
|
|
status: subscription.status,
|
|
|
|
start_date: subscription.start_date,
|
|
|
|
default_payment_card_last4: subscription.default_payment_card_last4,
|
|
|
|
current_period_end: subscription.current_period_end
|
|
|
|
};
|
|
|
|
});
|
2019-09-25 06:24:45 +03:00
|
|
|
}
|
|
|
|
|
2019-10-02 10:37:56 +03:00
|
|
|
async getActiveSubscriptions(member) {
|
|
|
|
const subscriptions = await this.getSubscriptions(member);
|
|
|
|
|
|
|
|
return subscriptions.filter((subscription) => {
|
|
|
|
return subscription.status !== 'cancelled' && subscription.status !== 'unpaid';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:59 +03:00
|
|
|
async handleCheckoutSessionCompletedWebhook(member, customer) {
|
|
|
|
await this._addCustomerToMember(member, customer);
|
2019-10-08 10:09:59 +03:00
|
|
|
if (!customer.subscriptions || !customer.subscriptions.data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const subscription of customer.subscriptions.data) {
|
|
|
|
await this._updateSubscription(subscription);
|
|
|
|
}
|
2019-10-08 09:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async _addCustomerToMember(member, customer) {
|
2019-10-02 07:14:21 +03:00
|
|
|
debug(`Attaching customer to member ${member.email} ${customer.id}`);
|
2019-10-08 10:09:59 +03:00
|
|
|
await this.storage.set({
|
|
|
|
customer: {
|
|
|
|
customer_id: customer.id,
|
|
|
|
member_id: member.id,
|
|
|
|
name: customer.name,
|
|
|
|
email: customer.email
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateSubscription(subscription) {
|
|
|
|
debug(`Attaching subscription to customer ${subscription.customer} ${subscription.id}`);
|
|
|
|
const payment = subscription.default_payment_method;
|
|
|
|
await this.storage.set({
|
|
|
|
subscription: {
|
|
|
|
customer_id: subscription.customer,
|
|
|
|
|
|
|
|
subscription_id: subscription.id,
|
|
|
|
status: subscription.status,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
2019-09-25 06:23:01 +03:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:59 +03:00
|
|
|
async _customerForMemberCheckoutSession(member) {
|
2019-09-25 06:23:25 +03:00
|
|
|
const metadata = await this.storage.get(member);
|
|
|
|
|
2019-10-08 10:09:59 +03:00
|
|
|
for (const data in metadata.customers) {
|
2019-10-02 06:36:13 +03:00
|
|
|
try {
|
|
|
|
const customer = await this.getCustomer(data.customer_id);
|
|
|
|
if (!customer.deleted) {
|
|
|
|
return customer;
|
|
|
|
}
|
2019-10-02 07:14:21 +03:00
|
|
|
} catch (err) {
|
|
|
|
debug(`Ignoring Error getting customer for member ${err.message}`);
|
2019-09-25 06:23:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 07:14:21 +03:00
|
|
|
debug(`Creating customer for member ${member.email}`);
|
2019-09-25 06:23:25 +03:00
|
|
|
const customer = await create(this._stripe, 'customers', {
|
|
|
|
email: member.email
|
|
|
|
});
|
|
|
|
|
2019-10-08 09:31:59 +03:00
|
|
|
await this._addCustomerToMember(member, customer);
|
2019-09-25 06:23:25 +03:00
|
|
|
|
|
|
|
return customer;
|
|
|
|
}
|
|
|
|
|
2019-10-02 14:10:19 +03:00
|
|
|
async getCustomer(id, options) {
|
|
|
|
return retrieve(this._stripe, 'customers', id, options);
|
2019-09-24 13:27:53 +03:00
|
|
|
}
|
2019-09-06 08:13:35 +03:00
|
|
|
};
|