node-easywechat 3.0.0-beta.4 → 3.0.1
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 +30 -0
- package/README.md +36 -1
- package/dist/Core/Contracts/RefreshableAccessTokenInterface.d.ts +2 -2
- package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +2 -2
- package/dist/Core/Http/Minxins/MessageMixin.js +1 -2
- package/dist/Core/Http/ServerRequest.d.ts +1 -1
- package/dist/Core/Http/ServerRequest.js +16 -6
- package/dist/Core/HttpClient/HttpClient.js +3 -1
- package/dist/Core/HttpClient/HttpClientResponse.d.ts +16 -5
- package/dist/Core/HttpClient/HttpClientResponse.js +64 -22
- package/dist/Core/HttpClient/Mixins/PresetMixin.d.ts +0 -12
- package/dist/Core/HttpClient/Mixins/PresetMixin.js +0 -18
- package/dist/Core/Message.d.ts +0 -127
- package/dist/Core/Message.js +5 -1
- package/dist/Core/Mixins/DecryptXmlMessageMixin.js +1 -1
- package/dist/Core/Mixins/ResponseXmlMessageMixin.js +2 -2
- package/dist/Core/Support/PrivateKey.d.ts +20 -0
- package/dist/Core/Support/PrivateKey.js +38 -0
- package/dist/Core/Support/PublicKey.d.ts +14 -0
- package/dist/Core/Support/PublicKey.js +36 -0
- package/dist/Core/Support/RSA.d.ts +2 -2
- package/dist/Core/Support/RSA.js +4 -4
- package/dist/Core/Support/Utils.d.ts +6 -0
- package/dist/Core/Support/Utils.js +15 -1
- package/dist/MiniApp/Application.d.ts +4 -4
- package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +2 -2
- package/dist/OfficialAccount/AccessToken.d.ts +2 -2
- package/dist/OfficialAccount/AccessToken.js +2 -2
- package/dist/OfficialAccount/Application.d.ts +4 -4
- package/dist/OfficialAccount/Application.js +0 -5
- package/dist/OfficialAccount/Contracts/ApplicationInterface.d.ts +2 -2
- package/dist/OfficialAccount/JsApiTicket.js +2 -2
- package/dist/OfficialAccount/Message.d.ts +139 -0
- package/dist/OfficialAccount/Message.js +1 -0
- package/dist/Pay/Application.d.ts +52 -0
- package/dist/Pay/Application.js +96 -0
- package/dist/Pay/Client.d.ts +54 -0
- package/dist/Pay/Client.js +146 -0
- package/dist/Pay/Config.d.ts +5 -0
- package/dist/Pay/Config.js +17 -0
- package/dist/Pay/Contracts/ApplicationInterface.d.ts +50 -0
- package/dist/Pay/Contracts/ApplicationInterface.js +45 -0
- package/dist/Pay/Contracts/MerchantInterface.d.ts +35 -0
- package/dist/Pay/Contracts/MerchantInterface.js +35 -0
- package/dist/Pay/Contracts/ResponseValidatorInterface.d.ts +9 -0
- package/dist/Pay/Contracts/ResponseValidatorInterface.js +10 -0
- package/dist/Pay/LegacySignature.d.ts +12 -0
- package/dist/Pay/LegacySignature.js +59 -0
- package/dist/Pay/Merchant.d.ts +25 -0
- package/dist/Pay/Merchant.js +53 -0
- package/dist/Pay/Message.d.ts +175 -0
- package/dist/Pay/Message.js +24 -0
- package/dist/Pay/Server.d.ts +41 -0
- package/dist/Pay/Server.js +122 -0
- package/dist/Pay/Signature.d.ts +15 -0
- package/dist/Pay/Signature.js +44 -0
- package/dist/Pay/Utils.d.ts +54 -0
- package/dist/Pay/Utils.js +142 -0
- package/dist/Types/global.d.ts +110 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
const merge_1 = __importDefault(require("merge"));
|
|
15
|
+
const Utils_1 = require("../Core/Support/Utils");
|
|
16
|
+
const HttpClient_1 = __importDefault(require("../Core/HttpClient/HttpClient"));
|
|
17
|
+
const HttpClientMethodsMixin_1 = __importDefault(require("../Core/HttpClient/Mixins/HttpClientMethodsMixin"));
|
|
18
|
+
const PresetMixin_1 = __importDefault(require("../Core/HttpClient/Mixins/PresetMixin"));
|
|
19
|
+
const url_1 = __importDefault(require("url"));
|
|
20
|
+
const Signature_1 = __importDefault(require("./Signature"));
|
|
21
|
+
const LegacySignature_1 = __importDefault(require("./LegacySignature"));
|
|
22
|
+
class Client {
|
|
23
|
+
constructor(merchant, client, defaultOptions = {}) {
|
|
24
|
+
var _a;
|
|
25
|
+
this.merchant = merchant;
|
|
26
|
+
this.throw = true;
|
|
27
|
+
this.client = null;
|
|
28
|
+
this.defaultOptions = {
|
|
29
|
+
baseURL: 'https://api.mch.weixin.qq.com',
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/json',
|
|
32
|
+
'Accept': 'application/json',
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
this.V3_URI_PREFIXES = [
|
|
36
|
+
'/v3/',
|
|
37
|
+
'/sandbox/v3/',
|
|
38
|
+
'/hk/v3/',
|
|
39
|
+
'/global/v3/',
|
|
40
|
+
];
|
|
41
|
+
this.throw = !!((_a = defaultOptions['throw']) !== null && _a !== void 0 ? _a : true);
|
|
42
|
+
this.defaultOptions = merge_1.default.recursive(true, this.defaultOptions, defaultOptions);
|
|
43
|
+
this.client = client || HttpClient_1.default.create();
|
|
44
|
+
}
|
|
45
|
+
getInstance() {
|
|
46
|
+
return this.client.getInstance();
|
|
47
|
+
}
|
|
48
|
+
setInstance(instance) {
|
|
49
|
+
this.client.setInstance(instance);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
setLogger(logger) {
|
|
53
|
+
this.client.setLogger(logger);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
request(method, url, payload = {}) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
if (!payload.headers)
|
|
59
|
+
payload.headers = {};
|
|
60
|
+
if (!payload.headers['user-agent'] && !payload.headers['User-Agent']) {
|
|
61
|
+
payload.headers['user-agent'] = (0, Utils_1.createUserAgent)();
|
|
62
|
+
}
|
|
63
|
+
if (this.isV3Request(url)) {
|
|
64
|
+
payload.headers['authorization'] = this.createSignature(method, url, payload);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (payload.xml) {
|
|
68
|
+
if (Array.isArray(payload.xml)) {
|
|
69
|
+
payload.xml = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.xml));
|
|
70
|
+
}
|
|
71
|
+
if (typeof payload.xml !== 'string') {
|
|
72
|
+
throw new Error('The `xml` option must be a string or object.');
|
|
73
|
+
}
|
|
74
|
+
payload.data = payload.xml;
|
|
75
|
+
delete payload.xml;
|
|
76
|
+
}
|
|
77
|
+
if (payload.data && typeof payload.data === 'object') {
|
|
78
|
+
payload.data = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.data));
|
|
79
|
+
}
|
|
80
|
+
if (!payload.headers['content-type'] && !payload.headers['Content-Type']) {
|
|
81
|
+
payload.headers['content-type'] = 'text/xml';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (this.prependData && Object.keys(this.prependData).length > 0) {
|
|
85
|
+
payload.data = Object.assign(Object.assign({}, this.prependData), payload.data);
|
|
86
|
+
}
|
|
87
|
+
if (this.prependHeaders && Object.keys(this.prependHeaders).length > 0) {
|
|
88
|
+
payload.headers = Object.assign(Object.assign({}, this.prependHeaders), payload.headers);
|
|
89
|
+
}
|
|
90
|
+
return this.client.request(method, (0, Utils_1.ltrim)(url, '\\/+'), payload);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 判断是否是V3请求
|
|
95
|
+
* @param url 请求地址
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
isV3Request(url) {
|
|
99
|
+
let urlObj = url_1.default.parse(url);
|
|
100
|
+
for (let i = 0; i < this.V3_URI_PREFIXES.length; i++) {
|
|
101
|
+
if (urlObj.pathname.startsWith(this.V3_URI_PREFIXES[i])) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 创建签名(V3)
|
|
109
|
+
* @param method 请求方式
|
|
110
|
+
* @param url 请求地址
|
|
111
|
+
* @param payload 请求载荷
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
createSignature(method, url, payload) {
|
|
115
|
+
return (new Signature_1.default(this.merchant)).createHeader(method, url, payload);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 创建签名(V2)
|
|
119
|
+
* @param body 请求参数
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
attachLegacySignature(body) {
|
|
123
|
+
return (new LegacySignature_1.default(this.merchant)).sign(body);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 预设置mch_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
|
|
127
|
+
* @param new_mch_id
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
withMchId(new_mch_id = null) {
|
|
131
|
+
this.with('mch_id', new_mch_id);
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 预设置mch_id别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
|
|
136
|
+
* @param new_alias
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
withMchIdAs(new_alias = null) {
|
|
140
|
+
this.with(new_alias, this.presets['mch_id']);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
;
|
|
145
|
+
(0, Utils_1.applyMixins)(Client, [HttpClientMethodsMixin_1.default, PresetMixin_1.default]);
|
|
146
|
+
module.exports = Client;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const Config_1 = __importDefault(require("../Core/Config"));
|
|
6
|
+
class Config extends Config_1.default {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.requiredKeys = [
|
|
10
|
+
'mch_id',
|
|
11
|
+
'secret_key',
|
|
12
|
+
'private_key',
|
|
13
|
+
'certificate',
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
module.exports = Config;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import CacheInterface from "../../Core/Contracts/CacheInterface";
|
|
2
|
+
import ConfigInterface from "../../Core/Contracts/ConfigInterface";
|
|
3
|
+
import HttpClientInterface from "../../Core/HttpClient/Contracts/HttpClientInterface";
|
|
4
|
+
import ServerInterface from "../../Core/Contracts/ServerInterface";
|
|
5
|
+
import ServerRequestInterface from "../../Core/Http/Contracts/ServerRequestInterface";
|
|
6
|
+
import MerchantInterface from "./MerchantInterface";
|
|
7
|
+
import Utils from "../Utils";
|
|
8
|
+
declare abstract class ApplicationInterface {
|
|
9
|
+
/**
|
|
10
|
+
* 获取当前账户实例
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
getMerchant(): MerchantInterface;
|
|
14
|
+
/**
|
|
15
|
+
* 获取服务端实例
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
getServer(): ServerInterface;
|
|
19
|
+
/**
|
|
20
|
+
* 获取当前请求实例
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
getRequest(): ServerRequestInterface;
|
|
24
|
+
/**
|
|
25
|
+
* 获取客户端实例
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
getClient(): HttpClientInterface;
|
|
29
|
+
/**
|
|
30
|
+
* 获取网络请求客户端实例
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
getHttpClient(): HttpClientInterface;
|
|
34
|
+
/**
|
|
35
|
+
* 获取配置信息实例
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
getConfig(): ConfigInterface;
|
|
39
|
+
/**
|
|
40
|
+
* 获取缓存实例
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
getCache(): CacheInterface;
|
|
44
|
+
/**
|
|
45
|
+
* 获取工具实例
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
getUtils(): Utils;
|
|
49
|
+
}
|
|
50
|
+
export = ApplicationInterface;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class ApplicationInterface {
|
|
3
|
+
/**
|
|
4
|
+
* 获取当前账户实例
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
getMerchant() { return null; }
|
|
8
|
+
/**
|
|
9
|
+
* 获取服务端实例
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
getServer() { return null; }
|
|
13
|
+
/**
|
|
14
|
+
* 获取当前请求实例
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
getRequest() { return null; }
|
|
18
|
+
/**
|
|
19
|
+
* 获取客户端实例
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
getClient() { return null; }
|
|
23
|
+
/**
|
|
24
|
+
* 获取网络请求客户端实例
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
getHttpClient() { return null; }
|
|
28
|
+
/**
|
|
29
|
+
* 获取配置信息实例
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
getConfig() { return null; }
|
|
33
|
+
/**
|
|
34
|
+
* 获取缓存实例
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
getCache() { return null; }
|
|
38
|
+
/**
|
|
39
|
+
* 获取工具实例
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
getUtils() { return null; }
|
|
43
|
+
}
|
|
44
|
+
;
|
|
45
|
+
module.exports = ApplicationInterface;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { PrivateKey } from "../../Core/Support/PrivateKey";
|
|
2
|
+
import { PublicKey } from "../../Core/Support/PublicKey";
|
|
3
|
+
declare abstract class MerchantInterface {
|
|
4
|
+
/**
|
|
5
|
+
* 获取merchant_id
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
getMerchantId(): string;
|
|
9
|
+
/**
|
|
10
|
+
* 获取证书私钥
|
|
11
|
+
*/
|
|
12
|
+
getPrivateKey(): PrivateKey;
|
|
13
|
+
/**
|
|
14
|
+
* 获取secret_key
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
getSecretKey(): string;
|
|
18
|
+
/**
|
|
19
|
+
* 获取secret_key(V2版本)
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
getV2SecretKey(): string;
|
|
23
|
+
/**
|
|
24
|
+
* 获取证书
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
getCertificate(): PublicKey;
|
|
28
|
+
/**
|
|
29
|
+
* 获取平台证书
|
|
30
|
+
* @param serial
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
getPlatformCert(serial: string): PublicKey;
|
|
34
|
+
}
|
|
35
|
+
export = MerchantInterface;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
class MerchantInterface {
|
|
3
|
+
/**
|
|
4
|
+
* 获取merchant_id
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
getMerchantId() { return null; }
|
|
8
|
+
/**
|
|
9
|
+
* 获取证书私钥
|
|
10
|
+
*/
|
|
11
|
+
getPrivateKey() { return null; }
|
|
12
|
+
/**
|
|
13
|
+
* 获取secret_key
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
getSecretKey() { return null; }
|
|
17
|
+
/**
|
|
18
|
+
* 获取secret_key(V2版本)
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
getV2SecretKey() { return null; }
|
|
22
|
+
/**
|
|
23
|
+
* 获取证书
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
getCertificate() { return null; }
|
|
27
|
+
/**
|
|
28
|
+
* 获取平台证书
|
|
29
|
+
* @param serial
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
getPlatformCert(serial) { return null; }
|
|
33
|
+
}
|
|
34
|
+
;
|
|
35
|
+
module.exports = MerchantInterface;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import MerchantInterface from "./Contracts/MerchantInterface";
|
|
2
|
+
declare class LegacySignature {
|
|
3
|
+
protected merchant: MerchantInterface;
|
|
4
|
+
constructor(merchant: MerchantInterface);
|
|
5
|
+
/**
|
|
6
|
+
* V2版本的签名计算,并返回带签名字段的参数集合
|
|
7
|
+
* @param params 参数集合
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
sign(params: Record<string, string | number>): Record<string, string | number>;
|
|
11
|
+
}
|
|
12
|
+
export = LegacySignature;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const Utils_1 = require("../Core/Support/Utils");
|
|
6
|
+
const merge_1 = __importDefault(require("merge"));
|
|
7
|
+
class LegacySignature {
|
|
8
|
+
constructor(merchant) {
|
|
9
|
+
this.merchant = merchant;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* V2版本的签名计算,并返回带签名字段的参数集合
|
|
13
|
+
* @param params 参数集合
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
sign(params) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
let nonce = (0, Utils_1.randomString)();
|
|
19
|
+
let attributes = (0, merge_1.default)(true, {
|
|
20
|
+
nonce_str: nonce,
|
|
21
|
+
sub_mch_id: (_a = params['sub_mch_id']) !== null && _a !== void 0 ? _a : null,
|
|
22
|
+
sub_appid: (_b = params['sub_appid']) !== null && _b !== void 0 ? _b : null,
|
|
23
|
+
}, params);
|
|
24
|
+
let signString = '';
|
|
25
|
+
let sparator = '';
|
|
26
|
+
let keys = Object.keys(attributes);
|
|
27
|
+
keys = keys.sort();
|
|
28
|
+
for (let i = 0; i < keys.length; i++) {
|
|
29
|
+
if (keys[i] == 'sign' || typeof attributes[keys[i]] === undefined || attributes[keys[i]] === null)
|
|
30
|
+
continue;
|
|
31
|
+
signString += sparator + keys[i] + '=' + attributes[keys[i]];
|
|
32
|
+
sparator = '&';
|
|
33
|
+
}
|
|
34
|
+
let key = this.merchant.getV2SecretKey();
|
|
35
|
+
if (!key) {
|
|
36
|
+
throw new Error('Missing V2 API key.');
|
|
37
|
+
}
|
|
38
|
+
signString += '&key=' + key;
|
|
39
|
+
let sign = '';
|
|
40
|
+
let type = params['sign_type'] ? (params['sign_type'] + '').toLowerCase() : 'md5';
|
|
41
|
+
switch (type) {
|
|
42
|
+
case 'sha1':
|
|
43
|
+
case 'md5':
|
|
44
|
+
sign = (0, Utils_1.createHash)(signString, type);
|
|
45
|
+
break;
|
|
46
|
+
case 'hmac-sha256':
|
|
47
|
+
case 'hmac_sha256':
|
|
48
|
+
type = type.replace(/^hmac[\-|_]/i, '');
|
|
49
|
+
sign = (0, Utils_1.createHmac)(signString, key, type);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
if (!sign) {
|
|
53
|
+
throw new Error('Failed to sign the request.');
|
|
54
|
+
}
|
|
55
|
+
params.sign = (sign + '').toUpperCase();
|
|
56
|
+
return params;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
module.exports = LegacySignature;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PrivateKey } from "../Core/Support/PrivateKey";
|
|
2
|
+
import { PublicKey } from "../Core/Support/PublicKey";
|
|
3
|
+
import MerchantInterface from "./Contracts/MerchantInterface";
|
|
4
|
+
declare class Merchant implements MerchantInterface {
|
|
5
|
+
protected mchId: string;
|
|
6
|
+
protected privateKey: PrivateKey;
|
|
7
|
+
protected certificate: PublicKey;
|
|
8
|
+
protected secretKey: string;
|
|
9
|
+
protected v2SecretKey: string;
|
|
10
|
+
protected platformCerts: Record<string, PublicKey>;
|
|
11
|
+
constructor(mchId: string, privateKey: PrivateKey, certificate: PublicKey, secretKey: string, v2SecretKey?: string, platformCerts?: Record<string, string | PublicKey> | string[] | PublicKey[]);
|
|
12
|
+
/**
|
|
13
|
+
* 统一规范化平台证书
|
|
14
|
+
* @param platformCerts 平台证书列表
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
protected normalizePlatformCerts(platformCerts: Record<string, string | PublicKey> | string[] | PublicKey[]): Record<string, PublicKey>;
|
|
18
|
+
getMerchantId(): string;
|
|
19
|
+
getPrivateKey(): PrivateKey;
|
|
20
|
+
getSecretKey(): string;
|
|
21
|
+
getV2SecretKey(): string;
|
|
22
|
+
getCertificate(): PublicKey;
|
|
23
|
+
getPlatformCert(serial: string): PublicKey;
|
|
24
|
+
}
|
|
25
|
+
export = Merchant;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const PublicKey_1 = require("../Core/Support/PublicKey");
|
|
3
|
+
class Merchant {
|
|
4
|
+
constructor(mchId, privateKey, certificate, secretKey, v2SecretKey = null, platformCerts = []) {
|
|
5
|
+
this.mchId = mchId;
|
|
6
|
+
this.privateKey = privateKey;
|
|
7
|
+
this.certificate = certificate;
|
|
8
|
+
this.secretKey = secretKey;
|
|
9
|
+
this.v2SecretKey = v2SecretKey;
|
|
10
|
+
this.platformCerts = {};
|
|
11
|
+
this.platformCerts = this.normalizePlatformCerts(platformCerts);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 统一规范化平台证书
|
|
15
|
+
* @param platformCerts 平台证书列表
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
normalizePlatformCerts(platformCerts) {
|
|
19
|
+
let certs = {};
|
|
20
|
+
let isArray = Array.isArray(platformCerts);
|
|
21
|
+
for (let key in platformCerts) {
|
|
22
|
+
let publicKey = platformCerts[key];
|
|
23
|
+
if (typeof publicKey === 'string') {
|
|
24
|
+
publicKey = new PublicKey_1.PublicKey(publicKey);
|
|
25
|
+
}
|
|
26
|
+
if (!(publicKey instanceof PublicKey_1.PublicKey)) {
|
|
27
|
+
throw new Error('Invalid platform certficate.');
|
|
28
|
+
}
|
|
29
|
+
certs[isArray ? publicKey.getSerialNo() : key] = publicKey;
|
|
30
|
+
}
|
|
31
|
+
return certs;
|
|
32
|
+
}
|
|
33
|
+
getMerchantId() {
|
|
34
|
+
return this.mchId;
|
|
35
|
+
}
|
|
36
|
+
getPrivateKey() {
|
|
37
|
+
return this.privateKey;
|
|
38
|
+
}
|
|
39
|
+
getSecretKey() {
|
|
40
|
+
return this.secretKey;
|
|
41
|
+
}
|
|
42
|
+
getV2SecretKey() {
|
|
43
|
+
return this.v2SecretKey;
|
|
44
|
+
}
|
|
45
|
+
getCertificate() {
|
|
46
|
+
return this.certificate;
|
|
47
|
+
}
|
|
48
|
+
getPlatformCert(serial) {
|
|
49
|
+
var _a;
|
|
50
|
+
return (_a = this.platformCerts[serial]) !== null && _a !== void 0 ? _a : null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
module.exports = Merchant;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import BaseMessage from '../Core/Message';
|
|
2
|
+
declare class Message extends BaseMessage {
|
|
3
|
+
getOriginalAttributes(): Record<string, any>;
|
|
4
|
+
getEventType(): string;
|
|
5
|
+
}
|
|
6
|
+
interface Message {
|
|
7
|
+
/**
|
|
8
|
+
* 应用ID
|
|
9
|
+
*/
|
|
10
|
+
appid: string;
|
|
11
|
+
/**
|
|
12
|
+
* 商户号
|
|
13
|
+
*/
|
|
14
|
+
mchid: string;
|
|
15
|
+
/**
|
|
16
|
+
* 商户订单号
|
|
17
|
+
*/
|
|
18
|
+
out_trade_no: string;
|
|
19
|
+
/**
|
|
20
|
+
* 微信支付订单号
|
|
21
|
+
*/
|
|
22
|
+
transaction_id: string;
|
|
23
|
+
/**
|
|
24
|
+
* 交易类型
|
|
25
|
+
* - `JSAPI` 公众号支付
|
|
26
|
+
* - `NATIVE` 扫码支付
|
|
27
|
+
* - `APP` APP支付
|
|
28
|
+
* - `MICROPAY` 付款码支付
|
|
29
|
+
* - `MWEB` H5支付
|
|
30
|
+
* - `FACEPAY` 刷脸支付
|
|
31
|
+
*/
|
|
32
|
+
trade_type: string;
|
|
33
|
+
/**
|
|
34
|
+
* 交易状态
|
|
35
|
+
* - `SUCCESS` 支付成功
|
|
36
|
+
* - `REFUND` 转入退款
|
|
37
|
+
* - `NOTPAY` 未支付
|
|
38
|
+
* - `CLOSED` 已关闭
|
|
39
|
+
* - `REVOKED` 已撤销(付款码支付)
|
|
40
|
+
* - `USERPAYING` 用户支付中(付款码支付)
|
|
41
|
+
* - `PAYERROR` 支付失败(其他原因,如银行返回失败)
|
|
42
|
+
*/
|
|
43
|
+
trade_state: string;
|
|
44
|
+
/**
|
|
45
|
+
* 交易状态描述
|
|
46
|
+
*/
|
|
47
|
+
trade_state_desc: string;
|
|
48
|
+
/**
|
|
49
|
+
* 付款银行
|
|
50
|
+
* @see [银行对照表](https://pay.weixin.qq.com/wiki/doc/apiv3/terms_definition/chapter1_1_3.shtml#part-6)
|
|
51
|
+
*/
|
|
52
|
+
bank_type: string;
|
|
53
|
+
/**
|
|
54
|
+
* 附加数据
|
|
55
|
+
*/
|
|
56
|
+
attach?: string;
|
|
57
|
+
/**
|
|
58
|
+
* 支付完成时间。格式:yyyy-MM-DDTHH:mm:ss+TIMEZONE,如:2015-05-20T13:29:35+08:00
|
|
59
|
+
*/
|
|
60
|
+
success_time: string;
|
|
61
|
+
/**
|
|
62
|
+
* 支付者
|
|
63
|
+
*/
|
|
64
|
+
payer: {
|
|
65
|
+
/**
|
|
66
|
+
* 用户标识
|
|
67
|
+
*/
|
|
68
|
+
openid: string;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* 订单金额
|
|
72
|
+
*/
|
|
73
|
+
amount: {
|
|
74
|
+
/**
|
|
75
|
+
* 总金额,单位:分
|
|
76
|
+
*/
|
|
77
|
+
total: number;
|
|
78
|
+
/**
|
|
79
|
+
* 用户支付金额 ,单位:分
|
|
80
|
+
*/
|
|
81
|
+
payer_total: number;
|
|
82
|
+
/**
|
|
83
|
+
* 货币类型,如:`CNY` 表示人民币,境内商户号仅支持人民币
|
|
84
|
+
*/
|
|
85
|
+
currency: string;
|
|
86
|
+
/**
|
|
87
|
+
* 用户支付币种,如:`CNY` 表示人民币
|
|
88
|
+
*/
|
|
89
|
+
payer_currency: string;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* 场景信息
|
|
93
|
+
*/
|
|
94
|
+
scene_info?: {
|
|
95
|
+
/**
|
|
96
|
+
* 商户端设备号
|
|
97
|
+
*/
|
|
98
|
+
device_id?: string;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* 优惠信息
|
|
102
|
+
*/
|
|
103
|
+
promotion_detail?: {
|
|
104
|
+
/**
|
|
105
|
+
* 券ID
|
|
106
|
+
*/
|
|
107
|
+
coupon_id: string;
|
|
108
|
+
/**
|
|
109
|
+
* 优惠名称
|
|
110
|
+
*/
|
|
111
|
+
name?: string;
|
|
112
|
+
/**
|
|
113
|
+
* 优惠范围
|
|
114
|
+
* - `GLOBAL` 全场代金券
|
|
115
|
+
* - `SINGLE` 单品优惠
|
|
116
|
+
*/
|
|
117
|
+
scope?: string;
|
|
118
|
+
/**
|
|
119
|
+
* 优惠类型
|
|
120
|
+
* - `CASH` 充值型代金券
|
|
121
|
+
* - `NOCASH` 免充值型代金券
|
|
122
|
+
*/
|
|
123
|
+
type?: string;
|
|
124
|
+
/**
|
|
125
|
+
* 优惠券面额
|
|
126
|
+
*/
|
|
127
|
+
amount: number;
|
|
128
|
+
/**
|
|
129
|
+
* 活动ID
|
|
130
|
+
*/
|
|
131
|
+
stock_id?: number;
|
|
132
|
+
/**
|
|
133
|
+
* 微信出资,单位:分
|
|
134
|
+
*/
|
|
135
|
+
wechatpay_contribute?: number;
|
|
136
|
+
/**
|
|
137
|
+
* 商户出资,单位:分
|
|
138
|
+
*/
|
|
139
|
+
merchant_contribute?: number;
|
|
140
|
+
/**
|
|
141
|
+
* 其他出资,单位:分
|
|
142
|
+
*/
|
|
143
|
+
other_contribute?: number;
|
|
144
|
+
/**
|
|
145
|
+
* 优惠币种,如:`CNY` 表示人民币,境内商户号仅支持人民币
|
|
146
|
+
*/
|
|
147
|
+
currency?: string;
|
|
148
|
+
/**
|
|
149
|
+
* 单品列表
|
|
150
|
+
*/
|
|
151
|
+
goods_detail?: {
|
|
152
|
+
/**
|
|
153
|
+
* 商品编码
|
|
154
|
+
*/
|
|
155
|
+
goods_id: string;
|
|
156
|
+
/**
|
|
157
|
+
* 商品数量
|
|
158
|
+
*/
|
|
159
|
+
quantity: number;
|
|
160
|
+
/**
|
|
161
|
+
* 商品单价,单位:分
|
|
162
|
+
*/
|
|
163
|
+
unit_price: number;
|
|
164
|
+
/**
|
|
165
|
+
* 商品优惠金额
|
|
166
|
+
*/
|
|
167
|
+
discount_amount: number;
|
|
168
|
+
/**
|
|
169
|
+
* 商品备注
|
|
170
|
+
*/
|
|
171
|
+
goods_remark: string;
|
|
172
|
+
}[];
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export = Message;
|