Authored by 郭成尧

pay-ctx

@@ -6,74 +6,77 @@ @@ -6,74 +6,77 @@
6 6
7 'use strict'; 7 'use strict';
8 8
9 -const api = global.yoho.API;  
10 const serviceAPI = global.yoho.ServiceAPI; 9 const serviceAPI = global.yoho.ServiceAPI;
11 const utils = '../../../utils'; 10 const utils = '../../../utils';
12 const productProcess = require(`${utils}/product-process`); 11 const productProcess = require(`${utils}/product-process`);
13 const ApipayConfig = global.yoho.config.alipayConfig; 12 const ApipayConfig = global.yoho.config.alipayConfig;
14 const md5 = require('yoho-md5'); 13 const md5 = require('yoho-md5');
15 -const payApi = require('./pay-api');  
16 -const helpers = global.yoho.helpers; 14 +const payApiModel = require('./pay-api');
17 const _ = require('lodash'); 15 const _ = require('lodash');
18 const co = require('bluebird').coroutine; 16 const co = require('bluebird').coroutine;
19 const logger = global.yoho.logger; 17 const logger = global.yoho.logger;
20 const stringProcess = require(`${global.utils}/string-process`); 18 const stringProcess = require(`${global.utils}/string-process`);
21 19
22 -// 支付方式  
23 -const payments = {  
24 - alipay: 18,  
25 - wechat: 22  
26 -}; 20 +class payModel extends global.yoho.BaseModel {
  21 + constructor(ctx) {
  22 + super(ctx);
  23 + this.ctx = _.get(ctx, 'req.ctx');
  24 + }
27 25
28 -// 资源位  
29 -const _getBanner = (param) => {  
30 - return serviceAPI.get('operations/api/v5/resource/get', { 26 + // 资源位
  27 + _getBanner(param) {
  28 + return this.get({
  29 + api: serviceAPI,
  30 + url: 'operations/api/v5/resource/get',
  31 + data: {
31 content_code: param.contentCode, 32 content_code: param.contentCode,
32 platform: 'iphone' 33 platform: 'iphone'
33 - }, { code: 200 }).then((result) => { 34 + },
  35 + param: { code: 200 }
  36 + }).then((result) => {
34 result = result.data; 37 result = result.data;
35 38
36 return result; 39 return result;
37 }); 40 });
38 -}; 41 + }
39 42
40 -// 购买此商品的用户也购买了  
41 -const _getOthersBuy2 = (param) => {  
42 - return api.get('', { 43 + // 购买此商品的用户也购买了
  44 + _getOthersBuy2(param) {
  45 + return this.get({data: {
43 method: 'app.recommend.purchased', 46 method: 'app.recommend.purchased',
44 productSkn: param.skn, 47 productSkn: param.skn,
45 udid: param.uid, 48 udid: param.uid,
46 rec_pos: '100005', 49 rec_pos: '100005',
47 limit: 2, 50 limit: 2,
48 client_id: param.client_id 51 client_id: param.client_id
49 - }, { code: 200 }).then((result) => { 52 + }, param: { code: 200 }}).then((result) => {
50 if (result && result.data && result.data.product_list) { 53 if (result && result.data && result.data.product_list) {
51 return productProcess.processProductList(result.data.product_list); 54 return productProcess.processProductList(result.data.product_list);
52 } 55 }
53 56
54 }); 57 });
55 -}; 58 + }
56 59
57 -// 订单信息  
58 -const _getOtherDetail = (param) => { 60 + // 订单信息
  61 + getOtherDetail(param) {
59 if (!param.uid || !param.orderCode || !stringProcess.isNumeric(param.orderCode)) { 62 if (!param.uid || !param.orderCode || !stringProcess.isNumeric(param.orderCode)) {
60 return Promise.resolve({}); 63 return Promise.resolve({});
61 } 64 }
62 65
63 - return api.get('', { 66 + return this.get({data: {
64 method: 'app.SpaceOrders.detail', 67 method: 'app.SpaceOrders.detail',
65 uid: param.uid, 68 uid: param.uid,
66 order_code: param.orderCode, 69 order_code: param.orderCode,
67 session_key: param.sessionKey 70 session_key: param.sessionKey
68 - }, { code: 200 }).then(result => { 71 + }, param: { code: 200 }}).then(result => {
69 return result; 72 return result;
70 }); 73 });
71 -}; 74 + }
72 75
73 -// 购买此商品的用户也购买了,要先从订单详情获取商品skn  
74 -const _getOthersBuy = (param) => {  
75 - return api.all([  
76 - _getOtherDetail(param) 76 + // 购买此商品的用户也购买了,要先从订单详情获取商品skn
  77 + _getOthersBuy(param) {
  78 + return Promise.all([
  79 + this.getOtherDetail(param)
77 ]).then((result) => { 80 ]).then((result) => {
78 81
79 let goodSkn = ''; 82 let goodSkn = '';
@@ -82,109 +85,121 @@ const _getOthersBuy = (param) => { @@ -82,109 +85,121 @@ const _getOthersBuy = (param) => {
82 goodSkn = result[0].data.order_goods[0].product_skn; 85 goodSkn = result[0].data.order_goods[0].product_skn;
83 } 86 }
84 87
85 - return _getOthersBuy2(Object.assign(param, { skn: goodSkn })); 88 + return this._getOthersBuy2(Object.assign(param, { skn: goodSkn }));
86 89
87 }).then((result) => { 90 }).then((result) => {
88 91
89 return result; 92 return result;
90 }); 93 });
91 -}; 94 + }
92 95
93 -/** 96 + /**
94 * 获取订单支付银行信息 97 * 获取订单支付银行信息
95 * @param id 98 * @param id
96 */ 99 */
97 -const getBankByOrder = (id) => { 100 + getBankByOrder(id) {
  101 + let self = this;
  102 +
98 return co(function* () { 103 return co(function* () {
99 - let result = yield payApi.getBankByOrder(id); 104 + let result = yield self.ctx(payApiModel).getBankByOrder(id);
100 105
101 if (result && result.code === 200 && result.data) { 106 if (result && result.code === 200 && result.data) {
102 return result.data; 107 return result.data;
103 } 108 }
104 return {}; 109 return {};
105 })(); 110 })();
106 -}; 111 + }
107 112
108 -/** 113 + /**
109 * 设置订单支付银行 114 * 设置订单支付银行
110 * @param code 115 * @param code
111 * @param payment 116 * @param payment
112 * @param bankCode 117 * @param bankCode
113 */ 118 */
114 -const setOrderPayBank = (code, payment, bankCode) => { 119 + setOrderPayBank(code, payment, bankCode) {
  120 + let self = this;
  121 +
115 return co(function* () { 122 return co(function* () {
116 - let data = yield payApi.setOrderPayBank(code, payment, bankCode); 123 + let data = yield self.ctx(payApiModel).setOrderPayBank(code, payment, bankCode);
117 124
118 return data; 125 return data;
119 })(); 126 })();
120 -}; 127 + }
121 128
122 -/** 129 + /**
123 * 获取支付方式的相关参数, (密钥等信息) 130 * 获取支付方式的相关参数, (密钥等信息)
124 * @param id 131 * @param id
125 */ 132 */
126 -const getPaymentInfo = (id) => { 133 + getPaymentInfo(id) {
  134 + let self = this;
  135 +
127 return co(function* () { 136 return co(function* () {
128 - let result = yield payApi.getPaymentInfo(id); 137 + let result = yield self.ctx(payApiModel).getPaymentInfo(id);
129 138
130 if (result && result.code === 200 && result.data) { 139 if (result && result.code === 200 && result.data) {
131 return result.data; 140 return result.data;
132 } 141 }
133 return {}; 142 return {};
134 })(); 143 })();
135 -}; 144 + }
136 145
137 -/** 146 + /**
138 * 支付确认 147 * 支付确认
139 * @param code 148 * @param code
140 * @param payment 149 * @param payment
141 * @param uid 150 * @param uid
142 */ 151 */
143 -const sendPayConfirm = (code, payment, uid) => { 152 + sendPayConfirm(code, payment, uid) {
  153 + let self = this;
  154 +
144 return co(function* () { 155 return co(function* () {
145 - let data = yield payApi.sendPayConfirm(code, payment, uid); 156 + let data = yield self.ctx(payApiModel).sendPayConfirm(code, payment, uid);
146 157
147 return data; 158 return data;
148 })(); 159 })();
149 -}; 160 + }
150 161
151 -/** 162 + /**
152 * 更新订单支付方式 163 * 更新订单支付方式
153 * @param code 164 * @param code
154 * @param payment 165 * @param payment
155 * @param uid 166 * @param uid
156 * @returns {*} 167 * @returns {*}
157 */ 168 */
158 -const updateOrderPayment = (code, payment, uid) => {  
159 - return api.get('', { 169 + updateOrderPayment(code, payment, uid) {
  170 + return this.get({data: {
160 method: 'app.SpaceOrders.updateOrdersPaymentByCode', 171 method: 'app.SpaceOrders.updateOrdersPaymentByCode',
161 order_code: code, 172 order_code: code,
162 payment: payment, 173 payment: payment,
163 uid: uid 174 uid: uid
164 - });  
165 -}; 175 + }});
  176 + }
166 177
167 -/** 178 + /**
168 * 更新订单支付银行 179 * 更新订单支付银行
169 * @param code 180 * @param code
170 * @param payment 181 * @param payment
171 * @param bankCode 182 * @param bankCode
172 */ 183 */
173 -const updateOrderPayBank = (code, payment, bankCode) => { 184 + updateOrderPayBank(code, payment, bankCode) {
  185 + let self = this;
  186 +
174 return co(function* () { 187 return co(function* () {
175 - let data = yield payApi.updateOrderPayBank(code, payment, bankCode); 188 + let data = yield self.ctx(payApiModel).updateOrderPayBank(code, payment, bankCode);
176 189
177 return data; 190 return data;
178 })(); 191 })();
179 -}; 192 + }
180 193
181 -/** 194 + /**
182 * 支付成功,前端回调时,处理订单信息 195 * 支付成功,前端回调时,处理订单信息
183 * @param payResult 196 * @param payResult
184 * @param uid 197 * @param uid
185 * @param sessionKey 198 * @param sessionKey
186 */ 199 */
187 -const procOrderData = (payResult, uid, sessionKey) => { 200 + procOrderData(payResult, uid, sessionKey) {
  201 + let self = this;
  202 +
188 return co(function* () { 203 return co(function* () {
189 let orderCode = payResult.orderCode; 204 let orderCode = payResult.orderCode;
190 let result = { code: 400, message: '' }; 205 let result = { code: 400, message: '' };
@@ -193,7 +208,7 @@ const procOrderData = (payResult, uid, sessionKey) => { @@ -193,7 +208,7 @@ const procOrderData = (payResult, uid, sessionKey) => {
193 result.message = '未查到订单信息,订单状态更新失败!'; 208 result.message = '未查到订单信息,订单状态更新失败!';
194 return result; 209 return result;
195 } else { 210 } else {
196 - let orderInfo = yield _getOtherDetail({ 211 + let orderInfo = yield self._getOtherDetail({
197 uid: uid, 212 uid: uid,
198 orderCode: orderCode, 213 orderCode: orderCode,
199 sessionKey: sessionKey 214 sessionKey: sessionKey
@@ -206,7 +221,7 @@ const procOrderData = (payResult, uid, sessionKey) => { @@ -206,7 +221,7 @@ const procOrderData = (payResult, uid, sessionKey) => {
206 if (order.is_cancel === 'Y') { 221 if (order.is_cancel === 'Y') {
207 logger.warn('front pay success but order is cancel.', { payResult: payResult, order: order }); 222 logger.warn('front pay success but order is cancel.', { payResult: payResult, order: order });
208 223
209 - payApi.sendMessage(order.mobile, 'error_sms', '支付成功,但订单已取消,订单号为' + orderCode); 224 + self.ctx(payApiModel).sendMessage(order.mobile, 'error_sms', '支付成功,但订单已取消,订单号为' + orderCode);
210 return { code: 417, message: '支付成功,但订单已取消,需联系客服!' }; 225 return { code: 417, message: '支付成功,但订单已取消,需联系客服!' };
211 } 226 }
212 227
@@ -239,78 +254,29 @@ const procOrderData = (payResult, uid, sessionKey) => { @@ -239,78 +254,29 @@ const procOrderData = (payResult, uid, sessionKey) => {
239 254
240 return result; 255 return result;
241 })(); 256 })();
242 -};  
243 -  
244 -/**  
245 - * 支付相关的数据处理函数  
246 - */  
247 -const payTool = {  
248 -  
249 - /**  
250 - * 支持的支付方式列表  
251 - * @returns {[*,*]}  
252 - */  
253 - payAppInfo(orderCode) {  
254 - return [{  
255 - appIcon: '',  
256 - payLink: helpers.urlFormat('/cart/index/new/pay', {  
257 - payment: payments.alipay + '_platform',  
258 - order_code: orderCode  
259 - }),  
260 - appId: 'alipay',  
261 - app: '支付宝支付',  
262 - hint: '支付宝钱包支付',  
263 - subHint: '推荐支付宝用户使用'  
264 - }, {  
265 - appIcon: '',  
266 - payLink: '',  
267 - appId: 'weixin',  
268 - app: '微信支付',  
269 - hint: '推荐使用',  
270 - subHint: ''  
271 - }];  
272 - },  
273 -  
274 - /**  
275 - * 计算购买商品总数量  
276 - * @param goodsArray  
277 - * @returns {number}  
278 - * @private  
279 - */  
280 - calBuyNumCount(goodsArray) {  
281 - let buyNumCount = 0;  
282 -  
283 - if (_.isArray(goodsArray)) {  
284 - _.forEach(goodsArray, value => {  
285 - buyNumCount = value.buy_number ? parseInt(value.buy_number, 10) : 0;  
286 - });  
287 - }  
288 -  
289 - return buyNumCount;  
290 } 257 }
291 -};  
292 258
293 -/** 259 + /**
294 * 支付中心 260 * 支付中心
295 * @param params 261 * @param params
296 */ 262 */
297 -const payCenter = (params) => { 263 + payCenter(params) {
298 264
299 - return _getOtherDetail({ 265 + return this.getOtherDetail({
300 uid: params.uid, 266 uid: params.uid,
301 orderCode: params.orderCode, 267 orderCode: params.orderCode,
302 sessionKey: params.sessionKey 268 sessionKey: params.sessionKey
303 }).then(result => { 269 }).then(result => {
304 return _.get(result, 'data', {}); 270 return _.get(result, 'data', {});
305 }); 271 });
306 -};  
307 -  
308 -// 货到付款  
309 -const getPayCod = (param) => {  
310 - return api.all([  
311 - _getBanner(param),  
312 - _getOthersBuy(param),  
313 - _getOtherDetail(param) 272 + }
  273 +
  274 + // 货到付款
  275 + getPayCod(param) {
  276 + return Promise.all([
  277 + this._getBanner(param),
  278 + this._getOthersBuy(param),
  279 + this.getOtherDetail(param)
314 ]).then((result) => { 280 ]).then((result) => {
315 281
316 let resu = { 282 let resu = {
@@ -346,9 +312,9 @@ const getPayCod = (param) => { @@ -346,9 +312,9 @@ const getPayCod = (param) => {
346 312
347 return resu; 313 return resu;
348 }); 314 });
349 -}; 315 + }
350 316
351 -const _raw = (args) => { 317 + _raw(args) {
352 let keys = Object.keys(args); 318 let keys = Object.keys(args);
353 319
354 keys = keys.filter(k => { 320 keys = keys.filter(k => {
@@ -365,34 +331,33 @@ const _raw = (args) => { @@ -365,34 +331,33 @@ const _raw = (args) => {
365 return keys.map(k => { 331 return keys.map(k => {
366 return k + '=' + decodeURI(args[k]); 332 return k + '=' + decodeURI(args[k]);
367 }).join('&'); 333 }).join('&');
368 -}; 334 + }
369 335
370 -/** 336 + /**
371 * 验证返回结果的正确性 337 * 验证返回结果的正确性
372 */ 338 */
373 -const _checkResponse = (params) => { 339 + _checkResponse(params) {
374 if (!params.sign) { 340 if (!params.sign) {
375 return false; 341 return false;
376 } 342 }
377 - let rawResult = _raw(params); 343 + let rawResult = this._raw(params);
378 let sign = rawResult + ApipayConfig.alipayKey; 344 let sign = rawResult + ApipayConfig.alipayKey;
379 let md5Result = md5(sign); 345 let md5Result = md5(sign);
380 346
381 return md5Result === params.sign; 347 return md5Result === params.sign;
382 -};  
383 - 348 + }
384 349
385 -/** 350 + /**
386 * 支付宝支付结果校验 351 * 支付宝支付结果校验
387 */ 352 */
388 -const alipayResultVerify = (params) => { 353 + alipayResultVerify(params) {
389 let checkResult = {}; 354 let checkResult = {};
390 355
391 if (params.q) { 356 if (params.q) {
392 delete params.q; 357 delete params.q;
393 } 358 }
394 359
395 - if (!_checkResponse(params)) { 360 + if (!this._checkResponse(params)) {
396 checkResult.payResult = false; 361 checkResult.payResult = false;
397 } else { 362 } else {
398 _.assign(checkResult, { 363 _.assign(checkResult, {
@@ -409,14 +374,14 @@ const alipayResultVerify = (params) => { @@ -409,14 +374,14 @@ const alipayResultVerify = (params) => {
409 } 374 }
410 375
411 return checkResult; 376 return checkResult;
412 -};  
413 -  
414 -// 支付宝支付  
415 -const getPayAli = (param) => {  
416 - return api.all([  
417 - _getBanner(param),  
418 - _getOthersBuy(param),  
419 - _getOtherDetail(param) 377 + }
  378 +
  379 + // 支付宝支付
  380 + getPayAli(param) {
  381 + return Promise.all([
  382 + this._getBanner(param),
  383 + this._getOthersBuy(param),
  384 + this.getOtherDetail(param)
420 ]).then((result) => { 385 ]).then((result) => {
421 386
422 let resu = { 387 let resu = {
@@ -464,21 +429,7 @@ const getPayAli = (param) => { @@ -464,21 +429,7 @@ const getPayAli = (param) => {
464 429
465 return resu; 430 return resu;
466 }); 431 });
467 -};  
468 -  
469 -module.exports = {  
470 - payments,  
471 - getOtherDetail: _getOtherDetail,  
472 - updateOrderPayment,  
473 - updateOrderPayBank,  
474 - getBankByOrder,  
475 - getPaymentInfo,  
476 - setOrderPayBank,  
477 - sendPayConfirm,  
478 - procOrderData,  
479 - payCenter,  
480 - payTool,  
481 - getPayCod,  
482 - getPayAli,  
483 - alipayResultVerify  
484 -}; 432 + }
  433 +}
  434 +
  435 +module.exports = payModel;