Skip to content

Commit 57311fe

Browse files
committed
refactor: improve PhonePeBase error handling and enhance currency multiplier mapping
- Simplified error handling in PhonePeBase by removing unnecessary type assertions. - Updated currency multiplier mapping to use a Map for better clarity and performance. - Enhanced documentation for the currency multiplier function to improve understanding.
1 parent 7084a26 commit 57311fe

2 files changed

Lines changed: 43 additions & 37 deletions

File tree

src/core/phonepe-base.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ abstract class PhonePeBase extends AbstractPaymentProvider<PhonePeOptions> {
445445
data?.merchantTransactionId) as string;
446446

447447
if (!merchantOrderId || typeof merchantOrderId !== "string") {
448-
return { data: (data || {}) as Record<string, unknown> };
448+
return { data: data || {} };
449449
}
450450

451451
// PhonePe doesn't have a direct cancel API, so we check status
@@ -628,17 +628,17 @@ abstract class PhonePeBase extends AbstractPaymentProvider<PhonePeOptions> {
628628
callbackBody,
629629
);
630630

631-
const paymentData = event.data.object as PhonePeS2SResponse;
631+
const paymentData = event.data.object;
632632
const payload = paymentData?.data || (paymentData as any)?.data?.data || {};
633633

634634
const merchantOrderId =
635-
(payload.merchantOrderId as string | undefined) ||
635+
payload.merchantOrderId ||
636636
(payload.merchantTransactionId as string | undefined) ||
637637
((payload as any).orderId as string | undefined) ||
638638
event.id;
639639

640-
const amount = (payload.amount as number) || 0;
641-
const state = (payload.state as string)?.toUpperCase() || "";
640+
const amount = payload.amount || 0;
641+
const state = payload.state?.toUpperCase() || "";
642642
const code = paymentData?.code || event.event || "";
643643
const eventType: string = code || event.event;
644644

src/utils/get-smallest-unit.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,46 @@ import { BigNumberInput } from "@medusajs/framework/types";
22
import { BigNumber, MathBN } from "@medusajs/framework/utils";
33

44
/**
5-
*
6-
* @param currency
5+
* Map of currency codes to their decimal power (exponent for 10^power multiplier).
6+
* Currencies with power 0 have no decimal places (multiplier = 1).
7+
* Currencies with power 3 have 3 decimal places (multiplier = 1000).
8+
* Default power is 2 (multiplier = 100) for most currencies.
79
*/
8-
function getCurrencyMultiplier(currency: string): number {
9-
const currencyMultipliers: Record<string, string[]> = {
10-
0: [
11-
"BIF",
12-
"CLP",
13-
"DJF",
14-
"GNF",
15-
"JPY",
16-
"KMF",
17-
"KRW",
18-
"MGA",
19-
"PYG",
20-
"RWF",
21-
"UGX",
22-
"VND",
23-
"VUV",
24-
"XAF",
25-
"XOF",
26-
"XPF",
27-
],
28-
3: ["BHD", "IQD", "JOD", "KWD", "OMR", "TND"],
29-
};
10+
const CURRENCY_POWER_MAP = new Map<string, number>([
11+
// Power 0 currencies (multiplier = 1)
12+
["BIF", 0],
13+
["CLP", 0],
14+
["DJF", 0],
15+
["GNF", 0],
16+
["JPY", 0],
17+
["KMF", 0],
18+
["KRW", 0],
19+
["MGA", 0],
20+
["PYG", 0],
21+
["RWF", 0],
22+
["UGX", 0],
23+
["VND", 0],
24+
["VUV", 0],
25+
["XAF", 0],
26+
["XOF", 0],
27+
["XPF", 0],
28+
// Power 3 currencies (multiplier = 1000)
29+
["BHD", 3],
30+
["IQD", 3],
31+
["JOD", 3],
32+
["KWD", 3],
33+
["OMR", 3],
34+
["TND", 3],
35+
]);
3036

37+
/**
38+
* Gets the currency multiplier based on the currency code.
39+
* @param currency - The currency code (e.g., 'INR', 'USD', 'JPY').
40+
* @returns The multiplier (10^power) for the currency. Default is 100 (power 2).
41+
*/
42+
function getCurrencyMultiplier(currency: string): number {
3143
const currencyUpper = currency.toUpperCase();
32-
let power = 2;
33-
for (const [key, value] of Object.entries(currencyMultipliers)) {
34-
if (value.includes(currencyUpper)) {
35-
power = Number.parseInt(key, 10);
36-
break;
37-
}
38-
}
44+
const power = CURRENCY_POWER_MAP.get(currencyUpper) ?? 2;
3945
return Math.pow(10, power);
4046
}
4147

@@ -65,7 +71,7 @@ export function getSmallestUnit(
6571
numeric = Math.ceil(numeric / 10) * 10;
6672
}
6773

68-
return Number.parseInt(numeric.toString().split(".").shift()!, 10);
74+
return Math.floor(numeric);
6975
}
7076

7177
/**

0 commit comments

Comments
 (0)