oak-pay-business/es/utils/payClazz/WechatPay/WechatPay.debug.js

90 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let _PAY_STATE = '';
export function registerGetPayStateResult(payState) {
_PAY_STATE = payState;
}
export default class WechatPay {
type;
constructor(wpProduct, appId) {
// todo
this.type = wpProduct.type;
}
async refund(refund) {
return {
externalId: Math.random().toString(),
};
}
async closeRefund(refund) {
return;
}
async getRefundState(refund) {
const r = Math.random();
if (r < 0.5) {
return ['refunded', undefined];
}
return ['failed', {
reason: '微信觉得你长得太丑了,就不想给你退款,让你去深圳找马化腾',
}];
}
decodePayNotification(params, body) {
throw new Error("Method not implemented.");
}
/**
* 参照微信支付prepay接口模拟返回
* @param pay
* @param data
* @param context
*/
async prepay(pay, data, context) {
switch (this.type) {
case 'app':
case 'jsapi':
case 'mp': {
const prepayId = Math.random().toString();
data.externalId = prepayId;
data.meta = {
prepayId,
};
break;
}
case 'h5': {
const h5Url = Math.random().toString();
data.externalId = h5Url;
data.meta = {
h5Url,
};
break;
}
case 'native': {
const codeUrl = Math.random().toString();
data.externalId = codeUrl;
data.meta = {
codeUrl,
};
break;
}
}
;
data.timeoutAt = Date.now() + 7000 * 1000; // 微信官方文档过期时间2小时提前一点
}
async getState(pay) {
if (_PAY_STATE) {
return [_PAY_STATE, {}];
}
const r = Math.random();
if (r < 0.3) {
return ['paying', {}];
}
else if (r > 0.95) {
return ['closed', {}];
}
return ['paid', {
successAt: Date.now(),
}];
}
getRefundableAt(successAt) {
return successAt + 24 * 3600 * 1000;
}
async close(pay) {
}
}