@medusajs/core-flows
Version:
Set of workflow definitions for Medusa
118 lines • 4.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.processPaymentWorkflow = exports.processPaymentWorkflowId = void 0;
const utils_1 = require("@medusajs/utils");
const workflows_sdk_1 = require("@medusajs/workflows-sdk");
const common_1 = require("../../common");
const steps_1 = require("../steps");
const complete_cart_after_payment_1 = require("../steps/complete-cart-after-payment");
const capture_payment_1 = require("./capture-payment");
exports.processPaymentWorkflowId = "process-payment-workflow";
/**
* This workflow processes a payment to either complete its associated cart,
* capture the payment, or authorize the payment session. It's used when a
* [Webhook Event is received](https://docs.medusajs.com/resources/commerce-modules/payment/webhook-events).
*
* You can use this workflow within your own customizations or custom workflows, allowing you
* to process a payment in your custom flows based on a webhook action.
*
* @example
* const { result } = await processPaymentWorkflow(container)
* .run({
* input: {
* action: "captured",
* data: {
* session_id: "payses_123",
* amount: 10
* }
* }
* })
*
* @summary
*
* Process a payment based on a webhook event.
*/
exports.processPaymentWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.processPaymentWorkflowId, (input) => {
const paymentData = (0, common_1.useQueryGraphStep)({
entity: "payment",
fields: ["id"],
filters: { payment_session_id: input.data?.session_id },
}).config({
name: "payment-query",
});
const paymentSessionResult = (0, common_1.useQueryGraphStep)({
entity: "payment_session",
fields: ["payment_collection_id"],
filters: { id: input.data?.session_id },
}).config({
name: "payment-session-query",
});
const cartPaymentCollection = (0, common_1.useQueryGraphStep)({
entity: "cart_payment_collection",
fields: ["cart_id"],
filters: {
payment_collection_id: paymentSessionResult.data[0].payment_collection_id,
},
}).config({
name: "cart-payment-query",
});
(0, workflows_sdk_1.when)({ input, paymentData }, ({ input, paymentData }) => {
return (input.action === utils_1.PaymentActions.SUCCESSFUL && !!paymentData.data.length);
}).then(() => {
capture_payment_1.capturePaymentWorkflow
.runAsStep({
input: {
payment_id: paymentData.data[0].id,
amount: input.data?.amount,
},
})
.config({
name: "capture-payment",
});
});
(0, workflows_sdk_1.when)({ input, paymentData }, ({ input, paymentData }) => {
// payment is captured with the provider but we dont't have any payment data which means we didn't call authorize yet - autocapture flow
return (input.action === utils_1.PaymentActions.SUCCESSFUL && !paymentData.data.length);
}).then(() => {
const payment = (0, steps_1.authorizePaymentSessionStep)({
id: input.data.session_id,
context: {},
}).config({
name: "authorize-payment-session-autocapture",
});
capture_payment_1.capturePaymentWorkflow
.runAsStep({
input: {
payment_id: payment.id,
amount: input.data?.amount,
},
})
.config({
name: "capture-payment-autocapture",
});
});
(0, workflows_sdk_1.when)({ input, cartPaymentCollection }, ({ input, cartPaymentCollection }) => {
// Authorize payment session if no Cart is linked to the payment
// When associated with a Cart, the complete cart workflow will handle the authorization
return (!cartPaymentCollection.data.length &&
input.action === utils_1.PaymentActions.AUTHORIZED &&
!!input.data?.session_id);
}).then(() => {
(0, steps_1.authorizePaymentSessionStep)({
id: input.data.session_id,
context: {},
}).config({
name: "authorize-payment-session",
});
});
(0, workflows_sdk_1.when)({ cartPaymentCollection }, ({ cartPaymentCollection }) => {
return !!cartPaymentCollection.data.length;
}).then(() => {
(0, complete_cart_after_payment_1.completeCartAfterPaymentStep)({
cart_id: cartPaymentCollection.data[0].cart_id,
}).config({
continueOnPermanentFailure: true, // Continue payment processing even if cart completion fails
});
});
});
//# sourceMappingURL=process-payment.js.map
;