oak-pay-business/es/utils/pay.js

86 lines
2.0 KiB
JavaScript

import { getPayClazz } from './payClazz';
import assert from 'assert';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
export const fullPayProjection = {
id: 1,
applicationId: 1,
price: 1,
meta: 1,
iState: 1,
depositId: 1,
deposit: {
id: 1,
price: 1,
iState: 1,
accountId: 1,
},
entity: 1,
entityId: 1,
paid: 1,
refunded: 1,
timeoutAt: 1,
forbidRefundAt: 1,
orderId: 1,
order: {
id: 1,
title: 1,
desc: 1,
},
};
export async function payNotify(context, body, payId, headers) {
const [pay] = await context.select('pay', {
data: {
id: 1,
price: 1,
iState: 1,
entity: 1,
entityId: 1,
meta: 1,
applicationId: 1,
},
filter: {
id: payId,
}
}, {});
if (!pay) {
console.error('接受到无效的payId', payId);
return;
}
const { applicationId, entity, entityId, price } = pay;
const payClazz = await getPayClazz(entity, entityId, context);
const { payId: payId2, iState, extra } = await payClazz.decodePayNotification({
headers,
}, body);
assert(payId2 === payId);
if (iState !== pay.iState) {
let action = 'close';
const updateData = {};
switch (iState) {
case 'closed': {
// action = 'close';
break;
}
case 'paid': {
action = 'succeedPaying';
updateData.paid = price;
if (extra) {
Object.assign(updateData, extra);
}
break;
}
default: {
assert(false);
}
}
await context.operate('pay', {
id: await generateNewIdAsync(),
action,
data: updateData,
filter: {
id: payId,
}
}, {});
}
return;
}