UNPKG

@medusajs/core-flows

Version:

Set of workflow definitions for Medusa

114 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.refundPaymentsWorkflow = exports.refundPaymentsWorkflowId = exports.validatePaymentsRefundStep = void 0; const utils_1 = require("@medusajs/framework/utils"); const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk"); const common_1 = require("../../common"); const add_order_transaction_1 = require("../../order/steps/add-order-transaction"); const refund_payments_1 = require("../steps/refund-payments"); /** * This step validates that the refund is valid for the payment. * If the payment's refundable amount is less than the amount to be refunded, * the step throws an error. * * :::note * * You can retrieve a payment's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * * ::: * * @example * const data = validatePaymentsRefundStep({ * payment: [{ * id: "payment_123", * // other payment details... * }], * input: [ * { * payment_id: "payment_123", * amount: 10, * } * ] * }) */ exports.validatePaymentsRefundStep = (0, workflows_sdk_1.createStep)("validate-payments-refund-step", async function ({ payments, input }) { const paymentIdAmountMap = new Map(input.map(({ payment_id, amount }) => [payment_id, amount])); for (const payment of payments) { const capturedAmount = (payment.captures || []).reduce((acc, capture) => utils_1.MathBN.sum(acc, capture.amount), utils_1.MathBN.convert(0)); const refundedAmount = (payment.refunds || []).reduce((acc, capture) => utils_1.MathBN.sum(acc, capture.amount), utils_1.MathBN.convert(0)); const refundableAmount = utils_1.MathBN.sub(capturedAmount, refundedAmount); const amountToRefund = paymentIdAmountMap.get(payment.id); if (utils_1.MathBN.gt(amountToRefund, refundableAmount)) { throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Payment with id ${payment.id} is trying to refund amount greater than the refundable amount`); } } }); exports.refundPaymentsWorkflowId = "refund-payments-workflow"; /** * This workflow refunds payments. * * You can use this workflow within your customizations or your own custom workflows, allowing you to * refund payments in your custom flow. * * @example * const { result } = await refundPaymentsWorkflow(container) * .run({ * input: [ * { * payment_id: "pay_123", * amount: 10, * } * ] * }) * * @summary * * Refund one or more payments. */ exports.refundPaymentsWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.refundPaymentsWorkflowId, (input) => { const paymentIds = (0, workflows_sdk_1.transform)({ input }, ({ input }) => input.map((paymentInput) => paymentInput.payment_id)); const paymentsQuery = (0, common_1.useQueryGraphStep)({ entity: "payments", fields: [ "id", "currency_code", "refunds.id", "refunds.amount", "captures.id", "captures.amount", "payment_collection.order.id", "payment_collection.order.currency_code", ], filters: { id: paymentIds }, options: { throwIfKeyNotFound: true }, }).config({ name: "get-cart" }); const payments = (0, workflows_sdk_1.transform)({ paymentsQuery }, ({ paymentsQuery }) => paymentsQuery.data); (0, exports.validatePaymentsRefundStep)({ payments, input }); const refundedPayments = (0, refund_payments_1.refundPaymentsStep)(input); const orderTransactionData = (0, workflows_sdk_1.transform)({ payments, input }, ({ payments, input }) => { const paymentsMap = {}; for (const payment of payments) { paymentsMap[payment.id] = payment; } return input .map((paymentInput) => { const payment = paymentsMap[paymentInput.payment_id]; const order = payment.payment_collection?.order; if (!order) { return; } return { order_id: order.id, amount: utils_1.MathBN.mult(paymentInput.amount, -1), currency_code: payment.currency_code, reference_id: payment.id, reference: "refund", }; }) .filter(utils_1.isDefined); }); (0, add_order_transaction_1.addOrderTransactionStep)(orderTransactionData); return new workflows_sdk_1.WorkflowResponse(refundedPayments); }); //# sourceMappingURL=refund-payments.js.map