Skip to content

Commit

Permalink
🐛 Fixed using Offers with Stripe Checkout v2
Browse files Browse the repository at this point in the history
refs 1f300fb781f0

The Stripe API was giving inconsistent responses when passing the
customerEmail param, sometimes it would allow a null/empty value and
sometimes it wouldn't. In order to support both behaviours we need to
not attach the customerEmail at all unless necessary.
  • Loading branch information
allouis committed Nov 1, 2022
1 parent 7fda360 commit db340b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('Create Stripe Checkout Session', function () {
if (uri === '/v1/checkout/sessions') {
const bodyJSON = querystring.parse(body);
// TODO: Actually work out what Stripe checks and when/how it errors
if (bodyJSON.customerEmail) {
if (Reflect.has(bodyJSON, 'customerEmail')) {
return [400, {error: 'Invalid Email'}];
}
return [200, {id: 'cs_123', url: 'https://site.com'}];
Expand Down
15 changes: 9 additions & 6 deletions ghost/payments/lib/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,19 @@ class PaymentsService {

const email = options.email || null;

const session = await this.stripeAPIService.createCheckoutSession(price.id, customer, {
const data = {
metadata,
successUrl: options.successUrl,
cancelUrl: options.cancelUrl,
customerEmail: customer ? email : null,
trialDays: trialDays ?? tier.trialDays,
coupon: coupon?.id
});
};

if (!customer && email) {
data.customerEmail = email;
}

const session = await this.stripeAPIService.createCheckoutSession(price.id, customer, data);

return session.url;
}
Expand All @@ -108,9 +113,7 @@ class PaymentsService {
for (const row of rows) {
const customer = await this.stripeAPIService.getCustomer(row.customer_id);
if (!customer.deleted) {
return {
id: customer.id
};
return customer;
}
}

Expand Down

0 comments on commit db340b9

Please sign in to comment.