feat: Add support for creating payment methods to payment module (#11063)

CLOSES CLO-407
This commit is contained in:
Stevche Radevski
2025-01-21 12:31:44 +01:00
committed by GitHub
parent cd758067d4
commit 05c8a67d8e
11 changed files with 240 additions and 79 deletions

View File

@@ -585,18 +585,6 @@ export interface PaymentProviderDTO {
is_enabled: boolean
}
export interface PaymentMethodDTO {
/**
* The ID of the payment method in the payment provider's system.
*/
id: string
/**
* The data of the payment method, as returned by the payment provider.
*/
data: Record<string, unknown>
}
/**
* The filters to apply on the retrieved payment providers.
*/
@@ -657,3 +645,20 @@ export interface RefundReasonDTO {
*/
updated_at: Date | string
}
export interface PaymentMethodDTO {
/**
* The ID of the payment method.
*/
id: string
/**
* The data of the payment method, as returned by the payment provider.
*/
data: Record<string, unknown>
/**
* The ID of the associated payment provider.
*/
provider_id: string
}

View File

@@ -318,3 +318,23 @@ export interface UpdateRefundReasonDTO {
*/
metadata?: Record<string, unknown> | null
}
/**
* The payment method to be created.
*/
export interface CreatePaymentMethodDTO {
/**
* The provider's ID.
*/
provider_id: string
/**
* Necessary data for the associated payment provider to process the payment.
*/
data: Record<string, unknown>
/**
* Necessary context data for the associated payment provider.
*/
context: PaymentProviderContext
}

View File

@@ -78,6 +78,18 @@ export type CreatePaymentProviderSession = {
currency_code: string
}
export type SavePaymentMethod = {
/**
* Any data that should be used by the provider for saving the payment method.
*/
data: Record<string, unknown>
/**
* The context of the payment provider, such as the customer ID.
*/
context: PaymentProviderContext
}
/**
* @interface
*
@@ -118,6 +130,18 @@ export type PaymentProviderSessionResponse = {
data: Record<string, unknown>
}
export type SavePaymentMethodResponse = {
/**
* The ID of the payment method in the payment provider.
*/
id: string
/**
* The data returned from the payment provider after saving the payment method.
*/
data: Record<string, unknown>
}
/**
* @interface
*
@@ -254,10 +278,14 @@ export interface IPaymentProvider {
paymentSessionData: Record<string, unknown>
): Promise<PaymentProviderError | PaymentProviderSessionResponse["data"]>
listPaymentMethods(
listPaymentMethods?(
context: PaymentProviderContext
): Promise<PaymentMethodResponse[]>
savePaymentMethod?(
input: SavePaymentMethod
): Promise<PaymentProviderError | SavePaymentMethodResponse>
getPaymentStatus(
paymentSessionData: Record<string, unknown>
): Promise<PaymentSessionStatus>

View File

@@ -6,6 +6,7 @@ import {
CaptureDTO,
FilterableCaptureProps,
FilterablePaymentCollectionProps,
FilterablePaymentMethodProps,
FilterablePaymentProps,
FilterablePaymentProviderProps,
FilterablePaymentSessionProps,
@@ -13,6 +14,7 @@ import {
FilterableRefundReasonProps,
PaymentCollectionDTO,
PaymentDTO,
PaymentMethodDTO,
PaymentProviderDTO,
PaymentSessionDTO,
RefundDTO,
@@ -749,6 +751,74 @@ export interface IPaymentModuleService extends IModuleService {
sharedContext?: Context
): Promise<[PaymentProviderDTO[], number]>
/**
* This method retrieves all payment methods based on the context and configuration.
*
* @param {FilterablePaymentMethodProps} filters - The filters to apply on the retrieved payment methods.
* @param {FindConfig<PaymentMethodDTO>} config - The configurations determining how the payment method is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a payment method.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<PaymentMethodDTO[]>} The list of payment methods.
*
* @example
* To retrieve a list of payment methods for a customer:
*
* ```ts
* const paymentMethods =
* await paymentModuleService.listPaymentMethods({
* provider_id: "pp_stripe_stripe",
* context: {
* customer: {
* id: "cus_123",
* metadata: {
* pp_stripe_stripe_customer_id: "str_1234"
* }
* },
* },
* })
* ```
*
*/
listPaymentMethods(
filters: FilterablePaymentMethodProps,
config: FindConfig<PaymentMethodDTO>,
sharedContext?: Context
): Promise<PaymentMethodDTO[]>
/**
* This method retrieves all payment methods along with the total count of available payment methods, based on the context and configuration.
*
* @param {FilterablePaymentMethodProps} filters - The filters to apply on the retrieved payment methods.
* @param {FindConfig<PaymentMethodDTO>} config - The configurations determining how the payment method is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a payment method.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<[PaymentMethodDTO[], number]>} The list of payment methods along with their total count.
*
* @example
* To retrieve a list of payment methods for a customer:
*
* ```ts
* const [paymentMethods, count] =
* await paymentModuleService.listAndCountPaymentMethods({
* provider_id: "pp_stripe_stripe",
* context: {
* customer: {
* id: "cus_123",
* metadata: {
* pp_stripe_stripe_customer_id: "str_1234"
* }
* },
* },
* })
* ```
*
*/
listAndCountPaymentMethods(
filters: FilterablePaymentMethodProps,
config: FindConfig<PaymentMethodDTO>,
sharedContext?: Context
): Promise<[PaymentMethodDTO[], number]>
/**
* This method retrieves a paginated list of captures based on optional filters and configuration.
*

View File

@@ -1,8 +1,6 @@
import {
CreatePaymentProviderSession,
IPaymentProvider,
PaymentMethodResponse,
PaymentProviderContext,
PaymentProviderError,
PaymentProviderSessionResponse,
PaymentSessionStatus,
@@ -625,59 +623,6 @@ export abstract class AbstractPaymentProvider<TConfig = Record<string, unknown>>
context: UpdatePaymentProviderSession
): Promise<PaymentProviderError | PaymentProviderSessionResponse>
/**
* List the payment methods associated with the context (eg. customer) of the payment provider, if any.
*
* @param context - The context for which the payment methods are listed. Usually the customer should be provided.
* @returns An object whose `payment_methods` property is set to the data returned by the payment provider.
*
* @example
* // other imports...
* import {
* PaymentProviderContext,
* PaymentProviderError,
* PaymentMethodResponse
* PaymentProviderSessionResponse,
* } from "@medusajs/framework/types"
*
*
* class MyPaymentProviderService extends AbstractPaymentProvider<
* Options
* > {
* async listPaymentMethods(
* context: PaymentProviderContext
* ): Promise<PaymentMethodResponse> {
* const {
* customer,
* } = context
* const externalCustomerId = customer.metadata.stripe_id
*
* try {
* // assuming you have a client that updates the payment
* const response = await this.client.listPaymentMethods(
* {customer: externalCustomerId}
* )
*
* return response.map((method) => ({
* id: method.id,
* data: method
* }))
* } catch (e) {
* return {
* error: e,
* code: "unknown",
* detail: e
* }
* }
* }
*
* // ...
* }
*/
abstract listPaymentMethods(
context: PaymentProviderContext
): Promise<PaymentMethodResponse[]>
/**
* This method is executed when a webhook event is received from the third-party payment provider. Use it
* to process the action of the payment provider.