pay.js
5.43 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
/**
* 订单支付
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/07/18
*/
'use strict';
const api = require('./pay-api');
const Promise = require('bluebird');
const co = Promise.coroutine;
const camelCase = global.yoho.camelCase;
const OrderData = require('./order');
const logger = global.yoho.logger;
const _ = require('lodash');
/**
* 获取在线支付方式
* @returns {*|Promise.<TResult>}
*/
const getOnlinePayProvider = () => {
return api.getPayProvider().then(result => {
if (!(result.code && result.code === 200)) {
return [];
}
let online = _(result.data)
.filter(i => _.includes(['Alipay'], i.payCode))
.map(i => {
return {
id: i.id,
name: i.payName,
img: i.payIcon,
selected: i.payCode === 'Alipay'
};
})
.value();
let nav = [
{
id: 'onlinePay',
name: '支付宝等平台',
selected: true
}
];
return {
type: nav,
onlinePay: online
};
});
};
/**
* 获取订单详情
* @param uid
* @param code
*/
const getOrderInfo = (uid, code) => {
return co(function *() {
let orderData = yield OrderData.orderDetail(uid, code);
if (orderData && orderData.data) {
return camelCase(orderData.data);
} else {
return {};
}
})();
};
/**
* 获取支付页数据
* @param uid
* @param code
*/
const getPayInfo = (uid, code) => {
return co(function *() {
let payment = yield Promise.all([getOnlinePayProvider(), getOrderInfo(uid, code)]);
return {
pay: payment[0],
order: payment[1]
};
})();
};
/**
* 获取支付方式的相关参数, (密钥等信息)
* @param id
*/
const getPaymentInfo = (id) => {
return co(function *() {
let data = yield api.getPaymentInfo(id);
if (data && data.code === 200 && data.data) {
return camelCase(data.data);
}
return {};
})();
};
/**
* 获取订单支付银行信息
* @param id
*/
const getBankByOrder = (id) => {
return co(function *() {
let data = yield api.getBankByOrder(id);
if (data && data.code === 200 && data.data) {
return camelCase(data.data);
}
return {};
})();
};
/**
* 设置订单支付银行
* @param code
* @param payment
* @param bankCode
*/
const setOrderPayBank = (code, payment, bankCode) => {
return co(function *() {
let data = yield api.setOrderPayBank(code, payment, bankCode);
return data;
})();
};
/**
* 更新订单支付银行
* @param code
* @param payment
* @param bankCode
*/
const updateOrderPayBank = (code, payment, bankCode) => {
return co(function *() {
let data = yield api.updateOrderPayBank(code, payment, bankCode);
return data;
})();
};
/**
* 支付确认
* @param code
* @param payment
* @param uid
*/
const sendPayConfirm = (code, payment, uid) => {
return co(function *() {
let data = yield api.sendPayConfirm(code, payment, uid);
return data;
})();
};
/**
* 支付成功,前端回调时,处理订单信息
* @param payResult
* @param uid
*/
const procOrderData = (payResult, uid) => {
return co(function *() {
let orderCode = payResult.orderCode;
let result = {code: 400, message: ''};
if (!orderCode) {
result.message = '未查到订单信息,订单状态更新失败!';
return result;
} else {
let orderInfo = yield OrderData.orderDetail(uid, orderCode);
if (orderInfo && orderInfo.data) {
let order = camelCase(orderInfo.data);
let amount = order.paymentAmount;
if (order.is_cancel === 'Y') {
logger.warn('front pay success but order is cancel.', {payResult: payResult, order: order});
api.sendMessage(order.mobile, 'error_sms', '支付成功,但订单已取消,订单号为' + orderCode);
return {code: 417, message: '支付成功,但订单已取消,需联系客服!'};
}
if (order.payment_status === 'N') {
logger.warn('front pay success but may be notify fail');
}
if (_.round(parseFloat(amount), 2) !== _.round(parseFloat(payResult.totalFee), 2)) {
logger.warn('front pay success but the amount is not same.', {payResult: payResult, order: order});
return {
code: 415,
message: '支付金额与订单金额不一致,订单状态更新失败!'
};
}
return {
code: 200,
message: '支付成功,请等待发货',
data: {
pay: amount,
orderCode: orderCode
}
};
} else {
result.message = '未查到订单信息,订单状态更新失败!';
}
}
return result;
})();
};
module.exports = {
getPayInfo,
getPaymentInfo,
getBankByOrder,
setOrderPayBank,
updateOrderPayBank,
sendPayConfirm,
procOrderData
};