From 3a428f02b02e80c9f2c7a468c7a7b82be32cb303 Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Thu, 12 Dec 2024 14:32:15 +0100 Subject: [PATCH] fix(Stripe): set expiry date for payment intent cards --- ...-set-expiry-date-stripe-payment-intents.js | 28 +++++++++++++++++++ server/paymentProviders/stripe/common.ts | 8 +++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 migrations/20241212132217-set-expiry-date-stripe-payment-intents.js 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, );