Skip to content

Commit

Permalink
Refactor Waafi handler and remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
iamshabell committed Dec 16, 2023
1 parent 996b5a6 commit 672f58e
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions packages/marupay/src/handlers/waafi/waafi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ const waafiPurchase = z.object({
accountNumber: soPurchaseNumber,
});

async function sendRequest(url: string, data: API.PurchaseData) {
const response = await axios.post<API.PurchasePaymentReq, { data: API.PurchasePaymentRes }>(url, data);
const { responseCode, responseMsg, errorCode, params } = response.data;

if (responseCode !== '2001' || params == null) {
console.log(`WAAFI: API-RES: ${responseMsg} ERROR-CODE: ${errorCode}`);
throw new VendorErrorException(errorCode, responseMsg);
}

return {
transactionId: params.transactionId,
paymentStatus: params.state,
referenceId: params.referenceId.toString(),
raw: response.data,
};
}

export const createWaafiHandler = defineHandler({
schema: {
config: z.object({
Expand All @@ -34,15 +51,15 @@ export const createWaafiHandler = defineHandler({
baseUrl: 'https://api.waafipay.net/asm',
},
},
purchase: async ({ ctx, options }: { ctx: PaymentCtx, options: PaymentOptions }) => {
purchase: async ({ ctx, options }) => {
const parsedData = safeParse(waafiPurchase.pick({ accountNumber: true }), { accountNumber: options.accountNumber });
const accountNumber = parsedData.accountNumber.replace("+", '');
const requestUrl = `${ctx.links.baseUrl}`;
const PurchaseData = prepareRequest('request', { ...options, accountNumber }, ctx, generateUuid());

return await sendRequest(requestUrl, PurchaseData);
},
credit: async ({ ctx, options }: { ctx: PaymentCtx, options: PaymentOptions }) => {
credit: async ({ ctx, options }) => {
const parsedData = safeParse(waafiPurchase.pick({ accountNumber: true }), { accountNumber: options.accountNumber });
const accountNumber = parsedData.accountNumber.replace("+", '');
const requestUrl = `${ctx.links.baseUrl}`;
Expand All @@ -53,20 +70,3 @@ export const createWaafiHandler = defineHandler({
});

export type WaafiHandler = ReturnType<typeof createWaafiHandler>

async function sendRequest(url: string, data: API.PurchaseData) {
const response = await axios.post<API.PurchasePaymentReq, { data: API.PurchasePaymentRes }>(url, data);
const { responseCode, responseMsg, errorCode, params } = response.data;

if (responseCode !== '2001' || params == null) {
console.log(`WAAFI: API-RES: ${responseMsg} ERROR-CODE: ${errorCode}`);
throw new VendorErrorException(errorCode, responseMsg);
}

return {
transactionId: params.transactionId,
paymentStatus: params.state,
referenceId: params.referenceId.toString(),
raw: response.data,
};
}

0 comments on commit 672f58e

Please sign in to comment.