Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: proactively set stripe ids #169

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .xo-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module.exports = {
prettier: true,
space: true,
extends: ['xo-lass'],
rules: {
'no-warning-comments': 'off'
},
overrides: [
{
files: ['assets/js/*.js', 'assets/js/**/*.js'],
Expand Down
49 changes: 46 additions & 3 deletions app/controllers/web/my-account/create-domain-billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const striptags = require('striptags');

const env = require('#config/env');
const config = require('#config');
const emailHelper = require('#helpers/email');

const payPalClient = new checkoutNodeJssdk.core.PayPalHttpClient(
config.payments.paypalCheckoutSdkConfig
Expand Down Expand Up @@ -106,14 +107,56 @@ async function createDomainBilling(ctx) {
// if the user didn't have JavaScript enabled, then redirect them to Stripe page
if (ctx.accepts('html')) throw ctx.translateError('JAVASCRIPT_REQUIRED');

//
// create and validate stripe customer here
// (ensure valid customer before webhooks hit)
//
try {
let customer = isSANB(
ctx.state.user[config.userFields.stripeCustomerID]
)
? await stripe.customers.retrieve(
ctx.state.user[config.userFields.stripeCustomerID]
)
: await stripe.customers.create({ email: ctx.state.user.email });

if (customer.deleted) {
ctx.logger.warn('Stripe customer previously deleted', { customer });
customer = await stripe.customers.create({
email: ctx.state.user.email
});
}

ctx.state.user[config.userFields.stripeCustomerID] = customer.id;
await ctx.state.user.save();
} catch (err) {
ctx.logger.fatal(err);
// email admins here
try {
await emailHelper({
template: 'alert',
message: {
to: config.email.message.from,
subject: `Error creating Stripe customer for ${ctx.state.user.email}`
},
locals: { message: err.message }
});
} catch (err) {
ctx.logger.fatal(err);
}

throw ctx.translateError('UNKNOWN_ERROR');
}

const options = {
// TODO: add alipay and others
payment_method_types: ['card'],
mode: paymentType === 'one-time' ? 'payment' : 'subscription',
...(isSANB(ctx.state.user[config.userFields.stripeCustomerID])
? { customer: ctx.state.user[config.userFields.stripeCustomerID] }
: { customer_email: ctx.state.user.email }),
customer: ctx.state.user[config.userFields.stripeCustomerID],
client_reference_id: reference,
metadata: {
plan
},
line_items: [
{
price,
Expand Down