-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpayir.js
94 lines (89 loc) · 3.52 KB
/
payir.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
/**
* Pay.ir Node.js Module
* @module Pay.ir
* @author Erfan Sahafnejad <Erfan.Sahaf[at]gmail.com>
* @copyright Pay.ir 2017
* @version 1.0.0
* @license Apache-2.0
*/
/** Pay.ir Class */
class Payir
{
/**
* Get the API Key
* @param {string} api Your gateway API Key.
* @throws Will throw an error if the API isn't string.
*/
constructor(api){
if(api != '' && typeof api === 'string'){
this.request = require('request');
this.api = api;
this.sendEndPoint = 'https://pay.ir/payment/send';
this.verifyEndPoint = 'https://pay.ir/payment/verify';
this.gateway = 'https://pay.ir/payment/gateway/';
}
else
throw new Error('You should pass your Pay.ir API Key to the constructor.');
}
/**
* Build and prepare transaction URL
* @param {number} amount Transaction's Amount
* @param {string} callbackURL User will redirect to this URL to check transaction status
* @param {string} [null] factorNumber Order ID or Invoice Number
* @throws Will throw an error if URL building isn't successfull.
*/
send(amount, callbackURL, factorNumber){
const $this = this;
factorNumber = factorNumber || null;
return new Promise((resolve, reject) => {
if(typeof amount !== 'number' || amount < 1000)
throw new Error('Transaction\'s amount must be a number and equal/greater than 1000');
else if(typeof callbackURL !== 'string' || callbackURL.length < 5)
throw new Error('Callback (redirect) URL must be a string.');
else if(callbackURL.slice(0,4) != 'http')
throw new Error('Callback URL must start with http/https');
this.request.post({
url: this.sendEndPoint,
form: {api: $this.api, amount, redirect: callbackURL, factorNumber}
}, (error, response, body) => {
if(error)
reject(error.code);
else if(response.statusCode != 200)
reject(new Error('Request status code was not OK.'));
else if(typeof body != 'undefined' && JSON.parse(body).status != 1)
reject(JSON.parse(body).errorMessage);
resolve(this.gateway + JSON.parse(body).transId);
});
});
}
/**
*
* @param {Object} req Your webframework POST body/payload
*/
verify(requestBody){
const $this = this;
let transId = parseInt(requestBody.transId);
return new Promise((resolve, reject) => {
if(!transId || typeof transId !== 'number')
throw new Error('Transaction ID is not valid.');
this.request.post({
url: this.verifyEndPoint,
form: {api: this.api, transId}
}, (error, response, body) => {
if(error)
reject(error.code);
else if(response.statusCode != 200)
reject(new Error('Request status code was not OK.'));
else if(typeof body != 'undefined' && JSON.parse(body).status != 1)
reject(JSON.parse(body).errorMessage);
resolve({
amount: JSON.parse(body).amount,
cardNumber: requestBody.cardNumber,
transactionId: transId,
factorNumber: requestBody.factorNumber
});
});
});
}
}
module.exports = Payir;