feat: Capture payment (#6601)

* feat: Capture payment

* add amount to workflow input
This commit is contained in:
Oli Juhl
2024-03-07 11:02:26 +01:00
committed by GitHub
parent 12b035cb18
commit 3d0da980cf
23 changed files with 584 additions and 86 deletions
@@ -1,5 +1,10 @@
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import { IRegionModuleService } from "@medusajs/types"
import { capturePaymentWorkflow } from "@medusajs/core-flows"
import {
LinkModuleUtils,
ModuleRegistrationName,
Modules,
} from "@medusajs/modules-sdk"
import { IPaymentModuleService, IRegionModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
jest.setTimeout(50000)
@@ -12,14 +17,17 @@ medusaIntegrationTestRunner({
describe("Payments", () => {
let appContainer
let regionService: IRegionModuleService
let paymentService: IPaymentModuleService
let remoteLink
beforeAll(async () => {
appContainer = getContainer()
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
remoteLink = appContainer.resolve("remoteLink")
paymentService = appContainer.resolve(ModuleRegistrationName.PAYMENT)
remoteLink = appContainer.resolve(LinkModuleUtils.REMOTE_LINK)
})
// TODO: Test should move to `integration-tests/api`
it("should list payment providers", async () => {
const region = await regionService.create({
name: "Test Region",
@@ -55,6 +63,119 @@ medusaIntegrationTestRunner({
}),
])
})
it("should capture a payment", async () => {
const paymentCollection = await paymentService.createPaymentCollections(
{
region_id: "test-region",
amount: 1000,
currency_code: "usd",
}
)
const paymentSession = await paymentService.createPaymentSession(
paymentCollection.id,
{
provider_id: "pp_system_default",
amount: 1000,
currency_code: "usd",
data: {},
}
)
const payment = await paymentService.authorizePaymentSession(
paymentSession.id,
{}
)
await capturePaymentWorkflow(appContainer).run({
input: {
payment_id: payment.id,
},
throwOnError: false,
})
const [paymentResult] = await paymentService.listPayments({
id: payment.id,
})
expect(paymentResult).toEqual(
expect.objectContaining({
id: payment.id,
amount: 1000,
payment_collection_id: paymentCollection.id,
})
)
const [capture] = await paymentService.listCaptures({
payment_id: payment.id,
})
expect(capture).toEqual(
expect.objectContaining({
id: expect.any(String),
payment: expect.objectContaining({ id: payment.id }),
amount: 1000,
})
)
})
it("should capture a payment with custom amount", async () => {
const paymentCollection = await paymentService.createPaymentCollections(
{
region_id: "test-region",
amount: 1000,
currency_code: "usd",
}
)
const paymentSession = await paymentService.createPaymentSession(
paymentCollection.id,
{
provider_id: "pp_system_default",
amount: 1000,
currency_code: "usd",
data: {},
}
)
const payment = await paymentService.authorizePaymentSession(
paymentSession.id,
{}
)
await capturePaymentWorkflow(appContainer).run({
input: {
payment_id: payment.id,
amount: 500,
},
throwOnError: false,
})
const [paymentResult] = await paymentService.listPayments({
id: payment.id,
})
expect(paymentResult).toEqual(
expect.objectContaining({
id: payment.id,
amount: 1000,
payment_collection_id: paymentCollection.id,
})
)
const [capture] = await paymentService.listCaptures({
payment_id: payment.id,
})
expect(capture).toEqual(
expect.objectContaining({
id: expect.any(String),
payment: expect.objectContaining({ id: payment.id }),
amount: 500,
})
)
})
})
},
})