googleplay.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //GooglePlay
  2. let mysql = require('mysql');
  3. let mysqlcn = require('../../terry/mysqlcn.js');
  4. let query = mysqlcn.query;
  5. let qs = require('querystring');
  6. let checkorder = require('../../terry/checkorder.js');
  7. let config = require('../config.js');
  8. let express = require('express');
  9. let router = express.Router();
  10. let crypto = require('crypto');
  11. // let IS_TEST = 2 //1是测试2是正式
  12. Date.prototype.Format = function (fmt) { //author: meizz
  13. let o = {
  14. "M+": this.getMonth() + 1, //月份
  15. "d+": this.getDate(), //日
  16. "h+": this.getHours(), //小时
  17. "m+": this.getMinutes(), //分
  18. "s+": this.getSeconds(), //秒
  19. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  20. "S": this.getMilliseconds() //毫秒
  21. };
  22. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  23. for (let k in o)
  24. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  25. return fmt;
  26. }
  27. let sql_insert = function(orderid, serverid,uid,rmb,moneytype,cfid,platform,channel,overtime,flag,product_id,user_id,purchase_date_ms,talkingdata,payment_type,istest){
  28. let sql = "INSERT INTO payinfo SET orderid=?, serverid=?, channel=?, submittime=?, flag=?, user_id=?, channel_order_id=?, money=?, cfid=?, platform=?,overtime=?,product_id=?,uid=?,moneytype=?,talkingdata=?,payment_type=?,istest=?";
  29. let values = [orderid, serverid, channel,purchase_date_ms,flag,user_id,orderid,rmb,cfid,platform,overtime,product_id,uid,moneytype,talkingdata,payment_type,istest];
  30. sql = mysql.format(sql, values);
  31. return sql;
  32. }
  33. function insert_str(str, insert_str, sn) {
  34. let newstr = "";
  35. for (let i = 0; i < str.length; i += sn) {
  36. let tmp = str.substring(i, i + sn);
  37. newstr += tmp + insert_str;
  38. }
  39. return newstr;
  40. }
  41. let RsaSha1Check = function(str,sig,publicKey){
  42. let public_key = insert_str(publicKey, '\n', 64);
  43. public_key = '-----BEGIN PUBLIC KEY-----\n' + public_key + '-----END PUBLIC KEY-----';
  44. let verifier = crypto.createVerify('RSA-SHA1');
  45. console.log('验证签名public key:\n' + public_key);
  46. console.log('验证签名src_sign:' + str);
  47. verifier.update(new Buffer(str, 'utf-8'));
  48. return verifier.verify(public_key, sig, 'base64');
  49. };
  50. let pay_success = function(args, res){
  51. if (args==null){
  52. console.log("[GooglePlay] 无效的充值请求!");
  53. try{res.send("error,1");}
  54. catch(e){}
  55. return;
  56. }
  57. let obj = null
  58. try {
  59. obj = qs.parse(args);
  60. } catch(e){
  61. console.log("[GooglePlay] 无效的充值请求!");
  62. try{res.send("error,1");}
  63. catch(e){}
  64. return
  65. }
  66. let orderid = obj.orderid; //订单号
  67. let channel_order_id = obj.channel_order_id //渠道订单id
  68. let serverid = obj.serverid;//服务器id
  69. let userid = obj.userid; //玩家的渠道帐号
  70. let uid = obj.uid; //玩家游戏uid
  71. let cfid = obj.cfid // 计费点
  72. let platform = "googleplay";
  73. let payment_type = "Android"
  74. let ti = new Date().Format("yyyy-MM-dd hh:mm:ss");
  75. let istest = 1
  76. console.log("[GooglePlay] 订单 %s 请求 (%s):\n\tchannel_order_id=%s, serverid=%s,uid=%s,userid=%s,platform=%s",
  77. orderid, ti, channel_order_id, serverid, uid, userid, platform);
  78. let sql = mysql.format("SELECT * FROM payinfo where orderid=?", [orderid]);//在mysql库中检查订单号
  79. console.log(sql)
  80. query(sql,function(qerr,vals,fields){
  81. if (qerr==null){
  82. if (vals[0]==null){
  83. console.log("[GooglePlay] 没有找到该订单");
  84. try{res.send("FAILURE");}
  85. catch(e){}
  86. return;
  87. }
  88. if (vals[0].overtime>0){
  89. console.log("[GooglePlay] 已经处理的订单");
  90. try{res.send("SUCCESS");}
  91. catch(e){}
  92. return;
  93. }
  94. //检查订单
  95. let channel = vals[0].channel
  96. let conf = config.parse(platform, channel, cfid);
  97. let amountnum = conf.price
  98. let moneytype = conf.typename;
  99. if (conf.cfid == 0){
  100. console.log("[GooglePlay] channel:%s获取cfid失败为platform:%s,cfid:%s",channel, platform, cfid);
  101. try{res.send("fail");}
  102. catch(e){}
  103. return
  104. }
  105. console.log("[GooglePlay] 订单 %s 处理:\n\t商品配置:%s, 金额:%s, 类型:%s, 用户:%s, 服务器:%s",
  106. channel_order_id,cfid,amountnum,moneytype,uid,serverid);
  107. if (cfid != vals[0].cfid) {
  108. console.log("[GooglePlay] 充值订单不匹配:cfid = %s, vals[0].cfid = %s!!", cfid, vals[0].cfid);
  109. }
  110. let timestamp = Date.parse(new Date()) / 1000;
  111. //填充充值信息
  112. let sql_1 = "UPDATE payinfo SET \
  113. channel_order_id=?,\
  114. overtime=?,\
  115. flag=?,\
  116. money=?,\
  117. platform=?,\
  118. moneytype=?,\
  119. payment_type=?, \
  120. istest=?\
  121. WHERE orderid=? \
  122. AND flag=2";//增加flag判断防止重复写入
  123. let values_2 = [channel_order_id,timestamp,1,amountnum,platform,moneytype,payment_type,istest,orderid];
  124. let sql_2 = mysql.format(sql_1, values_2);
  125. console.log(sql_2)
  126. query(sql_2,function(qerr,rows,fields_)
  127. {
  128. if (qerr){
  129. console.log("[GooglePlay]逻辑错误,请检查数据表结构和sql语句:\n\t%s", qerr);
  130. try{res.send("FAILURE");}
  131. catch(e){}
  132. return;
  133. }
  134. if (rows.affectedRows > 0){//判断修改了多少个记录
  135. console.log("[GooglePlay]支付成功了,现在通知游戏服务器, orderId:%s, uid:%s, serverid:%s ,cfid:%s",
  136. orderid,vals[0].uid,vals[0].serverid,cfid);
  137. checkorder.pushproduct(query,vals[0].serverid, orderid, function(ok){
  138. if (ok) {
  139. console.log("[GooglePlay] 订单 %s: 成功发货!", orderid);
  140. } else{
  141. console.log("[GooglePlay] 订单 %s: 发货地址无效!", orderid);//在无效时返回成功,我们自己补单中会派发flag为1支付成功但是没成功发货的单
  142. }
  143. try{res.send("SUCCESS");}
  144. catch(e){}
  145. });
  146. res.send("SUCCESS");
  147. return;
  148. }else{
  149. console.log("SQL重复写入[GooglePlay]已经处理的订单");
  150. try{res.send("SUCCESS");}
  151. catch(e){}
  152. return;
  153. }
  154. })
  155. }else{
  156. console.log("[GooglePlay] 查询订单列表错误,请检查数据表结构和sql语句:\n\t%s", qerr);
  157. try{res.send("FAILURE");}
  158. catch(e){}
  159. return;
  160. }
  161. })
  162. }
  163. router.post('/googleplay', function(req, res) {
  164. console.log("谷歌,post ,host:%s, ip:%s,date:%s",req.hostname,req.ip,new Date());
  165. let data = "";
  166. req.on("data",function(chunk){
  167. data += chunk;
  168. })
  169. req.on("end",function(){
  170. pay_success(data,res);
  171. })
  172. req.on("error",function(err){
  173. res.send('erron,1');
  174. })
  175. });
  176. module.exports = router;