-
Notifications
You must be signed in to change notification settings - Fork 17
/
paypay-rest-sdk.ts
466 lines (423 loc) · 18 KB
/
paypay-rest-sdk.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Main file, methods that are exposed to end-user
*/
import { auth } from "./auth";
import { Conf } from "./conf";
import { httpsClient } from "./httpsClient";
import { HmacSHA256, enc, algo } from "crypto-js";
import { v4 as uuidv4 } from "uuid";
import * as jwt from "jsonwebtoken";
export interface HttpsClientMessage {
(message: string): void;
}
class PayPayRestSDK {
private options: any = "";
private productionMode: boolean = false;
private config: Conf;
constructor() {
this.config = new Conf(this.productionMode);
}
/**
* Set authentication passed by end-user
* @param {string} clientId API_KEY provided by end-user
* @param {string} clientSecret API_SECRET provided by end-user
* @param {string} merchantId MERCHANT_ID provided by end-user
*/
public configure = (clientConfig: { clientId: string; clientSecret: string; merchantId?: string; productionMode: boolean; }) => {
auth.setAuth(clientConfig.clientId, clientConfig.clientSecret, clientConfig.merchantId);
if (clientConfig.productionMode) {
this.productionMode = clientConfig.productionMode
} else {
this.productionMode = false;
}
this.config = new Conf(this.productionMode);
}
private createAuthHeader = (method: string, resourceUrl: string, body: any, auth: any) => {
const epoch = Math.floor(Date.now() / 1000);
const nonce = uuidv4();
let contentType = "application/json";
let payload = JSON.stringify(body);
let isempty: any = [undefined, null, "", "undefined", "null"];
if (isempty.includes(payload)) {
contentType = "empty";
payload = "empty";
} else {
let md5 = algo.MD5.create();
md5.update(contentType);
md5.update(payload);
payload = md5
.finalize()
.toString(enc.Base64)
.toString();
}
const signatureRawList = [resourceUrl, method, nonce, epoch, contentType, payload];
const signatureRawData = signatureRawList.join("\n");
const hashed = HmacSHA256(signatureRawData, auth.clientSecret);
const hashed64 = enc.Base64.stringify(hashed);
const headList = [auth.clientId, hashed64, nonce, epoch, payload];
const header = headList.join(":");
return `hmac OPA-Auth:${header}`;
}
private setHttpsOptions(header: string) {
let isempty: any = [undefined, null, "", "undefined", "null"];
this.options.hostname = this.config.getHostname();
this.options.port = this.config.getPortNumber();
this.options.headers = {
"Authorization": header,
"X-ASSUME-MERCHANT": auth.merchantId,
};
if (isempty.includes(auth.merchantId)) {
this.options.headers = {
"Authorization": header,
};
}
this.config.setHttpsOptions(this.options);
}
private paypaySetupOptions = (nameApi: string, nameMethod: string, input: any) => {
let queryParams = [];
this.options = this.config.getHttpsOptions();
this.options.path = this.config.getHttpsPath(nameApi, nameMethod);
this.options.method = this.config.getHttpsMethod(nameApi, nameMethod);
this.options.apiKey = this.config.getApiKey(nameApi, nameMethod);
if (this.options.method === "GET" || this.options.method === "DELETE") {
queryParams = this.options.path.match(/{\w+}/g);
if (queryParams) {
queryParams.forEach((q: any, n: string | number) => {
this.options.path = this.options.path.replace(q, input[n]);
});
}
} else if (this.options.method === "POST") {
input.requestedAt = Math.round(new Date().getTime() / 1000);
}
let cleanPath = this.options.path.split("?")[0];
const authHeader = this.createAuthHeader(this.options.method,
cleanPath,
this.options.method === "GET" || this.options.method === "DELETE" ? null : input,
auth);
this.setHttpsOptions(authHeader);
if (this.options.method === "POST") {
this.options.headers["Content-Type"] = "application/json";
this.options.headers["Content-Length"] = Buffer.byteLength(JSON.stringify(input));
}
return this.options;
}
/**
* Create a dynamic QR Code to receive payments
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public createPayment = (payload: any, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "CREATE_PAYMENT", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Create a dynamic QR Code to receive payments
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public qrCodeCreate = (payload: any, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "QRCODE_CREATE", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Delete a created dynamic QR Code
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of codeId : QR Code that is to be deleted
*/
public qrCodeDelete = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "QRCODE_DELETE", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Get payment details for web cashier and Dynamic QR
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getCodePaymentDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "GET_CODE_PAYMENT_DETAILS", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Get payment details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getPaymentDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "GET_PAYMENT_DETAILS", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Cancel a payment
* This api is used in case, while creating a payment, the client can not determine the status of the payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public paymentCancel = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "CANCEL_PAYMENT", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Capture a payment authorization
* This api is used to capture the payment authorization for a payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public paymentAuthCapture = (payload: any, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "PAYMENT_AUTH_CAPTURE", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Revert a payment authorization
* The following api is used when a payment needs to be cancelled. For example, when the user cancels the order
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public paymentAuthRevert = (payload: any, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "PAYMENT_AUTH_REVERT", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Refund a payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public paymentRefund = (payload: any, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "REFUND_PAYMENT", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Get refund details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getRefundDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "GET_REFUND_DETAILS", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Check user wallet balance
* Check if user has enough balance to make a payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Array} inputParams Array of userAuthorizationId, amount, currency, productType
*/
public checkUserWalletBalance = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_WALLET", "CHECK_BALANCE", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Direct user to the authorization web page
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Array} inputParams Array of apiKey, jwtToken
*/
public authorization = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_DIRECT_DEBIT", "AUTHORIZATION", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Receive the user authorization result
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Array} inputParams Array of apiKey, jwtToken
*/
public authorizationResult = (inputParams: Array<string | number>, callback: HttpsClientMessage) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_DIRECT_DEBIT", "AUTHORIZATION_RESULT", inputParams), "", (result: any) => {
callback(result);
});
}
/**
* Create an account link QR Code to authorise OPA client
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public accountLinkQRCodeCreate = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_ACCOUNT_LINK", "QRCODE_CREATE", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Verify the validity of a JWT token
* @returns {Object} Returns the decoded token
* @param {Object} token The token to be verified
* @param {Object} clientSecret The client secret to be used for verification
*/
public validateJWT = (token: string, clientSecret: string): string | object => {
return jwt.verify(token, Buffer.from(clientSecret, "base64"));
}
public paymentPreauthorize = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "PREAUTHORIZE", payload), payload, (result: any) => {
callback(result);
});
}
public paymentSubscription = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_SUBSCRIPTION", "PAYMENTS", payload), payload, (result: any) => {
callback(result);
});
}
/**
* Create pending payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public createPendingPayment = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "PENDING_PAYMENT_CREATE", payload), payload, (result: any) => {
callback(result);
})
}
/**
* Get pending order details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getPendingPaymentDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "GET_ORDER_DETAILS", inputParams), "", (result: any) => {
callback(result);
})
}
/**
* Cancel pending order
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public cancelPendingOrder = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "PENDING_ORDER_CANCEL", inputParams), "", (result: any) => {
callback(result);
})
}
/**
* Refund pending payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public refundPendingPayment = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "REFUND_PAYMENT", payload), payload, (result: any) => {
callback(result);
})
}
/**
* Get user authorization status
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
*/
public getUserAuthorizationStatus = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("USER_AUTHORIZATION", "GET_USER_AUTHORIZATION_STATUS", inputParams), "", (result: any) => {
callback(result);
})
}
/**
* Unlink user
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
*/
public unlinkUser = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("USER_AUTHORIZATION", "UNLINK_USER", inputParams), "", (result: any) => {
callback(result);
})
}
/**
* Cash back
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
* @param inputParams
* @param callback
*/
public cashBack = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "GIVE_CASH_BACK", payload), payload, (result: any) => {
callback(result);
})
}
/**
* Get Cash back details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
* @param inputParams
* @param callback
*/
public getCashBackDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "CHECK_CASHBACK_DETAILS", inputParams), "", (result: any) => {
callback(result);
})
}
/**
* Reverse Cash back
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
* @param inputParams
* @param callback
*/
public reverseCashBack = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "REVERSAL_CASHBACK", payload), payload, (result: any) => {
callback(result);
})
}
/**
* Get Reverse Cash back details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
* @param inputParams
* @param callback
*/
public getReverseCashBackDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("API_PAYMENT", "CHECK_CASHBACK_REVERSE_DETAILS", inputParams), "", (result: any) => {
callback(result);
})
}
}
/**
* These are methods and variables that are exposed to end-user
*/
export let payPayRestSDK = new PayPayRestSDK();