medusa-payment-paystack
is a Medusa plugin that adds Paystack as a payment provider to Medusa ecommerce stores.
- Paystack account
- Paystack account's secret key
- Medusa server
If you don’t have a Medusa server installed yet, you must follow the quickstart guide first.
In the root of your Medusa server (backend), run the following command to install the Paystack plugin:
yarn add medusa-payment-paystack
Next, you need to enable the plugin in your Medusa server.
In medusa-config.ts
add the following to the plugins
array:
module.exports = defineConfig({
projectConfig: {
databaseUrl: process.env.DATABASE_URL,
// ... other config
},
modules: [
// other modules
{
resolve: "@medusajs/medusa/payment",
options: {
providers: [
// other payment providers like stripe, paypal etc
{
resolve: "medusa-payment-paystack",
options: {
secret_key: <PAYSTACK_SECRET_KEY>,
} satisfies import("medusa-payment-paystack").PluginOptions,
},
],
},
},
],
});
The full list of configuration options you can pass to the plugin can be found in Config
To ensure that Medusa is notified of successful payments, you need to set up webhooks in your Paystack dashboard. If you're installing this plugin for production use, this is a required step.
Go to your Paystack dashboard and navigate to the "API Keys & Webhooks" section.
Set the Webhook URL to <your-medusa-backend-url>/hooks/payment/paystack
. Eg. https://your-medusa-backend.com/hooks/payment/paystack
.
This step is required for you to be able to use Paystack as a payment provider in your storefront.
Refer to this documentation in the user guide to learn how to add a payment provider like Paystack to a region.
Follow Medusa's Storefront Development Checkout Flow guide using pp_paystack
as the provider_id
to add Paystack to your checkout flow.
Paystack requires the customer's email address to create a transaction.
You need to pass the customer's email address in the initiatePaymentSession
context to create a transaction.
If your storefront does not collect customer email addresses, you can provide a dummy email but be aware all transactions on your Paystack dashboard will be associated with that email address.
await initiatePaymentSession(cart, {
provider_id: selectedPaymentMethod,
context: {
email: cart.email,
},
});
medusa-payment-paystack
returns an access code and authorization URL that you should use to complete the Paystack payment flow on the storefront.
Using the returned access code and authorization URL allows the plugin to confirm the status of the transaction on your backend, and then relay that information to Medusa.
medusa-payment-paystack
inserts the access code (paystackTxAccessCode
) and authorization URL (paystackTxAuthorizationUrl
) into the PaymentSession
's data.
You can use the access code to resume the payment flow, or the authorization URL to redirect the customer to Paystack's hosted payment page.
Extract the access code from the payment session's data:
const { paystackTxAccessCode } = paymentSession.data;
Provide this access code to the resumeTransaction
method from Paystack's InlineJS library.
import Paystack from "@paystack/inline-js"
const PaystackPaymentButton = ({
session,
notReady,
}: {
session: HttpTypes.StorePaymentSession | undefined
notReady: boolean
}) => {
const paystackRef = useRef<Paystack | null>(null)
// If the session is not ready, we don't want to render the button
if (notReady || !session) return null
// Get the accessCode added to the session data by the Paystack plugin
const accessCode = session.data.paystackTxAccessCode
if (!accessCode) throw new Error("Transaction access code is not defined")
return (
<button
onClick={() => {
if (!paystackRef.current) {
paystackRef.current = new Paystack()
}
const paystack = paystackRef.current
paystack.resumeTransaction(accessCode, {
async onSuccess() {
// Call Medusa checkout complete here
await placeOrder()
},
onError(error: unknown) {
// Handle error
},
})
}}
>
Pay with Paystack
</button>
)
}
As a pre-requisite, you must have configured a "Callback URL" in your Paystack dashboard. Follow this guide to set it up.
The callback URL can be a custom route on your Medusa backend, it can be a page in your storefront or a view in your mobile application. That route just needs to call the Medusa Complete Cart method.
Extract the authorization URL from the payment session's data:
const { paystackTxAuthorizationUrl } = session.data;
Redirect the customer to the authorization URL to complete the payment.
// Redirect the customer to Paystack's hosted payment page
window.open(paystackTxAuthorizationUrl, "_self");
Once the payment is successful, the customer will be redirected back to the callback URL. This page can then call the Medusa Complete Cart method to complete the checkout flow and show a success message to the customer.
Call the Medusa Complete Cart method in the payment completion callback of your chosen flow as mentioned in Completing the Payment Flow above.
medusa-payment-paystack
will verify the transaction with Paystack and mark the cart as paid for in Medusa.
Even if the "Complete Cart" method is not called for any reason, with webhooks set up correctly, the transaction will still be marked as paid for in Medusa when the user pays for it.
You can refund captured payments made with Paystack from the Admin dashboard.
medusa-payment-paystack
handles refunding the given amount using Paystack and marks the order in Medusa as refunded.
Partial refunds are also supported.
Name | Type | Default | Description |
---|---|---|---|
secret_key | string |
- | Your Paystack secret key. Should be in the format sk_test-... or sk_live-... Obtainable from the Paystack dashboard - Settings -> API Keys & Webhooks. |
disable_retries | boolean |
false |
Disable retries on network errors and 5xx errors on idempotent requests to Paystack. Generally, you should not disable retries, these errors are usually temporary but it can be useful for debugging. |
debug | boolean |
false |
Enable debug mode for the plugin. If true, logs helpful debug information to the console. Logs are prefixed with "PS_P_Debug". |
The examples
directory contains a simple Medusa server with the Paystack plugin installed and configured.
It also contains a storefront built with Next.js that uses the inline-js Paystack library to complete the payment flow.