-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
173 lines (151 loc) · 4.48 KB
/
app.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { default: axios } = require("axios");
const endpoints = require("./endpoints");
const { encodeRequest, signRequest, request } = require("./helpers");
const CHARGE_ENDPOINT = "/pg/v1/pay";
const QRINIT_ENDPOINT = "/v3/qr/init";
const TRANSACTION_ENDPOINT = "/v3/transaction";
const REFUND_ENDPOINT = "/v3/credit/backToSource";
const PAYOUT_ENDPOINT = "/pg/v1/pay";
class Client {
constructor(config) {
this.env = config.env || "UAT";
this.merchantId = config.merchantId || "unknown-merchant";
this.apiKeys = config.apiKeys;
this.hostname = "https://api-preprod.phonepe.com/apis/pg-sandbox";
if (!this.apiKeys) {
throw new Error("API keys missing");
}
}
charge(amount, transactionId, mobile, apiKeyIndex) {
const payload = {
merchantId: this.merchantId, // 'M10HR1UHP5A8', //
merchantTransactionId: transactionId,
merchantUserId: "akash",
amount: amount,
redirectUrl: "https://webhook.site/redirect-url",
redirectMode: "REDIRECT",
callbackUrl: "https://webhook.site/callback-url",
mobileNumber: mobile,
paymentInstrument: {
type: "PAY_PAGE",
},
};
console.log(transactionId);
const saltKey = this.apiKeys[this.env];
// 4915c02d-e87b-47e3-b2fc-d56dbf65e387`
const base64 = encodeRequest(payload);
const sign = `${base64}${PAYOUT_ENDPOINT}${saltKey}`;
const X_VERIFY = `${signRequest(sign)}###${apiKeyIndex || 1}`;
return axios.post(
`${this.hostname}${PAYOUT_ENDPOINT}`,
{ request: base64 },
{
headers: {
"Content-Type": "application/json",
"X-VERIFY": X_VERIFY,
},
withCredentials: false,
}
);
/* return request(
{
method: "POST",
hostname: endpoints[this.env] + CHARGE_ENDPOINT,
// path: CHARGE_ENDPOINT,
headers: {
"Content-Type": "application/json",
"X-VERIFY": X_VERIFY,
},
},
{ request: base64 }
); */
}
qrcode(amount, transactionId, apiKeyIndex) {
const payload = {
amount: amount, // Amount in paise
expiresIn: 180,
merchantId: this.merchantId,
merchantOrderId: transactionId,
storeId: this.storeId,
terminalId: this.terminalId,
transactionId: transactionId,
message: "Payment for " + transactionId,
};
const base64 = encodeRequest(payload);
const sign = base64 + QRINIT_ENDPOINT + this.apiKeys[apiKeyIndex];
const X_VERIFY = make_hash(sign) + "###" + apiKeyIndex;
return request(
{
method: "POST",
hostname: endpoints[this.env],
path: QRINIT_ENDPOINT,
headers: {
"Content-Type": "application/json",
"X-VERIFY": X_VERIFY,
},
},
{ request: base64 }
);
}
status(transactionId, apiKeyIndex) {
const endpoint =
TRANSACTION_ENDPOINT +
"/" +
this.merchantId +
"/" +
transactionId +
"/status";
const sign = endpoint + this.apiKeys[apiKeyIndex];
const X_VERIFY = make_hash(sign) + "###" + apiKeyIndex;
return request({
method: "GET",
hostname: endpoints[this.env],
path: endpoint,
headers: {
"Content-Type": "application/json",
"X-VERIFY": X_VERIFY,
},
});
}
cancel(transactionId, apiKeyIndex) {
const endpoint =
CHARGE_ENDPOINT + "/" + this.merchantId + "/" + transactionId + "/cancel";
const sign = endpoint + this.apiKeys[apiKeyIndex];
const X_VERIFY = signRequest(sign) + "###" + apiKeyIndex;
return request({
method: "POST",
hostname: endpoints[this.env],
path: endpoint,
headers: {
"Content-Type": "application/json",
"X-VERIFY": X_VERIFY,
},
});
}
refund(transactionId, providerReferenceId, apiKeyIndex) {
const payload = {
amount: 100,
merchantId: this.merchantId,
providerReferenceId: providerReferenceId,
transactionId: transactionId + "_refund",
message: "Refund",
};
const base64 = encodeRequest(payload);
const sign = base64 + REFUND_ENDPOINT + this.apiKeys[apiKeyIndex];
const X_VERIFY = signRequest(sign) + "###" + apiKeyIndex;
/*
return request(
{
method: "POST",
hostname: endpoints[this.env],
path: REFUND_ENDPOINT,
headers: {
"Content-Type": "application/json",
"X-VERIFY": X_VERIFY,
},
},
{ request: base64_payload }
); */
}
}
module.exports = Client;