oak-pay-business/lib/utils/payClazz/AliPay/AliPay.debug.js

163 lines
5.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerGetPayStateResult = registerGetPayStateResult;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
let _PAY_STATE = '';
function registerGetPayStateResult(payState) {
_PAY_STATE = payState;
}
class AliPay {
apProduct;
constructor(apProduct) {
this.apProduct = apProduct;
}
decodeRefundNotification(params, body) {
throw new Error('Method not implemented.');
}
getAccountEntity() {
return ['apAccount', this.apProduct.apAccountId];
}
async getAccountAmount(context) {
const [apAccount] = await context.select('apAccount', {
data: {
id: 1,
price: 1,
},
filter: {
id: this.apProduct.apAccountId,
}
}, { dontCollect: true });
return apAccount.price;
}
calcTransferTax(price) {
const { apAccount } = this.apProduct;
const { allowWithdrawTransfer, withdrawTransferLossRatio } = apAccount;
(0, assert_1.default)(allowWithdrawTransfer);
if (typeof withdrawTransferLossRatio !== 'number') {
throw new Error('渠道的withdrawTransferLossRatio未设置');
}
return [Math.round(price * withdrawTransferLossRatio / 100), 'apAccount', apAccount.id];
}
calcRefundTax(price) {
const { apAccount } = this.apProduct;
const refundCompensateRatio = typeof this.apProduct.refundCompensateRatio === 'number' ? this.apProduct.refundCompensateRatio : apAccount.refundCompensateRatio;
const taxLossRatio = typeof this.apProduct.taxLossRatio === 'number' ? this.apProduct.taxLossRatio : apAccount.taxLossRatio;
// 返回taxLossRatio * refundCompensateRatio / 100
if (typeof taxLossRatio !== 'number') {
throw new Error('渠道的taxLossRatio未设置');
}
if (typeof refundCompensateRatio !== 'number') {
throw new Error('渠道的refundCompensateRatio未设置');
}
const value = Math.round(price * taxLossRatio * refundCompensateRatio / 100 / 100);
return [-value, 'apAccount', apAccount.id];
}
calcPayTax(price) {
const { apProduct } = this;
const taxLossRatio = apProduct.taxLossRatio || apProduct.apAccount.taxLossRatio;
(0, assert_1.default)(typeof taxLossRatio === 'number', '支付宝渠道的手续费率未配置');
return [Math.round(price * taxLossRatio / 100), 'apAccount', apProduct.apAccountId];
}
async refund(refund, context) {
return {
externalId: Math.random().toString(),
};
}
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.apProduct.type) {
case 'app': {
const orderStr = Math.random().toString();
data.externalId = orderStr;
data.meta = {
orderStr,
};
break;
}
case 'jsapi':
case 'mp': {
const tradeNO = Math.random().toString();
data.externalId = tradeNO;
data.meta = {
tradeNO,
};
break;
}
case 'h5': {
const h5Url = Math.random().toString();
data.externalId = h5Url;
data.meta = {
h5Url,
};
break;
}
case 'native': {
// post请求返回html get请求返回url 现在默认使用get请求
const htmlUrl = 'https://www.baidu.com';
data.externalId = htmlUrl;
data.meta = {
htmlUrl,
};
break;
}
case 'person': {
const tradeNO = Math.random().toString();
data.externalId = tradeNO;
data.meta = {
tradeNO,
};
break;
}
case 'code': {
const codeUrl = Math.random().toString();
data.externalId = codeUrl;
data.meta = {
codeUrl,
};
break;
}
}
;
data.timeoutAt = Date.now() + 7000 * 1000;
}
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) {
}
}
exports.default = AliPay;