lzrefill.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. class lzrefillControl extends lzbaseControl
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. public function goodsOp()
  9. {
  10. $result = refill\RefillFactory::instance()->goods();
  11. $sorter = function (array $items) {
  12. $ret = [];
  13. foreach ($items as $key => $val) {
  14. sort($val);
  15. $ret[$key] = $val;
  16. }
  17. return $ret;
  18. };
  19. $result = $sorter($result);
  20. return self::outsuccess($result);
  21. }
  22. private function check_params($params)
  23. {
  24. if(empty($params['mob'])) {
  25. return -9;
  26. }
  27. if(empty($params['amt'])) {
  28. return -9;
  29. }
  30. $card_no = $params['mob'];
  31. if(empty($card_no)) {
  32. return -11;
  33. }
  34. if(empty($params['notifyurl'])) {
  35. return -9;
  36. }
  37. if(empty($params['ord'])) {
  38. return -9;
  39. }
  40. if(empty($params['tim'])) {
  41. return -9;
  42. }
  43. $tim = strtotime($params['tim']);
  44. if($tim === false) {
  45. return -9;
  46. }
  47. $cur = time();
  48. $start = strtotime(date('Y-m-d',$cur - 1800));
  49. if($tim < $start || $tim > $cur + 1800) {
  50. return -9;
  51. }
  52. return true;
  53. }
  54. public function add_mobOp()
  55. {
  56. $code = $this->check_params($_GET);
  57. if($code !== true) {
  58. return self::outerr($code,$this->merchant_available);
  59. }
  60. $amount = intval($_GET['amt']);
  61. $card_no = $_GET['mob'];
  62. $notify_url = $_GET['notifyurl'];
  63. $mch_order = $_GET['ord']; //对方的order编号
  64. $quality = $_GET['quality'] ?? 0;
  65. //此处判断是对的
  66. if($amount > $this->member_available) {
  67. return self::outerr(4,$this->merchant_available);
  68. }
  69. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  70. return self::outerr(11,$this->merchant_available);
  71. }
  72. // if(!$this->can_refill($card_no)) {
  73. // return self::outerr(10,$this->merchant_available);
  74. // }
  75. $params = [ 'mchid' => $this->mchid(),
  76. 'buyer_id' => $this->adminid(),
  77. 'amount' => $amount,
  78. 'card_no' => $card_no,
  79. 'mch_order' => $mch_order,
  80. 'notify_url' => $notify_url,
  81. 'org_quality' => $quality
  82. ];
  83. refill\util::push_queue_order($this->mchid(), $mch_order, ORDER_STATE_QUEUE);
  84. Model('refill_order')->add_detail($this->mchid(),$mch_order,$params,ORDER_STATE_QUEUE);
  85. $ret = refill\util::push_add($params);
  86. if ($ret) {
  87. return self::outsuccess($this->merchant_available);
  88. } else {
  89. refill\util::del_queue_order($this->mchid(),$mch_order);
  90. Model('refill_order')->del_detail($this->mchid(),$mch_order);
  91. return self::outerr(-6, $this->merchant_available);
  92. }
  93. }
  94. private function check_third($params)
  95. {
  96. if(empty($params['product_code'])) {
  97. Log::record("check_third product_code",Log::ERR);
  98. return -9;
  99. }
  100. $pcode = $params['product_code'];
  101. if($this->check_pcode($pcode) == false) {
  102. Log::record("check_third check_pcode",Log::ERR);
  103. return -10;
  104. }
  105. if(empty($params['quantity']) || intval($params['quantity']) < 1) {
  106. Log::record("check_third quantity",Log::ERR);
  107. return -9;
  108. }
  109. $card_no = $params['mob'];
  110. if(empty($card_no)) {
  111. Log::record("check_third mob",Log::ERR);
  112. return -9;
  113. }
  114. $third_card_type = $params['card_type'];
  115. if(empty($third_card_type)) {
  116. Log::record("check_third card_type",Log::ERR);
  117. return -9;
  118. }
  119. if(empty($params['notifyurl'])) {
  120. Log::record("check_third notifyurl",Log::ERR);
  121. return -9;
  122. }
  123. if(empty($params['ord'])) {
  124. Log::record("check_third ord",Log::ERR);
  125. return -9;
  126. }
  127. if(empty($params['tim'])) {
  128. Log::record("check_third tim",Log::ERR);
  129. return -9;
  130. }
  131. $tim = strtotime($params['tim']);
  132. if($tim === false) {
  133. Log::record("check_third strtotime tim",Log::ERR);
  134. return -9;
  135. }
  136. $cur = time();
  137. $start = strtotime(date('Y-m-d',$cur - 1800));
  138. if($tim < $start || $tim > $cur + 1800) {
  139. Log::record("check_third check tim",Log::ERR);
  140. return -9;
  141. }
  142. return true;
  143. }
  144. private function check_pcode($pcode)
  145. {
  146. $mod_third = Model('thrid_refill');
  147. $product = $mod_third->getProduct(['system_code' => $pcode]);
  148. return !empty($product);
  149. }
  150. public function add_thirdOp()
  151. {
  152. $code = $this->check_third($_GET);
  153. if($code !== true) {
  154. return self::outerr($code,$this->merchant_available);
  155. }
  156. $amount = refill\util::ThirdRefillAmount;
  157. $mch_order = $_GET['ord'];
  158. $notify_url = $_GET['notifyurl'];
  159. //三方充值没有质量
  160. $quality = 1;
  161. $card_type = mtopcard\ThirdRefillCard;
  162. $product_code = $_GET['product_code'];
  163. $card_no = $_GET['mob'];
  164. $quantity = $_GET['quantity'];
  165. $third_card_type = $_GET['card_type'];
  166. //此处判断是对的
  167. if($this->member_available <= 0) {
  168. return self::outerr(4,$this->merchant_available);
  169. }
  170. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  171. return self::outerr(11,$this->merchant_available);
  172. }
  173. $params = [ 'mchid' => $this->mchid(),
  174. 'buyer_id' => $this->adminid(),
  175. 'amount' => $amount,
  176. 'mch_order' => $mch_order,
  177. 'notify_url' => $notify_url,
  178. 'org_quality' => $quality,
  179. 'card_type' => $card_type,
  180. 'card_no' => $card_no,
  181. 'product_code' => $product_code,
  182. 'quantity' => $quantity,
  183. 'third_card_type' => $third_card_type
  184. ];
  185. refill\util::push_queue_order($this->mchid(), $mch_order, ORDER_STATE_QUEUE);
  186. Model('refill_order')->add_detail($this->mchid(),$mch_order,$params,ORDER_STATE_QUEUE);
  187. $ret = refill\util::push_addthird($params);
  188. if($ret) {
  189. return self::outsuccess($this->merchant_available);
  190. }
  191. else {
  192. refill\util::del_queue_order($this->mchid(),$mch_order);
  193. Model('refill_order')->del_detail($this->mchid(),$mch_order);
  194. return self::outerr(-6,$this->merchant_available);
  195. }
  196. }
  197. private function can_refill($cardno)
  198. {
  199. $card_info = refill\util::read_card($cardno);
  200. if(empty($card_info)) return false;
  201. return intval($card_info['black_card']) === 0;
  202. }
  203. private function check_mchorder($mchid,$mch_order)
  204. {
  205. if (empty($mch_order)) {
  206. return false;
  207. } else {
  208. $refill_order = Model('refill_order');
  209. $ret = $refill_order->exist($mchid, $mch_order);
  210. return ($ret == false);
  211. }
  212. }
  213. public function balanceOp()
  214. {
  215. $uid = $this->adminid();
  216. $minfo = new member_info($uid);
  217. $available = $minfo->available_predeposit();
  218. $content = "0|".ncPriceFormat($available)."|0.00";
  219. echo $content;
  220. return true;
  221. }
  222. public function queryOp()
  223. {
  224. $mchid = $this->mchid();
  225. $order_sn = $_GET['ord']; //用户方的订单号,对应数据库中的mch_order
  226. if(empty($order_sn)) {
  227. return self::outerr(-9,$this->merchant_available);
  228. }
  229. $order_state = refill\util::query_queue_order($mchid,$order_sn);
  230. if ($order_state == ORDER_STATE_QUEUE || $order_state == ORDER_STATE_SEND) {
  231. Log::record("query_state in queue mchid={$mchid} mch_order={$order_sn} order_state={$order_state}" ,Log::DEBUG);
  232. $state = 0;
  233. $msg = "{$order_sn}-充值中";
  234. $remark = "";
  235. $content = "|{$state}|{$msg}|{$remark}";
  236. echo $content;
  237. $this->query_request($mchid,$order_sn);
  238. return true;
  239. }
  240. else
  241. {
  242. $mod_refill = Model('refill_order');
  243. $refill_info = $mod_refill->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid,'inner_status' => 0]);
  244. if(empty($refill_info))
  245. {
  246. Log::record("query_state in db no order mchid={$mchid} mch_order={$order_sn}" ,Log::DEBUG);
  247. $state = 3;
  248. $msg = "{$order_sn}-无此订单";
  249. $remark = "";
  250. $content = "|{$state}|{$msg}|{$remark}";
  251. echo $content;
  252. return true;
  253. }
  254. else
  255. {
  256. $vr_order = Model('vr_order');
  257. $order_info = $vr_order->getOrderInfo(['order_sn' => $refill_info['order_sn']]);
  258. $order_state = $order_info['order_state'];
  259. Log::record("query_state in db mchid={$mchid} mch_order={$order_sn} order_state={$order_state}" ,Log::DEBUG);
  260. if ($order_state == ORDER_STATE_CANCEL) {
  261. $state = 2;
  262. $msg = "{$order_sn}-充值失败";
  263. $remark = "";
  264. }
  265. elseif($order_state == ORDER_STATE_SUCCESS) {
  266. $state = 1;
  267. $msg = "{$order_sn}-充值成功";
  268. $remark = "{$refill_info['official_sn']}";
  269. }
  270. else {
  271. $state = 0;
  272. $msg = "{$order_sn}-充值中";
  273. $remark = "";
  274. }
  275. if($refill_info['card_type'] == mtopcard\ThirdRefillCard) {
  276. $remark = $this->third_remark($refill_info);
  277. }
  278. $content = "|{$state}|{$msg}|{$remark}";
  279. echo $content;
  280. return true;
  281. }
  282. }
  283. }
  284. private function query_request($mchid,$order_sn)
  285. {
  286. $mod_refill = Model('refill_order');
  287. $refill_info = $mod_refill->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid,'inner_status' => 0]);
  288. if(!empty($refill_info))
  289. {
  290. $vr_order = Model('vr_order');
  291. $order_info = $vr_order->getOrderInfo(['order_sn' => $refill_info['order_sn']]);
  292. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  293. QueueClient::push("QueryRefillState",['order_id' => $refill_info['order_id']]);
  294. }
  295. }
  296. }
  297. private function third_remark($refill_info)
  298. {
  299. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $this->mchid()]);
  300. $mod_third = Model('thrid_refill');
  301. $thrid_info = $mod_third->getThird($refill_info['order_id']);
  302. $secure_key = $mchinfo['secure_key'];
  303. $remark = '';
  304. if (!empty($thrid_info))
  305. {
  306. $card_info = $thrid_info['card_info'];
  307. if (!empty($card_info)) {
  308. $encrypt = openssl_encrypt($card_info,'AES-128-CBC',$secure_key);
  309. if($encrypt != false) {
  310. $remark = $encrypt;
  311. }
  312. }
  313. }
  314. return $remark;
  315. }
  316. }