feat: Refund payment (#6610)

Essentially the same as #6601 but for refunds
This commit is contained in:
Oli Juhl
2024-03-07 12:34:36 +00:00
committed by GitHub
parent 7dee1a3fd3
commit e5cbe28d54
10 changed files with 246 additions and 37 deletions
@@ -1,4 +1,7 @@
import { capturePaymentWorkflow } from "@medusajs/core-flows"
import {
capturePaymentWorkflow,
refundPaymentWorkflow,
} from "@medusajs/core-flows"
import {
LinkModuleUtils,
ModuleRegistrationName,
@@ -120,7 +123,7 @@ medusaIntegrationTestRunner({
)
})
it("should capture a payment with custom amount", async () => {
it("should partially capture a payment", async () => {
const paymentCollection = await paymentService.createPaymentCollections(
{
region_id: "test-region",
@@ -176,6 +179,109 @@ medusaIntegrationTestRunner({
})
)
})
it("should refund 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,
})
await refundPaymentWorkflow(appContainer).run({
input: {
payment_id: payment.id,
},
throwOnError: false,
})
const [refund] = await paymentService.listRefunds({
payment_id: payment.id,
})
expect(refund).toEqual(
expect.objectContaining({
id: expect.any(String),
payment: expect.objectContaining({ id: payment.id }),
amount: 1000,
})
)
})
it("should partially refund 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,
})
await refundPaymentWorkflow(appContainer).run({
input: {
payment_id: payment.id,
amount: 500,
},
throwOnError: false,
})
const [refund] = await paymentService.listRefunds({
payment_id: payment.id,
})
expect(refund).toEqual(
expect.objectContaining({
id: expect.any(String),
payment: expect.objectContaining({ id: payment.id }),
amount: 500,
})
)
})
})
},
})