payment.js
9.21 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* 各种支付的入口
*
* @author: xiaoxiao.hao<xiaoxiao.hao@yoho.cn>
* @date: 16/7/22
*/
'use strict';
const Promise = require('bluebird');
const PayData = require('../models/pay');
const Alipay = require('./pay/alipay');
const Wechat = require('./pay/wechat');
const Unionpay = require('./pay/unionpay');
const Alibank = require('./pay/alibank');
const Tenpay = require('./pay/tenpay');
const Chinabank = require('./pay/chinabank');
const common = require('./pay/common');
const co = Promise.coroutine;
const logger = global.yoho.logger;
const md5 = require('md5');
const paySign = require('./pay/sign');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const Payment = {
pay(user, order, payType, protocol) {
return co(function*() {
let result = {
code: 400,
message: '获取支付方式信息失败'
};
let paymentPars = payType.split('_');
let payInfo;
let bankCode = '';
if (paymentPars.length !== 2) {
return result;
}
if (!order.order_code) {
result.message = '没有找到该订单';
return result;
}
if (order.is_cancel && order.is_cancel === 'Y') {
result.message = '该订单已经取消';
return result;
}
if (order.pay_expire && common.getPayExpireMin(order.pay_expire) <= 0) {
result.message = '当前订单不可支付';// 该订单已超过2个小时
return result;
}
let method = parseInt(paymentPars[0], 10);
if (method === PayData.payments.wechat) {
// 如果是微信支付,不需要调用获取支付方式详情接口
result = yield Wechat.pay(user, order, {id: method});
} else if (method === PayData.payments.unionpay) {
// 不需要调用获取支付方式详情接口
result = yield Unionpay.pay(user, order, {id: method, protocol: protocol});
} else {
payInfo = yield PayData.getPaymentInfo(method);
if (!payInfo.payParams) {
return result;
}
switch (payInfo.id) {
case PayData.payments.alipay:
result = Alipay.pay(user, order, payInfo, protocol);
break;
case PayData.payments.tenpay:
result = Tenpay.pay(user, order, payInfo, protocol);
break;
case PayData.payments.chinabank:
result = Chinabank.pay(user, order, payInfo, protocol);
break;
case PayData.payments.alibank:
bankCode = paymentPars[1];
payInfo.bankCode = bankCode;// 设置默认银行
result = Alibank.pay(user, order, payInfo, protocol);
break;
default:
break;
}
}
logger.info(`pay to url, params = ${JSON.stringify(result)}`);
if (result.code === 200) {
let updateInfo = yield Payment.beforePay(user, order, method, bankCode);
if (updateInfo && updateInfo.code !== 200) {
return updateInfo;
}
}
return result;
})();
},
beforePay(user, order, method, bankCode) {
return Promise.all([
PayData.updateOrderPayment(order.order_code, method, user.uid),
PayData.getBankByOrder(order.order_code)
]).then(result => {
let paymentRecord = result[0];
let bankRecord = result[1];
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.order_code, method, bankCode);
} else {
return PayData.setOrderPayBank(order.order_code, method, bankCode);
}
}).catch(e => {
logger.error('update order pay info error.', e);
return Promise.resolve({
code: 400,
message: '更新订单支付信息失败'
});
});
},
_validate(query, payId, user) {
return co(function*() {
let payInfo = yield PayData.getPaymentInfo(payId);
let payResult = {};
let payData = {};
let payName = '';
let isPcpayNotify = false;// 回调地址,java未做,走前端逻辑
if (payId === PayData.payments.alipay) {
payResult = Alipay.notify(query, payInfo);
} else if (payId === PayData.payments.alibank) {
payResult = Alibank.notify(query, payInfo);
} else if (payId === PayData.payments.unionpay) {
payResult = Unionpay.notify(query);
} else if (payId === PayData.payments.tenpay) {
payResult = Tenpay.notify(query, payInfo);
isPcpayNotify = true;
} else if (payId === PayData.payments.chinabank) {
payResult = Chinabank.notify(query, payInfo);
isPcpayNotify = true;
}
payResult.bankName = payName = (payResult.bankName || payInfo.payName || '');
payResult.bankCode = (payResult.bankCode || '');
// 记录日志
logger.info(`\r\n\r\n pay back confirm,req = ${JSON.stringify({
query: query,
payId: payId,
user: user,
payResult: payResult
})}`);
if (payResult && payResult.payResult === 200) {
if (payResult.orderCode) {
logger.info('pay back confirm');
PayData.sendPayConfirm(payResult.orderCode, payId, user.uid);
}
payData = yield PayData.procOrderData(payResult, user.uid, payId);
if (isPcpayNotify && payData.code === 200 && payData.data.order.payment_status === 'N') {
// 更新订单状态
yield PayData.pcpayNotify(Object.assign({payId: payId}, payResult));
}
} else {
payData = {
code: 500,
message: '支付失败'
};
}
payData.payName = payName;
return payData;
})();
},
afterPay(query, payId, user) {
let _this = this;
return co(function*() {
let payData = {code: 500, message: '支付失败'};
let sign = query.sign || '';
switch (payId) {
case PayData.payments.wechat: // 微信支付不须要验证,但前端必须校验sign
delete query.sign;
if (md5(paySign.raw(Object.assign({tradeStatus: 'Y'}, query))) === sign) {
payData = yield PayData.procOrderData(query, user.uid, payId);
}
payData.payName = '微信';
break;
default:// 须要验证的支付方式
payData = _this._validate(query, payId, user);
break;
}
return payData;
})();
},
getWxSign(uid, code) {
return PayData.orderDetail(uid, code).then(result => {
let data = {};
result = (result.code === 200 ? result.data : {});
if (result.is_cancel === 'Y') {
return {
code: 401,
message: '订单已取消'
};
}
if (result.payment_status === 'Y') {
let payParams = {
orderCode: code,
tradeStatus: 'Y',
totalFee: _.round(parseFloat(result.payment_amount), 2)
};
data = {
code: 200,
data: {
href: helpers.urlFormat('/shopping/newpay/callback/wechat', Object.assign(payParams, {
sign: md5(paySign.raw(payParams))
}))
}
};
}
return data;
});
},
// 异步通知地址
notifyPay(query, payId) {
return co(function*() {
let d = '';
let payInfo = yield PayData.getPaymentInfo(payId);
if (payId === PayData.payments.tenpay) {
let payResult = Tenpay.notify(query, payInfo);
if (payResult.payResult === 200) {
// 更新订单状态
yield PayData.pcpayNotify(Object.assign({payId: payId}, payResult));
return 'success';
}
return 'fail';
}
return d;
})();
}
};
module.exports = Payment;