feat: switch mini program recharge to virtual payment
This commit is contained in:
159
miniprogram/pages/wechat-pay/index.test.js
Normal file
159
miniprogram/pages/wechat-pay/index.test.js
Normal file
@@ -0,0 +1,159 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
import wechatPayBridge from './index.shared.js';
|
||||
|
||||
const {
|
||||
appendPayResult,
|
||||
createWechatPayPage,
|
||||
parsePayParams,
|
||||
requestWechatPayment,
|
||||
} = wechatPayBridge;
|
||||
|
||||
describe('wechat-pay mini program payment bridge', () => {
|
||||
beforeEach(() => {
|
||||
globalThis.wx = {
|
||||
requestPayment: vi.fn(),
|
||||
requestVirtualPayment: vi.fn(),
|
||||
setStorageSync: vi.fn(),
|
||||
navigateBack: vi.fn(),
|
||||
};
|
||||
globalThis.getCurrentPages = vi.fn(() => []);
|
||||
});
|
||||
|
||||
test('routes virtual payloads to wx.requestVirtualPayment', async () => {
|
||||
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
||||
options.success?.({ errMsg: 'requestVirtualPayment:ok' });
|
||||
});
|
||||
const payParams = {
|
||||
mode: 'short_series_coin',
|
||||
signData:
|
||||
'{"offerId":"offer-1","buyQuantity":1,"env":0,"currencyType":"CNY","outTradeNo":"order-virtual-1","attach":"mud_points_60"}',
|
||||
paySig: 'pay-sig',
|
||||
signature: 'user-sig',
|
||||
};
|
||||
|
||||
const status = await requestWechatPayment(payParams);
|
||||
|
||||
expect(status).toBe('success');
|
||||
expect(globalThis.wx.requestVirtualPayment).toHaveBeenCalledWith({
|
||||
mode: 'short_series_coin',
|
||||
signData: payParams.signData,
|
||||
paySig: 'pay-sig',
|
||||
signature: 'user-sig',
|
||||
success: expect.any(Function),
|
||||
fail: expect.any(Function),
|
||||
});
|
||||
expect(globalThis.wx.requestPayment).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('routes goods virtual payloads to wx.requestVirtualPayment', async () => {
|
||||
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
||||
options.success?.({ errMsg: 'requestVirtualPayment:ok' });
|
||||
});
|
||||
const payParams = {
|
||||
mode: 'short_series_goods',
|
||||
signData:
|
||||
'{"offerId":"offer-1","buyQuantity":1,"env":0,"currencyType":"CNY","productId":"member_month","goodsPrice":2800,"outTradeNo":"order-goods-1","attach":"member_month"}',
|
||||
paySig: 'pay-sig',
|
||||
signature: 'user-sig',
|
||||
};
|
||||
|
||||
const status = await requestWechatPayment(payParams);
|
||||
|
||||
expect(status).toBe('success');
|
||||
expect(globalThis.wx.requestVirtualPayment).toHaveBeenCalledWith({
|
||||
mode: 'short_series_goods',
|
||||
signData: payParams.signData,
|
||||
paySig: 'pay-sig',
|
||||
signature: 'user-sig',
|
||||
success: expect.any(Function),
|
||||
fail: expect.any(Function),
|
||||
});
|
||||
expect(globalThis.wx.requestPayment).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('keeps ordinary requestPayment payloads on wx.requestPayment', async () => {
|
||||
globalThis.wx.requestPayment.mockImplementationOnce((options) => {
|
||||
options.success?.();
|
||||
});
|
||||
|
||||
const status = await requestWechatPayment({
|
||||
timeStamp: '1777110165',
|
||||
nonceStr: 'nonce',
|
||||
package: 'prepay_id=wx-prepay',
|
||||
signType: 'RSA',
|
||||
paySign: 'signature',
|
||||
});
|
||||
|
||||
expect(status).toBe('success');
|
||||
expect(globalThis.wx.requestPayment).toHaveBeenCalledWith({
|
||||
timeStamp: '1777110165',
|
||||
nonceStr: 'nonce',
|
||||
package: 'prepay_id=wx-prepay',
|
||||
signType: 'RSA',
|
||||
paySign: 'signature',
|
||||
success: expect.any(Function),
|
||||
fail: expect.any(Function),
|
||||
});
|
||||
expect(globalThis.wx.requestVirtualPayment).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('maps virtual payment cancel errCode to cancel result', async () => {
|
||||
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
||||
options.fail?.({ errCode: -2, errMsg: 'requestVirtualPayment:fail cancel' });
|
||||
});
|
||||
|
||||
await expect(
|
||||
requestWechatPayment({
|
||||
mode: 'short_series_coin',
|
||||
signData: '{}',
|
||||
paySig: 'pay-sig',
|
||||
signature: 'user-sig',
|
||||
}),
|
||||
).resolves.toBe('cancel');
|
||||
});
|
||||
|
||||
test('page notifies previous web-view after virtual payment', async () => {
|
||||
const previousPage = {
|
||||
data: { webViewUrl: 'https://web.test/#tab=profile' },
|
||||
setData: vi.fn(),
|
||||
};
|
||||
globalThis.getCurrentPages = vi.fn(() => [{}, previousPage]);
|
||||
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
||||
options.success?.({ errMsg: 'requestVirtualPayment:ok' });
|
||||
});
|
||||
const page = createWechatPayPage({
|
||||
setData: vi.fn(),
|
||||
});
|
||||
|
||||
await page.onLoad({
|
||||
requestId: 'request-1',
|
||||
payParams: encodeURIComponent(
|
||||
JSON.stringify({
|
||||
mode: 'short_series_coin',
|
||||
signData: '{}',
|
||||
paySig: 'pay-sig',
|
||||
signature: 'user-sig',
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
expect(globalThis.wx.setStorageSync).toHaveBeenCalledWith(
|
||||
'genarrative:wechat-pay-result',
|
||||
'request-1:success',
|
||||
);
|
||||
expect(previousPage.setData).toHaveBeenCalledWith({
|
||||
webViewUrl: 'https://web.test/#tab=profile&wx_pay_result=request-1%3Asuccess',
|
||||
});
|
||||
expect(globalThis.wx.navigateBack).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('parsePayParams and appendPayResult keep existing behavior', () => {
|
||||
expect(parsePayParams(encodeURIComponent('{"paySign":"sig"}'))).toEqual({
|
||||
paySign: 'sig',
|
||||
});
|
||||
expect(appendPayResult('https://web.test/#old=1', 'req', 'fail')).toBe(
|
||||
'https://web.test/#old=1&wx_pay_result=req%3Afail',
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user