From a39f8165f344e5991ea8f12761d131f8143ea4d3 Mon Sep 17 00:00:00 2001 From: iamshabell <91321698+iamshabell@users.noreply.github.com> Date: Sat, 16 Dec 2023 11:18:14 +0300 Subject: [PATCH] add(marupay): exception vendor error handling --- .../marupay/src/handlers/edahab/edahab.ts | 30 +++++++------------ packages/marupay/src/handlers/exeptions.ts | 15 ++++++++++ packages/marupay/src/handlers/waafi/waafi.ts | 3 +- 3 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 packages/marupay/src/handlers/exeptions.ts diff --git a/packages/marupay/src/handlers/edahab/edahab.ts b/packages/marupay/src/handlers/edahab/edahab.ts index d999416..0746f6e 100644 --- a/packages/marupay/src/handlers/edahab/edahab.ts +++ b/packages/marupay/src/handlers/edahab/edahab.ts @@ -8,18 +8,19 @@ import { PaymentCtx, PaymentOptions } from '../types'; import { prepareRequest } from './prepareRequest'; import { SO_ACCOUNT_NUMBER, soPurchaseNumber } from '../constants' import { safeParse } from '../../utils/safeParser'; +import { VendorErrorException } from 'handlers/exeptions'; const edahabPurchase = z.object({ accountNumber: soPurchaseNumber, }); -const requestFn = async (url: string, data: any, referenceId: string) => { - const response = await axios.post(url, data); +const requestFn = async (url: string, data: API.PurchasePaymentData, referenceId: string) => { + const response = await axios.post(url, data); const { TransactionId, InvoiceStatus, StatusCode, StatusDescription } = response.data; const responseCode = `${StatusCode}`; if (responseCode !== '0') { console.log(`${StatusDescription}`); - throw responseCode; + throw new VendorErrorException(responseCode, StatusDescription); } return { transactionId: TransactionId, @@ -29,30 +30,21 @@ const requestFn = async (url: string, data: any, referenceId: string) => { }; }; -const creditFn = async (url: string, data: any, referenceId: string) => { - const response = await axios.post(url, data).catch((e) => { - return { - data: { - PhoneNumber: data.phoneNumber, - TransactionId: referenceId, - TransactionStatus: 'error', - TransactionMesage: e.message, - } as API.CreditPaymentRes, - }; - }); +const creditFn = async (url: string, data: API.CreditPaymentData, referenceId: string) => { + const response = await axios.post(url, data); const { TransactionId, TransactionMesage, TransactionStatus } = response.data; const responseCode = `${TransactionStatus}`; - if (responseCode === 'error') { + if (responseCode !== 'Approved') { console.log(`credit error: ${TransactionMesage}`); - throw TransactionMesage; + throw new VendorErrorException(responseCode, 'EDAHAB-CREDIT-ERROR'); } return { transactionId: TransactionId, paymentStatus: TransactionStatus, - referenceId: generateUuid(), + referenceId, raw: response.data, }; }; @@ -69,7 +61,7 @@ export const createEdahabHandler = defineHandler({ creditUrl: z.string(), }), }), - purchase: edahabPurchase, + purchase: edahabPurchase, credit: edahabPurchase, }, defaultConfig: { @@ -79,7 +71,7 @@ export const createEdahabHandler = defineHandler({ creditUrl: '/api/agentPayment?hash=', }, }, - purchase: async ({ ctx, options }: { ctx: PaymentCtx, options: PaymentOptions }) => { + purchase: async ({ ctx, options }: { ctx: PaymentCtx, options: PaymentOptions }) => { const parsedData = safeParse(edahabPurchase.pick({ accountNumber: true }), { accountNumber: options.accountNumber }); const accountNumber = parsedData.accountNumber.replace(SO_ACCOUNT_NUMBER, ''); const { links } = ctx; diff --git a/packages/marupay/src/handlers/exeptions.ts b/packages/marupay/src/handlers/exeptions.ts new file mode 100644 index 0000000..c713892 --- /dev/null +++ b/packages/marupay/src/handlers/exeptions.ts @@ -0,0 +1,15 @@ +class MaruPayException extends Error { + constructor(public code: string, public message: string) { + super(message); + this.name = 'MaruPayException'; + } +} + +class VendorErrorException extends MaruPayException { + constructor(public code: string, public message: string) { + super(code, message || 'Vendor error'); + this.name = 'VendorErrorException'; + } +} + +export { MaruPayException, VendorErrorException }; diff --git a/packages/marupay/src/handlers/waafi/waafi.ts b/packages/marupay/src/handlers/waafi/waafi.ts index b7313ec..d6f2352 100644 --- a/packages/marupay/src/handlers/waafi/waafi.ts +++ b/packages/marupay/src/handlers/waafi/waafi.ts @@ -7,6 +7,7 @@ import { PaymentCtx, PaymentOptions } from '../types'; import { prepareRequest } from './prepareRequest'; import { safeParse } from '../../utils/safeParser'; import { soPurchaseNumber } from 'handlers/constants'; +import { VendorErrorException } from 'handlers/exeptions'; const waafiPurchase = z.object({ accountNumber: soPurchaseNumber, @@ -59,7 +60,7 @@ async function sendRequest(url: string, data: API.PurchaseData) { if (responseCode !== '2001' || params == null) { console.log(`WAAFI: API-RES: ${responseMsg} ERROR-CODE: ${errorCode}`); - throw responseCode; + throw new VendorErrorException(errorCode, responseMsg); } return {