diff --git a/src/services/ibex/webhook-server/routes/on-receive.ts b/src/services/ibex/webhook-server/routes/on-receive.ts index 7a7ad2b5d..63a8c4e9d 100644 --- a/src/services/ibex/webhook-server/routes/on-receive.ts +++ b/src/services/ibex/webhook-server/routes/on-receive.ts @@ -96,6 +96,37 @@ const sendLightningNotification = async ( next() } +const sendOnchainNotification = async ( + req: PaymentRequest, + res: Response, + next: NextFunction +) => { + if (!req.paymentContext) return next() + + const { transaction } = req.body; + + const { receiverWallet, recipientAccount, recipientUser } = req.paymentContext; + + const nResp = await NotificationsService().onChainTxReceived({ + recipientAccountId: recipientAccount.id, + recipientWalletId: receiverWallet.id, + paymentAmount: toPaymentAmount(receiverWallet.currency)(transaction.amount), + displayPaymentAmount: await toDisplayAmount(recipientAccount.displayCurrency)(transaction.amount), + recipientDeviceTokens: recipientUser.deviceTokens, + recipientNotificationSettings: recipientAccount.notificationSettings, + recipientLanguage: recipientUser.language, + txHash: transaction.hash + }) + + if (nResp instanceof DeviceTokensNotRegisteredNotificationsServiceError) { + await removeDeviceTokens({ userId: recipientUser.id, deviceTokens: nResp.tokens }) + } else if (nResp instanceof NotificationsServiceError) { + logger.error(nResp) + } + + next() +} + const sendZapReceipt = async ( req: PaymentRequest, _resp: Response, @@ -169,8 +200,8 @@ router.post(paths.cashout, authenticate, logRequest, (_req, resp) => { resp.status(200).end() }) -router.post(paths.onchain, authenticate, logRequest, (_req, resp) => { - baseLogger.info("Received onchain payment (not implemented).") +router.post(paths.onchain, authenticate, logRequest, fetchPaymentContext, sendOnchainNotification, (_req, resp) => { + baseLogger.info("Received onchain payment.") resp.status(200).end() })