Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(core): refund a captured payment when split-order cart completion fails (#1741)

Viktor Holikcd1c598aade8
diff --git a/integration-tests/http/payment/payment-webhook-cart-completion.spec.ts b/integration-tests/http/payment/payment-webhook-cart-completion.spec.ts
index b042965f4..42179f736 100644
--- a/integration-tests/http/payment/payment-webhook-cart-completion.spec.ts
+++ b/integration-tests/http/payment/payment-webhook-cart-completion.spec.ts
@@ -432,6 +432,66 @@ medusaIntegrationTestRunner({
})
expect((after as { captures?: unknown[] }).captures).toHaveLength(1)
})
+
+ it("refunds a payment captured by the webhook when the split-order completion fails", async () => {
+ const { cart, sessionId } = await seedReadyCart(
+ sellerSeed.offer.id
+ )
+ const query = appContainer.resolve(
+ ContainerRegistrationKeys.QUERY
+ )
+ const cartModule = appContainer.resolve(Modules.CART)
+ const { processPaymentWorkflow } = await import(
+ "@mercurjs/core/workflows/payment/workflows/process-payment"
+ )
+
+ const {
+ data: [withMethods],
+ } = await query.graph({
+ entity: "cart",
+ fields: ["shipping_methods.id"],
+ filters: { id: cart.id },
+ })
+ await cartModule.deleteShippingMethods(
+ (withMethods.shipping_methods ?? []).map((sm) => sm!.id)
+ )
+
+ await processPaymentWorkflow(appContainer).run({
+ input: {
+ action: "captured",
+ data: { session_id: sessionId },
+ },
+ })
+
+ const { data: groups } = await query.graph({
+ entity: "order_group",
+ fields: ["id"],
+ filters: { cart_id: cart.id },
+ })
+ expect(groups).toHaveLength(0)
+
+ const {
+ data: [payment],
+ } = await query.graph({
+ entity: "payment",
+ fields: [
+ "amount",
+ "captured_at",
+ "captures.amount",
+ "refunds.amount",
+ "refunds.note",
+ ],
+ filters: { payment_session_id: sessionId },
+ })
+ expect(payment.captured_at).toBeTruthy()
+ expect(payment.captures).toHaveLength(1)
+ expect(payment.refunds).toEqual([
+ expect.objectContaining({
+ amount: payment.amount,
+ note: "Refunded due to cart completion failure",
+ }),
+ ])
+ })
})
},
})
diff --git a/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts b/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts
index b04dfc90e..7b5c93845 100644
--- a/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts
+++ b/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts
@@ -25,6 +25,7 @@ import {
import {
acquireLockStep,
authorizePaymentSessionStep,
+ compensatePaymentIfNeededStep,
createOrdersStep,
emitEventStep,
releaseLockStep,
@@ -122,6 +123,9 @@ export const completeCartWithSplitOrdersWorkflow = createWorkflow(
})
const paymentSessions = validateCartPaymentsStep({ cart: cartData.data })
+ compensatePaymentIfNeededStep({
+ payment_session_id: paymentSessions[0].id,
+ })
const validate = createHook("validate", {
input,