From 1f41e4f4d8899464749df3333fc2ba0c3c6fe6ce Mon Sep 17 00:00:00 2001 From: shij Date: Wed, 6 Nov 2024 18:19:41 +0800 Subject: [PATCH] feat(payment): support create pending payment --- packages/core/types/src/payment/mutations.ts | 7 +- ...02113510.ts => Migration20241106090002.ts} | 5 +- .../payment/src/services/payment-module.ts | 151 ++++++++++++------ 3 files changed, 110 insertions(+), 53 deletions(-) rename packages/modules/payment/src/migrations/{Migration20240902113510.ts => Migration20241106090002.ts} (78%) diff --git a/packages/core/types/src/payment/mutations.ts b/packages/core/types/src/payment/mutations.ts index 642aef7579f10..6e20a7a76dcad 100644 --- a/packages/core/types/src/payment/mutations.ts +++ b/packages/core/types/src/payment/mutations.ts @@ -226,7 +226,7 @@ export interface CreatePaymentSessionDTO { /** * The provider's ID. */ - provider_id: string + provider_id?: string /** * The provider's payment method token @@ -258,6 +258,11 @@ export interface UpdatePaymentSessionDTO { */ id: string + /** + * The provider's ID. + */ + provider_id?: string + /** * The provider's payment method token */ diff --git a/packages/modules/payment/src/migrations/Migration20240902113510.ts b/packages/modules/payment/src/migrations/Migration20241106090002.ts similarity index 78% rename from packages/modules/payment/src/migrations/Migration20240902113510.ts rename to packages/modules/payment/src/migrations/Migration20241106090002.ts index 3509c56b49f61..cae1daf311b5d 100644 --- a/packages/modules/payment/src/migrations/Migration20240902113510.ts +++ b/packages/modules/payment/src/migrations/Migration20241106090002.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class Migration20240902113510 extends Migration { +export class Migration20241106090002 extends Migration { async up(): Promise { this.addSql( 'alter table if exists "capture" add column if not exists "data" jsonb not null;' @@ -14,6 +14,9 @@ export class Migration20240902113510 extends Migration { this.addSql( 'alter table if exists "payment_session" drop constraint if exists "payment_session_status_check";' ) + this.addSql( + 'alter table if exists "payment_session" alter column "provider_id" drop not null;' + ) } async down(): Promise { diff --git a/packages/modules/payment/src/services/payment-module.ts b/packages/modules/payment/src/services/payment-module.ts index f0e98b3d8a4d0..6feab2f9cf63c 100644 --- a/packages/modules/payment/src/services/payment-module.ts +++ b/packages/modules/payment/src/services/payment-module.ts @@ -345,24 +345,32 @@ export default class PaymentModuleService sharedContext ) - try { - res = await this.paymentProviderService_.createSession(data.provider_id, { - amount, - currency_code: paymentCollection.currency_code, - context: { ...data.context, session_id: paymentSession.id }, - token: data.provider_token, - }) - } catch (err) { - await this.paymentSessionService_.delete(paymentSession.id, sharedContext) - throw err - } + if (data.provider_id) { + try { + res = await this.paymentProviderService_.createSession( + data.provider_id, + { + amount, + currency_code: paymentCollection.currency_code, + context: { ...data.context, session_id: paymentSession.id }, + token: data.provider_token, + } + ) + } catch (err) { + await this.paymentSessionService_.delete( + paymentSession.id, + sharedContext + ) + throw err + } - await this.handleProviderSessionResponse_(res, sharedContext) - paymentSession = await this.paymentSessionService_.retrieve( - paymentSession.id, - {}, - sharedContext - ) + await this.handleProviderSessionResponse_(res, sharedContext) + paymentSession = await this.paymentSessionService_.retrieve( + paymentSession.id, + {}, + sharedContext + ) + } return this.baseRepository_.serialize(paymentSession, { populate: true }) } @@ -372,35 +380,65 @@ export default class PaymentModuleService data: UpdatePaymentSessionDTO, @MedusaContext() sharedContext?: Context ): Promise { + const session = await this.paymentSessionService_.retrieve(data.id, { + select: ["id", "data", "context", "provider_id", "metadata"], + }) + let res: PaymentProviderSessionResponse | undefined + if ( - !data.amount && - !data.context && - !data.provider_token && - !data.metadata + data.provider_id && + session.provider_id && + data.provider_id !== session.provider_id ) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `The payment session update requires at least an amount, context, provider token or metadata.` + res = await this.paymentProviderService_.cancelPayment( + session.provider_id, + session.data ) } - const session = await this.paymentSessionService_.retrieve(data.id, { - select: ["id", "data", "context", "provider_id", "metadata"], - }) + if ( + data.provider_id && + (!session.provider_id || data.provider_id !== session.provider_id) + ) { + try { + res = await this.paymentProviderService_.createSession( + data.provider_id, + { + amount: data.amount ?? session.raw_amount, + currency_code: session.currency_code, + context: { + ...session.context, + ...data.context, + session_id: session.id, + }, + token: data.provider_token, + } + ) + } catch (err) { + if (res) { + await this.handleProviderSessionResponse_(res, sharedContext) + } + throw err + } + } else if (session.provider_id) { + res = await this.paymentProviderService_.updateSession( + session.provider_id, + { + data: session.data, + context: { + ...session.context, + ...data.context, + session_id: session.id, + }, + amount: data.amount, + token: data.provider_token, + } + ) + } - await this.handleProviderSessionResponse_( - await this.paymentProviderService_.updateSession(session.provider_id, { - data: session.data, - context: { - ...session.context, - ...data.context, - session_id: session.id, - }, - amount: data.amount, - token: data.provider_token, - }), - sharedContext - ) + if (res) { + await this.handleProviderSessionResponse_(res, sharedContext) + } if (data.amount || data.context || data.metadata) { const toUpdate: any = {} @@ -430,10 +468,12 @@ export default class PaymentModuleService sharedContext ) - await this.paymentProviderService_.deleteSession( - session.provider_id, - session.data - ) + if (session.provider_id) { + await this.paymentProviderService_.deleteSession( + session.provider_id, + session.data + ) + } await this.paymentSessionService_.delete(id, sharedContext) } @@ -450,6 +490,13 @@ export default class PaymentModuleService sharedContext ) + if (!paymentSession.provider_id) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + `The payment session has not yet specified a provider.` + ) + } + await this.handleProviderSessionResponse_( await this.paymentProviderService_.authorizePayment( paymentSession.provider_id, @@ -549,13 +596,15 @@ export default class PaymentModuleService sharedContext ) - await this.handleProviderSessionResponse_( - await this.paymentProviderService_.cancelPayment( - paymentSession.provider_id, - paymentSession.data - ), - sharedContext - ) + if (paymentSession.provider_id) { + await this.handleProviderSessionResponse_( + await this.paymentProviderService_.cancelPayment( + paymentSession.provider_id, + paymentSession.data + ), + sharedContext + ) + } return this.retrievePaymentSession(id, {}, sharedContext) }