Skip to content

Commit

Permalink
fix(Stripe): set expiry date for payment intent cards (#10541)
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree authored Dec 19, 2024
1 parent 83ba678 commit 65e6f84
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query(`
UPDATE "PaymentMethods"
SET
"expiryDate" = to_date((data ->> 'expYear')::varchar || '-' || (data ->> 'expMonth')::varchar, 'YYYY-MM') + INTERVAL '1 month' - INTERVAL '1 ms',
"data" = JSONB_SET(data, '{expiryDateSetFrom20241212132217Migration}', 'true'::jsonb)
WHERE "expiryDate" IS NULL
AND data ->> 'expYear' IS NOT NULL
AND data ->> 'expMonth' IS NOT NULL
AND "deletedAt" IS NULL
`);
},

async down(queryInterface) {
await queryInterface.sequelize.query(`
UPDATE "PaymentMethods"
SET
"expiryDate" = NULL,
"data" = data - 'expiryDateSetFrom20241212132217Migration'
WHERE data -> 'expiryDateSetFrom20241212132217Migration' IS NOT NULL
AND "deletedAt" IS NULL
`);
},
};
8 changes: 7 additions & 1 deletion server/paymentProviders/stripe/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,19 @@ export async function createPaymentMethod(
const paymentIntentCharge: Stripe.Charge = (originPaymentIntent as any)?.charges?.data?.[0];
const paymentMethodChargeDetails = paymentIntentCharge?.payment_method_details;

const paymentMethodData = {
const paymentMethodData: Record<string, unknown> = {
stripePaymentMethodId: stripePaymentMethod.id,
stripeAccount,
...mapStripePaymentMethodExtraData(stripePaymentMethod, paymentMethodChargeDetails),
...extraData,
};

const paymentMethodName = formatPaymentMethodName(stripePaymentMethod, paymentMethodChargeDetails);
let expiryDate;
if (paymentMethodData.expYear && paymentMethodData.expMonth) {
const { expYear, expMonth } = paymentMethodData;
expiryDate = moment.utc(`${expYear}-${expMonth}`, 'YYYY-MM').endOf('month');
}

return await PaymentMethod.create(
{
Expand All @@ -515,6 +520,7 @@ export async function createPaymentMethod(
(stripePaymentMethod.type !== 'bancontact' && originPaymentIntent?.setup_future_usage === 'off_session'),
confirmedAt: new Date(),
data: paymentMethodData,
expiryDate,
},
createOptions,
);
Expand Down
40 changes: 40 additions & 0 deletions test/server/paymentProviders/stripe/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,46 @@ describe('webhook', () => {
await order.reload();
expect(order.status).to.equal(OrderStatuses.PROCESSING);
expect(order.data.paymentIntent.charges).to.not.be.null;

const paymentMethod = await order.getPaymentMethod();
expect(paymentMethod.service).to.equal(PAYMENT_METHOD_SERVICE.STRIPE);
expect(paymentMethod.type).to.equal(PAYMENT_METHOD_TYPE.US_BANK_ACCOUNT);
});

it('stores proper card info in payment method', async () => {
sandbox.stub(stripe.paymentMethods, 'retrieve').resolves({
type: 'card',
id: 'pm_123456789abcdef123456789',
card: {
brand: 'Visa',
last4: '1234',
country: 'US',
exp_year: 2024,
exp_month: 12,
},
});

await webhook.paymentIntentProcessing(event);
await order.reload();
expect(order.status).to.equal(OrderStatuses.PROCESSING);
expect(order.data.paymentIntent.charges).to.not.be.null;

const paymentMethod = await order.getPaymentMethod();
expect(paymentMethod.service).to.equal(PAYMENT_METHOD_SERVICE.STRIPE);
expect(paymentMethod.type).to.equal(PAYMENT_METHOD_TYPE.CREDITCARD);
expect(paymentMethod.data).to.deep.eq({
brand: 'Visa',
country: 'US',
expYear: 2024,
expMonth: 12,
stripeAccount: 'acct_15vekcDjPFcHOcTm',
stripePaymentMethodId: 'pm_123456789abcdef123456789',
});
expect(paymentMethod.name).to.equal('1234');
expect(paymentMethod.expiryDate).to.not.be.null;
expect(paymentMethod.expiryDate.getFullYear()).to.equal(2024);
expect(paymentMethod.expiryDate.getMonth()).to.equal(11);
expect(paymentMethod.expiryDate.getDate()).to.equal(31);
});
});

Expand Down

0 comments on commit 65e6f84

Please sign in to comment.