Skip to content

Commit f57b942

Browse files
authored
fix(core-flows, payment, types): expose metadata on refund creation through refundPaymentsWorkflow (#15273)
Closes #14816 ## What Expose the `metadata` field on refund creation when using `refundPaymentsWorkflow`. ## Why The `Refund` data model already has a `metadata` column (see `packages/modules/payment/src/models/refund.ts`), but the field is not present on: - `CreateRefundDTO` - `RefundPaymentsStepInput` - `RefundPaymentsWorkflowInput` So callers of `refundPaymentsWorkflow` (and the `PaymentModuleService.refundPayment` API directly) have no way to attach metadata to a refund — even though the underlying storage supports it. The reporter in #14816 hit this exact gap. ## How 1. Add `metadata?: Record<string, unknown>` to `CreateRefundDTO` in `@medusajs/types`. 2. Add the same optional field to `RefundPaymentsStepInput` and `RefundPaymentsWorkflowInput` in `@medusajs/core-flows`. The step already spreads the input into `paymentModule.refundPayment`, so no behavioral change is needed at the workflow layer once the type allows it. 3. Forward `data.metadata` to `refundService_.create(...)` inside `PaymentModuleService.refundPayment_` so the value actually lands on the refund row. Changes are additive and the new property is optional — no behavior change for existing callers. ## Testing - Added an integration test in `packages/modules/payment/integration-tests/__tests__/services/payment-module/index.spec.ts` that calls `refundPayment` with a `metadata` payload and asserts the refund persists those values. - Existing refund tests continue to pass (no signature changes, field is optional). ## Changeset Included: `.changeset/refund-payments-metadata.md` (patch bumps for `@medusajs/core-flows`, `@medusajs/payment`, `@medusajs/types`). --- > [!NOTE] > **Medium Risk** > Touches refund creation in the payment module (a money-critical path), but the change is additive (optional `metadata`) and covered by an integration test. > > **Overview** > Exposes optional `metadata` when creating refunds via `refundPaymentsWorkflow` by extending `RefundPaymentsStepInput`, `RefundPaymentsWorkflowInput`, and `CreateRefundDTO`. > > Threads the new field through `PaymentModuleService.refundPayment` into `refundService_.create(...)`, and adds an integration test asserting refund metadata is persisted. Includes a changeset bumping `@medusajs/core-flows`, `@medusajs/payment`, and `@medusajs/types`. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c100f18. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
1 parent b1c4aee commit f57b942

6 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
"@medusajs/payment": patch
4+
"@medusajs/types": patch
5+
---
6+
7+
fix(core-flows, payment, types): expose `metadata` on refund creation through `refundPaymentsWorkflow`
8+
9+
The `Refund` data model supports a `metadata` field, but it was not exposed on
10+
`CreateRefundDTO`, `RefundPaymentsStepInput`, or `RefundPaymentsWorkflowInput`,
11+
making it impossible to set metadata on a refund created via
12+
`refundPaymentsWorkflow`. This adds the optional field to all three types and
13+
threads it through `PaymentModuleService.refundPayment` so the value reaches
14+
the underlying refund row.

packages/core/core-flows/src/payment/steps/refund-payments.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export type RefundPaymentsStepInput = {
3232
* The note to attach to the refund.
3333
*/
3434
note?: string
35+
/**
36+
* Holds custom data in key-value pairs.
37+
*/
38+
metadata?: Record<string, unknown>
3539
}[]
3640

3741
export const refundPaymentsStepId = "refund-payments-step"

packages/core/core-flows/src/payment/workflows/refund-payments.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export type RefundPaymentsWorkflowInput = {
102102
* The note to attach to the refund.
103103
*/
104104
note?: string
105+
/**
106+
* Holds custom data in key-value pairs.
107+
*/
108+
metadata?: Record<string, unknown>
105109
}[]
106110

107111
export const refundPaymentsWorkflowId = "refund-payments-workflow"

packages/core/types/src/payment/mutations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ export interface CreateRefundDTO {
187187
* a user's ID.
188188
*/
189189
created_by?: string
190+
191+
/**
192+
* Holds custom data in key-value pairs.
193+
*/
194+
metadata?: Record<string, unknown>
190195
}
191196

192197
/**

packages/modules/payment/integration-tests/__tests__/services/payment-module/index.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,34 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
944944
)
945945
})
946946

947+
it("should persist metadata passed when refunding a payment", async () => {
948+
await service.capturePayment({
949+
amount: 100,
950+
payment_id: "pay-id-2",
951+
})
952+
953+
const refundedPayment = await service.refundPayment({
954+
amount: 100,
955+
payment_id: "pay-id-2",
956+
metadata: { reason: "customer-request", ticket_id: "42" },
957+
})
958+
959+
expect(refundedPayment).toEqual(
960+
expect.objectContaining({
961+
id: "pay-id-2",
962+
refunds: [
963+
expect.objectContaining({
964+
amount: 100,
965+
metadata: {
966+
reason: "customer-request",
967+
ticket_id: "42",
968+
},
969+
}),
970+
],
971+
})
972+
)
973+
})
974+
947975
it("should fully refund a payment through two refunds", async () => {
948976
await service.capturePayment({
949977
amount: 100,

packages/modules/payment/src/services/payment-module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ export default class PaymentModuleService
926926
created_by: data.created_by,
927927
note: data.note,
928928
refund_reason_id: data.refund_reason_id,
929+
metadata: data.metadata,
929930
},
930931
sharedContext
931932
)

0 commit comments

Comments
 (0)