haiwai_quick.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //QUICK
  2. var mysql = require('mysql');
  3. var config = require('../../pay/config');
  4. var crypto = require('crypto');
  5. var qs = require('querystring');
  6. var checkorder = require('../checkorder');
  7. //md5加密
  8. var md5 = function (str){
  9. var md5sum = crypto.createHash('md5');
  10. md5sum.update(str);
  11. str = md5sum.digest('hex');
  12. return str;
  13. };
  14. //充值成功的时候修改库中数据
  15. var sql_update_quick = function(order_no,timestamp,flag,ordermoney,cfida,platform,channel,payment_type,moneytype,sdcustomno){
  16. //填充充值信息
  17. var sql_2 = "UPDATE payinfo SET channel_order_id=?,overtime=?,flag=?,money=?,cfid=?,platform=?,channel=?,payment_type=?,moneytype=?,talkingdata=? WHERE orderid=? AND flag=2";
  18. var timestamp = Date.parse(new Date()) / 1000;
  19. var cfid = ordermoney;
  20. var values_2 = [order_no,timestamp,1,ordermoney,cfida,platform,channel,payment_type,moneytype,1,sdcustomno];
  21. var sql = mysql.format(sql_2, values_2);
  22. return sql;
  23. }
  24. exports.quick_pay_success = function(args,query,res,quick_proto){
  25. var obj = qs.parse(args);
  26. // var nt_data = obj.nt_data; //通知数据(XML格式)
  27. // var sign = obj.sign; //签名串
  28. // var md5Sign = obj.md5Sign; //string 签名计算方法为MD5
  29. var outputname = quick_proto.outputname //使用quick的渠道
  30. var callback_key = quick_proto.callback_key //quick解密编码
  31. // var md5_key = quick_proto.md5_key //MD5加密编码
  32. var uid = obj.uid //必传购买道具的用户uid
  33. var username = obj.username//必有购买道具的用户username
  34. var cpOrderNo = obj.cpOrderNo//可为空游戏下单时传递的游戏订单号,原样返回
  35. var orderNo = obj.orderNo//必有SDK唯一订单号
  36. var payTime = obj.payTime//必有用户支付时间,如2017-02-0614:22:32
  37. var payAmount = obj.payAmount//必有用户支付金额
  38. var payCurrency = obj.payCurrency//必有用户支持的币种,如RMB,USD等
  39. var payType = obj.payType//表示支付方式的 30是谷歌支付 8为appstore支付 其他的为第三方金流渠道
  40. var usdAmount = obj.usdAmount//必有用户支付的游戏道具以美元计价的金额
  41. var extrasParams = obj.extrasParams//可为空游戏下单时传递的扩展参数,将原样返回。//额外参数,现在使用的配置sid
  42. var sign = obj.sign//必有签名值,游戏应根据签名约定,本地计算后与此值进行比对
  43. var payStatus = obj.payStatus
  44. var actRate = obj.actRate
  45. console.log(outputname+"quick回调来了");
  46. console.log(args);
  47. //验证签名
  48. // var strsign = 'actRate='+actRate+
  49. // "&cpOrderNo="+cpOrderNo+
  50. // "&extrasParams="+extrasParams+
  51. // "&orderNo="+orderNo+
  52. // "&payAmount="+payAmount+
  53. // "&payCurrency="+payCurrency+
  54. // "&payStatus="+payStatus+
  55. // "&payTime="+payTime+
  56. // "&payType="+payType+
  57. // "&uid="+uid+
  58. // "&usdAmount="+usdAmount+
  59. // "&username="+username+"&"+callback_key;
  60. // var url_sort = config.url_sort(obj)
  61. // var strsign = url_sort +"&"+callback_key;
  62. var sort_data = Object.keys(obj).sort();
  63. // 生成本地签名验证签名
  64. var strsign = sort_data[0] + "=" + obj[sort_data[0]];
  65. for(var i=1;i<sort_data.length;i++){
  66. if(sort_data[i] != "sign"){
  67. strsign = strsign + '&'+ sort_data[i] +"="+ obj[sort_data[i]]
  68. }
  69. }
  70. strsign = strsign + "&" + callback_key;
  71. // var strsign = 'actRate='+actRate+
  72. // "&cpOrderNo="+cpOrderNo+
  73. // "&extrasParams="+extrasParams+
  74. // "&orderNo="+orderNo+
  75. // "&payAmount="+payAmount+
  76. // "&payCurrency="+payCurrency+
  77. // "&payStatus="+payStatus+
  78. // "&payTime="+payTime+
  79. // "&payType="+payType+
  80. // "&uid="+uid+
  81. // "&usdAmount="+usdAmount+
  82. // "&username="+username+"&"+callback_key;
  83. var genSign = md5(strsign);
  84. if(sign != genSign){
  85. console.log(outputname+"quick支付签名验证失败,sign:%s,genSign:%s,strsign:%s",sign,genSign,strsign);
  86. try{res.send("FAILED");}
  87. catch(e){}
  88. return;
  89. }
  90. try{
  91. var sql_1 = mysql.format("SELECT * FROM payinfo where orderid=?", [cpOrderNo]);//在mysql库中检查订单号
  92. query(sql_1,function(qerr,vals,fields_)
  93. {
  94. console.log("quick_pay_success,1");
  95. if(qerr){
  96. //"逻辑错误,请检查数据表结构和sql语句"
  97. console.log(outputname+"quick逻辑错误,请检查数据表结构和sql语句");
  98. try{res.send("FAILED");}
  99. catch(e){}
  100. return;
  101. }
  102. if (vals[0]==null){
  103. console.log(outputname+"quick没有找到该订单");
  104. try{res.send("FAILED");}
  105. catch(e){}
  106. return;
  107. }
  108. if (vals[0].overtime>0){
  109. console.log(outputname+"quick已经处理的订单");
  110. try{res.send("SUCCESS");}
  111. catch(e){}
  112. return;
  113. }
  114. var info_s = extrasParams.split('|');
  115. var len = info_s.length;
  116. var platform;// 只存储 ios 和 android
  117. var product_id;
  118. if (len > 1) {
  119. platform = info_s[1];
  120. if (platform == 'ios'){
  121. product_id = info_s[0];
  122. }else if(platform == 'android'){
  123. product_id = parseInt(info_s[0]);
  124. }else{
  125. console.log(outputname+"quick错误的平台platform:%s",platform);
  126. try{res.send("FAILED");}
  127. catch(e){}
  128. return;
  129. }
  130. }else{
  131. console.log(outputname+"quick获取产品包失败extrasParams:%s",extrasParams);
  132. try{res.send("FAILED");}
  133. catch(e){}
  134. return;
  135. }
  136. //var des = "null";//描述订单支付成功或失败的系统备注
  137. var remarks = vals[0].remarks
  138. var channel = quick_proto.channel; // 只存储 qianxun、zaya、limayao, zhangyu, friday, wudiyongshi, banli, zhiyu, cloud
  139. var payment_type = "QUICK";//pay_channel.toString(); // 具体的子渠道,比如 quick 的 微信、手Q、UC、应用宝等, 新增一个 paymentType 字段来填充
  140. var timestamp = Date.parse(new Date()) / 1000;
  141. var option = config.parse(platform,channel,product_id);
  142. var cfid = option.cfid;//购买商品ID--parseInt转int
  143. if (cfid == 0){
  144. console.log(outputname+"QUICK获取cfid失败为:%s,channel:%s,product_id:%s,platform:%s", cfid,channel,product_id,platform);
  145. try{res.send("FAILED");}
  146. catch(e){}
  147. return;
  148. }
  149. if (usdAmount != option.price){
  150. console.log(outputname+"QUICK金额不一致为usdAmount:%s,option.price:%s", usdAmount,option.price);
  151. try{res.send("FAILED");}
  152. catch(e){}
  153. return;
  154. }
  155. var price = option.price;
  156. var typename = option.typename;
  157. if (remarks){
  158. if(remarks == "fr" || remarks == "de"){
  159. price = option.price_eur
  160. typename = option.typename_eur
  161. }
  162. }
  163. var sql_2 = sql_update_quick(orderNo,timestamp,1,usdAmount,cfid,platform,channel,payType,option.typename,cpOrderNo)
  164. query(sql_2,function(qerr,rows,fields_)
  165. {
  166. if (qerr){
  167. console.log(qerr);
  168. try{res.send("FAILED");} //"逻辑错误,请检查数据表结构和sql语句"}
  169. catch(e){}
  170. return;
  171. }
  172. if (rows.affectedRows <= 0){//判断修改了多少个记录
  173. console.log(outputname+"quick已经处理的订单");
  174. try{res.send("SUCCESS");}
  175. catch(e){}
  176. return;
  177. }
  178. console.log(outputname+"quick支付成功了,现在通知游戏服务器, cpOrderNo:%s, username:%s, playerid:%s, serverid:%s ,cfid:%s",
  179. cpOrderNo,vals[0].username,vals[0].playerid,vals[0].serverid,cfid);
  180. //send_2_game(query,cpOrderNo,vals[0].username,vals[0].playerid,vals[0].serverid,cfid, function(ok){
  181. checkorder.pushproduct(query,cpOrderNo,vals[0].uid,vals[0].serverid,cfid, function(ok){
  182. if (ok) {
  183. console.log(outputname+"海外QUICK 订单 %s: 成功发货!", cpOrderNo);
  184. try{res.send("SUCCESS");}
  185. catch(e){}
  186. } else{
  187. console.log(outputname+"海外QUICK 订单 %s: 发货地址无效!", cpOrderNo);
  188. try{res.send("FAILED");}
  189. catch(e){}
  190. }
  191. });
  192. // 向 TalkingData 报告充值数据
  193. var appId = config.getAppID(channel);
  194. console.log(outputname+"海外QUICK 向 TalkingData 报告充值数据 appId:%s,channel:%s", appId,channel);
  195. checkorder.pushtalkingdata(query,appId,platform,vals[0].uid,cpOrderNo,price,typename,option.diamond,timestamp,payment_type,vals[0].serverid);
  196. })
  197. });
  198. }catch(err){
  199. console.log(outputname+"quick_err ,%s",err);
  200. try{res.send("FAILED");}
  201. catch(e){}
  202. }
  203. }