diff --git a/migrations/20241212132217-set-expiry-date-stripe-payment-intents.js b/migrations/20241212132217-set-expiry-date-stripe-payment-intents.js new file mode 100644 index 00000000000..c8d2f2bf102 --- /dev/null +++ b/migrations/20241212132217-set-expiry-date-stripe-payment-intents.js @@ -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 + `); + }, +}; diff --git a/server/paymentProviders/stripe/common.ts b/server/paymentProviders/stripe/common.ts index 1a4817f2134..4754970a63c 100644 --- a/server/paymentProviders/stripe/common.ts +++ b/server/paymentProviders/stripe/common.ts @@ -492,7 +492,7 @@ 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 = { stripePaymentMethodId: stripePaymentMethod.id, stripeAccount, ...mapStripePaymentMethodExtraData(stripePaymentMethod, paymentMethodChargeDetails), @@ -500,6 +500,11 @@ export async function createPaymentMethod( }; 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( { @@ -515,6 +520,7 @@ export async function createPaymentMethod( (stripePaymentMethod.type !== 'bancontact' && originPaymentIntent?.setup_future_usage === 'off_session'), confirmedAt: new Date(), data: paymentMethodData, + expiryDate, }, createOptions, ); diff --git a/test/server/paymentProviders/stripe/webhook.test.ts b/test/server/paymentProviders/stripe/webhook.test.ts index 7c1dfceae88..d3d03e947a2 100644 --- a/test/server/paymentProviders/stripe/webhook.test.ts +++ b/test/server/paymentProviders/stripe/webhook.test.ts @@ -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); }); });