Skip to content

Commit

Permalink
bump to v0.7.8
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory committed Jul 11, 2021
1 parent 021c7b3 commit f3a5674
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 变更历史

## v0.7.8 (2021-07-11)
- 改进 `Formatter.nonce` 发生器算法;
- 调整 `Formatter.authorization` 字典排序,关键信息优先展示;
- 修正 内置常量拼写错误,优化文档;

## v0.7.7 (2021-06-28)
- 优化 APIv3 `Decorator.jsonBased` 逻辑,平台证书`certs`配置项,排除掉配置项的商户`serial`序列号;

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ Options:
```
./node_modules/.bin/wxpay v3.pay.transactions.native \
-c.mchid 1230000109 \
-c.serial HEXADECIAL \
-c.serial MCHSERIAL \
-c.privateKey /path/your/merchant/mchid.key \
-c.certs.HEXADECIAL /path/the/platform/certificates/HEXADECIAL.pem \
-c.certs.PLATSERIAL /path/the/platform/certificates/HEXADECIAL.pem \
-d.appid wxd678efh567hg6787 \
-d.mchid 1230000109 \
-d.description 'Image形象店-深圳腾大-QQ公仔' \
Expand All @@ -140,7 +140,7 @@ Options:
```
./node_modules/.bin/wxpay v2.pay.micropay \
-c.mchid 1230000109 \
-c.serial any \
-c.serial nop \
-c.privateKey any \
-c.certs.any \
-c.secret your_merchant_secret_key_string \
Expand All @@ -165,7 +165,7 @@ Options:
```
./node_modules/.bin/wxpay v2/tools/authcodetoopenid \
-c.mchid 1230000109 \
-c.serial any \
-c.serial nop \
-c.privateKey any \
-c.certs.any \
-c.secret your_merchant_secret_key_string \
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ export namespace WechatpayAxiosPlugin {
*/
static castCsvLine(row: string, keys?: any[], skipFirstChar?: string, separator?: string): object;
/**
* Generate a random string aka `nonce`, similar as `crypto.randomBytes`.
* Generate a Base62 random string aka `nonce`, similar as `crypto.randomBytes`.
*
* @param {number} size - Nonce string length, default is 32 bytes.
*
* @returns {string} 62 radix random string.
* @returns {string} Base62 random string.
*/
static nonce(size?: number): string;
/**
Expand Down
8 changes: 4 additions & 4 deletions lib/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const ERR_INIT_MCHID_IS_MANDATORY = 'The merchant\' ID aka `mchid` is required,
const ERR_INIT_SERIAL_IS_MANDATORY = 'The serial number of the merchant\'s certificate aka `serial` is required, usually hexadecial.';
const ERR_INIT_PRIVATEKEY_IS_MANDATORY = 'The merchant\'s private key aka `privateKey` is required, usual as pem format.';
const ERR_INIT_CERTS_IS_MANDATORY = 'The platform certificate(s) aka `certs` is required, paired as of `{$serial: $certificate}`.';
const ERR_INIT_CERTS_EXCLUDE_MCHSERRIAL = 'The `certs` contains the merchant\'s certificate serial number which is not allowed here.';
const ERR_INIT_CERTS_EXCLUDE_MCHSERIAL = 'The `certs` contains the merchant\'s certificate serial number which is not allowed here.';

const EV3_RES_HEADERS_INCOMPLATE = 'The response\'s Headers incomplete, must have(`%s`, `%s`, `%s` and `%s`).';
const EV3_RES_HEADER_TIMESTAMP_OFFSET = 'It\'s allowed time offset in ± %s seconds, the response(header:%s) was on %s, your\'s localtime on %s.';
const EV3_RES_HEADER_PLATFORM_SERIAL = 'Cannot found the serial(`%s`)\'s configuration, which\'s from the response(header:%s), your\'s %O.';
const EV3_RES_HEADER_SIGNATURE_DEGIST = 'Verify the response\'s data with: timestamp=%s, nonce=%s, signature=%s, cert={%s: ...} failed.';
const EV3_RES_HEADER_SIGNATURE_DIGEST = 'Verify the response\'s data with: timestamp=%s, nonce=%s, signature=%s, cert={%s: ...} failed.';

/**
* Decorate the `Axios` instance
Expand Down Expand Up @@ -167,7 +167,7 @@ class Decorator {

assert.ok(
Rsa.verify(Formatter.response(timestamp, nonce, data), signature, certs[serial]),
utils.implicityReturnValues(EV3_RES_HEADER_SIGNATURE_DEGIST, { headers, data }, timestamp, nonce, signature, serial),
utils.implicityReturnValues(EV3_RES_HEADER_SIGNATURE_DIGEST, { headers, data }, timestamp, nonce, signature, serial),
);

return data;
Expand Down Expand Up @@ -208,7 +208,7 @@ class Decorator {
);
assert(
!Object.prototype.hasOwnProperty.call(certs, serial),
ERR_INIT_CERTS_EXCLUDE_MCHSERRIAL,
ERR_INIT_CERTS_EXCLUDE_MCHSERIAL,
);

const instance = axios.create(this.withDefaults({
Expand Down
11 changes: 5 additions & 6 deletions lib/formatter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint no-bitwise: ["error", { "allow": ["|"] }] */
const { randomFillSync } = require('crypto');
/**
* Provides easy used methods using in this project.
*/
Expand Down Expand Up @@ -40,16 +41,14 @@ class Formatter {
}

/**
* Generate a random string aka `nonce`, similar as `crypto.randomBytes`.
* Generate a Base62 random string aka `nonce`, similar as `crypto.randomBytes`.
*
* @param {number} size - Nonce string length, default is 32 bytes.
*
* @returns {string} 62 radix random string.
* @returns {string} - Base62 random string.
*/
static nonce(size = 32) {
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

return '0'.repeat(size).replace(/0/g, () => chars[Math.random() * 62 | 0]);
return randomFillSync(Buffer.from(Array(size))).map((i) => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'[i % 62].charCodeAt(0)).toString();
}

/**
Expand All @@ -73,7 +72,7 @@ class Formatter {
* @returns {string} - The APIv3 Authorization `header` value
*/
static authorization(mchid, nonce, signature, timestamp, serial) {
return `WECHATPAY2-SHA256-RSA2048 mchid="${mchid}",nonce_str="${nonce}",signature="${signature}",timestamp="${timestamp}",serial_no="${serial}"`;
return `WECHATPAY2-SHA256-RSA2048 mchid="${mchid}",serial_no="${serial}",timestamp="${timestamp}",nonce_str="${nonce}",signature="${signature}"`;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechatpay-axios-plugin",
"version": "0.7.7",
"version": "0.7.8",
"description": "微信支付APIv2及v3 NodeJS SDK,支持CLI模式请求OpenAPI,支持v3证书下载,v2付款码支付、企业付款、退款,企业微信-企业支付-企业红包/向员工付款,v2&v3 Native支付、扫码支付、H5支付、JSAPI/小程序支付、合单支付...",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('lib/formatter', () => {
.and.match(/,signature="3"/)
.and.match(/,timestamp="4"/)
.and.match(/,serial_no="5"/)
.and.equal('WECHATPAY2-SHA256-RSA2048 mchid="1",nonce_str="2",signature="3",timestamp="4",serial_no="5"');
.and.equal('WECHATPAY2-SHA256-RSA2048 mchid="1",serial_no="5",timestamp="4",nonce_str="2",signature="3"');
});
});

Expand Down

0 comments on commit f3a5674

Please sign in to comment.