lzrefill.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(!preg_match('/^1\d{10}$/',$card_no,$matches)) {
  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($this->mchid(),$mch_order,time());
  84. $ret = refill\util::push_add($params);
  85. if($ret) {
  86. return self::outsuccess($this->merchant_available);
  87. }
  88. else {
  89. return self::outerr(-6,$this->merchant_available);
  90. }
  91. }
  92. private function check_third($params)
  93. {
  94. if(empty($params['product_code'])) {
  95. return -9;
  96. }
  97. $pcode = $params['product_code'];
  98. if($this->check_pcode($pcode) == false) {
  99. return -10;
  100. }
  101. if(empty($params['quantity']) || intval($params['quantity']) < 1) {
  102. return -9;
  103. }
  104. $card_no = $params['card_no'];
  105. if(empty($card_no)) {
  106. return -9;
  107. }
  108. $third_card_type = $params['card_type'];
  109. if(empty($third_card_type)) {
  110. return -9;
  111. }
  112. if(empty($params['notifyurl'])) {
  113. return -9;
  114. }
  115. if(empty($params['ord'])) {
  116. return -9;
  117. }
  118. if(empty($params['tim'])) {
  119. return -9;
  120. }
  121. $tim = strtotime($params['tim']);
  122. if($tim === false) {
  123. return -9;
  124. }
  125. $cur = time();
  126. $start = strtotime(date('Y-m-d',$cur - 1800));
  127. if($tim < $start || $tim > $cur + 1800) {
  128. return -9;
  129. }
  130. return true;
  131. }
  132. private function check_pcode($pcode)
  133. {
  134. $mod_third = Model('thrid_refill');
  135. $product = $mod_third->getProduct(['system_code' => $pcode]);
  136. return !empty($product);
  137. }
  138. public function add_thirdOp()
  139. {
  140. $code = $this->check_third($_GET);
  141. if($code !== true) {
  142. return self::outerr($code,$this->merchant_available);
  143. }
  144. $amount = refill\util::ThirdRefillAmount;
  145. $mch_order = $_GET['ord'];
  146. $notify_url = $_GET['notifyurl'];
  147. //三方充值没有质量
  148. $quality = 1;
  149. $card_type = mtopcard\ThirdRefillCard;
  150. $product_code = $_GET['product_code'];
  151. $card_no = $_GET['mob'];
  152. $quantity = $_GET['quantity'];
  153. $third_card_type = $_GET['card_type'];
  154. //此处判断是对的
  155. if($this->member_available <= 0) {
  156. return self::outerr(4,$this->merchant_available);
  157. }
  158. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  159. return self::outerr(11,$this->merchant_available);
  160. }
  161. $params = [ 'mchid' => $this->mchid(),
  162. 'buyer_id' => $this->adminid(),
  163. 'amount' => $amount,
  164. 'mch_order' => $mch_order,
  165. 'notify_url' => $notify_url,
  166. 'org_quality' => $quality,
  167. 'card_type' => $card_type,
  168. 'card_no' => $card_no,
  169. 'product_code' => $product_code,
  170. 'quantity' => $quantity,
  171. 'third_card_type' => $third_card_type
  172. ];
  173. $ret = refill\util::push_addthird($params);
  174. if($ret) {
  175. return self::outsuccess($this->merchant_available);
  176. }
  177. else {
  178. return self::outerr(-6,$this->merchant_available);
  179. }
  180. }
  181. private function can_refill($cardno)
  182. {
  183. $card_info = refill\util::read_card($cardno);
  184. if(empty($card_info)) return false;
  185. return intval($card_info['black_card']) === 0;
  186. }
  187. private function check_mchorder($mchid,$mch_order)
  188. {
  189. if(empty($mch_order)) {
  190. return false;
  191. }
  192. else {
  193. $refill_order = Model('refill_order');
  194. $ret = $refill_order->getOrderInfo(['mchid' => $mchid,'mch_order' => $mch_order]);
  195. return empty($ret);
  196. }
  197. }
  198. public function balanceOp()
  199. {
  200. $uid = $this->adminid();
  201. $minfo = new member_info($uid);
  202. $available = $minfo->available_predeposit();
  203. $content = "0|".ncPriceFormat($available)."|0.00";
  204. echo $content;
  205. return true;
  206. }
  207. public function queryOp()
  208. {
  209. $mchid = $this->mchid();
  210. $order_sn = $_GET['ord']; //用户方的订单号,对应数据库中的mch_order
  211. if(empty($order_sn)) {
  212. return self::outerr(-9,$this->merchant_available);
  213. }
  214. $mod_refill = Model('refill_order');
  215. $refill_info = $mod_refill->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid,'inner_status' => 0]);
  216. if(empty($refill_info))
  217. {
  218. $state = 3;
  219. $msg = "{$order_sn}-无此订单";
  220. $remark = "";
  221. $content = "|{$state}|{$msg}|{$remark}";
  222. echo $content;
  223. return true;
  224. }
  225. if(empty($refill_info))
  226. {
  227. $ret = refill\util::query_queue($mchid,$order_sn);
  228. if ($ret > 0) {
  229. $state = 0;
  230. $msg = "{$order_sn}-充值中";
  231. $remark = "";
  232. $content = "|{$state}|{$msg}|{$remark}";
  233. echo $content;
  234. return true;
  235. } else {
  236. $state = 3;
  237. $msg = "{$order_sn}-无此订单";
  238. $remark = "";
  239. $content = "|{$state}|{$msg}|{$remark}";
  240. echo $content;
  241. return true;
  242. }
  243. }
  244. $vr_order = Model('vr_order');
  245. $order_info = $vr_order->getOrderInfo(['order_sn' => $refill_info['order_sn']]);
  246. if($refill_info['is_retrying'])
  247. {
  248. QueueClient::push("QueryRefillState",['order_id' => $refill_info['order_id']]);
  249. $state = 0;
  250. $msg = "{$order_sn}-充值中";
  251. $remark = "";
  252. }
  253. else
  254. {
  255. $order_state = $order_info['order_state'];
  256. if($order_state == ORDER_STATE_SEND || $order_state == ORDER_STATE_PAY || $order_state == ORDER_STATE_NEW) {
  257. QueueClient::push("QueryRefillState",['order_id' => $refill_info['order_id']]);
  258. $state = 0;
  259. $msg = "{$order_sn}-充值中";
  260. $remark = "";
  261. }
  262. elseif($order_state == ORDER_STATE_CANCEL) {
  263. $state = 2;
  264. $msg = "{$order_sn}-充值失败";
  265. $remark = "";
  266. }
  267. else {
  268. $state = 1;
  269. $msg = "{$order_sn}-充值成功";
  270. $remark = "{$refill_info['official_sn']}";
  271. }
  272. }
  273. if($refill_info['card_type'] == mtopcard\ThirdRefillCard) {
  274. $remark = $this->third_remark($refill_info);
  275. }
  276. $content = "|{$state}|{$msg}|{$remark}";
  277. echo $content;
  278. return true;
  279. }
  280. private function third_remark($refill_info)
  281. {
  282. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $this->mchid()]);
  283. $mod_third = Model('thrid_refill');
  284. $thrid_info = $mod_third->getThird($refill_info['order_id']);
  285. $secure_key = $mchinfo['secure_key'];
  286. $remark = '';
  287. if (!empty($thrid_info))
  288. {
  289. $card_info = $thrid_info['card_info'];
  290. if (!empty($card_info)) {
  291. $encrypt = openssl_encrypt($card_info,'AES-128-CBC',$secure_key);
  292. if($encrypt != false) {
  293. $remark = $encrypt;
  294. }
  295. }
  296. }
  297. return $remark;
  298. }
  299. }