payment.js
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* 各种支付的入口
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/7/22
*/
'use strict';
const PayData = require('../models/pay');
const OrderData = require('../models/order');
const Alipay = require('./pay/alipay');
const Promise = require('bluebird');
const co = Promise.coroutine;
const logger = global.yoho.logger;
const Payment = {
pay(user, order, method) {
return co(function*() {
let payInfo = yield PayData.getPaymentInfo(method);
let result = {
code: 400,
message: '获取支付方式信息失败'
};
if (payInfo && payInfo.id === method) {
if (method === 2) {
result = Alipay.pay(user, order, payInfo);
}
}
if (result.code === 200) {
let updateInfo = yield Payment.beforePay(user, order, method);
if (updateInfo && updateInfo.code !== 200) {
return updateInfo;
}
}
return result;
})();
},
beforePay(user, order, method) {
return Promise.all([
OrderData.updateOrderPayment(order.orderCode, method, user.uid),
PayData.getBankByOrder(order.orderCode)
]).then(result => {
let paymentRecord = result[0];
let bankRecord = result[1];
let bankCode = ''; // 暂时写成'', 参考php代码 Payment.php:564
if (!paymentRecord || paymentRecord.code !== 200) {
let message = paymentRecord && paymentRecord.message ? paymentRecord.message : '系统繁忙,请稍后再试';
return {code: 400, message: message};
}
if (bankRecord && bankRecord.bankCode) {
return PayData.updateOrderPayBank(order.orderCode, method, bankCode);
} else {
return PayData.setOrderPayBank(order.orderCode, method, bankCode);
}
}).catch(e => {
logger.error('update order pay info error.', e);
return Promise.resolve({
code: 400,
message: '更新订单支付信息失败'
});
});
},
afterPay(query, payId, user) {
return co(function*() {
let payInfo = yield PayData.getPaymentInfo(payId);
let payResult = {};
if (payId === 2) {
payResult = Alipay.notify(query, payInfo);
payResult.bankName = payResult.bankName || payInfo.payName || '';
payResult.bankCode = payResult.bankCode || payInfo.pay_code || '';
}
console.log(payResult);
if (payResult && payResult.payResult === 200) {
if (payResult.orderCode) {
logger.info('pay back confirm');
yield PayData.sendPayConfirm(payResult.orderCode, payId, user.uid);
}
return yield PayData.procOrderData(payResult, user.uid);
} else {
return {
code: 500,
message: '支付失败'
};
}
})();
}
};
module.exports = Payment;