Accept Payment in Checkout Flow
In this guide, you'll learn how to implement it using workflows or the Payment Module.
Why Implement the Payment Flow?#
Medusa already provides a built-in payment flow that allows you to accept payments from customers, which you can learn about in the Accept Payment Flow in Checkout guide.
You may need to implement a custom payment flow if you have a different use case, or you're using the Payment Module separately from the Medusa application.
This guide will help you understand how to implement a payment flow using the Payment Module's main service or workflows.
You can also follow this guide to get a general understanding of how the payment flow works in the Medusa application.
How to Implement the Accept Payment Flow?#
1. Create a Payment Collection#
A payment collection holds all details related to a resource’s payment operations. So, you start off by creating a payment collection.
In the Medusa application, you associate the payment collection with a cart, which is the resource that the customer is trying to pay for.
For example:
2. Show Payment Providers#
Next, you'll show the customer the available payment providers to choose from.
In the Medusa application, you need to use Query to retrieve the available payment providers in a region.
3. Create Payment Sessions#
The payment collection has one or more payment sessions, each being a payment amount to be authorized by a payment provider.
So, once the customer selects a payment provider, create a payment session for the selected payment provider.
This will also initialize the payment session in the third-party payment provider.
For example:
4. Authorize Payment Session#
Once the customer places the order, you need to authorize the payment session with the third-party payment provider.
For example:
When the payment authorization is successful, a payment is created and returned.
Handling Additional Action
authorizePaymentSessionStep
, you don't need to implement this logic as it's implemented in the step.If the payment authorization isn’t successful, whether because it requires additional action or for another reason, the method updates the payment session with the new status and throws an error.
In that case, you can catch that error and, if the session's status
property is requires_more
, handle the additional action, then retry the authorization.
For example:
1try {2 const payment =3 await paymentModuleService.authorizePaymentSession(4 paymentSession.id,5 {}6 )7} catch (e) {8 // retrieve the payment session again9 const updatedPaymentSession = (10 await paymentModuleService.listPaymentSessions({11 id: [paymentSession.id],12 })13 )[0]14 15 if (updatedPaymentSession.status === "requires_more") {16 // TODO perform required action17 // TODO authorize payment again.18 }19}
5. Payment Flow Complete#
The payment flow is complete once the payment session is authorized and the payment is created.
You can then:
- Complete the cart using the completeCartWorkflow if you're using the Medusa application.
- Capture the payment either using the capturePaymentWorkflow or capturePayment method.
- Refund captured amounts using the refundPaymentWorkflow or refundPayment method.