IPaymentProvider

Methods#

initiatePayment#

Parameters

The data used initiate a payment in a provider when a payment session is created.

Returns

PromisePromise<InitiatePaymentOutput>

updatePayment#

Parameters

The attributes to update a payment related to a payment session in a provider.

Returns

PromisePromise<UpdatePaymentOutput>

deletePayment#

Parameters

The data to delete a payment.

Returns

PromisePromise<DeletePaymentOutput>

authorizePayment#

Parameters

The data to authorize a payment.

Returns

PromisePromise<AuthorizePaymentOutput>

capturePayment#

Parameters

The data to capture a payment.

Returns

PromisePromise<CapturePaymentOutput>

refundPayment#

Parameters

The data to refund a payment.

Returns

PromisePromise<RefundPaymentOutput>

retrievePayment#

Parameters

The data to retrieve a payment.

Returns

PromisePromise<RetrievePaymentOutput>

cancelPayment#

Parameters

The data to cancel a payment.

Returns

PromisePromise<CancelPaymentOutput>

createAccountHolder#

optional

This method is used when creating an account holder in Medusa, allowing you to create the equivalent account in the third-party payment provider. An account holder is useful to later save payment methods, such as credit cards, for a customer in the third-party payment provider using the savePaymentMethod method.

The returned data will be stored in the account holder created in Medusa. For example, the returned id property will be stored in the account holder's external_id property.

Medusa creates an account holder when a payment session initialized for a registered customer.

Note: This is only available after Medusa v2.5.0.

Example

Code
1import { MedusaError } from "@medusajs/framework/utils"2
3class MyPaymentProviderService extends AbstractPaymentProvider<4 Options5> {6 async createAccountHolder({ context, data }: CreateAccountHolderInput) {7  const { account_holder, customer } = context8
9  if (account_holder?.data?.id) {10    return { id: account_holder.data.id as string }11  }12
13  if (!customer) {14    throw new MedusaError(15      MedusaError.Types.INVALID_DATA,16      "Missing customer data."17    )18  }19
20  // assuming you have a client that creates the account holder21  const providerAccountHolder = await this.client.createAccountHolder({22    email: customer.email,23   ...data24  })25
26  return {27    id: providerAccountHolder.id,28    data: providerAccountHolder as unknown as Record<string, unknown>29  }30}

Parameters

Input data including the details of the account holder to create.

Returns

The result of creating the account holder. If an error occurs, throw it.

updateAccountHolder#

optional

This method is used when updating an account holder in Medusa, allowing you to update the equivalent account in the third-party payment provider.

The returned data will be stored in the account holder created in Medusa. For example, the returned id property will be stored in the account holder's external_id property.

Note: This is only available after Medusa v2.5.1.

Example

Code
1import { MedusaError } from "@medusajs/framework/utils"2
3class MyPaymentProviderService extends AbstractPaymentProvider<4 Options5> {6 async updateAccountHolder({ context, data }: UpdateAccountHolderInput) {7  const { account_holder, customer } = context8
9  if (!account_holder?.data?.id) {10    throw new MedusaError(11      MedusaError.Types.INVALID_DATA,12      "Missing account holder ID."13    )14  }15
16  // assuming you have a client that updates the account holder17  const providerAccountHolder = await this.client.updateAccountHolder({18    id: account_holder.data.id,19   ...data20  })21
22  return {23    id: providerAccountHolder.id,24    data: providerAccountHolder as unknown as Record<string, unknown>25  }26}

Parameters

Input data including the details of the account holder to update.

Returns

The result of updating the account holder. If an error occurs, throw it.

deleteAccountHolder#

optional

This method is used when an account holder is deleted in Medusa, allowing you to also delete the equivalent account holder in the third-party payment provider.

Note: This is only available after Medusa v2.5.0.

Example

Code
1import { MedusaError } from "@medusajs/framework/utils"2
3class MyPaymentProviderService extends AbstractPaymentProvider<4 Options5> {6  async deleteAccountHolder({ context }: DeleteAccountHolderInput) {7    const { account_holder } = context8    const accountHolderId = account_holder?.data?.id as string | undefined9    if (!accountHolderId) {10      throw new MedusaError(11        MedusaError.Types.INVALID_DATA,12        "Missing account holder ID."13      )14    }15
16    // assuming you have a client that deletes the account holder17    await this.client.deleteAccountHolder({18      id: accountHolderId19    })20
21    return {}22  }23}

Parameters

Input data including the details of the account holder to delete.

Returns

The result of deleting the account holder. If an error occurs, throw it.

listPaymentMethods#

optional

This method is used to retrieve the list of saved payment methods for an account holder in the third-party payment provider. A payment provider that supports saving payment methods must implement this method.

Note: This is only available after Medusa v2.5.0.

Example

Code
1import { MedusaError } from "@medusajs/framework/utils"2
3class MyPaymentProviderService extends AbstractPaymentProvider<4  Options5> {6  async listPaymentMethods({ context }: ListPaymentMethodsInput) {7    const { account_holder } = context8    const accountHolderId = account_holder?.data?.id as string | undefined9
10    if (!accountHolderId) {11      throw new MedusaError(12        MedusaError.Types.INVALID_DATA,13        "Missing account holder ID."14      )15    }16
17   // assuming you have a client that lists the payment methods18   const paymentMethods = await this.client.listPaymentMethods({19     customer_id: accountHolderId20   })21
22   return paymentMethods.map((pm) => ({23     id: pm.id,24     data: pm as unknown as Record<string, unknown>25   }))26 }27}

Parameters

Input data including the details of the account holder to list payment methods for.

Returns

PromisePromise<ListPaymentMethodsOutput>
The list of payment methods saved for the account holder. If an error occurs, throw it.

savePaymentMethod#

optional

This method is used to save a customer's payment method, such as a credit card, in the third-party payment provider. A payment provider that supports saving payment methods must implement this method.

Note: This is only available after Medusa v2.5.0.

Example

Code
1import { MedusaError } from "@medusajs/framework/utils"2
3class MyPaymentProviderService extends AbstractPaymentProvider<4  Options5> {6  async savePaymentMethod({ context, data }: SavePaymentMethodInput) {   *7    const accountHolderId = context?.account_holder?.data?.id as8      | string9      | undefined10
11    if (!accountHolderId) {12      throw new MedusaError(13        MedusaError.Types.INVALID_DATA,14        "Missing account holder ID."15      )16    }17
18   // assuming you have a client that saves the payment method19   const paymentMethod = await this.client.savePaymentMethod({20     customer_id: accountHolderId,21     ...data22   })23
24  return {25    id: paymentMethod.id,26    data: paymentMethod as unknown as Record<string, unknown>27  }28 }29}

Parameters

The details of the payment method to save.

Returns

PromisePromise<SavePaymentMethodOutput>
The result of saving the payment method. If an error occurs, throw it.

getPaymentStatus#

Parameters

The data to get the payment status.

Returns

PromisePromise<GetPaymentStatusOutput>

getWebhookActionAndData#

Parameters

dataobject

Returns

PromisePromise<WebhookActionResult>
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break