Skip to content

Commit

Permalink
chore: proactively set stripe ids
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Jun 25, 2022
1 parent 46c27d8 commit 9102a99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
yarn commitlint --edit $1
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install lint-staged && npm test
FORCE_COLOR=1 yarn lint-staged && npm test
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
34 changes: 31 additions & 3 deletions app/controllers/web/my-account/create-domain-billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,42 @@ 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/validate stripe customer here - this ensures we have the valid customer saved before all future stripe interaction.
// it is currently needed to be created here to properly process the first webhook
try {
const customer = await stripe.customers.retrieve(
ctx.state.user[config.userFields.stripeCustomerID]
);
if (customer.deleted)
throw new Error('Stripe customer previously deleted');
} catch (err) {
ctx.logger.warn(err);
ctx.logger.info('creating new stripe customer', {
user: ctx.state.user.toObject()
});
try {
const 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) {
// handle any stripe customer creation errors as we need
// to stop all flow here if we cannot create the customer
ctx.logger.fatal(_err);
throw ctx.translate('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

0 comments on commit 9102a99

Please # to comment.