@zsjinwei/wechatpay-node-v3 4.0.3

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