checkorder.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //检查订单
  2. var https = require('https');
  3. var http = require('http');
  4. var mysql = require('mysql');
  5. var sql_select = function(orderid){
  6. var sql = "SELECT * FROM payinfo WHERE ?? = ?";
  7. var inserts = ['orderid',orderid];
  8. sql = mysql.format(sql, inserts);
  9. return sql;
  10. }
  11. //更新 talkingdata 报道标记
  12. var sql_update_talkingdata = function(orderid,value){
  13. var sql = "UPDATE payinfo SET talkingdata=? WHERE orderid=?";
  14. var values = [value,orderid];
  15. sql = mysql.format(sql, values);
  16. return sql;
  17. }
  18. //更新数据库 talkingdata 字段
  19. var update_talkingdata_by_orderid=function(query,orderid,value){
  20. console.log("update_talkingdata_by_orderid, orderid:%s,value:%d",orderid,value);
  21. var sql = sql_update_talkingdata(orderid,value);
  22. query(sql,function(qerr,vals,fields){
  23. if (qerr==null){
  24. return 0;
  25. }else{
  26. console.log(qerr);
  27. return 1;
  28. }
  29. })
  30. }
  31. //查询游戏服务器列表
  32. var sql_select_server = function(id){
  33. var sql = "SELECT * FROM services WHERE id = ?";
  34. var inserts = [id];
  35. sql = mysql.format(sql, inserts);
  36. return sql;
  37. }
  38. //更新是否完成对游戏服务器的充值
  39. var sql_update_flag = function(orderid,flag){
  40. var sql = "UPDATE payinfo SET flag=? WHERE orderid=?";
  41. var values = [flag,orderid];
  42. sql = mysql.format(sql, values);
  43. return sql;
  44. }
  45. //更新数据库flag
  46. var update_flag_by_orderid=function(query,orderid,flag){
  47. console.log("update_overtime_by_orderid, orderid:%s,flag:%d",orderid,flag);
  48. var sql = sql_update_flag(orderid,flag);
  49. query(sql,function(qerr,vals,fields){
  50. if (qerr==null){
  51. return 0;
  52. }else{
  53. console.log(qerr);
  54. return 1;
  55. }
  56. })
  57. }
  58. //更新完成时间戳,针对渠道的报道时间
  59. var sql_update_overtime = function(orderid,overtime){
  60. var sql = "UPDATE payinfo SET overtime=? WHERE orderid=?";
  61. var values = [overtime,orderid];
  62. sql = mysql.format(sql, values);
  63. return sql;
  64. }
  65. //更新数据库overtime
  66. var update_overtime_by_orderid=function(query,orderid,overtime){
  67. console.log("update_overtime_by_orderid, orderid:%s,overtime:%d",orderid,overtime);
  68. var sql = sql_update_overtime(orderid,overtime);
  69. query(sql,function(qerr,vals,fields){
  70. if (qerr==null){
  71. return 0;
  72. }else{
  73. console.log(qerr);
  74. return 1;
  75. }
  76. })
  77. }
  78. // 读取订单详细数据 发送至游戏服务器
  79. var order_detail = function(orderid){
  80. var sql = "SELECT * FROM payinfo WHERE orderid = ?";
  81. var inserts = [orderid];
  82. return mysql.format(sql, inserts);
  83. // var detail
  84. // query(sql,function(qerr,vals,fields){
  85. // if (vals.length > 0){
  86. // // return 0, vals[0];
  87. // console.log("########## 查询到了数据")
  88. // detail = vals[0];
  89. // }else{
  90. // console.log("########## 没有查询到数据")
  91. // console.log(qerr);
  92. // // return 1;
  93. // }
  94. // })
  95. // return detail
  96. }
  97. //发送订单完成信息给游戏服务器
  98. //rmb 实质是配置id,并非真是的钱
  99. var send_2_game = function(query,serverid, orderid, callback){
  100. var sql = sql_select_server(serverid);
  101. var sql_detail = order_detail(orderid);
  102. query(sql_detail,function(qerr_d,vals_d,fields_d){
  103. if (vals_d.length > 0){
  104. var detail = vals_d[0];
  105. var data2game = JSON.stringify(detail);
  106. console.log("[订单数据结果 ] %s", data2game)
  107. query(sql,function(qerr,vals,fields){
  108. if (vals.length>0){
  109. queryRet = vals[0];
  110. // var data2game = {
  111. // 'orderid': orderid,
  112. // 'uid': uid,
  113. // 'cfid' : cfid
  114. // };
  115. console.log("[queryRet] %s", JSON.stringify(queryRet))
  116. var opt = {
  117. method: "POST",
  118. host: queryRet.ip,
  119. port: queryRet.port,
  120. path: "/recharge",
  121. headers: {
  122. "Content-Type": 'application/json',
  123. "Content-Length": Buffer.byteLength(data2game, "utf8")
  124. }
  125. };
  126. var req = http.request(opt, function (res){
  127. if (res.statusCode == 200){
  128. var datas = [];
  129. var size = 0;
  130. res.on('data', function (data){
  131. datas.push(data);
  132. size += data.length;
  133. });
  134. res.on('end', function (){
  135. var buff = Buffer.concat(datas, size);
  136. var result = buff.toString();
  137. var result = JSON.parse(result);
  138. if (parseInt(result.code)==0){
  139. // 至此,一笔充值圆满的结束了
  140. update_flag_by_orderid(query,orderid,0);
  141. if (callback) {
  142. callback(true)
  143. return
  144. }
  145. }
  146. else if(parseInt(result.code)==2){
  147. update_flag_by_orderid(query,orderid,3);
  148. if (callback) {
  149. callback(true)
  150. return
  151. }
  152. }
  153. else if (callback) {
  154. console.log("充值失败:%s,%s",parseInt(result.code),result.code);
  155. callback(false)
  156. return
  157. }
  158. });
  159. res.on('error', function (err){
  160. console.log(err);
  161. if (callback) {
  162. callback(false)
  163. return
  164. }
  165. });
  166. }
  167. }).on('error', function(){
  168. if (callback) {
  169. callback(false)
  170. return
  171. }
  172. });
  173. req.write(data2game);
  174. req.end();
  175. } else {
  176. if (callback) {
  177. callback(false)
  178. return
  179. }
  180. }
  181. })
  182. }else{
  183. console.log(qerr_d);
  184. if (callback) { callback(false) }
  185. return
  186. }
  187. })
  188. }
  189. var share = require("../terry/share/index.js");
  190. var send_2_talkingdata = function(
  191. query, // Mysql 查询函数
  192. appID, // String, 是您在TalkingGame中创建游戏时获得的AppID,用来唯一标识您的游戏。
  193. OS, // String, 玩家此次充值时所用的设备系统平台,只支持格式要求限定的字符串。(ios, android, h5)
  194. accountID, // String, 玩家的账户ID(与客户端调用SDK的setAccount需传入玩家的accountId类似。)
  195. orderID, // String, 订单ID,唯一标识一次交易。
  196. currencyAmount, // Double, 充值金额;
  197. currencyType, // String, 充值货币类型(人民币CNY;美元USD;新台币TWD);
  198. virtualCurrencyAmount, // Double, 充值获得的虚拟币额度。
  199. chargeTime, // Long, 玩家充值发生的时间。
  200. paymentType, // String, 支付方式(如:支付宝、苹果iap、银联支付、爱贝支付聚合等)。
  201. gameServer) { // Long, 玩家充值的区服。
  202. if (appID == null) {
  203. console.log("订单 %s: 未能向 TalkingData 报道,因为没有获得正确的 AppID", orderID)
  204. return;
  205. }
  206. var req = new share.TalkingData()
  207. req.on('error', function(err){
  208. console.log("订单 %s: 向 TalkingData 报告失败!\n\t%s", orderID, err.message)
  209. });
  210. req.on('end', function(){
  211. update_talkingdata_by_orderid(query, orderID, 0)
  212. console.log("订单 %s: 向 TalkingData 报告成功!", orderID)
  213. })
  214. req.send(appID, OS, accountID, orderID, currencyAmount, currencyType, virtualCurrencyAmount, chargeTime, paymentType, gameServer)
  215. }
  216. //通知服务器发送钻石
  217. exports.pushproduct = function(query,serverid,OrderId,functionnum){
  218. send_2_game(query,serverid,OrderId,functionnum);
  219. }
  220. //向talkingdata推送数据
  221. exports.pushtalkingdata = function(query,appId,platform,uid,order_id,price,typename,diamond,timestamp,payment_type,serv_id){
  222. // send_2_talkingdata(query,appId,platform,uid,order_id,price,typename,diamond,timestamp,payment_type,serv_id);
  223. }