328 lines
11 KiB
JavaScript
328 lines
11 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.registerFrontendPayRoutine = void 0;
|
||
const tslib_1 = require("tslib");
|
||
const money_1 = require("oak-domain/lib/utils/money");
|
||
const constants_1 = require("../../../config/constants");
|
||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||
const utils_1 = require("oak-frontend-base/es/utils/utils");
|
||
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
||
const wpProductFrontend_1 = require("../../../utils/wpProductFrontend");
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
const Exception_1 = require("../../../types/Exception");
|
||
const PayRoutineDict = {
|
||
wpProduct: {
|
||
projection: {
|
||
wpProduct: {
|
||
id: 1,
|
||
type: 1,
|
||
},
|
||
},
|
||
routine: async (pay, features) => {
|
||
const { iState, wpProduct, meta } = pay;
|
||
switch (wpProduct.type) {
|
||
case 'mp': {
|
||
if (process.env.OAK_PLATFORM === 'wechatMp') {
|
||
const { prepayMeta } = meta;
|
||
if (prepayMeta) {
|
||
try {
|
||
const result = await wx.requestPayment(prepayMeta);
|
||
process.env.NODE_ENV === 'development' && console.log(result);
|
||
}
|
||
catch (err) {
|
||
throw new Exception_1.StartPayFailure(err.errMsg.includes('cancel')
|
||
? features.locales.t('startPayError.userCancel')
|
||
: features.locales.t('startPayError.unknown'));
|
||
}
|
||
}
|
||
else {
|
||
throw new Exception_1.StartPayFailure(features.locales.t('startPayError.illegalPrepayData'));
|
||
}
|
||
}
|
||
else {
|
||
throw new Exception_1.StartPayFailure(features.locales.t('startPayError.falseEnv', { env: 'wechatMp' }));
|
||
}
|
||
return;
|
||
}
|
||
case 'jsapi': {
|
||
if (process.env.OAK_PLATFORM === 'web' && utils_1.isWeiXin) {
|
||
const { prepayMeta } = meta;
|
||
if (prepayMeta) {
|
||
const { timeStamp, ...rest } = prepayMeta;
|
||
// chooseWXPay文档都找不到了,网上查出来这里timestamp的s要小写,吐血了
|
||
await features.wechatSdk.loadWxAPi('chooseWXPay', {
|
||
timestamp: timeStamp,
|
||
...rest,
|
||
});
|
||
}
|
||
else {
|
||
throw new Exception_1.StartPayFailure(features.locales.t('startPayError.illegalPrepayData'));
|
||
}
|
||
}
|
||
}
|
||
default: {
|
||
(0, assert_1.default)('尚未实现');
|
||
}
|
||
}
|
||
},
|
||
judgeCanPay: wpProductFrontend_1.canStartPay,
|
||
}
|
||
};
|
||
function registerFrontendPayRoutine(entity, routine, projection, judgeCanPay) {
|
||
PayRoutineDict[entity] = {
|
||
routine,
|
||
projection,
|
||
judgeCanPay,
|
||
};
|
||
}
|
||
exports.registerFrontendPayRoutine = registerFrontendPayRoutine;
|
||
exports.default = OakComponent({
|
||
entity: 'pay',
|
||
isList: false,
|
||
projection: () => {
|
||
const baseProjection = {
|
||
id: 1,
|
||
applicationId: 1,
|
||
price: 1,
|
||
meta: 1,
|
||
iState: 1,
|
||
paid: 1,
|
||
refunded: 1,
|
||
timeoutAt: 1,
|
||
forbidRefundAt: 1,
|
||
externalId: 1,
|
||
orderId: 1,
|
||
depositId: 1,
|
||
deposit: {
|
||
id: 1,
|
||
accountId: 1,
|
||
price: 1,
|
||
loss: 1,
|
||
},
|
||
order: {
|
||
id: 1,
|
||
creatorId: 1,
|
||
},
|
||
entity: 1,
|
||
entityId: 1,
|
||
creatorId: 1,
|
||
phantom3: 1,
|
||
};
|
||
for (const k in PayRoutineDict) {
|
||
(0, lodash_1.merge)(baseProjection, PayRoutineDict[k].projection);
|
||
}
|
||
return baseProjection;
|
||
},
|
||
properties: {
|
||
onClose: () => undefined,
|
||
onPaid: () => undefined,
|
||
onPayFailure: () => undefined,
|
||
disableAutoPay: false,
|
||
closeWhenFailure: false,
|
||
},
|
||
data: {
|
||
showCloseConfirmMp: false,
|
||
showChannelSelectMp: false,
|
||
unsub: undefined,
|
||
},
|
||
formData({ data }) {
|
||
const application = this.features.application.getApplication();
|
||
const iState = data?.iState;
|
||
const iStateColor = iState && this.features.style.getColor('pay', 'iState', iState);
|
||
const startPayable = iState === 'paying' && !['account', 'offlineAccount'].includes(data.entity) && (PayRoutineDict[data.entity] && PayRoutineDict[data.entity].judgeCanPay(data, this.features));
|
||
const payChannels = this.features.pay.getPayChannels();
|
||
const offlines = this.features.cache.get('offlineAccount', {
|
||
data: {
|
||
id: 1,
|
||
type: 1,
|
||
channel: 1,
|
||
name: 1,
|
||
qrCode: 1,
|
||
},
|
||
filter: {
|
||
systemId: this.features.application.getApplication().systemId,
|
||
}
|
||
}).map(ele => {
|
||
const color = this.features.style.getColor('offlineAccount', 'type', ele.type);
|
||
return {
|
||
color,
|
||
...ele,
|
||
};
|
||
});
|
||
const offline = offlines?.find(ele => ele.id === data.entityId);
|
||
return {
|
||
type: data?.orderId ? 'order' : 'deposit',
|
||
pay: data,
|
||
application,
|
||
iStateColor,
|
||
closable: !!(data?.["#oakLegalActions"]?.includes('close')),
|
||
startPayable,
|
||
offline,
|
||
offlines,
|
||
notSameApp: data && data.applicationId !== application.id && data.entity !== 'offlineAccount',
|
||
priceStr: data?.price && (0, money_1.CentToString)(data.price, 2),
|
||
oakExecutable: this.tryExecute(),
|
||
};
|
||
},
|
||
features: [{
|
||
feature: 'application',
|
||
callback() {
|
||
this.refreshOfflineAccounts();
|
||
}
|
||
}],
|
||
actions: ['close', 'startPaying', {
|
||
action: 'update',
|
||
attrs: ['entityId'],
|
||
}],
|
||
methods: {
|
||
refreshOfflineAccounts() {
|
||
const { entity } = this.state.pay || {};
|
||
if (entity === 'offlineAccount') {
|
||
this.features.cache.refresh('offlineAccount', {
|
||
data: {
|
||
id: 1,
|
||
channel: 1,
|
||
name: 1,
|
||
type: 1,
|
||
qrCode: 1,
|
||
},
|
||
filter: {
|
||
systemId: this.features.application.getApplication().systemId,
|
||
}
|
||
});
|
||
}
|
||
},
|
||
executeMp() {
|
||
return this.execute();
|
||
},
|
||
resetMp() {
|
||
return this.clean();
|
||
},
|
||
closeMp() {
|
||
this.setState({
|
||
showCloseConfirmMp: true,
|
||
});
|
||
},
|
||
cancelCloseMp() {
|
||
this.setState({
|
||
showCloseConfirmMp: false,
|
||
});
|
||
},
|
||
async confirmCloseMp() {
|
||
await this.execute('close');
|
||
this.cancelCloseMp();
|
||
const { onClose } = this.props;
|
||
onClose && onClose();
|
||
},
|
||
goBack() {
|
||
this.navigateBack();
|
||
},
|
||
async startPay() {
|
||
const { onPaid, onClose, onPayFailure } = this.props;
|
||
const { pay } = this.state;
|
||
try {
|
||
await PayRoutineDict[pay.entity].routine(pay, this.features);
|
||
onPaid && onPaid();
|
||
}
|
||
catch (err) {
|
||
if (this.props.closeWhenFailure) {
|
||
await this.execute(undefined, undefined, undefined, [
|
||
{
|
||
entity: 'pay',
|
||
operation: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'close',
|
||
data: {},
|
||
filter: {
|
||
id: this.props.oakId,
|
||
},
|
||
},
|
||
}
|
||
]);
|
||
onClose && onClose();
|
||
}
|
||
else {
|
||
onPayFailure && onPayFailure();
|
||
}
|
||
this.features.message.setMessage({
|
||
type: 'warning',
|
||
content: err.message,
|
||
});
|
||
}
|
||
},
|
||
openChannelSelectMp() {
|
||
this.setState({
|
||
showChannelSelectMp: true,
|
||
});
|
||
},
|
||
closeChannelSelectMp() {
|
||
this.setState({
|
||
showChannelSelectMp: false,
|
||
});
|
||
},
|
||
async updateOfflineIdMp(touch) {
|
||
const { detail } = touch;
|
||
const { currentKey } = detail;
|
||
const { oakId } = this.props;
|
||
if (currentKey) {
|
||
await this.execute(undefined, undefined, undefined, [
|
||
{
|
||
entity: 'pay',
|
||
operation: {
|
||
id: await (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'update',
|
||
data: {
|
||
entity: 'offlineAccount',
|
||
entityId: currentKey,
|
||
},
|
||
filter: {
|
||
id: oakId,
|
||
},
|
||
}
|
||
}
|
||
]);
|
||
this.setState({
|
||
showChannelSelectMp: false,
|
||
});
|
||
}
|
||
},
|
||
updateExternalIdMp(input) {
|
||
const { detail } = input;
|
||
this.update({
|
||
externalId: detail.value,
|
||
});
|
||
},
|
||
clearExternalIdMp() {
|
||
this.update({
|
||
externalId: null,
|
||
});
|
||
}
|
||
},
|
||
lifetimes: {
|
||
async ready() {
|
||
const { oakId } = this.props;
|
||
(0, assert_1.default)(typeof oakId === 'string');
|
||
const unsub = await this.subDataEvents([`${constants_1.DATA_SUBSCRIBER_KEYS.payStateChanged}-${oakId}`]);
|
||
this.setState({
|
||
unsub,
|
||
});
|
||
},
|
||
detached() {
|
||
const { unsub } = this.state;
|
||
unsub && unsub();
|
||
}
|
||
},
|
||
listeners: {
|
||
startPayable(prev, next) {
|
||
if (next.startPayable && !this.props.disableAutoPay) {
|
||
this.startPay();
|
||
}
|
||
},
|
||
pay(prev, next) {
|
||
if (!prev.pay && next.pay) {
|
||
this.refreshOfflineAccounts();
|
||
}
|
||
}
|
||
},
|
||
});
|