replenishment.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //补单
  2. var checkorder = require('../../terry/checkorder.js');
  3. var mysql = require('mysql');
  4. var mysqlcn = require('../../terry/mysqlcn.js');
  5. var config = require('../config.js');
  6. var encryption = require('./encryption.js');
  7. var qs = require('querystring');
  8. var query = mysqlcn.query;
  9. // var orderid //cp订单号
  10. // var serverid //服务器id
  11. // var uid //玩家的uid
  12. // var platformid //'android'或'ios'
  13. // var channel //渠道
  14. // var cfid //商品的sid
  15. // var money //金额
  16. // var moneytype //货币类型
  17. // var flag //等待支付
  18. // var product_id //计费点名称
  19. // var channel_order_id //渠道订单号
  20. // var talkingdata //1未向talkingdata数据0发送成功
  21. // var payment_type //附加的子渠道
  22. // var submittime //订单创建时间
  23. // var overtime //订单结束时间
  24. // var userid //平台或渠道的账号id
  25. var SIG_KEY_A = "d06ede1fc820e59c7fb795c8d310aaa4";
  26. var sql_insert = function(orderid, serverid,uid,money,moneytype,cfid,platform,channel,submittime,overtime,flag,product_id,talkingdata){
  27. var sql = "INSERT INTO payinfo SET orderid=?, serverid=?, uid=?,money=?,moneytype=?,cfid=?,platform=?,channel=?,submittime=?,overtime=?,flag=?,product_id=?,talkingdata=?";
  28. var values = [orderid, serverid,uid,money,moneytype,cfid,platform,channel,submittime,overtime,flag,product_id,talkingdata];
  29. sql = mysql.format(sql, values);
  30. return sql;
  31. }
  32. var sql_select = function(orderid){
  33. var sql = "SELECT * FROM payinfo WHERE ?? = ?";
  34. var inserts = ['orderid',orderid];
  35. sql = mysql.format(sql, inserts);
  36. return sql;
  37. }
  38. var istest = 2 // 充值模式 istest=1沙盒测试 2:正式
  39. //手动补单
  40. var shoudong_repair_order=function(args,res){
  41. try {
  42. console.log("补单消息:%s",args)
  43. var obj = JSON.parse(args);
  44. var serverid = obj.serverid;
  45. var uid = obj.uid;
  46. var cfid = obj.cfid;
  47. var money = obj.money;
  48. var moneytype = obj.moneytype;
  49. var platform = obj.platform;
  50. var channel = obj.channel;
  51. var orderid = obj.orderid;
  52. var act_id = obj.act_id;
  53. var act_type = obj.act_type;
  54. var goods_id = obj.goods_id;
  55. var sign = obj.key;
  56. if (serverid==null ||
  57. uid==null ||
  58. cfid==null ||
  59. money==null ||
  60. moneytype==null ||
  61. platform==null ||
  62. channel==null ||
  63. orderid==null ||
  64. act_id==null ||
  65. act_type==null ||
  66. goods_id==null ||
  67. sign == null
  68. ){
  69. res.send(JSON.stringify({ 'error':'参数不全'}));
  70. return;
  71. }
  72. var submittime = Date.parse(new Date()) / 1000;
  73. if (obj.desc==null){
  74. obj.desc = "gm";
  75. }
  76. var str_sign = "serverid="+serverid+
  77. "&uid="+uid+
  78. "&cfid="+cfid+
  79. "&money="+money+
  80. "&moneytype="+moneytype+
  81. "&platform="+platform+
  82. "&channel="+channel+
  83. "&orderid="+orderid+
  84. "&act_id="+act_id+
  85. "&act_type="+act_type+
  86. "&goods_id="+goods_id+
  87. "&key="+SIG_KEY_A;
  88. var mysign = encryption.md5(str_sign);
  89. if (mysign != sign){
  90. try{res.send( JSON.stringify( {'erron':'签名错误'}));}
  91. catch(e){}
  92. return;
  93. }
  94. query(sql_select(orderid),function(qerr,vals,fields){
  95. if (qerr==null){
  96. if (vals[0]){
  97. console.log("订单重复");
  98. try{res.send( JSON.stringify( {'erron':'订单重复'}));}
  99. catch(e){}
  100. returnorderID = 2;
  101. return;
  102. }
  103. var timestamp = Date.parse(new Date()) / 1000;
  104. var product_id = option.name
  105. var sql_1 = "INSERT INTO payinfo SET\
  106. orderid=?,\
  107. overtime=?,\
  108. flag=?,\
  109. money=?,\
  110. cfid=?,\
  111. platform=?,\
  112. channel=?,\
  113. moneytype=?,\
  114. product_id=?,\
  115. talkingdata=?,\
  116. act_id=?, \
  117. act_type=?, \
  118. goods_id=?, \
  119. istest=?";
  120. var values_2 = [orderid,timestamp,1,money,cfid,platform,channel,option.typename,product_id,1,act_id, act_type, goods_id,istest];
  121. var sql = mysql.format(sql_1, values_2);
  122. query(sql,function(qeer,vals,fields_){
  123. if(qeer==null){
  124. checkorder.pushproduct(query,orderid,uid,serverid,cfid,function(ok){
  125. if (ok) {
  126. console.log("[补单] 订单 %s: 成功发货!", orderid);
  127. } else{
  128. console.log("[补单] 订单 %s: 发货地址无效!", orderid);//在无效时返回成功,我们自己补单中会派发flag为1支付成功但是没成功发货的单
  129. }
  130. try{
  131. res.send(JSON.stringify({ 'error':'成功'}));
  132. }
  133. catch(e){}
  134. })
  135. }else{
  136. console.log(qeer);
  137. res.send(JSON.stringify({ 'error':'插入数据失败,请重新提交'}));
  138. }
  139. })
  140. }
  141. })
  142. }catch(err){
  143. console.log(err);
  144. res.send(JSON.stringify({ 'error':'请重新提交'}));
  145. }
  146. }
  147. //手动补单 成功 status:200 失败 status:400 其他错误 status:500
  148. var url_repair_order=function(args,res){
  149. try {
  150. console.log("补单消息:%s",args)
  151. var obj
  152. try {
  153. obj = qs.parse(args)
  154. console.log("qs解析成功")
  155. } catch(e) {
  156. console.log(e)
  157. res.send("FAILURE");
  158. return;
  159. }
  160. var serverid = obj.serverid;
  161. var uid = obj.uid;
  162. var cfid = obj.cfid;
  163. var money = obj.money;
  164. var moneytype = obj.moneytype;
  165. var platform = obj.platform;
  166. var channel = obj.channel;
  167. var orderid = obj.orderid;
  168. var act_id = obj.act_id;
  169. var act_type = obj.act_type;
  170. var goods_id = obj.goods_id;
  171. var sign = obj.key;
  172. var check_ret = false;
  173. var name = ""
  174. if (serverid==null){
  175. name = "serverid"
  176. }else if(uid==null) {
  177. name = "uid"
  178. }else if(cfid==null) {
  179. name = "cfid"
  180. }else if(money==null) {
  181. name = "money"
  182. }else if(moneytype==null) {
  183. name = "moneytype"
  184. }else if(platform==null) {
  185. name = "platform"
  186. }else if(channel==null) {
  187. name = "channel"
  188. }else if(orderid==null) {
  189. name = "orderid"
  190. }else if(act_id==null) {
  191. name = "act_id"
  192. }else if(act_type==null) {
  193. name = "act_type"
  194. }else if(goods_id==null) {
  195. name = "goods_id"
  196. }else if(sign==null) {
  197. name = "sign"
  198. }else{
  199. check_ret = true
  200. }
  201. if(!check_ret){
  202. console.log(name);
  203. res.send(JSON.stringify({ 'status':'500','msg':'参数不全'}));
  204. }
  205. var submittime = Date.parse(new Date()) / 1000;
  206. if (obj.desc==null){
  207. obj.desc = "gm";
  208. }
  209. var str_sign = "serverid="+serverid+
  210. "&uid="+uid+
  211. "&cfid="+cfid+
  212. "&money="+money+
  213. "&moneytype="+moneytype+
  214. "&platform="+platform+
  215. "&channel="+channel+
  216. "&orderid="+orderid+
  217. "&act_id="+act_id+
  218. "&act_type="+act_type+
  219. "&goods_id="+goods_id+
  220. "&key="+SIG_KEY_A;
  221. console.log(str_sign);
  222. console.log(obj.str);
  223. var mysign = encryption.md5(str_sign);
  224. if (mysign != sign){
  225. console.log(mysign);
  226. console.log(sign);
  227. try{res.send( JSON.stringify( {'status':'500','msg':'缺少参数'}));}
  228. catch(e){}
  229. return;
  230. }
  231. query(sql_select(orderid),function(qerr,vals,fields){
  232. if (qerr==null){
  233. if (vals[0]){
  234. console.log("订单重复");
  235. try{res.send( JSON.stringify( {'status':'400','msg':'订单重复'}));}
  236. catch(e){}
  237. return
  238. }
  239. var platform = 'android';
  240. var channel = 'replenish';
  241. var option = config.parse(platform,channel,cfid)
  242. var timestamp = Date.parse(new Date()) / 1000;
  243. var product_id = option.name
  244. var sql_1 = "INSERT INTO payinfo SET\
  245. orderid=?,\
  246. serverid=?,\
  247. overtime=?,\
  248. flag=?,\
  249. money=?,\
  250. cfid=?,\
  251. platform=?,\
  252. channel=?,\
  253. moneytype=?,\
  254. product_id=?,\
  255. talkingdata=?,\
  256. act_id=?, \
  257. act_type=?, \
  258. goods_id=?, \
  259. uid=?, \
  260. istest=?";
  261. var values_2 = [orderid,serverid, timestamp,1,money,cfid,platform,channel,moneytype,product_id,0,act_id, act_type, goods_id,uid,istest];
  262. var sql = mysql.format(sql_1, values_2);
  263. console.log(sql)
  264. query(sql,function(qeer,vals,fields_){
  265. if(qeer==null){
  266. checkorder.pushproduct(query,serverid,orderid,function(ok){
  267. if (ok) {
  268. console.log("[补单] 订单 %s: 成功发货!", orderid);
  269. } else{
  270. console.log("[补单] 订单 %s: 发货地址无效!", orderid);//在无效时返回成功,我们自己补单中会派发flag为1支付成功但是没成功发货的单
  271. }
  272. try{
  273. res.send(JSON.stringify({ 'status':'200','msg':'成功'}));
  274. }
  275. catch(e){}
  276. })
  277. }else{
  278. console.log(qeer);
  279. res.send(JSON.stringify({ 'status':'400','msg':'插入数据失败,请重新提交'}));
  280. }
  281. })
  282. }
  283. })
  284. }catch(err){
  285. console.log(err);
  286. res.send(JSON.stringify({ 'status':'400','msg':'请重新提交'}));
  287. }
  288. }
  289. var express = require('express');
  290. var router = express.Router();
  291. router.get('/repair_order', function(req, res) {
  292. try{
  293. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  294. console.log("get shoudong_repair_order,host:%s, ip:%s,time:%s",req.hostname,real_ip,new Date());
  295. var data = "";
  296. req.on("data",function(chunk){
  297. data += chunk;
  298. })
  299. req.on("end",function(){
  300. shoudong_repair_order(data,res);
  301. })
  302. req.on("error",function(err){
  303. var info = JSON.stringify({ 'error':'充值系统错误'});
  304. res.send(info);
  305. })
  306. }catch(err){
  307. console.log(err);
  308. var info = JSON.stringify({ 'error':'充值系统错误'});
  309. res.send(info);
  310. }
  311. })
  312. router.post('/repair_order', function(req, res) {
  313. var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;
  314. console.log("post repair_order,host:%s, ip:%s,path:%s,time:%s",req.hostname,real_ip,req.path,new Date());
  315. var data = "";
  316. req.on("data",function(chunk){
  317. data += chunk;
  318. });
  319. req.on("end",function(){
  320. try{
  321. url_repair_order(data,res)
  322. } catch(e) {
  323. console.log(e)
  324. res.send("FAILURE");
  325. }
  326. });
  327. req.on("error",function(err){
  328. console.log("error:%s",err);
  329. res.send("FAILURE");
  330. });
  331. });
  332. module.exports = router;