|
1
|
{"slug":"/references/payment/provider","title":"How to Create a Payment Module Provider","frontmatter":{"slug":"/references/payment/provider","tags":["payment","server","how to"],"sidebar_label":"Create Payment Provider","keywords":["payment","provider","integration"]},"toc":[{"title":"Implementation Example","id":"implementation-example","level":2},{"title":"Understanding Payment Module Provider Implementation","id":"understanding-payment-module-provider-implementation","level":2},{"title":"1. Create Module Provider Directory","id":"1-create-module-provider-directory","level":2},{"title":"2. Create the Payment Module Provider's Service","id":"2-create-the-payment-module-provider-s-service","level":2},{"title":"constructor","id":"constructor","level":3},{"title":"Example","id":"example","level":4},{"title":"Type Parameters","id":"constructor-type-parameters","level":4},{"title":"Parameters","id":"constructor-parameters","level":4},{"title":"identifier","id":"identifier","level":3},{"title":"Example","id":"example-1","level":4},{"title":"authorizePayment","id":"authorizepayment","level":3},{"title":"Deferred / Asynchronous Authorization","id":"deferred-asynchronous-authorization","level":4},{"title":"Understanding `data` property","id":"understanding-data-property","level":4},{"title":"Example","id":"example-2","level":4},{"title":"Parameters","id":"authorizepayment-parameters","level":4},{"title":"Returns","id":"authorizepayment-returns","level":4},{"title":"cancelPayment","id":"cancelpayment","level":3},{"title":"Understanding `data` property","id":"understanding-data-property-1","level":4},{"title":"Example","id":"example-3","level":4},{"title":"Parameters","id":"cancelpayment-parameters","level":4},{"title":"Returns","id":"cancelpayment-returns","level":4},{"title":"capturePayment","id":"capturepayment","level":3},{"title":"Understanding `data` property","id":"understanding-data-property-2","level":4},{"title":"Example","id":"example-4","level":4},{"title":"Parameters","id":"capturepayment-parameters","level":4},{"title":"Returns","id":"capturepayment-returns","level":4},{"title":"createAccountHolder","id":"createaccountholder","level":3},{"title":"Example","id":"example-5","level":4},{"title":"Parameters","id":"createaccountholder-parameters","level":4},{"title":"Returns","id":"createaccountholder-returns","level":4},{"title":"deleteAccountHolder","id":"deleteaccountholder","level":3},{"title":"Example","id":"example-6","level":4},{"title":"Parameters","id":"deleteaccountholder-parameters","level":4},{"title":"Returns","id":"deleteaccountholder-returns","level":4},{"title":"deletePayment","id":"deletepayment","level":3},{"title":"Understanding `data` property","id":"understanding-data-property-3","level":4},{"title":"Example","id":"example-7","level":4},{"title":"Parameters","id":"deletepayment-parameters","level":4},{"title":"Returns","id":"deletepayment-returns","level":4},{"title":"deletePaymentMethod","id":"deletepaymentmethod","level":3},{"title":"Example","id":"example-8","level":4},{"title":"Parameters","id":"deletepaymentmethod-parameters","level":4},{"title":"Returns","id":"deletepaymentmethod-returns","level":4},{"title":"getPaymentStatus","id":"getpaymentstatus","level":3},{"title":"Example","id":"example-9","level":4},{"title":"Parameters","id":"getpaymentstatus-parameters","level":4},{"title":"Returns","id":"getpaymentstatus-returns","level":4},{"title":"getWebhookActionAndData","id":"getwebhookactionanddata","level":3},{"title":"Example","id":"example-10","level":4},{"title":"Parameters","id":"getwebhookactionanddata-parameters","level":4},{"title":"Returns","id":"getwebhookactionanddata-returns","level":4},{"title":"initiatePayment","id":"initiatepayment","level":3},{"title":"Understanding `data` property","id":"understanding-data-property-4","level":4},{"title":"Example","id":"example-11","level":4},{"title":"Parameters","id":"initiatepayment-parameters","level":4},{"title":"Returns","id":"initiatepayment-returns","level":4},{"title":"listPaymentMethods","id":"listpaymentmethods","level":3},{"title":"Example","id":"example-12","level":4},{"title":"Parameters","id":"listpaymentmethods-parameters","level":4},{"title":"Returns","id":"listpaymentmethods-returns","level":4},{"title":"refundPayment","id":"refundpayment","level":3},{"title":"Understanding `data` property","id":"understanding-data-property-5","level":4},{"title":"Example","id":"example-13","level":4},{"title":"Parameters","id":"refundpayment-parameters","level":4},{"title":"Returns","id":"refundpayment-returns","level":4},{"title":"retrieveAccountHolder","id":"retrieveaccountholder","level":3},{"title":"Example","id":"example-14","level":4},{"title":"Parameters","id":"retrieveaccountholder-parameters","level":4},{"title":"Returns","id":"retrieveaccountholder-returns","level":4},{"title":"retrievePayment","id":"retrievepayment","level":3},{"title":"Example","id":"example-15","level":4},{"title":"Parameters","id":"retrievepayment-parameters","level":4},{"title":"Returns","id":"retrievepayment-returns","level":4},{"title":"savePaymentMethod","id":"savepaymentmethod","level":3},{"title":"Example","id":"example-16","level":4},{"title":"Parameters","id":"savepaymentmethod-parameters","level":4},{"title":"Returns","id":"savepaymentmethod-returns","level":4},{"title":"updateAccountHolder","id":"updateaccountholder","level":3},{"title":"Example","id":"example-17","level":4},{"title":"Parameters","id":"updateaccountholder-parameters","level":4},{"title":"Returns","id":"updateaccountholder-returns","level":4},{"title":"updatePayment","id":"updatepayment","level":3},{"title":"Example","id":"example-18","level":4},{"title":"Parameters","id":"updatepayment-parameters","level":4},{"title":"Returns","id":"updatepayment-returns","level":4},{"title":"validateOptions","id":"validateoptions","level":3},{"title":"Example","id":"example-19","level":4},{"title":"Parameters","id":"validateoptions-parameters","level":4},{"title":"3. Create Module Provider Definition File","id":"3-create-module-provider-definition-file","level":2},{"title":"4. Use Module Provider","id":"4-use-module-provider","level":2},{"title":"5. Test it Out","id":"5-test-it-out","level":2},{"title":"Useful Guides","id":"useful-guides","level":2}],"blocks":[{"kind":"markdown","html":"In this document, you’ll learn how to create a Payment Module Provider to be used with the Payment Module."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Implementation Example","id":"implementation-example"},{"kind":"markdown","html":"As you implement your Payment Module Provider, it can be useful to refer to an existing provider and how it's implemeted.\n\nIf you need to refer to an existing implementation as an example, check the [Stripe Payment Module Provider in the Medusa repository](https://github.com/medusajs/medusa/tree/develop/packages/modules/providers/payment-stripe).\n\n---"},{"kind":"heading","level":2,"text":"Understanding Payment Module Provider Implementation","id":"understanding-payment-module-provider-implementation"},{"kind":"markdown","html":"The Payment Module Provider handles processing payment with a third-party provider. However, it's not responsible for managing payment concepts within Medusa, such as payment sessions or collections. These concepts are handled by the Payment Module which uses your Payment Module Provider within core operations.\n\nFor example, when the merchant captures an order's payment, the Payment Module uses the Payment Module Provider to capture the payment, the makes updates to the `Payment` record associated with the order. So, you only have to implement the third-party payment processing logic in your Payment Module Provider.\n\n\n---"},{"kind":"heading","level":2,"text":"1. Create Module Provider Directory","id":"1-create-module-provider-directory"},{"kind":"markdown","html":"Start by creating a new directory for your module provider.\n\nIf you're creating the module provider in a Medusa application, create it under the `src/modules` directory. For example, `src/modules/my-payment`.\n\nIf you're creating the module provider in a plugin, create it under the `src/providers` directory. For example, `src/providers/my-payment`."},{"kind":"note","variant":"note","html":"The rest of this guide always uses the `src/modules/my-payment` directory as an example."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"2. Create the Payment Module Provider's Service","id":"2-create-the-payment-module-provider-s-service"},{"kind":"markdown","html":"Create the file `src/modules/my-payment/service.ts` that holds the module provider's main service. It must extend the `AbstractPaymentProvider` class imported from `@medusajs/framework/utils`:\n\n```ts title=\"src/modules/my-payment/service.ts\"\nimport { AbstractPaymentProvider } from \"@medusajs/framework/utils\"\n\ntype Options = {\n apiKey: string\n}\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n // TODO implement methods\n}\n\nexport default MyPaymentProviderService\n```"},{"kind":"heading","level":3,"text":"constructor","id":"constructor"},{"kind":"markdown","html":"The constructor allows you to access resources from the [module's container](https://docs.medusajs.com/learn/fundamentals/modules/container)\nusing the first parameter, and the module's options using the second parameter.\n\nIf you're creating a client or establishing a connection with a third-party service, do it in the constructor."},{"kind":"note","variant":"note","html":"A module's options are passed when you register it in the Medusa application."},{"kind":"heading","level":4,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts\nimport { AbstractPaymentProvider } from \"@medusajs/framework/utils\"\nimport { Logger } from \"@medusajs/framework/types\"\n\ntype Options = {\n apiKey: string\n}\n\ntype InjectedDependencies = {\n logger: Logger\n}\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<Options> {\n protected logger_: Logger\n protected options_: Options\n // assuming you're initializing a client\n protected client\n\n constructor(\n container: InjectedDependencies,\n options: Options\n ) {\n super(container, options)\n\n this.logger_ = container.logger\n this.options_ = options\n\n // TODO initialize your client\n }\n // ...\n}\n\nexport default MyPaymentProviderService\n```"},{"kind":"heading","level":4,"text":"Type Parameters","id":"constructor-type-parameters"},{"kind":"typeList","sectionTitle":"constructor","types":[{"name":"TConfig","type":"`object`","description":"The type of the provider's options passed as a second parameter.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":4,"text":"Parameters","id":"constructor-parameters"},{"kind":"typeList","sectionTitle":"constructor","types":[{"name":"cradle","type":"`Record<string, unknown>`","description":"The module's container used to resolve resources.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"identifier","id":"identifier"},{"kind":"markdown","html":"Each payment provider has a unique identifier defined in its class. The provider's ID\nwill be stored as `pp_{identifier}_{id}`, where `{id}` is the provider's `id`\nproperty in the `medusa-config.ts`."},{"kind":"heading","level":4,"text":"Example","id":"example-1"},{"kind":"markdown","html":"```ts\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n static identifier = \"my-payment\"\n // ...\n}\n```"},{"kind":"heading","level":3,"text":"authorizePayment","id":"authorizepayment"},{"kind":"markdown","html":"This method authorizes a payment session using the third-party payment provider.\n\nDuring checkout, the customer may need to perform actions required by the payment provider,\nsuch as entering their card details or confirming the payment. Once that is done,\nthe customer can place their order.\n\nDuring cart-completion before placing the order, this method is used to authorize the cart's payment session with the \nthird-party payment provider. The payment can later be captured\nusing the [capturePayment](/references/payment/provider#capturepayment) method.\n\n\n\nWhen authorized successfully, a `Payment` is created by the Payment\nModule, and it's associated with the order."},{"kind":"heading","level":4,"text":"Deferred / Asynchronous Authorization","id":"deferred-asynchronous-authorization"},{"kind":"markdown","html":"For payment methods where authorization does not happen immediately (e.g., bank transfers,\npayment links, vouchers like OXXO or boleto), return `status: \"pending_authorization\"` instead\nof `\"authorized\"`. This signals that:\n\n- No `Payment` record will be created yet.\n- The cart can still be completed and an order will be created with an \"awaiting\" payment status.\n- This method may be called again later (via a webhook or manual admin action) to check if the\n payment has been completed. When the payment is confirmed, return `\"authorized\"` or `\"captured\"`."},{"kind":"heading","level":4,"text":"Understanding `data` property","id":"understanding-data-property"},{"kind":"markdown","html":"The `data` property of the method's parameter contains the `PaymentSession` record's `data` property, which was\nreturned by the [initiatePayment](/references/payment/provider#initiatepayment) method.\n\nThe `data` property returned by this method is then stored in the created `Payment` record. You can store data relevant to later capture or process the payment.\nFor example, you can store the ID of the payment in the third-party provider to reference it later.\n\n"},{"kind":"heading","level":4,"text":"Example","id":"example-2"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n AuthorizePaymentInput,\n AuthorizePaymentOutput,\n PaymentSessionStatus\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async authorizePayment(\n input: AuthorizePaymentInput\n ): Promise<AuthorizePaymentOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that authorizes the payment\n const paymentData = await this.client.authorizePayment(externalId)\n\n return {\n data: paymentData,\n status: \"authorized\"\n }\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"authorizepayment-parameters"},{"kind":"typeList","sectionTitle":"authorizePayment","types":[{"name":"input","type":"[AuthorizePaymentInput](/references/types/interfaces/types.AuthorizePaymentInput)","description":"The input to authorize the payment. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"authorizepayment-returns"},{"kind":"typeList","sectionTitle":"authorizePayment","types":[{"name":"Promise","type":"Promise<[AuthorizePaymentOutput](/references/types/interfaces/types.AuthorizePaymentOutput)>","optional":false,"defaultValue":"","description":"The status of the authorization, along with the `data` field about the payment. Return `\"pending_authorization\"` for async payment methods. Throws in case of an error.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"cancelPayment","id":"cancelpayment"},{"kind":"markdown","html":"This method cancels a payment in the third-party payment provider. It's used when\nthe admin user cancels an order. The order can only be canceled if the payment\nis not captured yet."},{"kind":"heading","level":4,"text":"Understanding `data` property","id":"understanding-data-property-1"},{"kind":"markdown","html":"The `data` property of the method's parameter contains the `Payment` record's `data` property, which was\nreturned by the [authorizePayment](/references/payment/provider#authorizepayment) method.\n\nThe `data` property returned by this method is then stored in the `Payment` record. You can store data relevant for any further processing of the payment.\n\n"},{"kind":"heading","level":4,"text":"Example","id":"example-3"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n CancelPaymentInput,\n CancelPaymentOutput,\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async cancelPayment(\n input: CancelPaymentInput\n ): Promise<CancelPaymentOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that cancels the payment\n const paymentData = await this.client.cancelPayment(externalId)\n return { data: paymentData }\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"cancelpayment-parameters"},{"kind":"typeList","sectionTitle":"cancelPayment","types":[{"name":"input","type":"[CancelPaymentInput](/references/types/interfaces/types.CancelPaymentInput)","description":"The input to cancel the payment. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"cancelpayment-returns"},{"kind":"typeList","sectionTitle":"cancelPayment","types":[{"name":"Promise","type":"Promise<[CancelPaymentOutput](/references/types/interfaces/types.CancelPaymentOutput)>","optional":false,"defaultValue":"","description":"The new data to store in the payment's `data` property, if any. Throws in case of an error.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"capturePayment","id":"capturepayment"},{"kind":"markdown","html":"This method captures a payment using the third-party provider. In this method, use the third-party provider to capture the payment.\n\nWhen an order is placed, the payment is authorized using the [authorizePayment](/references/payment/provider#authorizepayment) method. Then, the admin\nuser can capture the payment, which triggers this method.\n\n\n\nThis method can also be triggered by a webhook event if the [getWebhookActionAndData](/references/payment/provider#getwebhookactionanddata) method returns the action `captured`."},{"kind":"heading","level":4,"text":"Understanding `data` property","id":"understanding-data-property-2"},{"kind":"markdown","html":"The `data` property of the input parameter contains data that was previously stored in the Payment record's `data` property, which was\nreturned by the [authorizePayment](/references/payment/provider#authorizepayment) method.\n\nThe `data` property returned by this method is then stored in the `Payment` record. You can store data relevant to later refund or process the payment.\nFor example, you can store the ID of the payment in the third-party provider to reference it later.\n\n"},{"kind":"heading","level":4,"text":"Example","id":"example-4"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n CapturePaymentInput,\n CapturePaymentOutput,\n} from \"@medusajs/framework/types\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async capturePayment(\n input: CapturePaymentInput\n ): Promise<CapturePaymentOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that captures the payment\n const newData = await this.client.capturePayment(externalId)\n return {\n data: {\n ...newData,\n id: externalId,\n }\n }\n }\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"capturepayment-parameters"},{"kind":"typeList","sectionTitle":"capturePayment","types":[{"name":"input","type":"[CapturePaymentInput](/references/types/interfaces/types.CapturePaymentInput)","description":"The input to capture the payment. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"capturepayment-returns"},{"kind":"typeList","sectionTitle":"capturePayment","types":[{"name":"Promise","type":"Promise<[CapturePaymentOutput](/references/types/interfaces/types.CapturePaymentOutput)>","optional":false,"defaultValue":"","description":"The new data to store in the payment's `data` property. Throws in case of an error.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"createAccountHolder","id":"createaccountholder"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used when creating an account holder in Medusa, allowing you to create\nthe equivalent account in the third-party payment provider. An account holder is useful to\nlater save payment methods, such as credit cards, for a customer in the\nthird-party payment provider using the [savePaymentMethod](/references/types/interfaces/types.IPaymentProvider#savepaymentmethod) method.\n\nThe returned data will be stored in the account holder created in Medusa. For example,\nthe returned `id` property will be stored in the account holder's `external_id` property.\n\nMedusa creates an account holder when a payment session initialized for a registered customer."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.5.0](https://github.com/medusajs/medusa/releases/tag/v2.5.0)."},{"kind":"heading","level":4,"text":"Example","id":"example-5"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async createAccountHolder({ context, data }: CreateAccountHolderInput) {\n const { account_holder, customer } = context\n\n if (account_holder?.data?.id) {\n return { id: account_holder.data.id as string }\n }\n\n if (!customer) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"Missing customer data.\"\n )\n }\n\n // assuming you have a client that creates the account holder\n const providerAccountHolder = await this.client.createAccountHolder({\n email: customer.email,\n ...data\n })\n\n return {\n id: providerAccountHolder.id,\n data: providerAccountHolder as unknown as Record<string, unknown>\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"createaccountholder-parameters"},{"kind":"typeList","sectionTitle":"createAccountHolder","types":[{"name":"data","type":"[CreateAccountHolderInput](/references/types/interfaces/types.CreateAccountHolderInput)","description":"Input data including the details of the account holder to create.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"Omit<[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext), \"customer\"> & `object`","description":"The context of creating the account holder.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The account holder's associated customer details.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"createaccountholder-returns"},{"kind":"typeList","sectionTitle":"createAccountHolder","types":[{"name":"Promise","type":"Promise<[CreateAccountHolderOutput](/references/types/interfaces/types.CreateAccountHolderOutput)>","optional":false,"defaultValue":"","description":"The result of creating the account holder. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.5.0"}]},{"kind":"heading","level":3,"text":"deleteAccountHolder","id":"deleteaccountholder"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used when an account holder is deleted in Medusa, allowing you\nto also delete the equivalent account holder in the third-party payment provider."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.5.0](https://github.com/medusajs/medusa/releases/tag/v2.5.0)."},{"kind":"heading","level":4,"text":"Example","id":"example-6"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async deleteAccountHolder({ context }: DeleteAccountHolderInput) {\n const { account_holder } = context\n const accountHolderId = account_holder?.data?.id as string | undefined\n if (!accountHolderId) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"Missing account holder ID.\"\n )\n }\n\n // assuming you have a client that deletes the account holder\n await this.client.deleteAccountHolder({\n id: accountHolderId\n })\n\n return {}\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"deleteaccountholder-parameters"},{"kind":"typeList","sectionTitle":"deleteAccountHolder","types":[{"name":"data","type":"[DeleteAccountHolderInput](/references/types/interfaces/types.DeleteAccountHolderInput)","description":"Input data including the details of the account holder to delete.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"Omit<[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext), \"account_holder\"> & `object`","description":"The context of deleting the account holder.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"account_holder","type":"Partial<[AccountHolderDTO](/references/payment/interfaces/payment.AccountHolderDTO)>","description":"The account holder's details.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"deleteaccountholder-returns"},{"kind":"typeList","sectionTitle":"deleteAccountHolder","types":[{"name":"Promise","type":"Promise<[DeleteAccountHolderOutput](/references/types/interfaces/types.DeleteAccountHolderOutput)>","optional":false,"defaultValue":"","description":"The result of deleting the account holder. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.5.0"}]},{"kind":"heading","level":3,"text":"deletePayment","id":"deletepayment"},{"kind":"markdown","html":"This method deletes a payment session in the third-party payment provider.\n\nWhen a customer chooses a payment method during checkout, then chooses a different one,\nthis method is triggered to delete the previous payment session.\n\nIf your provider doesn't support deleting a payment session, you can just return an empty object or\nan object that contains the same received `data` property.\n\n"},{"kind":"heading","level":4,"text":"Understanding `data` property","id":"understanding-data-property-3"},{"kind":"markdown","html":"The `data` property of the method's parameter contains the `PaymentSession` record's `data` property, which was\nreturned by the [initiatePayment](/references/payment/provider#initiatepayment) method.\n\n"},{"kind":"heading","level":4,"text":"Example","id":"example-7"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n DeletePaymentInput,\n DeletePaymentOutput,\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async deletePayment(\n input: DeletePaymentInput\n ): Promise<DeletePaymentOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that cancels the payment\n await this.client.cancelPayment(externalId)\n return {\n data: input.data\n }\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"deletepayment-parameters"},{"kind":"typeList","sectionTitle":"deletePayment","types":[{"name":"input","type":"[DeletePaymentInput](/references/types/interfaces/types.DeletePaymentInput)","description":"The input to delete the payment session. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"deletepayment-returns"},{"kind":"typeList","sectionTitle":"deletePayment","types":[{"name":"Promise","type":"Promise<[DeletePaymentOutput](/references/types/interfaces/types.DeletePaymentOutput)>","optional":false,"defaultValue":"","description":"The new data to store in the payment's `data` property, if any. Throws in case of an error.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"deletePaymentMethod","id":"deletepaymentmethod"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used to delete a customer's saved payment method from the\nthird-party payment provider. A payment provider that supports saving payment methods\nshould implement this method."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.16.0](https://github.com/medusajs/medusa/releases/tag/v2.16.0)."},{"kind":"heading","level":4,"text":"Example","id":"example-8"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async deletePaymentMethod({ data }: DeletePaymentMethodInput) {\n const paymentMethodId = data.id\n\n if (!paymentMethodId) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"Missing payment method ID.\"\n )\n }\n\n // assuming you have a client that deletes the payment method\n await this.client.deletePaymentMethod(paymentMethodId)\n\n return {}\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"deletepaymentmethod-parameters"},{"kind":"typeList","sectionTitle":"deletePaymentMethod","types":[{"name":"data","type":"[DeletePaymentMethodInput](/references/types/interfaces/types.DeletePaymentMethodInput)","description":"The details of the payment method to delete.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"data","type":"`object` & `Record<string, unknown>`","description":"The payment method's data.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the payment method in the payment provider.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"deletepaymentmethod-returns"},{"kind":"typeList","sectionTitle":"deletePaymentMethod","types":[{"name":"Promise","type":"Promise<[DeletePaymentMethodOutput](/references/types/interfaces/types.DeletePaymentMethodOutput)>","optional":false,"defaultValue":"","description":"The result of deleting the payment method. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.16.0"}]},{"kind":"heading","level":3,"text":"getPaymentStatus","id":"getpaymentstatus"},{"kind":"markdown","html":"This method gets the status of a payment session based on the status in the third-party integration."},{"kind":"heading","level":4,"text":"Example","id":"example-9"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n GetPaymentStatusInput,\n GetPaymentStatusOutput,\n PaymentSessionStatus\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async getPaymentStatus(\n input: GetPaymentStatusInput\n ): Promise<GetPaymentStatusOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that retrieves the payment status\n const status = await this.client.getStatus(externalId)\n\n switch (status) {\n case \"requires_capture\":\n return {status: \"authorized\"}\n case \"success\":\n return {status: \"captured\"}\n case \"canceled\":\n return {status: \"canceled\"}\n default:\n return {status: \"pending\"}\n }\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"getpaymentstatus-parameters"},{"kind":"typeList","sectionTitle":"getPaymentStatus","types":[{"name":"input","type":"[GetPaymentStatusInput](/references/types/interfaces/types.GetPaymentStatusInput)","description":"The input to get the payment status. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"getpaymentstatus-returns"},{"kind":"typeList","sectionTitle":"getPaymentStatus","types":[{"name":"Promise","type":"Promise<[GetPaymentStatusOutput](/references/types/interfaces/types.GetPaymentStatusOutput)>","optional":false,"defaultValue":"","description":"The payment session's status. It can also return additional `data` from the payment provider.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"getWebhookActionAndData","id":"getwebhookactionanddata"},{"kind":"markdown","html":"This method is executed when a webhook event is received from the third-party payment provider. Medusa uses\nthe data returned by this method to perform actions in the Medusa application, such as completing the associated cart\nif the payment was authorized successfully.\n\nLearn more in the [Webhook Events](https://docs.medusajs.com/resources/commerce-modules/payment/webhook-events) documentation."},{"kind":"heading","level":4,"text":"Example","id":"example-10"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n BigNumber\n} from \"@medusajs/framework/utils\"\nimport {\n ProviderWebhookPayload,\n WebhookActionResult\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async getWebhookActionAndData(\n payload: ProviderWebhookPayload[\"payload\"]\n ): Promise<WebhookActionResult> {\n const {\n data,\n rawData,\n headers\n } = payload\n\n try {\n switch(data.event_type) {\n case \"authorized_amount\":\n return {\n action: \"authorized\",\n data: {\n // assuming the session_id is stored in the metadata of the payment\n // in the third-party provider\n session_id: (data.metadata as Record<string, any>).session_id,\n amount: new BigNumber(data.amount as number)\n }\n }\n case \"success\":\n return {\n action: \"captured\",\n data: {\n // assuming the session_id is stored in the metadata of the payment\n // in the third-party provider\n session_id: (data.metadata as Record<string, any>).session_id,\n amount: new BigNumber(data.amount as number)\n }\n }\n default:\n return {\n action: \"not_supported\",\n data: {\n session_id: \"\",\n amount: new BigNumber(0)\n }\n }\n }\n } catch (e) {\n return {\n action: \"failed\",\n data: {\n // assuming the session_id is stored in the metadata of the payment\n // in the third-party provider\n session_id: (data.metadata as Record<string, any>).session_id,\n amount: new BigNumber(data.amount as number)\n }\n }\n }\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"getwebhookactionanddata-parameters"},{"kind":"typeList","sectionTitle":"getWebhookActionAndData","types":[{"name":"data","type":"`object`","description":"The webhook event's data","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"The parsed webhook body.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"rawData","type":"`string` \\| Buffer<ArrayBufferLike>","description":"The raw webhook request body.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"headers","type":"`Record<string, unknown>`","description":"The headers of the webhook request.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"getwebhookactionanddata-returns"},{"kind":"typeList","sectionTitle":"getWebhookActionAndData","types":[{"name":"Promise","type":"Promise<[WebhookActionResult](/references/types/interfaces/types.WebhookActionResult)>","optional":false,"defaultValue":"","description":"The webhook result. If the `action`'s value is `captured`, the payment is captured within Medusa as well.\nIf the `action`'s value is `authorized`, the associated payment session is authorized within Medusa and the associated cart\nwill be completed to create an order.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"initiatePayment","id":"initiatepayment"},{"kind":"markdown","html":"This method initializes a payment session with the third-party payment provider.\n\nWhen a customer chooses a payment method during checkout, this method is triggered to\nperform any initialization action with the third-party provider, such as creating a payment session.\n\n"},{"kind":"heading","level":4,"text":"Understanding `data` property","id":"understanding-data-property-4"},{"kind":"markdown","html":"The `data` property returned by this method will be stored in the created `PaymentSession` record. You can store data relevant to later authorize or process the payment.\nFor example, you can store the ID of the payment session in the third-party provider to reference it later.\n\nThe `data` property is also available to storefronts, allowing you to store data necessary for the storefront to integrate\nthe payment provider in the checkout flow. For example, you can store the client token to use with the payment provider's SDK."},{"kind":"note","variant":"note","html":"This also means you shouldn't store sensitive data and tokens in the `data` property, as it's publicly accessible."},{"kind":"markdown","html":""},{"kind":"heading","level":4,"text":"Example","id":"example-11"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n InitiatePaymentInput,\n InitiatePaymentOutput,\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async initiatePayment(\n input: InitiatePaymentInput\n ): Promise<InitiatePaymentOutput> {\n const {\n amount,\n currency_code,\n context: customerDetails\n } = input\n\n // assuming you have a client that initializes the payment\n const response = await this.client.init(\n amount, currency_code, customerDetails\n )\n\n return {\n id: response.id,\n data: response,\n }\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"initiatepayment-parameters"},{"kind":"typeList","sectionTitle":"initiatePayment","types":[{"name":"input","type":"[InitiatePaymentInput](/references/types/interfaces/types.InitiatePaymentInput)","description":"The input to create the payment session.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"amount","type":"[BigNumberInput](/references/types/types/types.BigNumberInput)","description":"The amount to be authorized.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"value","type":"`string` \\| `number`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"numeric","type":"`number`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw","type":"[BigNumberRawValue](/references/types/types/types.BigNumberRawValue)","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"bigNumber","type":"`BigNumber`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"currency_code","type":"`string`","description":"The ISO 3 character currency code.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"initiatepayment-returns"},{"kind":"typeList","sectionTitle":"initiatePayment","types":[{"name":"Promise","type":"Promise<[InitiatePaymentOutput](/references/types/interfaces/types.InitiatePaymentOutput)>","optional":false,"defaultValue":"","description":"The new data to store in the payment's `data` property. Throws in case of an error.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"listPaymentMethods","id":"listpaymentmethods"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used to retrieve the list of saved payment methods for an account holder\nin the third-party payment provider. A payment provider that supports saving payment methods\nmust implement this method."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.5.0](https://github.com/medusajs/medusa/releases/tag/v2.5.0)."},{"kind":"heading","level":4,"text":"Example","id":"example-12"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async listPaymentMethods({ context }: ListPaymentMethodsInput) {\n const { account_holder } = context\n const accountHolderId = account_holder?.data?.id as string | undefined\n\n if (!accountHolderId) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"Missing account holder ID.\"\n )\n }\n\n // assuming you have a client that lists the payment methods\n const paymentMethods = await this.client.listPaymentMethods({\n customer_id: accountHolderId\n })\n\n return paymentMethods.map((pm) => ({\n id: pm.id,\n data: pm as unknown as Record<string, unknown>\n }))\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"listpaymentmethods-parameters"},{"kind":"typeList","sectionTitle":"listPaymentMethods","types":[{"name":"data","type":"[ListPaymentMethodsInput](/references/types/interfaces/types.ListPaymentMethodsInput)","description":"Input data including the details of the account holder to list payment methods for.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"listpaymentmethods-returns"},{"kind":"typeList","sectionTitle":"listPaymentMethods","types":[{"name":"Promise","type":"Promise<[ListPaymentMethodsOutput](/references/types/interfaces/types.ListPaymentMethodsOutput)>","optional":false,"defaultValue":"","description":"The list of payment methods saved for the account holder. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.5.0"}]},{"kind":"heading","level":3,"text":"refundPayment","id":"refundpayment"},{"kind":"markdown","html":"This method refunds an amount using the third-party payment provider. This method\nis triggered when the admin user refunds a payment of an order."},{"kind":"heading","level":4,"text":"Understanding `data` property","id":"understanding-data-property-5"},{"kind":"markdown","html":"The `data` property of the method's parameter contains the `Payment` record's `data` property, which was\nreturned by the [capturePayment](/references/payment/provider#capturepayment) or [refundPayment](/references/payment/provider#refundpayment) method.\n\nThe `data` property returned by this method is then stored in the `Payment` record. You can store data relevant to later refund or process the payment.\nFor example, you can store the ID of the payment in the third-party provider to reference it later."},{"kind":"note","variant":"note","html":"A payment may be refunded multiple times with different amounts. In this case, the `data` property\nof the input parameter contains the data from the last refund."},{"kind":"markdown","html":""},{"kind":"heading","level":4,"text":"Example","id":"example-13"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n RefundPaymentInput,\n RefundPaymentOutput,\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async refundPayment(\n input: RefundPaymentInput\n ): Promise<RefundPaymentOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that refunds the payment\n const newData = await this.client.refund(\n externalId,\n input.amount\n )\n\n return {\n data: input.data,\n }\n }\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"refundpayment-parameters"},{"kind":"typeList","sectionTitle":"refundPayment","types":[{"name":"input","type":"[RefundPaymentInput](/references/types/interfaces/types.RefundPaymentInput)","description":"The input to refund the payment. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"amount","type":"[BigNumberInput](/references/types/types/types.BigNumberInput)","description":"The amount to refund.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"value","type":"`string` \\| `number`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"numeric","type":"`number`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw","type":"[BigNumberRawValue](/references/types/types/types.BigNumberRawValue)","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"bigNumber","type":"`BigNumber`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"refundpayment-returns"},{"kind":"typeList","sectionTitle":"refundPayment","types":[{"name":"Promise","type":"Promise<[RefundPaymentOutput](/references/types/interfaces/types.RefundPaymentOutput)>","optional":false,"defaultValue":"","description":"The new data to store in the payment's `data` property, or an error object.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"retrieveAccountHolder","id":"retrieveaccountholder"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used when retrieving an account holder in Medusa, allowing you to retrieve\nthe equivalent account in the third-party payment provider. An account holder is useful to\nlater save payment methods, such as credit cards, for a customer in the\nthird-party payment provider using the [savePaymentMethod](/references/types/interfaces/types.IPaymentProvider#savepaymentmethod) method."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0)."},{"kind":"heading","level":4,"text":"Example","id":"example-14"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async retrieveAccountHolder({ id }: RetrieveAccountHolderInput) {\n\n // assuming you have a client that retrieves the account holder\n const providerAccountHolder = await this.client.retrieveAccountHolder({\n id\n })\n\n return {\n id: providerAccountHolder.id,\n data: providerAccountHolder as unknown as Record<string, unknown>\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"retrieveaccountholder-parameters"},{"kind":"typeList","sectionTitle":"retrieveAccountHolder","types":[{"name":"data","type":"[RetrieveAccountHolderInput](/references/types/interfaces/types.RetrieveAccountHolderInput)","description":"Input data including the details of the account holder to retrieve.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"id","type":"`string`","description":"The ID of the account holder to retrieve.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"retrieveaccountholder-returns"},{"kind":"typeList","sectionTitle":"retrieveAccountHolder","types":[{"name":"Promise","type":"Promise<[RetrieveAccountHolderOutput](/references/types/interfaces/types.RetrieveAccountHolderOutput)>","optional":false,"defaultValue":"","description":"The retrieved account holder. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.11.0"}]},{"kind":"heading","level":3,"text":"retrievePayment","id":"retrievepayment"},{"kind":"markdown","html":"This method retrieves the payment's data from the third-party payment provider."},{"kind":"heading","level":4,"text":"Example","id":"example-15"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n RetrievePaymentInput,\n RetrievePaymentOutput,\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async retrievePayment(\n input: RetrievePaymentInput\n ): Promise<RetrievePaymentOutput> {\n const externalId = input.data?.id\n\n // assuming you have a client that retrieves the payment\n return await this.client.retrieve(externalId)\n }\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"retrievepayment-parameters"},{"kind":"typeList","sectionTitle":"retrievePayment","types":[{"name":"input","type":"[RetrievePaymentInput](/references/types/interfaces/types.RetrievePaymentInput)","description":"The input to retrieve the payment. The `data` field should contain the data from the payment provider when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"retrievepayment-returns"},{"kind":"typeList","sectionTitle":"retrievePayment","types":[{"name":"Promise","type":"Promise<[RetrievePaymentOutput](/references/types/interfaces/types.RetrievePaymentOutput)>","optional":false,"defaultValue":"","description":"The payment's data as found in the payment provider.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"savePaymentMethod","id":"savepaymentmethod"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used to save a customer's payment method, such as a credit card, in the\nthird-party payment provider. A payment provider that supports saving payment methods\nmust implement this method."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.5.0](https://github.com/medusajs/medusa/releases/tag/v2.5.0)."},{"kind":"heading","level":4,"text":"Example","id":"example-16"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async savePaymentMethod({ context, data }: SavePaymentMethodInput) { *\n const accountHolderId = context?.account_holder?.data?.id as\n | string\n | undefined\n\n if (!accountHolderId) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"Missing account holder ID.\"\n )\n }\n\n // assuming you have a client that saves the payment method\n const paymentMethod = await this.client.savePaymentMethod({\n customer_id: accountHolderId,\n ...data\n })\n\n return {\n id: paymentMethod.id,\n data: paymentMethod as unknown as Record<string, unknown>\n }\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"savepaymentmethod-parameters"},{"kind":"typeList","sectionTitle":"savePaymentMethod","types":[{"name":"data","type":"[SavePaymentMethodInput](/references/types/interfaces/types.SavePaymentMethodInput)","description":"The details of the payment method to save.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"savepaymentmethod-returns"},{"kind":"typeList","sectionTitle":"savePaymentMethod","types":[{"name":"Promise","type":"Promise<[SavePaymentMethodOutput](/references/types/interfaces/types.SavePaymentMethodOutput)>","optional":false,"defaultValue":"","description":"The result of saving the payment method. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.5.0"}]},{"kind":"heading","level":3,"text":"updateAccountHolder","id":"updateaccountholder"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method is used when updating an account holder in Medusa, allowing you to update\nthe equivalent account in the third-party payment provider.\n\nThe returned data will be stored in the account holder created in Medusa. For example,\nthe returned `id` property will be stored in the account holder's `external_id` property."},{"kind":"note","variant":"note","html":"This is available starting from [Medusa v2.5.1](https://github.com/medusajs/medusa/releases/tag/v2.5.1)."},{"kind":"heading","level":4,"text":"Example","id":"example-17"},{"kind":"markdown","html":"```ts\nimport { MedusaError } from \"@medusajs/framework/utils\"\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async updateAccountHolder({ context, data }: UpdateAccountHolderInput) {\n const { account_holder, customer } = context\n\n if (!account_holder?.data?.id) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"Missing account holder ID.\"\n )\n }\n\n // assuming you have a client that updates the account holder\n const providerAccountHolder = await this.client.updateAccountHolder({\n id: account_holder.data.id,\n ...data\n })\n\n return {\n id: providerAccountHolder.id,\n data: providerAccountHolder as unknown as Record<string, unknown>\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"updateaccountholder-parameters"},{"kind":"typeList","sectionTitle":"updateAccountHolder","types":[{"name":"data","type":"[UpdateAccountHolderInput](/references/types/interfaces/types.UpdateAccountHolderInput)","description":"Input data including the details of the account holder to update.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext) & `object`","description":"The context of updating the account holder.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder's details.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"updateaccountholder-returns"},{"kind":"typeList","sectionTitle":"updateAccountHolder","types":[{"name":"Promise","type":"Promise<[UpdateAccountHolderOutput](/references/types/interfaces/types.UpdateAccountHolderOutput)>","optional":false,"defaultValue":"","description":"The result of updating the account holder. If an error occurs, throw it.","expandable":false,"children":[],"since":"2.5.1"}]},{"kind":"heading","level":3,"text":"updatePayment","id":"updatepayment"},{"kind":"markdown","html":"This method updates a payment in the third-party service that was previously initiated with the [initiatePayment](/references/payment/provider#initiatepayment) method."},{"kind":"heading","level":4,"text":"Example","id":"example-18"},{"kind":"markdown","html":"```ts\n// other imports...\nimport {\n UpdatePaymentInput,\n UpdatePaymentOutput,\n} from \"@medusajs/framework/types\"\n\n\nclass MyPaymentProviderService extends AbstractPaymentProvider<\n Options\n> {\n async updatePayment(\n input: UpdatePaymentInput\n ): Promise<UpdatePaymentOutput> {\n const { amount, currency_code, context } = input\n const externalId = input.data?.id\n\n // Validate context.customer\n if (!context || !context.customer) {\n throw new Error(\"Context must include a valid customer.\");\n }\n\n // assuming you have a client that updates the payment\n const response = await this.client.update(\n externalId,\n {\n amount,\n currency_code,\n customer: context.customer\n }\n )\n\n return response\n }\n\n // ...\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"updatepayment-parameters"},{"kind":"typeList","sectionTitle":"updatePayment","types":[{"name":"input","type":"[UpdatePaymentInput](/references/types/interfaces/types.UpdatePaymentInput)","description":"The input to update the payment. The `data` field should contain the data from the payment provider. when the payment was created.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"data","type":"`Record<string, unknown>`","description":"Data is a combination of the input from the user and what is stored in the database for the associated model.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[PaymentProviderContext](/references/types/interfaces/types.PaymentProviderContext)","description":"The context for this payment operation. The data is guaranteed to be validated and not directly provided by the user.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"account_holder","type":"[PaymentAccountHolderDTO](/references/types/types/types.PaymentAccountHolderDTO)","description":"The account holder information, if available for the payment provider.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"customer","type":"[PaymentCustomerDTO](/references/types/types/types.PaymentCustomerDTO)","description":"The customer information from Medusa.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotency_key","type":"`string`","description":"Idempotency key for the request, if the payment provider supports it. It will be ignored otherwise.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"amount","type":"[BigNumberInput](/references/types/types/types.BigNumberInput)","description":"The payment session's amount.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"value","type":"`string` \\| `number`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"numeric","type":"`number`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw","type":"[BigNumberRawValue](/references/types/types/types.BigNumberRawValue)","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"bigNumber","type":"`BigNumber`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"currency_code","type":"`string`","description":"The ISO 3 character code of the payment session.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"updatepayment-returns"},{"kind":"typeList","sectionTitle":"updatePayment","types":[{"name":"Promise","type":"Promise<[UpdatePaymentOutput](/references/types/interfaces/types.UpdatePaymentOutput)>","optional":false,"defaultValue":"","description":"The new data to store in the payment's `data` property. Throws in case of an error.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"validateOptions","id":"validateoptions"},{"kind":"markdown","html":"This method validates the options of the provider set in `medusa-config.ts`.\nImplementing this method is optional, but it's useful to ensure that the required\noptions are passed to the provider, or if you have any custom validation logic.\n\nIf the options aren't valid, throw an error."},{"kind":"heading","level":4,"text":"Example","id":"example-19"},{"kind":"markdown","html":"```ts\nclass MyPaymentProviderService extends AbstractPaymentProvider<Options> {\n static validateOptions(options: Record<any, any>) {\n if (!options.apiKey) {\n throw new MedusaError(\n MedusaError.Types.INVALID_DATA,\n \"API key is required in the provider's options.\"\n )\n }\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"validateoptions-parameters"},{"kind":"typeList","sectionTitle":"validateOptions","types":[{"name":"options","type":"`Record<any, any>`","description":"The provider's options passed in `medusa-config.ts`.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"3. Create Module Provider Definition File","id":"3-create-module-provider-definition-file"},{"kind":"markdown","html":"Create the file `src/modules/my-payment/index.ts` with the following content:\n\n```ts title=\"src/modules/my-payment/index.ts\"\nimport MyPaymentProviderService from \"./service\"\nimport { \n ModuleProvider, \n Modules\n} from \"@medusajs/framework/utils\"\n\nexport default ModuleProvider(Modules.PAYMENT, {\n services: [MyPaymentProviderService],\n})\n```\n\nThis exports the module provider's definition, indicating that the `MyPaymentProviderService` is the module provider's service."},{"kind":"note","variant":"note","title":"Tip","html":"A payment module provider can have export multiple provider services, where each are registered as a separate payment provider."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"4. Use Module Provider","id":"4-use-module-provider"},{"kind":"markdown","html":"To use your Payment Module Provider, add it to the `providers` array of the Payment Module in `medusa-config.ts`:\n\n```ts title=\"medusa-config.ts\"\nmodule.exports = defineConfig({\n // ...\n modules: [\n {\n resolve: \"@medusajs/medusa/payment\",\n options: {\n providers: [\n {\n // if module provider is in a plugin, use `plugin-name/providers/my-payment`\n resolve: \"./src/modules/my-payment\",\n id: \"my-payment\",\n options: {\n // provider options...\n apiKey: \"...\"\n }\n }\n ]\n }\n }\n ]\n})\n```\n\n\n---"},{"kind":"heading","level":2,"text":"5. Test it Out","id":"5-test-it-out"},{"kind":"markdown","html":"Before you use your Payment Module Provider, enable it in a region using the Medusa Admin.\n\nThen, go through checkout to place an order. Your Payment Module Provider is used to authorize the payment.\n\n\n---"},{"kind":"heading","level":2,"text":"Useful Guides","id":"useful-guides"},{"kind":"markdown","html":"- [Storefront Guide: how to implement UI for your Payment Module Provider during checkout](https://docs.medusajs.com/resources/storefront-development/checkout/payment)"}]} |