-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
178 lines (157 loc) · 4.12 KB
/
index.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
174
175
176
177
178
const request = require('request');
const fs = require("fs");
const path = require("path");
const crypto = require('crypto');
class AlipayRequest
{
constructor()
{
this.init();
}
init()
{
this._gateWayUrl = '';
this._rsaPrivateKey = '';
this._alipayrsaPublicKey = '';
this.initParam();
}
initParam()
{
this.param = {};
}
set gateWayUrl(gateWayUrl)
{
this._gateWayUrl = gateWayUrl;
}
get gateWayUrl()
{
return this._gateWayUrl;
}
set rsaPrivateKey(pemPath)
{
this._rsaPrivateKey = this.getPemStr(pemPath);
}
get rsaPrivateKey()
{
return this._rsaPrivateKey;
}
set alipayrsaPublicKey(pemPath)
{
this._alipayrsaPublicKey = this.getPemStr(pemPath);
}
get alipayrsaPublicKey()
{
return this._alipayrsaPublicKey;
}
setParam(name,value)
{
this.param[name] = value;
}
getParam()
{
return this.param;
}
paramSort()
{
let newParam = {};
let keyList = [];
for(let index in this.param)
{
keyList.push(index);
}
keyList = keyList.sort();
for(let index in keyList)
{
newParam[keyList[index]] = this.param[keyList[index]];
}
this.param = newParam;
}
paramQuery()
{
let i = 0;
let signBefore = '';
if (this.param.hasOwnProperty('sign'))
{
delete this.param['sign'];
}
this.paramSort();
for(let index in this.param)
{
if(i==0)
{
signBefore+=index+'='+this.param[index];
} else {
signBefore+='&'+index+'='+this.param[index];
}
i++;
}
return signBefore;
}
getSign()
{
let signBefore = this.paramQuery();
if (!this.param.hasOwnProperty('sign_type'))
{
throw new Error('Param not has property:sign_type');
}
switch (this.param['sign_type'])
{
case 'RSA':
var sign = crypto.createSign('RSA-SHA1');
break;
case 'RSA2':
var sign = crypto.createSign('RSA-SHA256');
break;
}
sign.update(signBefore);
let res = sign.sign(this._rsaPrivateKey,'base64');
// console.log(signBefore);
// console.log(res);
return res;
}
getPemStr(pemPath)
{
let pemStr = fs.readFileSync(pemPath).toString();
// pemStr = pemStr.replace(/--.*?-----/g, "");
// pemStr = pemStr.replace(/\s/g, "");
return pemStr;
}
getNowFormatDate(incrTime=0) {
var date = new Date();
date.setTime(date.getTime()+incrTime);
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var currentdate = date.getFullYear() + seperator1 + this.timeAddZero(month) + seperator1 + this.timeAddZero(strDate)
+ " " + this.timeAddZero(date.getHours()) + seperator2 + this.timeAddZero(date.getMinutes())
+ seperator2 + this.timeAddZero(date.getSeconds());
return currentdate;
}
timeAddZero(sz)
{
if (sz >= 0 && sz <= 9) {
sz = "0" + sz;
}
return sz;
}
getYmdFormatDate(incrTime=0) {
var date = new Date();
date.setTime(date.getTime()+incrTime);
var seperator1 = "-";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var currentdate = date.getFullYear() + seperator1 + this.timeAddZero(month) + seperator1 + this.timeAddZero(strDate);
return currentdate;
}
getRes()
{
return new Promise((resolve,reject)=>{
request.post({url:this._gateWayUrl, form: this.param}, function(err,response,body){
if(err)reject(err);
resolve({response:response,body:body});
})
});
}
}
module.exports = AlipayRequest;