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

61 lines 2.52 kB
import { createLogger } from '@unchainedshop/logger'; import { OrderStatus } from '@unchainedshop/core-orders'; const { CRYPTOPAY_SECRET, CRYPTOPAY_MAX_RATE_AGE = '360' } = process.env; const logger = createLogger('unchained:core-payment:cryptopay'); export default async function handleWebhook({ secret, wallet, price, ping, }, context) { const { modules, services } = context; if (secret !== CRYPTOPAY_SECRET) { logger.warn(`webhook called with invalid secret`); throw new Error('Secret invalid'); } if (wallet) { const { address, blockHeight, amount, contract, decimals, currencyCode } = wallet; logger.debug('webhook received wallet data', wallet); const { orderPaymentId } = await modules.cryptopay.updateWalletAddress({ address, blockHeight, amount, contract, decimals, currencyCode, }); const orderPayment = orderPaymentId && (await modules.orders.payments.findOrderPayment({ orderPaymentId, })); if (orderPayment) { // Try to process order to next status // TODO: Not sure if it's correct to use processOrder here if status is PENDING! const order = await modules.orders.findOrder({ orderId: orderPayment.orderId }); if (order.status === null) { await services.orders.checkoutOrder(order._id, {}); } else if (order.status === OrderStatus.PENDING) { await services.orders.processOrder(order, {}); } else { throw new Error('Already processed'); } } } if (price) { logger.debug('webhook received price data', price); const { baseCurrency, token, rate, timestamp } = price; const timestampDate = new Date(timestamp); const expiresAt = new Date(new Date().getTime() + parseInt(CRYPTOPAY_MAX_RATE_AGE, 10) * 1000); const rateData = { baseCurrency, quoteCurrency: token, rate, expiresAt, timestamp: timestampDate, }; await modules.products.prices.rates.updateRates([rateData]); } if (ping) { logger.debug('webhook received ping data', ping); const { blockHeight, currencyCode } = ping; await modules.cryptopay.updateMostRecentBlock(currencyCode, blockHeight); } } //# sourceMappingURL=handle-webhook.js.map