UNPKG

@unchainedshop/plugins

Version:

Because of a Typescript issue with upstream "postfinancecheckout", the Postfinance plugin has been disabled from transpilation, import the source ts files from src and enable node_module tsc or copy over the src/payment/postfinance-checkout to your projec

56 lines 2.03 kB
import { createLogger } from '@unchainedshop/logger'; import { buildSignature } from './buildSignature.js'; const logger = createLogger('unchained:core-payment:saferpay:handler'); export const saferpayHandler = async (request, reply) => { const resolvedContext = request.unchainedContext; const { modules, services } = resolvedContext; const { orderPaymentId, signature, transactionId } = request.query; const isValidRequest = typeof orderPaymentId === 'string' && typeof signature === 'string' && typeof transactionId === 'string' && orderPaymentId && transactionId && signature; if (!isValidRequest) { logger.warn(`orderPaymentId missing in query`); reply.status(404); return reply.send(); } try { logger.info(`checkout with orderPaymentId: ${orderPaymentId}`); const orderPayment = await modules.orders.payments.findOrderPayment({ orderPaymentId, }); if (!orderPayment) { throw new Error(`order payment not found with orderPaymentId: ${orderPaymentId}`); } const correctSignature = await buildSignature(transactionId, orderPaymentId); if (correctSignature !== signature) { throw new Error('Invalid signature'); } const order = await services.orders.checkoutOrder(orderPayment.orderId, { paymentContext: { transactionId, }, }); logger.info(`checkout successful`, { orderPaymentId, orderId: order._id, }); reply.status(200); return reply.send(JSON.stringify({ message: 'checkout successful', orderPaymentId, orderId: order._id, })); } catch (error) { logger.error(error, { orderPaymentId, transactionId, }); reply.status(500); return reply.send(error.message); } }; //# sourceMappingURL=handler-fastify.js.map