wxpay-node-v3 4.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1215 @@
1
+ 'use strict';
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ var __generator = (this && this.__generator) || function (thisArg, body) {
36
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
37
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
38
+ function verb(n) { return function (v) { return step([n, v]); }; }
39
+ function step(op) {
40
+ if (f) throw new TypeError("Generator is already executing.");
41
+ while (_) try {
42
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
43
+ if (y = 0, t) op = [op[0] & 2, t.value];
44
+ switch (op[0]) {
45
+ case 0: case 1: t = op; break;
46
+ case 4: _.label++; return { value: op[1], done: false };
47
+ case 5: _.label++; y = op[1]; op = [0]; continue;
48
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
49
+ default:
50
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
51
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
52
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
53
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
54
+ if (t[2]) _.ops.pop();
55
+ _.trys.pop(); continue;
56
+ }
57
+ op = body.call(thisArg, _);
58
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
59
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
60
+ }
61
+ };
62
+ var __importDefault = (this && this.__importDefault) || function (mod) {
63
+ return (mod && mod.__esModule) ? mod : { "default": mod };
64
+ };
65
+ var crypto_1 = __importDefault(require("crypto"));
66
+ var x509_1 = require('@fidm/x509');
67
+ var base_1 = require("./lib/base");
68
+ var pay_request_1 = require("./lib/pay-request");
69
+ var Pay = /** @class */ (function (_super) {
70
+ __extends(Pay, _super);
71
+ function Pay(arg1, mchid, publicKey, privateKey, options) {
72
+ var _this = _super.call(this) || this;
73
+ _this.serial_no = ''; // 证书序列号
74
+ _this.authType = 'WECHATPAY2-SHA256-RSA2048'; // 认证类型,目前为WECHATPAY2-SHA256-RSA2048
75
+ if (arg1 instanceof Object) {
76
+ _this.appid = arg1.appid;
77
+ _this.mchid = arg1.mchid;
78
+ if (arg1.serial_no)
79
+ _this.serial_no = arg1.serial_no;
80
+ _this.publicKey = arg1.publicKey;
81
+ if (!_this.publicKey)
82
+ throw new Error('缺少公钥');
83
+ _this.privateKey = arg1.privateKey;
84
+ if (!arg1.serial_no)
85
+ _this.serial_no = _this.getSN(_this.publicKey);
86
+ _this.authType = arg1.authType || 'WECHATPAY2-SHA256-RSA2048';
87
+ _this.userAgent = arg1.userAgent || '127.0.0.1';
88
+ _this.key = arg1.key;
89
+ }
90
+ else {
91
+ var _optipns = options || {};
92
+ _this.appid = arg1;
93
+ _this.mchid = mchid || '';
94
+ _this.publicKey = publicKey;
95
+ _this.privateKey = privateKey;
96
+ _this.authType = _optipns.authType || 'WECHATPAY2-SHA256-RSA2048';
97
+ _this.userAgent = _optipns.userAgent || '127.0.0.1';
98
+ _this.key = _optipns.key;
99
+ _this.serial_no = _optipns.serial_no || '';
100
+ if (!_this.publicKey)
101
+ throw new Error('缺少公钥');
102
+ if (!_this.serial_no)
103
+ _this.serial_no = _this.getSN(_this.publicKey);
104
+ }
105
+ _this.httpService = new pay_request_1.PayRequest();
106
+ return _this;
107
+ }
108
+ /**
109
+ * 自定义创建http 请求
110
+ */
111
+ Pay.prototype.createHttp = function (service) {
112
+ this.httpService = service;
113
+ };
114
+ /**
115
+ * 获取微信平台key
116
+ * @param apiSecret APIv3密钥
117
+ * @returns
118
+ */
119
+ Pay.prototype.get_certificates = function (apiSecret) {
120
+ var _a;
121
+ return __awaiter(this, void 0, void 0, function () {
122
+ var url, authorization, headers, result, data, _i, data_1, item, decryptCertificate;
123
+ return __generator(this, function (_b) {
124
+ switch (_b.label) {
125
+ case 0:
126
+ url = 'https://api.mch.weixin.qq.com/v3/certificates';
127
+ authorization = this.buildAuthorization('GET', url);
128
+ headers = this.getHeaders(authorization);
129
+ return [4 /*yield*/, this.httpService.get(url, headers)];
130
+ case 1:
131
+ result = _b.sent();
132
+ if (result.status === 200) {
133
+ data = (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.data;
134
+ for (_i = 0, data_1 = data; _i < data_1.length; _i++) {
135
+ item = data_1[_i];
136
+ decryptCertificate = this.decipher_gcm(item.encrypt_certificate.ciphertext, item.encrypt_certificate.associated_data, item.encrypt_certificate.nonce, apiSecret);
137
+ item.publicKey = x509_1.Certificate.fromPEM(Buffer.from(decryptCertificate)).publicKey.toPEM();
138
+ }
139
+ return [2 /*return*/, data];
140
+ }
141
+ else {
142
+ throw new Error('拉取平台证书失败');
143
+ }
144
+ return [2 /*return*/];
145
+ }
146
+ });
147
+ });
148
+ };
149
+ /**
150
+ * 拉取平台证书到 Pay.certificates 中
151
+ * @param apiSecret APIv3密钥
152
+ * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/wechatpay5_1.shtml
153
+ */
154
+ Pay.prototype.fetchCertificates = function (apiSecret) {
155
+ var _a;
156
+ return __awaiter(this, void 0, void 0, function () {
157
+ var url, authorization, headers, result, data, newCertificates_1;
158
+ var _this = this;
159
+ return __generator(this, function (_b) {
160
+ switch (_b.label) {
161
+ case 0:
162
+ url = 'https://api.mch.weixin.qq.com/v3/certificates';
163
+ authorization = this.buildAuthorization('GET', url);
164
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
165
+ return [4 /*yield*/, this.httpService.get(url, headers)];
166
+ case 1:
167
+ result = _b.sent();
168
+ if (result.status === 200) {
169
+ data = (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.data;
170
+ newCertificates_1 = {};
171
+ data.forEach(function (item) {
172
+ var decryptCertificate = _this.decipher_gcm(item.encrypt_certificate.ciphertext, item.encrypt_certificate.associated_data, item.encrypt_certificate.nonce, apiSecret);
173
+ newCertificates_1[item.serial_no] = x509_1.Certificate.fromPEM(Buffer.from(decryptCertificate)).publicKey.toPEM();
174
+ });
175
+ Pay.certificates = __assign(__assign({}, Pay.certificates), newCertificates_1);
176
+ }
177
+ else {
178
+ throw new Error('拉取平台证书失败');
179
+ }
180
+ return [2 /*return*/];
181
+ }
182
+ });
183
+ });
184
+ };
185
+ /**
186
+ * 验证签名,提醒:node 取头部信息时需要用小写,例如:req.headers['wechatpay-timestamp']
187
+ * @param params.timestamp HTTP头Wechatpay-Timestamp 中的应答时间戳
188
+ * @param params.nonce HTTP头Wechatpay-Nonce 中的应答随机串
189
+ * @param params.body 应答主体(response Body),需要按照接口返回的顺序进行验签,错误的顺序将导致验签失败。
190
+ * @param params.serial HTTP头Wechatpay-Serial 证书序列号
191
+ * @param params.signature HTTP头Wechatpay-Signature 签名
192
+ * @param params.apiSecret APIv3密钥,如果在 构造器 中有初始化该值(this.key),则可以不传入。当然传入也可以
193
+ */
194
+ Pay.prototype.verifySign = function (params) {
195
+ return __awaiter(this, void 0, void 0, function () {
196
+ var timestamp, nonce, body, serial, signature, apiSecret, publicKey, bodyStr, data, verify;
197
+ return __generator(this, function (_a) {
198
+ switch (_a.label) {
199
+ case 0:
200
+ timestamp = params.timestamp, nonce = params.nonce, body = params.body, serial = params.serial, signature = params.signature, apiSecret = params.apiSecret;
201
+ publicKey = Pay.certificates[serial];
202
+ if (!!publicKey) return [3 /*break*/, 2];
203
+ return [4 /*yield*/, this.fetchCertificates(apiSecret)];
204
+ case 1:
205
+ _a.sent();
206
+ _a.label = 2;
207
+ case 2:
208
+ publicKey = Pay.certificates[serial];
209
+ if (!publicKey) {
210
+ throw new Error('平台证书序列号不相符,未找到平台序列号');
211
+ }
212
+ bodyStr = typeof body === 'string' ? body : JSON.stringify(body);
213
+ data = timestamp + "\n" + nonce + "\n" + bodyStr + "\n";
214
+ verify = crypto_1.default.createVerify('RSA-SHA256');
215
+ verify.update(data);
216
+ return [2 /*return*/, verify.verify(publicKey, signature, 'base64')];
217
+ }
218
+ });
219
+ });
220
+ };
221
+ /**
222
+ * 敏感信息加密
223
+ * @param str 敏感信息字段(如用户的住址、银行卡号、手机号码等)
224
+ * @returns
225
+ */
226
+ Pay.prototype.publicEncrypt = function (str, wxPublicKey, padding) {
227
+ if (padding === void 0) { padding = crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING; }
228
+ if (![crypto_1.default.constants.RSA_PKCS1_PADDING, crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING].includes(padding)) {
229
+ throw new Error("Doesn't supported the padding mode(" + padding + "), here's only support RSA_PKCS1_OAEP_PADDING or RSA_PKCS1_PADDING.");
230
+ }
231
+ var encrypted = crypto_1.default.publicEncrypt({ key: wxPublicKey, padding: padding, oaepHash: 'sha1' }, Buffer.from(str, 'utf8')).toString('base64');
232
+ return encrypted;
233
+ };
234
+ /**
235
+ * 敏感信息解密
236
+ * @param str 敏感信息字段(如用户的住址、银行卡号、手机号码等)
237
+ * @returns
238
+ */
239
+ Pay.prototype.privateDecrypt = function (str, padding) {
240
+ if (padding === void 0) { padding = crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING; }
241
+ if (![crypto_1.default.constants.RSA_PKCS1_PADDING, crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING].includes(padding)) {
242
+ throw new Error("Doesn't supported the padding mode(" + padding + "), here's only support RSA_PKCS1_OAEP_PADDING or RSA_PKCS1_PADDING.");
243
+ }
244
+ var decrypted = crypto_1.default.privateDecrypt({ key: this.privateKey, padding: padding, oaepHash: 'sha1' }, Buffer.from(str, 'base64'));
245
+ return decrypted.toString('utf8');
246
+ };
247
+ /**
248
+ * 构建请求签名参数
249
+ * @param method Http 请求方式
250
+ * @param url 请求接口 例如/v3/certificates
251
+ * @param timestamp 获取发起请求时的系统当前时间戳
252
+ * @param nonceStr 随机字符串
253
+ * @param body 请求报文主体
254
+ */
255
+ Pay.prototype.getSignature = function (method, nonce_str, timestamp, url, body) {
256
+ var str = method + '\n' + url + '\n' + timestamp + '\n' + nonce_str + '\n';
257
+ var bodyString = body ? JSON.stringify(body) : '';
258
+ str = str + bodyString + '\n';
259
+ return this.sha256WithRsa(str);
260
+ };
261
+ // jsapi 和 app 支付参数签名 加密自动顺序如下 不能错乱
262
+ // 应用id
263
+ // 时间戳
264
+ // 随机字符串
265
+ // 预支付交易会话ID
266
+ Pay.prototype.sign = function (str) {
267
+ return this.sha256WithRsa(str);
268
+ };
269
+ // 获取序列号
270
+ Pay.prototype.getSN = function (fileData) {
271
+ if (!fileData && !this.publicKey)
272
+ throw new Error('缺少公钥');
273
+ if (!fileData)
274
+ fileData = this.publicKey;
275
+ if (typeof fileData == 'string') {
276
+ fileData = Buffer.from(fileData);
277
+ }
278
+ var certificate = x509_1.Certificate.fromPEM(fileData);
279
+ return certificate.serialNumber;
280
+ };
281
+ /**
282
+ * SHA256withRSA
283
+ * @param data 待加密字符
284
+ * @param privatekey 私钥key key.pem fs.readFileSync(keyPath)
285
+ */
286
+ Pay.prototype.sha256WithRsa = function (data) {
287
+ if (!this.privateKey)
288
+ throw new Error('缺少秘钥文件');
289
+ return crypto_1.default
290
+ .createSign('RSA-SHA256')
291
+ .update(data)
292
+ .sign(this.privateKey, 'base64');
293
+ };
294
+ /**
295
+ * 获取授权认证信息
296
+ * @param nonceStr 请求随机串
297
+ * @param timestamp 时间戳
298
+ * @param signature 签名值
299
+ */
300
+ Pay.prototype.getAuthorization = function (nonce_str, timestamp, signature) {
301
+ var _authorization = 'mchid="' +
302
+ this.mchid +
303
+ '",' +
304
+ 'nonce_str="' +
305
+ nonce_str +
306
+ '",' +
307
+ 'timestamp="' +
308
+ timestamp +
309
+ '",' +
310
+ 'serial_no="' +
311
+ this.serial_no +
312
+ '",' +
313
+ 'signature="' +
314
+ signature +
315
+ '"';
316
+ return this.authType.concat(' ').concat(_authorization);
317
+ };
318
+ /**
319
+ * 回调解密
320
+ * @param ciphertext Base64编码后的开启/停用结果数据密文
321
+ * @param associated_data 附加数据
322
+ * @param nonce 加密使用的随机串
323
+ * @param key APIv3密钥
324
+ */
325
+ Pay.prototype.decipher_gcm = function (ciphertext, associated_data, nonce, key) {
326
+ if (!key)
327
+ key = this.key;
328
+ if (!key)
329
+ throw new Error('缺少key');
330
+ var _ciphertext = Buffer.from(ciphertext, 'base64');
331
+ // 解密 ciphertext字符 AEAD_AES_256_GCM算法
332
+ var authTag = _ciphertext.slice(_ciphertext.length - 16);
333
+ var data = _ciphertext.slice(0, _ciphertext.length - 16);
334
+ var decipher = crypto_1.default.createDecipheriv('aes-256-gcm', key, nonce);
335
+ decipher.setAuthTag(authTag);
336
+ decipher.setAAD(Buffer.from(associated_data));
337
+ var decoded = decipher.update(data, undefined, 'utf8');
338
+ try {
339
+ return JSON.parse(decoded);
340
+ }
341
+ catch (e) {
342
+ return decoded;
343
+ }
344
+ };
345
+ /**
346
+ * 参数初始化
347
+ */
348
+ Pay.prototype.buildAuthorization = function (method, url, params) {
349
+ var nonce_str = Math.random()
350
+ .toString(36)
351
+ .substr(2, 15), timestamp = parseInt(+new Date() / 1000 + '').toString();
352
+ var signature = this.getSignature(method, nonce_str, timestamp, url.replace('https://api.mch.weixin.qq.com', ''), params);
353
+ var authorization = this.getAuthorization(nonce_str, timestamp, signature);
354
+ return authorization;
355
+ };
356
+ //#region 支付相关接口
357
+ /**
358
+ * h5支付
359
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml
360
+ */
361
+ Pay.prototype.transactions_h5 = function (params) {
362
+ return __awaiter(this, void 0, void 0, function () {
363
+ var _params, url, authorization, headers;
364
+ return __generator(this, function (_a) {
365
+ switch (_a.label) {
366
+ case 0:
367
+ _params = __assign({ appid: this.appid, mchid: this.mchid }, params);
368
+ url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/h5';
369
+ authorization = this.buildAuthorization('POST', url, _params);
370
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
371
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
372
+ case 1: return [2 /*return*/, _a.sent()];
373
+ }
374
+ });
375
+ });
376
+ };
377
+ /**
378
+ * 合单h5支付
379
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_2.shtml
380
+ */
381
+ Pay.prototype.combine_transactions_h5 = function (params) {
382
+ return __awaiter(this, void 0, void 0, function () {
383
+ var _params, url, authorization, headers;
384
+ return __generator(this, function (_a) {
385
+ switch (_a.label) {
386
+ case 0:
387
+ _params = __assign({ combine_appid: this.appid, combine_mchid: this.mchid }, params);
388
+ url = 'https://api.mch.weixin.qq.com/v3/combine-transactions/h5';
389
+ authorization = this.buildAuthorization('POST', url, _params);
390
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
391
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
392
+ case 1: return [2 /*return*/, _a.sent()];
393
+ }
394
+ });
395
+ });
396
+ };
397
+ /**
398
+ * native支付
399
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
400
+ */
401
+ Pay.prototype.transactions_native = function (params) {
402
+ return __awaiter(this, void 0, void 0, function () {
403
+ var _params, url, authorization, headers;
404
+ return __generator(this, function (_a) {
405
+ switch (_a.label) {
406
+ case 0:
407
+ _params = __assign({ appid: this.appid, mchid: this.mchid }, params);
408
+ url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/native';
409
+ authorization = this.buildAuthorization('POST', url, _params);
410
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
411
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
412
+ case 1: return [2 /*return*/, _a.sent()];
413
+ }
414
+ });
415
+ });
416
+ };
417
+ /**
418
+ * 合单native支付
419
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_5.shtml
420
+ */
421
+ Pay.prototype.combine_transactions_native = function (params) {
422
+ return __awaiter(this, void 0, void 0, function () {
423
+ var _params, url, authorization, headers;
424
+ return __generator(this, function (_a) {
425
+ switch (_a.label) {
426
+ case 0:
427
+ _params = __assign({ combine_appid: this.appid, combine_mchid: this.mchid }, params);
428
+ url = 'https://api.mch.weixin.qq.com/v3/combine-transactions/native';
429
+ authorization = this.buildAuthorization('POST', url, _params);
430
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
431
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
432
+ case 1: return [2 /*return*/, _a.sent()];
433
+ }
434
+ });
435
+ });
436
+ };
437
+ /**
438
+ * app支付
439
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml
440
+ */
441
+ Pay.prototype.transactions_app = function (params) {
442
+ return __awaiter(this, void 0, void 0, function () {
443
+ var _params, url, authorization, headers, result, data, str;
444
+ return __generator(this, function (_a) {
445
+ switch (_a.label) {
446
+ case 0:
447
+ _params = __assign({ appid: this.appid, mchid: this.mchid }, params);
448
+ url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/app';
449
+ authorization = this.buildAuthorization('POST', url, _params);
450
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
451
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
452
+ case 1:
453
+ result = _a.sent();
454
+ if (result.status === 200 && result.data.prepay_id) {
455
+ data = {
456
+ appid: _params.appid,
457
+ partnerid: _params.mchid,
458
+ package: 'Sign=WXPay',
459
+ timestamp: parseInt(+new Date() / 1000 + '').toString(),
460
+ noncestr: Math.random()
461
+ .toString(36)
462
+ .substr(2, 15),
463
+ prepayid: result.data.prepay_id,
464
+ sign: '',
465
+ };
466
+ str = [data.appid, data.timestamp, data.noncestr, data.prepayid, ''].join('\n');
467
+ data.sign = this.sign(str);
468
+ result.data = data;
469
+ }
470
+ return [2 /*return*/, result];
471
+ }
472
+ });
473
+ });
474
+ };
475
+ /**
476
+ * 合单app支付
477
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_1.shtml
478
+ */
479
+ Pay.prototype.combine_transactions_app = function (params) {
480
+ return __awaiter(this, void 0, void 0, function () {
481
+ var _params, url, authorization, headers, result, data, str;
482
+ return __generator(this, function (_a) {
483
+ switch (_a.label) {
484
+ case 0:
485
+ _params = __assign({ combine_appid: this.appid, combine_mchid: this.mchid }, params);
486
+ url = 'https://api.mch.weixin.qq.com/v3/combine-transactions/app';
487
+ authorization = this.buildAuthorization('POST', url, _params);
488
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
489
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
490
+ case 1:
491
+ result = _a.sent();
492
+ if (result.status === 200 && result.data.prepay_id) {
493
+ data = {
494
+ appid: _params.combine_appid,
495
+ partnerid: _params.combine_mchid,
496
+ package: 'Sign=WXPay',
497
+ timestamp: parseInt(+new Date() / 1000 + '').toString(),
498
+ noncestr: Math.random()
499
+ .toString(36)
500
+ .substr(2, 15),
501
+ prepayid: result.data.prepay_id,
502
+ sign: '',
503
+ };
504
+ str = [data.appid, data.timestamp, data.noncestr, data.prepayid, ''].join('\n');
505
+ data.sign = this.sign(str);
506
+ result.data = data;
507
+ }
508
+ return [2 /*return*/, result];
509
+ }
510
+ });
511
+ });
512
+ };
513
+ /**
514
+ * JSAPI支付 或者 小程序支付
515
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
516
+ */
517
+ Pay.prototype.transactions_jsapi = function (params) {
518
+ return __awaiter(this, void 0, void 0, function () {
519
+ var _params, url, authorization, headers, result, data, str;
520
+ return __generator(this, function (_a) {
521
+ switch (_a.label) {
522
+ case 0:
523
+ _params = __assign({ appid: this.appid, mchid: this.mchid }, params);
524
+ url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi';
525
+ authorization = this.buildAuthorization('POST', url, _params);
526
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
527
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
528
+ case 1:
529
+ result = _a.sent();
530
+ if (result.status === 200 && result.data.prepay_id) {
531
+ data = {
532
+ appId: _params.appid,
533
+ timeStamp: parseInt(+new Date() / 1000 + '').toString(),
534
+ nonceStr: Math.random()
535
+ .toString(36)
536
+ .substr(2, 15),
537
+ package: "prepay_id=" + result.data.prepay_id,
538
+ signType: 'RSA',
539
+ paySign: '',
540
+ };
541
+ str = [data.appId, data.timeStamp, data.nonceStr, data.package, ''].join('\n');
542
+ data.paySign = this.sign(str);
543
+ result.data = data;
544
+ }
545
+ return [2 /*return*/, result];
546
+ }
547
+ });
548
+ });
549
+ };
550
+ /**
551
+ * 合单JSAPI支付 或者 小程序支付
552
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_3.shtml
553
+ */
554
+ Pay.prototype.combine_transactions_jsapi = function (params) {
555
+ return __awaiter(this, void 0, void 0, function () {
556
+ var _params, url, authorization, headers, result, data, str;
557
+ return __generator(this, function (_a) {
558
+ switch (_a.label) {
559
+ case 0:
560
+ _params = __assign({ combine_appid: this.appid, combine_mchid: this.mchid }, params);
561
+ url = 'https://api.mch.weixin.qq.com/v3/combine-transactions/jsapi';
562
+ authorization = this.buildAuthorization('POST', url, _params);
563
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
564
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
565
+ case 1:
566
+ result = _a.sent();
567
+ if (result.status === 200 && result.data.prepay_id) {
568
+ data = {
569
+ appId: _params.combine_appid,
570
+ timeStamp: parseInt(+new Date() / 1000 + '').toString(),
571
+ nonceStr: Math.random()
572
+ .toString(36)
573
+ .substr(2, 15),
574
+ package: "prepay_id=" + result.data.prepay_id,
575
+ signType: 'RSA',
576
+ paySign: '',
577
+ };
578
+ str = [data.appId, data.timeStamp, data.nonceStr, data.package, ''].join('\n');
579
+ data.paySign = this.sign(str);
580
+ result.data = data;
581
+ }
582
+ return [2 /*return*/, result];
583
+ }
584
+ });
585
+ });
586
+ };
587
+ /**
588
+ * 查询订单
589
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_2.shtml
590
+ */
591
+ Pay.prototype.query = function (params) {
592
+ return __awaiter(this, void 0, void 0, function () {
593
+ var url, authorization, headers;
594
+ return __generator(this, function (_a) {
595
+ switch (_a.label) {
596
+ case 0:
597
+ url = '';
598
+ if (params.transaction_id) {
599
+ url = "https://api.mch.weixin.qq.com/v3/pay/transactions/id/" + params.transaction_id + "?mchid=" + this.mchid;
600
+ }
601
+ else if (params.out_trade_no) {
602
+ url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + params.out_trade_no + "?mchid=" + this.mchid;
603
+ }
604
+ else {
605
+ throw new Error('缺少transaction_id或者out_trade_no');
606
+ }
607
+ authorization = this.buildAuthorization('GET', url);
608
+ headers = this.getHeaders(authorization);
609
+ return [4 /*yield*/, this.httpService.get(url, headers)];
610
+ case 1: return [2 /*return*/, _a.sent()];
611
+ }
612
+ });
613
+ });
614
+ };
615
+ /**
616
+ * 合单查询订单
617
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_11.shtml
618
+ */
619
+ Pay.prototype.combine_query = function (combine_out_trade_no) {
620
+ return __awaiter(this, void 0, void 0, function () {
621
+ var url, authorization, headers;
622
+ return __generator(this, function (_a) {
623
+ switch (_a.label) {
624
+ case 0:
625
+ if (!combine_out_trade_no)
626
+ throw new Error('缺少combine_out_trade_no');
627
+ url = "https://api.mch.weixin.qq.com/v3/combine-transactions/out-trade-no/" + combine_out_trade_no;
628
+ authorization = this.buildAuthorization('GET', url);
629
+ headers = this.getHeaders(authorization);
630
+ return [4 /*yield*/, this.httpService.get(url, headers)];
631
+ case 1: return [2 /*return*/, _a.sent()];
632
+ }
633
+ });
634
+ });
635
+ };
636
+ /**
637
+ * 关闭订单
638
+ * @param out_trade_no 请求参数 商户订单号 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_3.shtml
639
+ */
640
+ Pay.prototype.close = function (out_trade_no) {
641
+ return __awaiter(this, void 0, void 0, function () {
642
+ var _params, url, authorization, headers;
643
+ return __generator(this, function (_a) {
644
+ switch (_a.label) {
645
+ case 0:
646
+ if (!out_trade_no)
647
+ throw new Error('缺少out_trade_no');
648
+ _params = {
649
+ mchid: this.mchid,
650
+ };
651
+ url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + out_trade_no + "/close";
652
+ authorization = this.buildAuthorization('POST', url, _params);
653
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
654
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
655
+ case 1: return [2 /*return*/, _a.sent()];
656
+ }
657
+ });
658
+ });
659
+ };
660
+ /**
661
+ * 合单关闭订单
662
+ * @param combine_out_trade_no 请求参数 总订单号 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_12.shtml
663
+ * @param sub_orders array 子单信息
664
+ */
665
+ Pay.prototype.combine_close = function (combine_out_trade_no, sub_orders) {
666
+ return __awaiter(this, void 0, void 0, function () {
667
+ var _params, url, authorization, headers;
668
+ return __generator(this, function (_a) {
669
+ switch (_a.label) {
670
+ case 0:
671
+ if (!combine_out_trade_no)
672
+ throw new Error('缺少out_trade_no');
673
+ _params = {
674
+ combine_appid: this.appid,
675
+ sub_orders: sub_orders,
676
+ };
677
+ url = "https://api.mch.weixin.qq.com/v3/combine-transactions/out-trade-no/" + combine_out_trade_no + "/close";
678
+ authorization = this.buildAuthorization('POST', url, _params);
679
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
680
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
681
+ case 1: return [2 /*return*/, _a.sent()];
682
+ }
683
+ });
684
+ });
685
+ };
686
+ /**
687
+ * 申请交易账单
688
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_6.shtml
689
+ */
690
+ Pay.prototype.tradebill = function (params) {
691
+ return __awaiter(this, void 0, void 0, function () {
692
+ var url, _params, querystring, authorization, headers;
693
+ return __generator(this, function (_a) {
694
+ switch (_a.label) {
695
+ case 0:
696
+ url = 'https://api.mch.weixin.qq.com/v3/bill/tradebill';
697
+ _params = __assign({}, params);
698
+ querystring = Object.keys(_params)
699
+ .filter(function (key) {
700
+ return !!_params[key];
701
+ })
702
+ .sort()
703
+ .map(function (key) {
704
+ return key + '=' + _params[key];
705
+ })
706
+ .join('&');
707
+ url = url + ("?" + querystring);
708
+ authorization = this.buildAuthorization('GET', url);
709
+ headers = this.getHeaders(authorization);
710
+ return [4 /*yield*/, this.httpService.get(url, headers)];
711
+ case 1: return [2 /*return*/, _a.sent()];
712
+ }
713
+ });
714
+ });
715
+ };
716
+ /**
717
+ * 申请资金账单
718
+ * @param params 请求参数 object 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_7.shtml
719
+ */
720
+ Pay.prototype.fundflowbill = function (params) {
721
+ return __awaiter(this, void 0, void 0, function () {
722
+ var url, _params, querystring, authorization, headers;
723
+ return __generator(this, function (_a) {
724
+ switch (_a.label) {
725
+ case 0:
726
+ url = 'https://api.mch.weixin.qq.com/v3/bill/fundflowbill';
727
+ _params = __assign({}, params);
728
+ querystring = Object.keys(_params)
729
+ .filter(function (key) {
730
+ return !!_params[key];
731
+ })
732
+ .sort()
733
+ .map(function (key) {
734
+ return key + '=' + _params[key];
735
+ })
736
+ .join('&');
737
+ url = url + ("?" + querystring);
738
+ authorization = this.buildAuthorization('GET', url);
739
+ headers = this.getHeaders(authorization);
740
+ return [4 /*yield*/, this.httpService.get(url, headers)];
741
+ case 1: return [2 /*return*/, _a.sent()];
742
+ }
743
+ });
744
+ });
745
+ };
746
+ /**
747
+ * 下载账单
748
+ * @param download_url 请求参数 路径 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_8.shtml
749
+ */
750
+ Pay.prototype.downloadBill = function (download_url) {
751
+ return __awaiter(this, void 0, void 0, function () {
752
+ var authorization, headers;
753
+ return __generator(this, function (_a) {
754
+ switch (_a.label) {
755
+ case 0:
756
+ authorization = this.buildAuthorization('GET', download_url);
757
+ headers = this.getHeaders(authorization);
758
+ return [4 /*yield*/, this.httpService.get(download_url, headers)];
759
+ case 1: return [2 /*return*/, _a.sent()];
760
+ }
761
+ });
762
+ });
763
+ };
764
+ /**
765
+ * 申请退款
766
+ * @param params 请求参数 路径 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_9.shtml
767
+ */
768
+ Pay.prototype.refunds = function (params) {
769
+ return __awaiter(this, void 0, void 0, function () {
770
+ var url, _params, authorization, headers;
771
+ return __generator(this, function (_a) {
772
+ switch (_a.label) {
773
+ case 0:
774
+ url = 'https://api.mch.weixin.qq.com/v3/refund/domestic/refunds';
775
+ _params = __assign({}, params);
776
+ authorization = this.buildAuthorization('POST', url, _params);
777
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
778
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
779
+ case 1: return [2 /*return*/, _a.sent()];
780
+ }
781
+ });
782
+ });
783
+ };
784
+ /**
785
+ * 查询单笔退款
786
+ * @documentation 请求参数 路径 参数介绍 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_10.shtml
787
+ */
788
+ Pay.prototype.find_refunds = function (out_refund_no) {
789
+ return __awaiter(this, void 0, void 0, function () {
790
+ var url, authorization, headers;
791
+ return __generator(this, function (_a) {
792
+ switch (_a.label) {
793
+ case 0:
794
+ if (!out_refund_no)
795
+ throw new Error('缺少out_refund_no');
796
+ url = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/" + out_refund_no;
797
+ authorization = this.buildAuthorization('GET', url);
798
+ headers = this.getHeaders(authorization);
799
+ return [4 /*yield*/, this.httpService.get(url, headers)];
800
+ case 1: return [2 /*return*/, _a.sent()];
801
+ }
802
+ });
803
+ });
804
+ };
805
+ //#endregion 支付相关接口
806
+ //#region 商家转账到零钱
807
+ /**
808
+ * 发起商家转账零钱
809
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_1.shtml
810
+ */
811
+ Pay.prototype.batches_transfer = function (params) {
812
+ return __awaiter(this, void 0, void 0, function () {
813
+ var url, _params, serial_no, authorization, headers;
814
+ return __generator(this, function (_a) {
815
+ switch (_a.label) {
816
+ case 0:
817
+ url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
818
+ _params = __assign({ appid: this.appid }, params);
819
+ serial_no = _params === null || _params === void 0 ? void 0 : _params.wx_serial_no;
820
+ delete _params.wx_serial_no;
821
+ authorization = this.buildAuthorization('POST', url, _params);
822
+ headers = this.getHeaders(authorization, { 'Wechatpay-Serial': serial_no || this.serial_no, 'Content-Type': 'application/json' });
823
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
824
+ case 1: return [2 /*return*/, _a.sent()];
825
+ }
826
+ });
827
+ });
828
+ };
829
+ /**
830
+ * 微信批次单号查询批次单API
831
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_2.shtml
832
+ */
833
+ Pay.prototype.query_batches_transfer_list_wx = function (params) {
834
+ return __awaiter(this, void 0, void 0, function () {
835
+ var baseUrl, url, authorization, headers;
836
+ return __generator(this, function (_a) {
837
+ switch (_a.label) {
838
+ case 0:
839
+ baseUrl = "https://api.mch.weixin.qq.com/v3/transfer/batches/batch-id/" + params.batch_id;
840
+ url = baseUrl + this.objectToQueryString(params, ['batch_id']);
841
+ authorization = this.buildAuthorization('GET', url);
842
+ headers = this.getHeaders(authorization);
843
+ return [4 /*yield*/, this.httpService.get(url, headers)];
844
+ case 1: return [2 /*return*/, _a.sent()];
845
+ }
846
+ });
847
+ });
848
+ };
849
+ /**
850
+ * 微信明细单号查询明细单API
851
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_3.shtml
852
+ */
853
+ Pay.prototype.query_batches_transfer_detail_wx = function (params) {
854
+ return __awaiter(this, void 0, void 0, function () {
855
+ var baseUrl, url, authorization, headers;
856
+ return __generator(this, function (_a) {
857
+ switch (_a.label) {
858
+ case 0:
859
+ baseUrl = "https://api.mch.weixin.qq.com/v3/transfer/batches/batch-id/" + params.batch_id + "/details/detail-id/" + params.detail_id;
860
+ url = baseUrl + this.objectToQueryString(params, ['batch_id', 'detail_id']);
861
+ authorization = this.buildAuthorization('GET', url);
862
+ headers = this.getHeaders(authorization);
863
+ return [4 /*yield*/, this.httpService.get(url, headers)];
864
+ case 1: return [2 /*return*/, _a.sent()];
865
+ }
866
+ });
867
+ });
868
+ };
869
+ /**
870
+ * 商家批次单号查询批次单API
871
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_5.shtml
872
+ */
873
+ Pay.prototype.query_batches_transfer_list = function (params) {
874
+ return __awaiter(this, void 0, void 0, function () {
875
+ var baseUrl, url, authorization, headers;
876
+ return __generator(this, function (_a) {
877
+ switch (_a.label) {
878
+ case 0:
879
+ baseUrl = "https://api.mch.weixin.qq.com/v3/transfer/batches/out-batch-no/" + params.out_batch_no;
880
+ url = baseUrl + this.objectToQueryString(params, ['out_batch_no']);
881
+ authorization = this.buildAuthorization('GET', url);
882
+ headers = this.getHeaders(authorization);
883
+ return [4 /*yield*/, this.httpService.get(url, headers)];
884
+ case 1: return [2 /*return*/, _a.sent()];
885
+ }
886
+ });
887
+ });
888
+ };
889
+ /**
890
+ * 商家明细单号查询明细单API
891
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter4_3_6.shtml
892
+ */
893
+ Pay.prototype.query_batches_transfer_detail = function (params) {
894
+ return __awaiter(this, void 0, void 0, function () {
895
+ var baseUrl, url, authorization, headers;
896
+ return __generator(this, function (_a) {
897
+ switch (_a.label) {
898
+ case 0:
899
+ baseUrl = "https://api.mch.weixin.qq.com/v3/transfer/batches/out-batch-no/" + params.out_batch_no + "/details/out-detail-no/" + params.out_detail_no;
900
+ url = baseUrl + this.objectToQueryString(params, ['out_batch_no', 'out_detail_no']);
901
+ authorization = this.buildAuthorization('GET', url);
902
+ headers = this.getHeaders(authorization);
903
+ return [4 /*yield*/, this.httpService.get(url, headers)];
904
+ case 1: return [2 /*return*/, _a.sent()];
905
+ }
906
+ });
907
+ });
908
+ };
909
+ //#endregion 商家转账到零钱
910
+ //#region 分账
911
+ /**
912
+ * 请求分账API
913
+ * @param params
914
+ * @returns
915
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_1.shtml
916
+ */
917
+ Pay.prototype.create_profitsharing_orders = function (params) {
918
+ return __awaiter(this, void 0, void 0, function () {
919
+ var url, _params, serial_no, authorization, headers;
920
+ return __generator(this, function (_a) {
921
+ switch (_a.label) {
922
+ case 0:
923
+ url = 'https://api.mch.weixin.qq.com/v3/profitsharing/orders';
924
+ _params = __assign({ appid: this.appid }, params);
925
+ serial_no = _params === null || _params === void 0 ? void 0 : _params.wx_serial_no;
926
+ delete _params.wx_serial_no;
927
+ authorization = this.buildAuthorization('POST', url, _params);
928
+ headers = this.getHeaders(authorization, { 'Wechatpay-Serial': serial_no || this.serial_no, 'Content-Type': 'application/json' });
929
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
930
+ case 1: return [2 /*return*/, _a.sent()];
931
+ }
932
+ });
933
+ });
934
+ };
935
+ /**
936
+ * 查询分账结果API
937
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_2.shtml
938
+ */
939
+ Pay.prototype.query_profitsharing_orders = function (transaction_id, out_order_no) {
940
+ return __awaiter(this, void 0, void 0, function () {
941
+ var url, authorization, headers;
942
+ return __generator(this, function (_a) {
943
+ switch (_a.label) {
944
+ case 0:
945
+ if (!transaction_id)
946
+ throw new Error('缺少transaction_id');
947
+ if (!out_order_no)
948
+ throw new Error('缺少out_order_no');
949
+ url = "https://api.mch.weixin.qq.com/v3/profitsharing/orders/" + out_order_no;
950
+ url = url + this.objectToQueryString({ transaction_id: transaction_id });
951
+ authorization = this.buildAuthorization('GET', url);
952
+ headers = this.getHeaders(authorization);
953
+ return [4 /*yield*/, this.httpService.get(url, headers)];
954
+ case 1: return [2 /*return*/, _a.sent()];
955
+ }
956
+ });
957
+ });
958
+ };
959
+ /**
960
+ * 请求分账回退API
961
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_3.shtml
962
+ */
963
+ Pay.prototype.profitsharing_return_orders = function (params) {
964
+ return __awaiter(this, void 0, void 0, function () {
965
+ var url, _params, authorization, headers;
966
+ return __generator(this, function (_a) {
967
+ switch (_a.label) {
968
+ case 0:
969
+ url = 'https://api.mch.weixin.qq.com/v3/profitsharing/return-orders';
970
+ _params = __assign({}, params);
971
+ authorization = this.buildAuthorization('POST', url, _params);
972
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
973
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
974
+ case 1: return [2 /*return*/, _a.sent()];
975
+ }
976
+ });
977
+ });
978
+ };
979
+ /**
980
+ * 查询分账回退结果API
981
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_4.shtml
982
+ */
983
+ Pay.prototype.query_profitsharing_return_orders = function (out_return_no, out_order_no) {
984
+ return __awaiter(this, void 0, void 0, function () {
985
+ var url, authorization, headers;
986
+ return __generator(this, function (_a) {
987
+ switch (_a.label) {
988
+ case 0:
989
+ if (!out_return_no)
990
+ throw new Error('缺少out_return_no');
991
+ if (!out_order_no)
992
+ throw new Error('缺少out_order_no');
993
+ url = "https://api.mch.weixin.qq.com/v3/profitsharing/return-orders/" + out_return_no;
994
+ url = url + this.objectToQueryString({ out_order_no: out_order_no });
995
+ authorization = this.buildAuthorization('GET', url);
996
+ headers = this.getHeaders(authorization);
997
+ return [4 /*yield*/, this.httpService.get(url, headers)];
998
+ case 1: return [2 /*return*/, _a.sent()];
999
+ }
1000
+ });
1001
+ });
1002
+ };
1003
+ /**
1004
+ * 解冻剩余资金API
1005
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_5.shtml
1006
+ */
1007
+ Pay.prototype.profitsharing_orders_unfreeze = function (params) {
1008
+ return __awaiter(this, void 0, void 0, function () {
1009
+ var url, _params, authorization, headers;
1010
+ return __generator(this, function (_a) {
1011
+ switch (_a.label) {
1012
+ case 0:
1013
+ url = 'https://api.mch.weixin.qq.com/v3/profitsharing/orders/unfreeze';
1014
+ _params = __assign({}, params);
1015
+ authorization = this.buildAuthorization('POST', url, _params);
1016
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
1017
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
1018
+ case 1: return [2 /*return*/, _a.sent()];
1019
+ }
1020
+ });
1021
+ });
1022
+ };
1023
+ /**
1024
+ * 查询剩余待分金额API
1025
+ * @documentation 请看文档https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_6.shtml
1026
+ */
1027
+ Pay.prototype.query_profitsharing_amounts = function (transaction_id) {
1028
+ return __awaiter(this, void 0, void 0, function () {
1029
+ var url, authorization, headers;
1030
+ return __generator(this, function (_a) {
1031
+ switch (_a.label) {
1032
+ case 0:
1033
+ if (!transaction_id)
1034
+ throw new Error('缺少transaction_id');
1035
+ url = "https://api.mch.weixin.qq.com/v3/profitsharing/transactions/" + transaction_id + "/amounts";
1036
+ authorization = this.buildAuthorization('GET', url);
1037
+ headers = this.getHeaders(authorization);
1038
+ return [4 /*yield*/, this.httpService.get(url, headers)];
1039
+ case 1: return [2 /*return*/, _a.sent()];
1040
+ }
1041
+ });
1042
+ });
1043
+ };
1044
+ /**
1045
+ * 添加分账接收方API
1046
+ * @documentation https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_8.shtml
1047
+ */
1048
+ Pay.prototype.profitsharing_receivers_add = function (params) {
1049
+ return __awaiter(this, void 0, void 0, function () {
1050
+ var url, _params, serial_no, authorization, headers;
1051
+ return __generator(this, function (_a) {
1052
+ switch (_a.label) {
1053
+ case 0:
1054
+ url = 'https://api.mch.weixin.qq.com/v3/profitsharing/receivers/add';
1055
+ _params = __assign({ appid: this.appid }, params);
1056
+ serial_no = _params === null || _params === void 0 ? void 0 : _params.wx_serial_no;
1057
+ delete _params.wx_serial_no;
1058
+ authorization = this.buildAuthorization('POST', url, _params);
1059
+ headers = this.getHeaders(authorization, { 'Wechatpay-Serial': serial_no || this.serial_no, 'Content-Type': 'application/json' });
1060
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
1061
+ case 1: return [2 /*return*/, _a.sent()];
1062
+ }
1063
+ });
1064
+ });
1065
+ };
1066
+ /**
1067
+ * 删除分账接收方API
1068
+ * @documentation https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_9.shtml
1069
+ */
1070
+ Pay.prototype.profitsharing_receivers_delete = function (params) {
1071
+ return __awaiter(this, void 0, void 0, function () {
1072
+ var url, _params, authorization, headers;
1073
+ return __generator(this, function (_a) {
1074
+ switch (_a.label) {
1075
+ case 0:
1076
+ url = 'https://api.mch.weixin.qq.com/v3/profitsharing/receivers/delete';
1077
+ _params = __assign({ appid: this.appid }, params);
1078
+ authorization = this.buildAuthorization('POST', url, _params);
1079
+ headers = this.getHeaders(authorization, { 'Content-Type': 'application/json' });
1080
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
1081
+ case 1: return [2 /*return*/, _a.sent()];
1082
+ }
1083
+ });
1084
+ });
1085
+ };
1086
+ /**
1087
+ * 申请分账账单API
1088
+ * @documentation https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_11.shtml
1089
+ */
1090
+ Pay.prototype.profitsharing_bills = function (bill_date, tar_type) {
1091
+ return __awaiter(this, void 0, void 0, function () {
1092
+ var url, authorization, headers;
1093
+ return __generator(this, function (_a) {
1094
+ switch (_a.label) {
1095
+ case 0:
1096
+ if (!bill_date)
1097
+ throw new Error('缺少bill_date');
1098
+ url = "https://api.mch.weixin.qq.com/v3/profitsharing/bills";
1099
+ url = url + this.objectToQueryString(__assign({ bill_date: bill_date }, (tar_type && { tar_type: tar_type })));
1100
+ authorization = this.buildAuthorization('GET', url);
1101
+ headers = this.getHeaders(authorization);
1102
+ return [4 /*yield*/, this.httpService.get(url, headers)];
1103
+ case 1: return [2 /*return*/, _a.sent()];
1104
+ }
1105
+ });
1106
+ });
1107
+ };
1108
+ //#endregion 分账
1109
+ Pay.prototype.upload_images = function (pic_buffer, filename) {
1110
+ return __awaiter(this, void 0, void 0, function () {
1111
+ var fileinfo, sign, url, authorization, headers;
1112
+ return __generator(this, function (_a) {
1113
+ switch (_a.label) {
1114
+ case 0:
1115
+ fileinfo = {
1116
+ filename: filename,
1117
+ sha256: '',
1118
+ };
1119
+ sign = crypto_1.default.createHash('sha256');
1120
+ sign.update(pic_buffer);
1121
+ fileinfo.sha256 = sign.digest('hex');
1122
+ url = '/v3/merchant/media/upload';
1123
+ authorization = this.buildAuthorization('POST', url, fileinfo);
1124
+ headers = this.getHeaders(authorization, { 'Content-Type': 'multipart/form-data;boundary=boundary' });
1125
+ return [4 /*yield*/, this.httpService.post(url, {
1126
+ fileinfo: fileinfo,
1127
+ pic_buffer: pic_buffer,
1128
+ }, headers)];
1129
+ case 1: return [2 /*return*/, _a.sent()];
1130
+ }
1131
+ });
1132
+ });
1133
+ };
1134
+ /**
1135
+ * 商家转账用户确认模式下,用户申请收款时,商户可通过此接口申请创建转账单
1136
+ */
1137
+ Pay.prototype.transfer_bills = function (params) {
1138
+ return __awaiter(this, void 0, void 0, function () {
1139
+ var url, _params, serial_no, authorization, headers;
1140
+ return __generator(this, function (_a) {
1141
+ switch (_a.label) {
1142
+ case 0:
1143
+ url = 'https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills';
1144
+ _params = __assign({ appid: this.appid }, params);
1145
+ serial_no = _params === null || _params === void 0 ? void 0 : _params.wx_serial_no;
1146
+ delete _params.wx_serial_no;
1147
+ authorization = this.buildAuthorization('POST', url, _params);
1148
+ headers = this.getHeaders(authorization, { 'Wechatpay-Serial': serial_no || this.serial_no, 'Content-Type': 'application/json' });
1149
+ return [4 /*yield*/, this.httpService.post(url, _params, headers)];
1150
+ case 1: return [2 /*return*/, _a.sent()];
1151
+ }
1152
+ });
1153
+ });
1154
+ };
1155
+ /**
1156
+ * 商户通过转账接口发起付款后,在用户确认收款之前可以通过该接口撤销付款
1157
+ */
1158
+ Pay.prototype.transfer_cancel = function (params) {
1159
+ return __awaiter(this, void 0, void 0, function () {
1160
+ var url, authorization, headers;
1161
+ return __generator(this, function (_a) {
1162
+ switch (_a.label) {
1163
+ case 0:
1164
+ url = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/" + params.out_bill_no + "/cancel";
1165
+ authorization = this.buildAuthorization('POST', url, undefined);
1166
+ headers = this.getHeaders(authorization, {
1167
+ mchid: this.mchid,
1168
+ 'Content-Type': 'application/json',
1169
+ });
1170
+ return [4 /*yield*/, this.httpService.post(url, undefined, headers)];
1171
+ case 1: return [2 /*return*/, _a.sent()];
1172
+ }
1173
+ });
1174
+ });
1175
+ };
1176
+ /**
1177
+ * 商家转账用户确认模式下,根据商户单号查询转账单的详细信息
1178
+ */
1179
+ Pay.prototype.transfer_out_bill_no = function (params) {
1180
+ return __awaiter(this, void 0, void 0, function () {
1181
+ var url, authorization, headers;
1182
+ return __generator(this, function (_a) {
1183
+ switch (_a.label) {
1184
+ case 0:
1185
+ url = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/" + params.out_bill_no;
1186
+ authorization = this.buildAuthorization('GET', url);
1187
+ headers = this.getHeaders(authorization, { mchid: this.mchid });
1188
+ return [4 /*yield*/, this.httpService.get(url, headers)];
1189
+ case 1: return [2 /*return*/, _a.sent()];
1190
+ }
1191
+ });
1192
+ });
1193
+ };
1194
+ /**
1195
+ * 商家转账用户确认模式下,根据微信转账单号查询转账单的详细信息
1196
+ */
1197
+ Pay.prototype.transfer_bill_no = function (params) {
1198
+ return __awaiter(this, void 0, void 0, function () {
1199
+ var url, authorization, headers;
1200
+ return __generator(this, function (_a) {
1201
+ switch (_a.label) {
1202
+ case 0:
1203
+ url = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills/transfer-bill-no/" + params.transfer_bill_no;
1204
+ authorization = this.buildAuthorization('GET', url);
1205
+ headers = this.getHeaders(authorization, { mchid: this.mchid });
1206
+ return [4 /*yield*/, this.httpService.get(url, headers)];
1207
+ case 1: return [2 /*return*/, _a.sent()];
1208
+ }
1209
+ });
1210
+ });
1211
+ };
1212
+ Pay.certificates = {}; // 微信平台证书 key 是 serialNo, value 是 publicKey
1213
+ return Pay;
1214
+ }(base_1.Base));
1215
+ module.exports = Pay;