Skip to main content
Skip to main content

Examples of the Payment Module

In this guide, you’ll find common examples of how you can use the Payment Module in your application.

Create a Payment Collection

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const paymentModuleService: IPaymentModuleService =
req.scope.resolve(ModuleRegistrationName.PAYMENT)

const paymentCollection =
await paymentModuleService.createPaymentCollections({
region_id: "reg_123",
currency_code: "usd",
amount: 4000,
})

res.json({
payment_collection: paymentCollection,
})
}

Create Payment Session

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const paymentModuleService: IPaymentModuleService =
req.scope.resolve(ModuleRegistrationName.PAYMENT)

const paymentSession =
await paymentModuleService.createPaymentSession(
"pay_col_123",
{
currency_code: "usd",
provider_id: "system",
amount: 4000,
data: {},
}
)

res.json({
payment_session: paymentSession,
})
}

List Payment Sessions of Payment Collection

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const paymentModuleService: IPaymentModuleService =
req.scope.resolve(ModuleRegistrationName.PAYMENT)

const paymentSessions =
await paymentModuleService.listPaymentSessions({
payment_collection_id: ["pay_col_123"],
})

res.json({
payment_sessions: paymentSessions,
})
}

Authorize Payment Session

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const paymentModuleService: IPaymentModuleService =
req.scope.resolve(ModuleRegistrationName.PAYMENT)

const payment =
await paymentModuleService.authorizePaymentSession(
"payses_123",
{}
)

res.json({
payment,
})
}

List Payments of Payment Session

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const paymentModuleService: IPaymentModuleService =
req.scope.resolve(ModuleRegistrationName.PAYMENT)

const payments = await paymentModuleService.listPayments({
session_id: "payses_123",
})

res.json({
payments,
})
}

Capture Payment

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPaymentModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const paymentModuleService: IPaymentModuleService =
req.scope.resolve(ModuleRegistrationName.PAYMENT)

const payment = await paymentModuleService.capturePayment({
payment_id: "pay_123",
})

res.json({
payment,
})
}

More Examples

The module interface reference provides a reference to all the methods available for use with examples for each.

Was this section helpful?