-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
149 lines (138 loc) · 3.87 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import axios, { AxiosInstance } from "axios";
import type {
InitiatePaymentRequest,
InitiatePaymentResponse,
} from "./epaymentv2/initiatePayment";
import type {
AllLookupResponse,
PaymentLookupRequest,
} from "./epaymentv2/paymentLookup";
/**
* Wraps a Promise in a try/catch block, returning a tuple for robust error handling.
*
* @template T - The expected data type of the resolved Promise.
* @param promise - The Promise to be wrapped.
* @returns A tuple of `[data, error]` where:
* - `data` is the resolved data of the Promise (or null on error).
* - `error` is the error object if the Promise rejected (or null on success).
*/
async function awTry<T>(promise: unknown) {
try {
const data = await promise;
return [data, null] as [T, never];
} catch (err) {
console.error(err);
return [null, err] as [never, unknown];
}
}
/**
* Handles payment lookups using an Axios instance.
*
* @param instance - A configured Axios instance.
* @returns A function to perform payment lookups.
*/
const paymentLookup = (instance: AxiosInstance) => {
return async ({ pidx }: PaymentLookupRequest) => {
const [response, error] = await awTry<{ data: AllLookupResponse }>(
instance.get(`/epayment/lookup?pidx=${pidx}`)
);
if (error) {
console.log("Error, in lookupPayment: ", error);
}
return response.data;
};
};
/**
* Handles payment initiation using an Axios instance.
*
* @param instance - A configured Axios instance.
* @returns A function to initiate payments.
*/
const initiatePayment = (instance: AxiosInstance) => {
return async (payload: InitiatePaymentRequest) => {
const [response, error] = await awTry<{ data: InitiatePaymentResponse }>(
instance.post("/epayment/initiate/", payload)
);
if (error) {
console.log("Error, in initiatePayment: ", error);
}
return response.data;
};
};
/**
* Creates a configured Khalti instance for handling payments for us.
*
* @param env - The environment to use (`sandbox` or `production`).
* @param secretKey - The Khalti live secret key.
* @returns An object with `paymentLookup` and `initiatePayment` functions bound to the instance.
*/
export const createKhaltiInstance = ({
env,
secretKey,
}: {
env: "sandbox" | "production";
secretKey: string;
}) => {
const urls = {
sandbox: "https://a.khalti.com/api/v2",
production: "https://khalti.com/api/v2",
};
const instance = axios.create({
baseURL: urls[env],
headers: {
"Content-Type": "application/json",
Authorization: `Key ${secretKey}`, // live secret key
},
responseType: "json",
});
return {
paymentLookup: paymentLookup(instance),
initiatePayment: initiatePayment(instance),
};
};
// const ins = createKhaltiInstance({
// env: "sandbox",
// secretKey: "Asd",
// });
// async function a() {
// const { payment_url, pidx } = await ins.initiatePayment({
// return_url: "https://example.com/payment/",
// website_url: "https://example.com/",
// amount: 1300,
// purchase_order_id: "test12",
// purchase_order_name: "test",
// customer_info: {
// name: "Khalti Bahadur",
// email: "example@gmail.com",
// phone: "9800000123",
// },
// amount_breakdown: [
// {
// label: "Mark Price",
// amount: 1000,
// },
// {
// label: "VAT",
// amount: 300,
// },
// ],
// product_details: [
// {
// identity: "1234567890",
// name: "Khalti logo",
// total_price: 1300,
// quantity: 1,
// unit_price: 1300,
// },
// ],
// merchant_username: "merchant_name",
// merchant_extra: "merchant_extra",
// });
// const ff = await ins.paymentLookup({
// pidx,
// });
// if (ff.status === "Completed") {
// console.log("Payment Success");
// }
// console.log({ payment_url, pidx });
// }