yuda.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. 充值测试处理模块
  3. */
  4. var mysql = require('mysql');
  5. var mysqlcn = require('../../terry/mysqlcn.js');
  6. var query = mysqlcn.query;
  7. var qs = require('querystring');
  8. var encryption = require('../ordinary/encryption.js');
  9. var checkorder = require('../../terry/checkorder.js');
  10. var config = require('../config.js');
  11. var pay_success = function(args,res){
  12. console.log("yuda 回调接收:%s", args);
  13. var receipt
  14. try {
  15. receipt = qs.parse(args)
  16. console.log("qs解析成功")
  17. } catch(e) {
  18. console.log(e)
  19. res.send("FAILURE");
  20. return;
  21. }
  22. var money = receipt.amount // 玩家付款金额
  23. //var goods_id = receipt.goods_id // 产品配置表id
  24. var channel_order_id = receipt.trade_no // SDK生成的订单号
  25. var payment_type = receipt.payment_type // 支付类型(具体使用的SDK)
  26. var istest = 1 // 充值模式 istest=1沙盒测试 2:正式
  27. var cp_order_id = receipt.game_trade_no // CP订单号
  28. // 玩家的付款金额、充值档位、购买份数 需要进行校验
  29. console.log("[yuda] 订单号:%s: ", cp_order_id);
  30. var sql = mysql.format("SELECT * FROM payinfo where orderid=?", [cp_order_id]);//在mysql库中检查订单号
  31. query(sql,function(qerr,vals,fields){
  32. if (qerr==null){
  33. if (vals[0]==null){
  34. console.log("[yuda] 没有找到该订单");
  35. try{res.send("FAILURE");}
  36. catch(e){}
  37. return;
  38. }
  39. if (vals[0].overtime>0){
  40. console.log("[yuda] 已经处理的订单");
  41. try{res.send("SUCCESS");}
  42. catch(e){}
  43. return;
  44. }
  45. var amountnum = money
  46. var platform = vals[0].platform;
  47. platform = platform ? platform.toLowerCase() : 'null'
  48. var str
  49. if (platform == 'ios') {
  50. try{res.send("FAILURE");}
  51. catch(e){}
  52. return;
  53. } else if (platform == 'android') {
  54. str = "com.yoogames.d17"
  55. }
  56. else {
  57. console.log("################## 错误的平台platform: %s", platform)
  58. try{res.send("FAILURE");}
  59. catch(e){}
  60. return
  61. }
  62. var pos1 = receipt.goods_id.indexOf(str) + str.length
  63. var pos2 = receipt.goods_id.length
  64. var goods_id = receipt.goods_id.substring(pos1,pos2)
  65. var channel = vals[0].channel
  66. var timestamp = Date.parse(new Date()) / 1000;
  67. console.log("################## goods_id %s", goods_id)
  68. console.log("platform:%s,channel:%s,amountnum:%s",platform,channel,amountnum)
  69. var option = config.parse(platform,channel,goods_id)
  70. var cfid = option.cfid;
  71. var product_id = option.name
  72. if (cfid == 0){
  73. console.log("[yuda] 获取cfid失败为cfid:%s,product_id:%s", cfid,product_id);
  74. try{res.send("FAILURE");}
  75. catch(e){}
  76. return
  77. }
  78. if (goods_id != cfid){
  79. console.log("[yuda] 充值计费点不同goods_id:%s,cfid:%s", goods_id,cfid);
  80. try{res.send("FAILURE");}
  81. catch(e){}
  82. return
  83. }
  84. if (amountnum != option.price){
  85. console.log("[yuda] 充值金额不同amountnum:%s,option.price:%s", amountnum, option.price);
  86. try{res.send("FAILURE");}
  87. catch(e){}
  88. return
  89. }
  90. //填充充值信息
  91. var sql_1 = "UPDATE payinfo SET \
  92. channel_order_id=?,\
  93. overtime=?,\
  94. flag=?,\
  95. money=?,\
  96. cfid=?,\
  97. platform=?,\
  98. channel=?,\
  99. moneytype=?,\
  100. product_id=?,\
  101. talkingdata=?,\
  102. payment_type=?, \
  103. istest=?\
  104. WHERE orderid=? \
  105. AND flag=2";//增加flag判断防止重复写入
  106. var values_2 = [channel_order_id,timestamp,1,amountnum,cfid,platform,channel,option.typename,product_id,1,payment_type,istest,cp_order_id];
  107. var sql_2 = mysql.format(sql_1, values_2);
  108. query(sql_2,function(qerr,rows,fields_)
  109. {
  110. if (qerr){
  111. console.log("[yuda]逻辑错误,请检查数据表结构和sql语句:\n\t%s", qerr);
  112. try{res.send("FAILURE");}
  113. catch(e){}
  114. return;
  115. }
  116. if (rows.affectedRows > 0){//判断修改了多少个记录
  117. console.log("[yuda]支付成功了,现在通知游戏服务器, orderId:%s, uid:%s, serverid:%s ,cfid:%s",
  118. cp_order_id,vals[0].uid,vals[0].serverid,cfid);
  119. checkorder.pushproduct(query,vals[0].serverid, cp_order_id, function(ok){
  120. if (ok) {
  121. console.log("[yuda] 订单 %s: 成功发货!", cp_order_id);
  122. } else{
  123. console.log("[yuda] 订单 %s: 发货地址无效!", cp_order_id);//在无效时返回成功,我们自己补单中会派发flag为1支付成功但是没成功发货的单
  124. }
  125. try{res.send("SUCCESS");}
  126. catch(e){}
  127. });
  128. // 向 TalkingData 报告充值数据
  129. var appId = config.getAppID(channel);
  130. if (istest == 2) {
  131. checkorder.pushtalkingdata(query,appId,platform,vals[0].uid,cp_order_id,option.price,option.typename,option.diamond,timestamp,payment_type,vals[0].serverid);
  132. }
  133. res.send("SUCCESS");
  134. return;
  135. }else{
  136. console.log("SQL重复写入[yuda]已经处理的订单");
  137. try{res.send("SUCCESS");}
  138. catch(e){}
  139. return;
  140. }
  141. })
  142. }else{
  143. console.log("[yuda] 查询订单列表错误,请检查数据表结构和sql语句:\n\t%s", qerr);
  144. try{res.send("FAILURE");}
  145. catch(e){}
  146. return;
  147. }
  148. })
  149. }
  150. var express = require('express');
  151. var router = express.Router();
  152. router.get('/yuda_pay', function(req, res) {
  153. var pos = req.url.indexOf('?')
  154. var str = req.url
  155. if (pos == -1) { //请求的url是否包含?包含返回-1,==-1为不包含
  156. console.log("yuda 回调接收未找到参数");
  157. res.send("FAILURE");
  158. } else {
  159. var data = str.substring(pos+1,str.length)
  160. pay_success(data, res)
  161. };
  162. req.on("error",function(err){
  163. console.log("error:%s",err);
  164. res.send("FAILURE");
  165. });
  166. });
  167. module.exports = router;