forked from baaxl9vh/nest-wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwepay.hb.spec.ts
116 lines (100 loc) · 3.37 KB
/
wepay.hb.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as fs from 'fs';
import * as path from 'path';
import { AxiosError } from 'axios';
import { GroupRedPackData, RedPackData } from './types';
import { WePayService } from './wepay.service';
import { XMLParser } from 'fast-xml-parser';
jest.setTimeout(20000);
describe('WePayService Test(Unit)', () => {
// 绑定的公众号appid
const appId = 'your/appid';
// 商户
const mchId = 'your/mch/id';
// 微信支付apiv2 key, important: v2
const apiKey = 'your/v2/apikey';
let service: WePayService;
let privateKey: Buffer;
let publicKey: Buffer;
const xmlParse = new XMLParser();
const SUCCESS = 'SUCCESS';
const FAIL = 'FAIL';
const NO = '20230809012611915';
beforeAll(() => {
service = new WePayService(true);
privateKey = fs.readFileSync(path.join(__dirname, '..', 'apiclient_key.pem'));
publicKey = fs.readFileSync(path.join(__dirname, '..', 'apiclient_cert.pem'));
});
it('Should send red pack', async () => {
try {
const dataObj: RedPackData = {
billNO: NO,
sendName: '测试发送者',
recipientOpenId: 'recipient/openid',
totalAmount: 1,
totalNum: 1,
wishing: '测试祝福',
clientIp: 'client/ip',
actName: '测试活动',
remark: '测试备注',
sceneId: 'PRODUCT_4',
}
const ret = await service.sendRedPack(dataObj, appId, mchId, apiKey, publicKey, privateKey);
expect(ret.status).toStrictEqual(200);
const data = xmlParse.parse(ret.data);
expect(data.xml.return_code).toStrictEqual(SUCCESS);
expect(data.xml.result_code).toStrictEqual(FAIL);
} catch (error) {
if (error instanceof AxiosError) {
console.log(error.response?.data || error.status);
} else {
console.log('error =', error);
}
expect(error).toBeUndefined();
}
});
it('Should send group red pack', async () => {
try {
const dataObj: GroupRedPackData = {
billNO: NO,
sendName: '测试发送者',
recipientOpenId: 'recipient/openid',
totalAmount: 1000,
totalNum: 3,
wishing: '测试祝福',
clientIp: 'client/ip',
actName: '测试活动',
remark: '测试备注',
sceneId: 'PRODUCT_4',
amtType: 'ALL_RAND',
}
const ret = await service.sendGroupRedPack(dataObj, appId, mchId, apiKey, publicKey, privateKey);
expect(ret.status).toStrictEqual(200);
const data = xmlParse.parse(ret.data);
expect(data.xml.return_code).toStrictEqual(SUCCESS);
expect(data.xml.result_code).toStrictEqual(FAIL);
} catch (error) {
if (error instanceof AxiosError) {
console.log(error.response?.data || error.status);
} else {
console.log('error =', error);
}
expect(error).toBeUndefined();
}
});
it('Should get hb info', async () => {
try {
const ret = await service.getHbInfo(NO, appId, mchId, apiKey, publicKey, privateKey);
expect(ret.status).toStrictEqual(200);
const data = xmlParse.parse(ret.data);
expect(data.xml.return_code).toStrictEqual(SUCCESS);
expect(data.xml.result_code).toStrictEqual(FAIL);
} catch (error) {
if (error instanceof AxiosError) {
console.log(error.response?.data || error.status);
} else {
console.log('error =', error);
}
expect(error).toBeUndefined();
}
});
});