node-easywechat 3.5.11 → 3.5.13

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/CHANGELOG.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
3
 
4
+ ## v3.5.13 (2023-12-20)
5
+
6
+ - Feat: 开放平台新增快速注册企业小程序审核事件的支持 (#62)
7
+
8
+ - Fix: 修复微信支付敏感信息加密方法证书错误的问题 (#57)
9
+
10
+ ## v3.5.12 (2023-12-19)
11
+
12
+ - Fix: 修复类型错误
13
+
4
14
  ## v3.5.11 (2023-12-19)
5
15
 
6
16
  - Fix: 微信支付获取平台证书优化为随用随获取的方式,并增加缓存 (#57)
@@ -63,8 +63,7 @@ class MessageMixin {
63
63
  }
64
64
  withBody(body) {
65
65
  if (Buffer.isBuffer(body)) {
66
- this.content = Buffer.from('');
67
- this.content.copy(body);
66
+ this.content = body;
68
67
  }
69
68
  else if (typeof body === 'string') {
70
69
  this.content = Buffer.from(body);
@@ -16,10 +16,12 @@ interface Message {
16
16
  * - unauthorized:取消授权
17
17
  * - updateauthorized:更新授权
18
18
  * - component_verify_ticket:验证票据
19
+ * - notify_third_fasteregister:快速注册企业小程序审核事件
19
20
  * @see [授权变更通知推送](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/authorize_event.html)
20
21
  * @see [验证票据](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/component_verify_ticket.html)
22
+ * @see [快速注册企业小程序审核事件](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/Fast_Registration_Interface_document.html#三、注册审核事件推送)
21
23
  */
22
- InfoType?: 'authorized' | 'unauthorized' | 'updateauthorized' | 'component_verify_ticket';
24
+ InfoType?: 'authorized' | 'unauthorized' | 'updateauthorized' | 'component_verify_ticket' | 'notify_third_fasteregister' | string;
23
25
  /**
24
26
  * 公众号或小程序的 appid
25
27
  */
@@ -44,6 +44,12 @@ declare class Server extends ServerInterface {
44
44
  * @returns
45
45
  */
46
46
  handleVerifyTicketRefreshed(handler: ServerHandlerClosure<Message>): this;
47
+ /**
48
+ * 处理快速注册企业小程序审核通知
49
+ * @param handler
50
+ * @returns
51
+ */
52
+ handleThirdFastRegister(handler: ServerHandlerClosure<Message>): this;
47
53
  protected decryptRequestMessage(): ServerHandlerClosure<Message>;
48
54
  /**
49
55
  * 获取来自微信服务器的推送消息
@@ -102,6 +102,18 @@ class Server extends ServerInterface_1.default {
102
102
  });
103
103
  });
104
104
  }
105
+ /**
106
+ * 处理快速注册企业小程序审核通知
107
+ * @param handler
108
+ * @returns
109
+ */
110
+ handleThirdFastRegister(handler) {
111
+ return this.with(function (message, next) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ return message.InfoType === 'notify_third_fasteregister' ? handler(message, next) : next(message);
114
+ });
115
+ });
116
+ }
105
117
  decryptRequestMessage() {
106
118
  let query = this.request.getQueryParams();
107
119
  return (message, next) => __awaiter(this, void 0, void 0, function* () {
@@ -5,12 +5,14 @@ import ConfigMixin from '../Core/Mixins/ConfigMixin';
5
5
  import HttpClientMixin from '../Core/Mixins/HttpClientMixin';
6
6
  import ServerRequestMixin from '../Core/Mixins/ServerRequestMixin';
7
7
  import { PayConfig } from '../Types/global';
8
+ import Merchant from './Merchant';
8
9
  import MerchantInterface from './Contracts/MerchantInterface';
9
10
  import ApplicationInterface from './Contracts/ApplicationInterface';
10
11
  import Server from './Server';
11
12
  import Utils from './Utils';
12
13
  import Client from './Client';
13
14
  import Validator from './Validator';
15
+ import ValidatorInterface from './Contracts/ValidatorInterface';
14
16
  /**
15
17
  * 微信支付应用
16
18
  */
@@ -21,8 +23,8 @@ declare class Application implements ApplicationInterface {
21
23
  protected server: Server;
22
24
  protected client: Client;
23
25
  protected utils: Utils;
24
- protected validator: Validator;
25
- getMerchant(): MerchantInterface;
26
+ protected validator: ValidatorInterface;
27
+ getMerchant(): Merchant;
26
28
  /**
27
29
  * 设置当前账户实例
28
30
  * @param merchant
@@ -46,7 +48,7 @@ declare class Application implements ApplicationInterface {
46
48
  * 设置验证器实例
47
49
  * @param validator
48
50
  */
49
- setValidator(validator: Validator): void;
51
+ setValidator(validator: ValidatorInterface): void;
50
52
  getValidator(): Validator;
51
53
  getClient(): Client;
52
54
  /**
@@ -35,16 +35,21 @@ declare abstract class MerchantInterface {
35
35
  */
36
36
  setPlatformCertKey(key: string): this;
37
37
  /**
38
- * 更具证书序列号获取平台证书
38
+ * 根据证书序列号获取平台证书
39
39
  * @param serial
40
40
  * @returns
41
41
  */
42
42
  getPlatformCert(serial: string): Promise<PublicKey>;
43
43
  /**
44
- * 获取平台证书
45
- * @param force 是否强制刷新缓存
44
+ * 获取所有平台证书
45
+ * @returns
46
+ */
47
+ getPlatformCerts(): Promise<Record<string, PublicKey>>;
48
+ /**
49
+ * 从缓存或者接口获取平台证书
50
+ * @param force 是否强制刷新缓存,默认:false
46
51
  */
47
- loadPlatformCerts(force: boolean): void;
52
+ loadPlatformCerts(force?: boolean): Promise<void>;
48
53
  /**
49
54
  * 设置平台证书
50
55
  * @param certs 键名:序列号,键值:PublicKey实例或者文件内容字符串
@@ -34,16 +34,21 @@ class MerchantInterface {
34
34
  */
35
35
  setPlatformCertKey(key) { return this; }
36
36
  /**
37
- * 更具证书序列号获取平台证书
37
+ * 根据证书序列号获取平台证书
38
38
  * @param serial
39
39
  * @returns
40
40
  */
41
41
  getPlatformCert(serial) { return null; }
42
42
  /**
43
- * 获取平台证书
44
- * @param force 是否强制刷新缓存
43
+ * 获取所有平台证书
44
+ * @returns
45
+ */
46
+ getPlatformCerts() { return null; }
47
+ /**
48
+ * 从缓存或者接口获取平台证书
49
+ * @param force 是否强制刷新缓存,默认:false
45
50
  */
46
- loadPlatformCerts(force) { }
51
+ loadPlatformCerts(force = false) { return; }
47
52
  /**
48
53
  * 设置平台证书
49
54
  * @param certs 键名:序列号,键值:PublicKey实例或者文件内容字符串
@@ -24,9 +24,10 @@ declare class Merchant implements MerchantInterface {
24
24
  getV2SecretKey(): string;
25
25
  getCertificate(): PublicKey;
26
26
  getPlatformCert(serial: string): Promise<PublicKey>;
27
+ getPlatformCerts(): Promise<Record<string, PublicKey>>;
27
28
  setPlatformCerts(certs: Record<string, PublicKey | string>): void;
28
29
  getPlatformCertKey(): string;
29
30
  setPlatformCertKey(key: string): this;
30
- loadPlatformCerts(force?: boolean): Promise<Record<string, string>>;
31
+ loadPlatformCerts(force?: boolean): Promise<void>;
31
32
  }
32
33
  export = Merchant;
@@ -76,6 +76,14 @@ class Merchant {
76
76
  return (_a = this.platformCerts[serial]) !== null && _a !== void 0 ? _a : null;
77
77
  });
78
78
  }
79
+ getPlatformCerts() {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ if (!this.platformCerts || Object.keys(this.platformCerts).length === 0) {
82
+ yield this.loadPlatformCerts();
83
+ }
84
+ return this.platformCerts;
85
+ });
86
+ }
79
87
  setPlatformCerts(certs) {
80
88
  let newCerts = {};
81
89
  for (let key of Object.keys(certs)) {
@@ -108,13 +116,13 @@ class Merchant {
108
116
  certs = {};
109
117
  let response = yield this.app.getClient().get('/v3/certificates');
110
118
  let data = response.toObject();
111
- if (!data || !data.data || data.data.length === 0)
112
- return certs;
113
- data.data.forEach((item) => {
114
- let content = AES_1.AES_GCM.decrypt(item.encrypt_certificate.ciphertext, this.app.getConfig().get('secret_key'), item.encrypt_certificate.nonce, item.encrypt_certificate.associated_data).toString();
115
- certs[item.serial_no] = content;
116
- });
117
- yield cache.set(cacheKey, certs, 36000); // 缓存10小时
119
+ if (data && data.data && data.data.length > 0) {
120
+ data.data.forEach((item) => {
121
+ let content = AES_1.AES_GCM.decrypt(item.encrypt_certificate.ciphertext, this.app.getConfig().get('secret_key'), item.encrypt_certificate.nonce, item.encrypt_certificate.associated_data).toString();
122
+ certs[item.serial_no] = content;
123
+ });
124
+ yield cache.set(cacheKey, certs, 36000); // 缓存10小时
125
+ }
118
126
  }
119
127
  this.setPlatformCerts(certs);
120
128
  });
@@ -125,6 +125,10 @@ class Server extends ServerInterface_1.default {
125
125
  */
126
126
  handlePaid(handler) {
127
127
  this.with((message, next) => __awaiter(this, void 0, void 0, function* () {
128
+ let isV2Message = message.getOriginalContents().startsWith('<xml');
129
+ if (isV2Message) {
130
+ return handler(message, next);
131
+ }
128
132
  return message.getEventType() === 'TRANSACTION.SUCCESS' && message.trade_state === 'SUCCESS'
129
133
  ? handler(message, next) : next(message);
130
134
  }));
@@ -1,16 +1,28 @@
1
1
  /// <reference types="node" />
2
+ import { PublicKey } from '../Core/Support/PublicKey';
2
3
  import { PayAppConfig, PayBridgeConfig, PaySdkConfig } from '../Types/global';
3
4
  import MerchantInterface from './Contracts/MerchantInterface';
4
5
  declare class Utils {
5
6
  protected merchant: MerchantInterface;
7
+ protected platformCert: PublicKey;
6
8
  constructor(merchant: MerchantInterface);
9
+ /**
10
+ * 设置加密所用的平台证书
11
+ * @param platformCert
12
+ */
13
+ setPlatformCert(platformCert: PublicKey): this;
14
+ /**
15
+ * 获取加密所用的平台证书
16
+ * @returns
17
+ */
18
+ getPlatformCert(): Promise<PublicKey>;
7
19
  /**
8
20
  * 加密字符串
9
21
  * @param plaintext 原文
10
22
  * @param encoding 密文的编码格式,默认:base64
11
23
  * @param hashType 哈希算法,默认:sha256
12
24
  */
13
- encrypt(plaintext: string, encoding?: BufferEncoding, hashType?: string): string;
25
+ encrypt(plaintext: string, encoding?: BufferEncoding, hashType?: string): Promise<string>;
14
26
  /**
15
27
  * 解密字符串
16
28
  * @param ciphertext 密文
package/dist/Pay/Utils.js CHANGED
@@ -1,4 +1,13 @@
1
1
  'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
@@ -8,6 +17,30 @@ class Utils {
8
17
  constructor(merchant) {
9
18
  this.merchant = merchant;
10
19
  }
20
+ /**
21
+ * 设置加密所用的平台证书
22
+ * @param platformCert
23
+ */
24
+ setPlatformCert(platformCert) {
25
+ this.platformCert = platformCert;
26
+ return this;
27
+ }
28
+ /**
29
+ * 获取加密所用的平台证书
30
+ * @returns
31
+ */
32
+ getPlatformCert() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ if (!this.platformCert) {
35
+ let certs = yield this.merchant.getPlatformCerts();
36
+ if (!certs || Object.keys(certs).length === 0) {
37
+ throw new Error('Fail to get platform certs');
38
+ }
39
+ this.platformCert = certs[Object.keys(certs)[0]];
40
+ }
41
+ return this.platformCert;
42
+ });
43
+ }
11
44
  /**
12
45
  * 加密字符串
13
46
  * @param plaintext 原文
@@ -15,9 +48,12 @@ class Utils {
15
48
  * @param hashType 哈希算法,默认:sha256
16
49
  */
17
50
  encrypt(plaintext, encoding = 'base64', hashType = 'sha256') {
18
- let rsa = new RSA_1.default;
19
- rsa.setPublicKey(this.merchant.getCertificate().toString());
20
- return rsa.encrypt(plaintext, encoding, hashType);
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ let rsa = new RSA_1.default;
53
+ let cert = yield this.getPlatformCert();
54
+ rsa.setPublicKey(cert.toString());
55
+ return rsa.encrypt(plaintext, encoding, hashType);
56
+ });
21
57
  }
22
58
  /**
23
59
  * 解密字符串
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-easywechat",
3
- "version": "3.5.11",
3
+ "version": "3.5.13",
4
4
  "description": "EasyWechat SDK for Node.js (NOT OFFICIAL)",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {