Authored by 郭成尧

verify-result-oko

@@ -73,8 +73,10 @@ const payAli = (req, res, next) => { @@ -73,8 +73,10 @@ const payAli = (req, res, next) => {
73 return; 73 return;
74 } 74 }
75 75
  76 + let verifyResult = payModel.alipayResultVerify(req.query);
  77 +
76 // 支付失败 78 // 支付失败
77 - if (req.query.trade_status !== 'TRADE_SUCCESS') { 79 + if (!verifyResult.payResult) {
78 return res.render('pay/pay-ali', responseData); 80 return res.render('pay/pay-ali', responseData);
79 } 81 }
80 82
@@ -11,15 +11,15 @@ const serviceAPI = global.yoho.ServiceAPI; @@ -11,15 +11,15 @@ const serviceAPI = global.yoho.ServiceAPI;
11 const utils = '../../../utils'; 11 const utils = '../../../utils';
12 const productProcess = require(`${utils}/product-process`); 12 const productProcess = require(`${utils}/product-process`);
13 const _ = require('lodash'); 13 const _ = require('lodash');
  14 +const ApipayConfig = global.yoho.config.alipayConfig;
  15 +const md5 = require('md5');
14 16
15 // 资源位 17 // 资源位
16 const _getBanner = (param) => { 18 const _getBanner = (param) => {
17 return serviceAPI.get('operations/api/v5/resource/get', { 19 return serviceAPI.get('operations/api/v5/resource/get', {
18 content_code: param.contentCode, 20 content_code: param.contentCode,
19 platform: 'iphone' 21 platform: 'iphone'
20 - }, {  
21 - code: 200  
22 - }).then((result) => { 22 + }, { code: 200 }).then((result) => {
23 23
24 result = result.data; 24 result = result.data;
25 25
@@ -36,9 +36,7 @@ const _getOthersBuy2 = (param) => { @@ -36,9 +36,7 @@ const _getOthersBuy2 = (param) => {
36 rec_pos: '100005', 36 rec_pos: '100005',
37 limit: 2, 37 limit: 2,
38 client_id: param.client_id 38 client_id: param.client_id
39 - }, {  
40 - code: 200  
41 - }).then((result) => { 39 + }, { code: 200 }).then((result) => {
42 40
43 if (result && result.data && result.data.product_list) { 41 if (result && result.data && result.data.product_list) {
44 return productProcess.processProductList(result.data.product_list); 42 return productProcess.processProductList(result.data.product_list);
@@ -53,9 +51,7 @@ const _getOtherDetail = (param) => { @@ -53,9 +51,7 @@ const _getOtherDetail = (param) => {
53 method: 'app.SpaceOrders.detail', 51 method: 'app.SpaceOrders.detail',
54 uid: param.uid, 52 uid: param.uid,
55 order_code: param.orderCode 53 order_code: param.orderCode
56 - }, {  
57 - code: 200  
58 - }).then((result) => { 54 + }, { code: 20 }).then((result) => {
59 55
60 return result; 56 return result;
61 57
@@ -74,7 +70,7 @@ const _getOthersBuy = (param) => { @@ -74,7 +70,7 @@ const _getOthersBuy = (param) => {
74 goodSkn = result[0].data.order_goods[0].product_skn; 70 goodSkn = result[0].data.order_goods[0].product_skn;
75 } 71 }
76 72
77 - return _getOthersBuy2(Object.assign(param, {skn: goodSkn})); 73 + return _getOthersBuy2(Object.assign(param, { skn: goodSkn }));
78 74
79 }).then((result) => { 75 }).then((result) => {
80 76
@@ -125,6 +121,69 @@ const getPayCod = (param) => { @@ -125,6 +121,69 @@ const getPayCod = (param) => {
125 }); 121 });
126 }; 122 };
127 123
  124 +const _raw = (args) => {
  125 + let keys = Object.keys(args);
  126 +
  127 + keys = keys.filter(k => {
  128 + let keyValueCheck =
  129 + k === 'sign' ||
  130 + k === 'sign_type' ||
  131 + k === 'code' ||
  132 + args[k] === '' ||
  133 + args[k] === 'undefined';
  134 +
  135 + return !keyValueCheck;
  136 + }).sort();
  137 +
  138 + return keys.map(k => {
  139 + return k + '=' + decodeURI(args[k]);
  140 + }).join('&');
  141 +};
  142 +
  143 +/**
  144 + * 验证返回结果的正确性
  145 + */
  146 +const _checkResponse = (params) => {
  147 + if (!params.sign) {
  148 + return false;
  149 + }
  150 + let rawResult = _raw(params);
  151 + let sign = rawResult + ApipayConfig.alipayKey;
  152 + let md5Result = md5(sign);
  153 +
  154 + return md5Result === params.sign;
  155 +};
  156 +
  157 +
  158 +/**
  159 + * 支付宝支付结果校验
  160 + */
  161 +const alipayResultVerify = (params) => {
  162 + let checkResult = {};
  163 +
  164 + if (params.q) {
  165 + delete params.q;
  166 + }
  167 +
  168 + if (!_checkResponse(params)) {
  169 + checkResult.payResult = false;
  170 + } else {
  171 + _.assign(checkResult, {
  172 + bankName: '',
  173 + orderCode: params.out_trade_no,
  174 + payResult: params.trade_status === 'TRADE_SUCCESS',
  175 + payTime: params.gmt_payment || '',
  176 + totalFee: params.total_fee,
  177 + resultMsg: params.notify_type,
  178 + payOrderCode: params.out_trade_no,
  179 + tradeNo: params.trade_no,
  180 + bankBillNo: ''
  181 + });
  182 + }
  183 +
  184 + return checkResult;
  185 +};
  186 +
128 // 支付宝支付 187 // 支付宝支付
129 const getPayAli = (param) => { 188 const getPayAli = (param) => {
130 return api.all([ 189 return api.all([
@@ -171,5 +230,6 @@ const getPayAli = (param) => { @@ -171,5 +230,6 @@ const getPayAli = (param) => {
171 230
172 module.exports = { 231 module.exports = {
173 getPayCod, 232 getPayCod,
174 - getPayAli 233 + getPayAli,
  234 + alipayResultVerify
175 }; 235 };
@@ -16,21 +16,21 @@ module.exports = { @@ -16,21 +16,21 @@ module.exports = {
16 siteUrl: '//m.yohobuy.com', 16 siteUrl: '//m.yohobuy.com',
17 assetUrl: '//127.0.0.1:5001', 17 assetUrl: '//127.0.0.1:5001',
18 domains: { 18 domains: {
19 - // api: 'http://api-test3.yohops.com:9999/',  
20 - // service: 'http://service-test3.yohops.com:9999/',  
21 - // liveApi: 'http://testapi.live.yohops.com:9999/',  
22 - // singleApi: 'http://api-test3.yohops.com:9999/',  
23 - // imSocket: 'ws://im.yohobuy.com:10240',  
24 - // imCs: 'http://im.yohobuy.com/api',  
25 - // imServer: 'http://im.yohobuy.com/server' 19 + api: 'http://api-test3.yohops.com:9999/',
  20 + service: 'http://service-test3.yohops.com:9999/',
  21 + liveApi: 'http://testapi.live.yohops.com:9999/',
  22 + singleApi: 'http://api-test3.yohops.com:9999/',
  23 + imSocket: 'ws://im.yohobuy.com:10240',
  24 + imCs: 'http://im.yohobuy.com/api',
  25 + imServer: 'http://im.yohobuy.com/server'
26 26
27 - api: 'http://api.yoho.cn/',  
28 - service: 'http://service.yoho.cn/',  
29 - liveApi: 'http://api.live.yoho.cn/',  
30 - singleApi: 'http://single.yoho.cn/',  
31 - imSocket: 'ws://imsocket.yohobuy.com:10000',  
32 - imCs: 'http://imhttp.yohobuy.com/api',  
33 - imServer: 'http://imhttp.yohobuy.com/server' 27 + // api: 'http://api.yoho.cn/',
  28 + // service: 'http://service.yoho.cn/',
  29 + // liveApi: 'http://api.live.yoho.cn/',
  30 + // singleApi: 'http://single.yoho.cn/',
  31 + // imSocket: 'ws://imsocket.yohobuy.com:10000',
  32 + // imCs: 'http://imhttp.yohobuy.com/api',
  33 + // imServer: 'http://imhttp.yohobuy.com/server'
34 }, 34 },
35 subDomains: { 35 subDomains: {
36 host: '.m.yohobuy.com', 36 host: '.m.yohobuy.com',
@@ -86,7 +86,10 @@ module.exports = { @@ -86,7 +86,10 @@ module.exports = {
86 appSecret: 'ce21ae4a3f93852279175a167e54509b' 86 appSecret: 'ce21ae4a3f93852279175a167e54509b'
87 } 87 }
88 }, 88 },
89 - zookeeperServer: '192.168.102.168:2188' 89 + zookeeperServer: '192.168.102.168:2188',
  90 + alipayConfig: {
  91 + alipayKey: 'kcxawi9bb07mzh0aq2wcirsf9znusobw'
  92 + }
90 }; 93 };
91 94
92 if (isProduction) { 95 if (isProduction) {