Skip to content

Commit

Permalink
Merge pull request #37 from iamshabell/dev
Browse files Browse the repository at this point in the history
add(marupay): exception vendor error handling
  • Loading branch information
iamshabell authored Dec 16, 2023
2 parents 2da12c6 + a39f816 commit 934958d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
30 changes: 11 additions & 19 deletions packages/marupay/src/handlers/edahab/edahab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<API.PurchasePaymentReq, { data: API.PurchasePaymentRes }>(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,
Expand All @@ -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<API.CreditPaymentReq, { data: API.CreditPaymentRes }>(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,
};
};
Expand All @@ -69,7 +61,7 @@ export const createEdahabHandler = defineHandler({
creditUrl: z.string(),
}),
}),
purchase: edahabPurchase,
purchase: edahabPurchase,
credit: edahabPurchase,
},
defaultConfig: {
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions packages/marupay/src/handlers/exeptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class MaruPayException extends Error {
constructor(public code: string, public message: string) {

Check warning on line 2 in packages/marupay/src/handlers/exeptions.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'code' is defined but never used

Check warning on line 2 in packages/marupay/src/handlers/exeptions.ts

View workflow job for this annotation

GitHub Actions / Release

'code' is defined but never used
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 };
3 changes: 2 additions & 1 deletion packages/marupay/src/handlers/waafi/waafi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 934958d

Please sign in to comment.