node-easywechat 3.0.0-beta.3 → 3.0.0

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +36 -1
  3. package/dist/Core/Contracts/RefreshableAccessTokenInterface.d.ts +2 -2
  4. package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +2 -2
  5. package/dist/Core/Http/Minxins/MessageMixin.js +1 -2
  6. package/dist/Core/Http/ServerRequest.d.ts +1 -1
  7. package/dist/Core/Http/ServerRequest.js +16 -6
  8. package/dist/Core/HttpClient/HttpClient.d.ts +7 -0
  9. package/dist/Core/HttpClient/HttpClient.js +43 -7
  10. package/dist/Core/HttpClient/HttpClientResponse.d.ts +16 -5
  11. package/dist/Core/HttpClient/HttpClientResponse.js +64 -22
  12. package/dist/Core/HttpClient/Mixins/PresetMixin.d.ts +20 -1
  13. package/dist/Core/HttpClient/Mixins/PresetMixin.js +41 -2
  14. package/dist/Core/Message.d.ts +0 -127
  15. package/dist/Core/Message.js +5 -1
  16. package/dist/Core/Mixins/DecryptXmlMessageMixin.js +1 -1
  17. package/dist/Core/Mixins/ResponseXmlMessageMixin.js +2 -2
  18. package/dist/Core/Support/PrivateKey.d.ts +20 -0
  19. package/dist/Core/Support/PrivateKey.js +38 -0
  20. package/dist/Core/Support/PublicKey.d.ts +14 -0
  21. package/dist/Core/Support/PublicKey.js +36 -0
  22. package/dist/Core/Support/RSA.d.ts +2 -2
  23. package/dist/Core/Support/RSA.js +4 -4
  24. package/dist/Core/Support/Utils.d.ts +6 -0
  25. package/dist/Core/Support/Utils.js +15 -1
  26. package/dist/MiniApp/Application.d.ts +4 -4
  27. package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +2 -2
  28. package/dist/OfficialAccount/AccessToken.d.ts +2 -2
  29. package/dist/OfficialAccount/AccessToken.js +2 -2
  30. package/dist/OfficialAccount/Application.d.ts +4 -4
  31. package/dist/OfficialAccount/Application.js +0 -5
  32. package/dist/OfficialAccount/Contracts/ApplicationInterface.d.ts +2 -2
  33. package/dist/OfficialAccount/JsApiTicket.js +2 -2
  34. package/dist/OfficialAccount/Message.d.ts +139 -0
  35. package/dist/OfficialAccount/Message.js +1 -0
  36. package/dist/OfficialAccount/Server.js +1 -1
  37. package/dist/Pay/Application.d.ts +52 -0
  38. package/dist/Pay/Application.js +96 -0
  39. package/dist/Pay/Client.d.ts +42 -0
  40. package/dist/Pay/Client.js +125 -0
  41. package/dist/Pay/Config.d.ts +5 -0
  42. package/dist/Pay/Config.js +17 -0
  43. package/dist/Pay/Contracts/ApplicationInterface.d.ts +50 -0
  44. package/dist/Pay/Contracts/ApplicationInterface.js +45 -0
  45. package/dist/Pay/Contracts/MerchantInterface.d.ts +35 -0
  46. package/dist/Pay/Contracts/MerchantInterface.js +35 -0
  47. package/dist/Pay/Contracts/ResponseValidatorInterface.d.ts +9 -0
  48. package/dist/Pay/Contracts/ResponseValidatorInterface.js +10 -0
  49. package/dist/Pay/LegacySignature.d.ts +12 -0
  50. package/dist/Pay/LegacySignature.js +59 -0
  51. package/dist/Pay/Merchant.d.ts +25 -0
  52. package/dist/Pay/Merchant.js +53 -0
  53. package/dist/Pay/Message.d.ts +175 -0
  54. package/dist/Pay/Message.js +24 -0
  55. package/dist/Pay/Server.d.ts +41 -0
  56. package/dist/Pay/Server.js +122 -0
  57. package/dist/Pay/Signature.d.ts +15 -0
  58. package/dist/Pay/Signature.js +44 -0
  59. package/dist/Pay/Utils.d.ts +54 -0
  60. package/dist/Pay/Utils.js +142 -0
  61. package/dist/Types/global.d.ts +114 -1
  62. package/dist/index.d.ts +3 -3
  63. package/dist/index.js +3 -3
  64. package/package.json +2 -2
@@ -0,0 +1,142 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const RSA_1 = __importDefault(require("../Core/Support/RSA"));
6
+ const Utils_1 = require("../Core/Support/Utils");
7
+ class Utils {
8
+ constructor(merchant) {
9
+ this.merchant = merchant;
10
+ }
11
+ /**
12
+ * 创建签名(V3),并返回签名字符串
13
+ * @param params 参数集合
14
+ * @returns
15
+ */
16
+ createSignature(message) {
17
+ let rsa = new RSA_1.default;
18
+ rsa.setPublicKey(this.merchant.getCertificate().toString());
19
+ rsa.setPrivateKey(this.merchant.getPrivateKey().toString());
20
+ return rsa.sign(message);
21
+ }
22
+ /**
23
+ * 创建签名(V2),并返回签名字符串
24
+ * @param params 参数集合
25
+ * @returns
26
+ */
27
+ createV2Signature(params) {
28
+ let signString = '';
29
+ let sparator = '';
30
+ let keys = Object.keys(params);
31
+ keys = keys.sort();
32
+ for (let i = 0; i < keys.length; i++) {
33
+ if (keys[i] == 'sign' || keys[i] == 'paySign' || typeof params[keys[i]] === undefined || params[keys[i]] === null)
34
+ continue;
35
+ signString += sparator + keys[i] + '=' + params[keys[i]];
36
+ sparator = '&';
37
+ }
38
+ let key = this.merchant.getV2SecretKey();
39
+ if (!key) {
40
+ throw new Error('Missing V2 API key.');
41
+ }
42
+ signString += '&key=' + key;
43
+ let sign = '';
44
+ let type = params['signType'] ? (params['signType'] + '').toLowerCase() : 'md5';
45
+ switch (type) {
46
+ case 'sha1':
47
+ case 'md5':
48
+ sign = (0, Utils_1.createHash)(signString, type);
49
+ break;
50
+ case 'hmac-sha256':
51
+ case 'hmac_sha256':
52
+ type = type.replace(/^hmac[\-|_]/i, '');
53
+ sign = (0, Utils_1.createHmac)(signString, key, type);
54
+ break;
55
+ }
56
+ if (!sign) {
57
+ throw new Error('Failed to sign the request.');
58
+ }
59
+ return (sign + '').toUpperCase();
60
+ }
61
+ /**
62
+ * 构建JSBridge支付参数
63
+ * @see [v3文档](https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_4.shtml)
64
+ * @see [v2文档](https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6)
65
+ * @param prepayId 下单接口返回的prepay_id
66
+ * @param appId 应用id
67
+ * @param signType v3仅支持RSA,V2支持MD5、HMAC-SHA256
68
+ * @returns
69
+ */
70
+ buildBridgeConfig(prepayId, appId, signType = 'RSA') {
71
+ let params = {
72
+ appId,
73
+ timeStamp: (0, Utils_1.getTimestamp)(),
74
+ nonceStr: (0, Utils_1.randomString)(),
75
+ package: 'prepay_id=' + prepayId,
76
+ signType,
77
+ paySign: '',
78
+ };
79
+ // v2
80
+ if (signType != 'RSA') {
81
+ params.paySign = this.createV2Signature(params);
82
+ }
83
+ // v3
84
+ else {
85
+ let message = `${params.appId}\n${params.timeStamp}\n${params.nonceStr}\n${params.package}\n`;
86
+ params.paySign = this.createSignature(message);
87
+ }
88
+ return params;
89
+ }
90
+ /**
91
+ * 构建JS-SDK支付参数
92
+ * @see https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#58
93
+ * @param prepayId 下单接口返回的prepay_id
94
+ * @param appId 应用id
95
+ * @param signType v3仅支持RSA,V2支持MD5、HMAC-SHA256
96
+ */
97
+ buildSdkConfig(prepayId, appId, signType = 'RSA') {
98
+ let config = this.buildBridgeConfig(prepayId, appId, signType);
99
+ return {
100
+ appId: config.appId,
101
+ timestamp: config.timeStamp,
102
+ nonceStr: config.nonceStr,
103
+ package: config.package,
104
+ signType: config.signType,
105
+ paySign: config.paySign,
106
+ };
107
+ }
108
+ /**
109
+ * 构建小程序支付参数
110
+ * @see https://developers.weixin.qq.com/miniprogram/dev/api/payment/wx.requestPayment.html
111
+ * @param prepayId 下单接口返回的prepay_id
112
+ * @param appId 应用id
113
+ * @param signType v3仅支持RSA,V2支持MD5、HMAC-SHA256
114
+ * @returns
115
+ */
116
+ buildMiniAppConfig(prepayId, appId, signType = 'RSA') {
117
+ return this.buildBridgeConfig(prepayId, appId, signType);
118
+ }
119
+ /**
120
+ * 构建App支付参数
121
+ * @see https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_2_4.shtml
122
+ * @param prepayId 下单接口返回的prepay_id
123
+ * @param appId 应用id
124
+ * @returns
125
+ */
126
+ buildAppConfig(prepayId, appId) {
127
+ let params = {
128
+ appId,
129
+ partnerid: this.merchant.getMerchantId(),
130
+ prepayid: prepayId,
131
+ nonceStr: (0, Utils_1.randomString)(),
132
+ timestamp: (0, Utils_1.getTimestamp)(),
133
+ package: 'Sign=WXPay',
134
+ sign: '',
135
+ };
136
+ let message = `${params.appId}\n${params.timestamp}\n${params.nonceStr}\n${params.prepayid}\n`;
137
+ params.sign = this.createSignature(message);
138
+ return params;
139
+ }
140
+ }
141
+ ;
142
+ module.exports = Utils;
@@ -3,6 +3,7 @@ import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
3
3
  import OfficialAccountApplicationInterface from '../OfficialAccount/Contracts/ApplicationInterface';
4
4
  import Message from '../Core/Message';
5
5
  import HttpClientResponseInterface from '../Core/HttpClient/Contracts/HttpClientResponseInterface';
6
+ import { PublicKey } from "../Core/Support/PublicKey";
6
7
 
7
8
  declare module 'axios' {
8
9
  export interface AxiosRequestConfig {
@@ -14,6 +15,10 @@ declare module 'axios' {
14
15
  * 要发送的json数据,会自动解析并赋值到data属性,同时设置content-type=application/json
15
16
  */
16
17
  json?: string | Record<string, any>;
18
+ /**
19
+ * 要发送的FormData数据,会自动解析并赋值到data属性,同时设置根据内容提取headers
20
+ */
21
+ formData?: Record<string, any>;
17
22
  }
18
23
  }
19
24
 
@@ -149,10 +154,24 @@ export declare interface PayConfig extends BaseConfig {
149
154
  mch_id?: string;
150
155
 
151
156
  /**
152
- * pfx 证书路径(敏感接口(如退款、发送红包等)需要)
157
+ * 商户证书路径
153
158
  */
154
159
  certificate?: string;
155
160
 
161
+ /**
162
+ * 商户证书私钥路径
163
+ */
164
+ private_key?: string;
165
+
166
+ /**
167
+ * 平台证书(v3接口需要)
168
+ *
169
+ * 支持路径列表或者PublicKey对象列表或者,以serial_no为key,证书内容或PublicKey对象为value的对象
170
+ *
171
+ * 下载工具:https://github.com/wechatpay-apiv3/CertificateDownloader
172
+ */
173
+ platform_certs?: string[] | PublicKey[] | Record<string, string | PublicKey>;
174
+
156
175
  /**
157
176
  * v3 API密钥
158
177
  */
@@ -320,3 +339,97 @@ export declare type PaymentScannedHandler = (message: Message, fail: PaymentFail
320
339
  * @param response 响应对象,仅在 type 为 after 时返回
321
340
  */
322
341
  export declare type LogHandler = (type: 'before' | 'after', options: AxiosRequestConfig, usedTime?: number, response?: AxiosResponse) => void | Promise<void>;
342
+
343
+ /**
344
+ * 支付参数 JsBridge
345
+ */
346
+ export declare interface PayBridgeConfig {
347
+ /**
348
+ * 应用id
349
+ */
350
+ appId: string;
351
+ /**
352
+ * 时间戳
353
+ */
354
+ timeStamp: number;
355
+ /**
356
+ * 随机字符串
357
+ */
358
+ nonceStr: string;
359
+ /**
360
+ * 订单详情扩展字符串
361
+ */
362
+ package: string;
363
+ /**
364
+ * 签名方式
365
+ */
366
+ signType: string;
367
+ /**
368
+ * 签名
369
+ */
370
+ paySign: string;
371
+ }
372
+
373
+ /**
374
+ * 支付参数 JsSdk
375
+ */
376
+ export declare interface PaySdkConfig {
377
+ /**
378
+ * 应用id
379
+ */
380
+ appId: string;
381
+ /**
382
+ * 时间戳
383
+ */
384
+ timestamp: number;
385
+ /**
386
+ * 随机字符串
387
+ */
388
+ nonceStr: string;
389
+ /**
390
+ * 订单详情扩展字符串
391
+ */
392
+ package: string;
393
+ /**
394
+ * 签名方式
395
+ */
396
+ signType: string;
397
+ /**
398
+ * 签名
399
+ */
400
+ paySign: string;
401
+ }
402
+
403
+ /**
404
+ * 支付参数 App
405
+ */
406
+ export declare interface PayAppConfig {
407
+ /**
408
+ * 应用id
409
+ */
410
+ appId: string;
411
+ /**
412
+ * 商户id
413
+ */
414
+ partnerid: string;
415
+ /**
416
+ * 预支付交易会话ID
417
+ */
418
+ prepayid: string;
419
+ /**
420
+ * 订单详情扩展字符串
421
+ */
422
+ package: string;
423
+ /**
424
+ * 随机字符串
425
+ */
426
+ nonceStr: string;
427
+ /**
428
+ * 时间戳
429
+ */
430
+ timestamp: number;
431
+ /**
432
+ * 签名
433
+ */
434
+ sign: string;
435
+ }
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { OfficialAccountConfig, MiniAppConfig, LogHandler, ServerEventType, ServerHandlerClosure } from './Types/global';
1
+ import { OfficialAccountConfig, MiniAppConfig, LogHandler, ServerEventType, ServerHandlerClosure, PayConfig } from './Types/global';
2
2
  import OfficialAccount from './OfficialAccount/Application';
3
3
  import MiniApp from './MiniApp/Application';
4
+ import Pay from './Pay/Application';
4
5
  import CacheInterface from './Core/Contracts/CacheInterface';
5
6
  import ServerRequest from './Core/Http/ServerRequest';
6
- import Message from './Core/Message';
7
7
  import FormData from 'form-data';
8
- export { OfficialAccount, OfficialAccountConfig, MiniApp, MiniAppConfig, CacheInterface, ServerRequest, LogHandler, ServerEventType, ServerHandlerClosure, Message,
8
+ export { OfficialAccount, OfficialAccountConfig, MiniApp, MiniAppConfig, Pay, PayConfig, CacheInterface, ServerRequest, LogHandler, ServerEventType, ServerHandlerClosure,
9
9
  /**
10
10
  * 表单对象
11
11
  * @see https://github.com/axios/axios#formdata
package/dist/index.js CHANGED
@@ -3,16 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FormData = exports.Message = exports.ServerRequest = exports.CacheInterface = exports.MiniApp = exports.OfficialAccount = void 0;
6
+ exports.FormData = exports.ServerRequest = exports.CacheInterface = exports.Pay = exports.MiniApp = exports.OfficialAccount = void 0;
7
7
  const Application_1 = __importDefault(require("./OfficialAccount/Application"));
8
8
  exports.OfficialAccount = Application_1.default;
9
9
  const Application_2 = __importDefault(require("./MiniApp/Application"));
10
10
  exports.MiniApp = Application_2.default;
11
+ const Application_3 = __importDefault(require("./Pay/Application"));
12
+ exports.Pay = Application_3.default;
11
13
  const CacheInterface_1 = __importDefault(require("./Core/Contracts/CacheInterface"));
12
14
  exports.CacheInterface = CacheInterface_1.default;
13
15
  const ServerRequest_1 = __importDefault(require("./Core/Http/ServerRequest"));
14
16
  exports.ServerRequest = ServerRequest_1.default;
15
- const Message_1 = __importDefault(require("./Core/Message"));
16
- exports.Message = Message_1.default;
17
17
  const form_data_1 = __importDefault(require("form-data"));
18
18
  exports.FormData = form_data_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-easywechat",
3
- "version": "3.0.0-beta.3",
3
+ "version": "3.0.0",
4
4
  "description": "EasyWechat SDK for Node.js (NOT OFFICIAL)",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -20,7 +20,7 @@
20
20
  "license": "MIT",
21
21
  "devDependencies": {
22
22
  "@types/node": "^17.0.23",
23
- "axios-mock-adapter": "^1.20.0",
23
+ "axios-mock-adapter": "^1.21.1",
24
24
  "mocha": "^9.2.2",
25
25
  "package-release": "^1.0.2",
26
26
  "typescript": "^4.6.3"