@medusajs/core-flows
Version:
Set of workflow definitions for Medusa
64 lines • 3.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.authorizePaymentSessionStep = exports.authorizePaymentSessionStepId = void 0;
const utils_1 = require("@medusajs/framework/utils");
const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk");
exports.authorizePaymentSessionStepId = "authorize-payment-session-step";
/**
* This step authorizes a payment session.
*
* @example
* const data = authorizePaymentSessionStep({
* id: "payses_123",
* context: {}
* })
*/
exports.authorizePaymentSessionStep = (0, workflows_sdk_1.createStep)(exports.authorizePaymentSessionStepId, async (input, { container }) => {
let payment;
const logger = container.resolve(utils_1.ContainerRegistrationKeys.LOGGER);
const paymentModule = container.resolve(utils_1.Modules.PAYMENT);
if (!input.id) {
return new workflows_sdk_1.StepResponse(null);
}
try {
payment = await paymentModule.authorizePaymentSession(input.id, input.context || {});
}
catch (e) {
logger.error(`Error was thrown trying to authorize payment session - ${input.id} - ${e}`);
}
const paymentSession = await paymentModule.retrievePaymentSession(input.id, {
relations: ["payment", "payment.captures"],
});
// Throw a special error type when the status is requires_more as it requires a specific further action
// from the consumer
if (paymentSession.status === utils_1.PaymentSessionStatus.REQUIRES_MORE) {
throw new utils_1.MedusaError(utils_1.MedusaError.Types.PAYMENT_REQUIRES_MORE_ERROR, `More information is required for payment`);
}
// If any other error other than requires_more shows up, this usually requires the consumer to create a new payment session
// This could also be a system error thats caused by invalid setup or a failure in connecting to external providers
if (paymentSession.status !== utils_1.PaymentSessionStatus.AUTHORIZED || !payment) {
throw new utils_1.MedusaError(utils_1.MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR, `Payment authorization failed`);
}
return new workflows_sdk_1.StepResponse(paymentSession.payment);
},
// If payment or any other part of complete cart fails post payment step, we cancel any payments made
async (payment, { container }) => {
if (!payment) {
return;
}
const logger = container.resolve(utils_1.ContainerRegistrationKeys.LOGGER);
const paymentModule = container.resolve(utils_1.Modules.PAYMENT);
// If the payment session status is requires_more, we don't have to revert the payment.
// Return the same status for the cart completion to be re-run.
if (payment.payment_session &&
payment.payment_session.status === utils_1.PaymentSessionStatus.REQUIRES_MORE) {
return;
}
try {
await paymentModule.cancelPayment(payment.id);
}
catch (e) {
logger.error(`Error was thrown trying to cancel payment - ${payment.id} - ${e}`);
}
});
//# sourceMappingURL=authorize-payment-session.js.map
;