payment.js
9.98 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* 各种支付的入口
*
* @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');
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
pay(user, order, payType, protocol) {
let that = this;
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.attribute === 9 || +order.attribute === 11) {
result.message = '定金预售商品只能在APP端操作';
return result;
}
if (order.pay_expire && common.getPayExpireMin(order.pay_expire) <= 0) {
result.message = '当前订单不可支付';// 该订单已超过2个小时
return result;
}
let method = parseInt(paymentPars[0], 10);
let payModel = new PayData(that.ctx);
if (method === payModel.payments.wechat) {
// 如果是微信支付,不需要调用获取支付方式详情接口
result = yield new Wechat(that.ctx).pay(user, order, {id: method});
} else if (method === payModel.payments.unionpay) {
// 不需要调用获取支付方式详情接口
result = yield new Unionpay(that.ctx).pay(user, order, {id: method, protocol: protocol});
} else {
payInfo = yield payModel.getPaymentInfo(method);
if (!payInfo.payParams) {
return result;
}
switch (payInfo.id) {
case payModel.payments.alipay:
result = new Alipay(that.ctx).pay(user, order, payInfo, protocol);
break;
case payModel.payments.tenpay:
result = new Tenpay(that.ctx).pay(user, order, payInfo, protocol);
break;
case payModel.payments.chinabank:
result = new Chinabank(that.ctx).pay(user, order, payInfo, protocol);
break;
case payModel.payments.alibank:
bankCode = paymentPars[1];
payInfo.bankCode = bankCode;// 设置默认银行
result = new Alibank(that.ctx).pay(user, order, payInfo, protocol);
break;
default:
break;
}
}
logger.info(`pay to url, params = ${JSON.stringify(result)}`);
if (result.code === 200) {
let updateInfo = yield that.beforePay(user, order, method, bankCode);
if (updateInfo && updateInfo.code !== 200) {
return updateInfo;
}
}
return result;
})();
}
beforePay(user, order, method, bankCode) {
let payModel = new PayData(this.ctx);
return Promise.all([
payModel.updateOrderPayment(order.order_code, method, user.uid),
payModel.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 payModel.updateOrderPayBank(order.order_code, method, bankCode);
} else {
return payModel.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) {
let payModel = new PayData(this.ctx);
let that = this;
return co(function*() {
let payInfo = yield payModel.getPaymentInfo(payId);
let payResult = {};
let lpayResult = {};
let payName = '';
let isPcpayNotify = false;// 回调地址,java未做,走前端逻辑
if (payId === payModel.payments.alipay) {
payResult = new Alipay(that.ctx).notify(query, payInfo);
} else if (payId === payModel.payments.alibank) {
payResult = new Alibank(that.ctx).notify(query, payInfo);
} else if (payId === payModel.payments.unionpay) {
payResult = new Unionpay(that.ctx).notify(query);
} else if (payId === payModel.payments.tenpay) {
payResult = new Tenpay(that.ctx).notify(query, payInfo);
isPcpayNotify = true;
} else if (payId === payModel.payments.chinabank) {
payResult = new Chinabank(that.ctx).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');
payModel.sendPayConfirm(payResult.orderCode, payId, user.uid);
}
lpayResult = yield payModel.procOrderData(payResult, user.uid, payId);
if (isPcpayNotify && lpayResult.code === 200 && lpayResult.data.order.payment_status === 'N') {
// 更新订单状态
yield payModel.pcpayNotify(Object.assign({payId: payId}, payResult));
}
} else {
lpayResult = {
code: 500,
message: '支付失败'
};
}
lpayResult.payName = payName;
return lpayResult;
})();
}
afterPay(query, payId, user) {
let _this = this;
let payModel = new PayData(this.ctx);
return co(function*() {
let payResult = {code: 500, message: '支付失败'};
let sign = query.sign || '';
switch (payId) {
case payModel.payments.wechat: // 微信支付不须要验证,但前端必须校验sign
delete query.sign;
if (md5(paySign.raw(Object.assign({tradeStatus: 'Y'}, query))) === sign) {
payResult = yield payModel.procOrderData(query, user.uid, payId);
}
payResult.payName = '微信';
break;
default:// 须要验证的支付方式
payResult = _this._validate(query, payId, user);
break;
}
return payResult;
})();
}
getWxSign(uid, code) {
let payModel = new PayData(this.ctx);
return payModel.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) {
let payModel = new PayData(this.ctx);
return co(function*() {
let d = '';
let payInfo = yield payModel.getPaymentInfo(payId);
if (payId === payModel.payments.tenpay) {
let payResult = Tenpay.notify(query, payInfo);
if (payResult.payResult === 200) {
// 更新订单状态
yield payModel.pcpayNotify(Object.assign({payId: payId}, payResult));
return 'success';
}
return 'fail';
}
return d;
})();
}
};