UNPKG

@unchainedshop/plugins

Version:

Official plugin collection for the Unchained Engine with payment, delivery, and pricing adapters

86 lines (85 loc) 3.24 kB
import { createLogger } from '@unchainedshop/logger'; const logger = createLogger('unchained:payrexx:handler'); export const payrexxHandler = async (request, response) => { const resolvedContext = request.unchainedContext; const { modules, services } = resolvedContext; const { transaction } = request.body; if (!transaction) { logger.info(`unhandled event type`, { type: Object.keys(request.body).join(','), }); response.status(200).send({ ignored: true, message: `Unhandled event type: ${Object.keys(request.body).join(',')}. Supported type: transaction`, }); return; } if (transaction.referenceId === '__IGNORE_WEBHOOK__' || transaction.status === 'waiting') { logger.info(`unhandled transaction state: ${transaction.status}`); response.status(200).send({ ignored: true, message: `Unhandled transaction state: ${transaction.status}`, }); return; } logger.info(`Processing event`, { transactionId: transaction.id, }); try { if (transaction.preAuthorizationId) { const { referenceId: paymentProviderId, invoice } = transaction; const userId = ''; logger.info(`register credentials for: ${userId}`); await services.orders.registerPaymentCredentials(paymentProviderId, { userId, transactionContext: { gatewayId: invoice.paymentRequestId }, }); logger.info(`registration successful`, { paymentProviderId, userId, }); response.status(200).send({ message: 'registration successful', paymentProviderId, userId, }); } else { const { referenceId: orderPaymentId, invoice } = transaction; logger.info(`checkout with orderPaymentId: ${orderPaymentId}`); await modules.orders.payments.logEvent(orderPaymentId, { transactionId: transaction.id, }); const orderPayment = await modules.orders.payments.findOrderPayment({ orderPaymentId, }); if (!orderPayment) { throw new Error(`order payment not found with orderPaymentId: ${orderPaymentId}`); } const order = await services.orders.checkoutOrder(orderPayment.orderId, { paymentContext: { gatewayId: invoice.paymentRequestId, }, }); if (!order) throw new Error(`Order with id ${orderPayment.orderId} not found`); logger.info(`checkout successful`, { orderPaymentId, orderId: order._id, }); response.status(200).send({ message: 'checkout successful', orderId: order._id, }); } } catch (error) { logger.error(error, { transactionId: transaction.id, }); response.status(500).send({ message: error.message, name: error.name, }); } };