refill.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. require_once(BASE_ROOT_PATH . '/mobile/control/merchant.php');
  3. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  4. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  5. require_once(BASE_HELPER_PATH . '/model/member_info.php');
  6. //商家充值接口中心
  7. class refillControl extends merchantControl
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function goodsOp()
  14. {
  15. $goods_list = refill\RefillFactory::instance()->goods();
  16. $sorter = function (array $items) {
  17. $ret = [];
  18. foreach ($items as $key => $val) {
  19. sort($val);
  20. $ret[$key] = $val;
  21. }
  22. return $ret;
  23. };
  24. $goods_list = $sorter($goods_list);
  25. $result = [];
  26. foreach ($goods_list as $card_type => $value) {
  27. $key = mtopcard\scard_type($card_type);
  28. $result[$key] = $value;
  29. }
  30. return self::outsuccess($result);
  31. }
  32. private function check_params($params)
  33. {
  34. if(empty($params['cardno'])) {
  35. return [false,'参数没有包含cardno'];
  36. }
  37. if(empty($params['amount'])) {
  38. return [false,'参数没有包含充值金额:amount'];
  39. }
  40. $card_no = $_GET['cardno'];
  41. $card_type = mtopcard\simple_card_type($card_no);
  42. if($card_type == mtopcard\UnknownCard) {
  43. return [false,'卡类型无法识别'];
  44. }
  45. if(empty($params['notifyurl'])) {
  46. return [false,'参数没有包含notifyurl'];
  47. }
  48. if(empty($params['order_sn'])) {
  49. return [false,'参数没有包含贵方唯一订单号:order_sn'];
  50. }
  51. return [true,""];
  52. }
  53. public function addOp()
  54. {
  55. [$success,$error] = $this->check_params($_GET);
  56. if($success === false) {
  57. return self::outerr(201,$error);
  58. }
  59. $amount = intval($_GET['amount']);
  60. $card_no = trim($_GET['cardno']);
  61. $notify_url = $_GET['notifyurl'];
  62. $mch_order = $_GET['order_sn']; //对方的order编号
  63. $quality = $_GET['quality'] ?? 0;
  64. $bind_phone = $_GET['bind_phone'] ?? '';
  65. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  66. return self::outerr(205,"客户订单号重复或者为空.");
  67. }
  68. $card_type = mtopcard\simple_card_type($card_no);
  69. if($card_type === mtopcard\SinopecCard || $card_type === mtopcard\PetroChinaCard)
  70. {
  71. refill\util::write_card($card_no,$card_type,$bind_phone);
  72. if(!$this->can_refill($card_no)) {
  73. return self::outerr(206,"平台不支持该卡充值.");
  74. }
  75. $allow = true;
  76. if(!$allow)
  77. {
  78. if ($card_type === mtopcard\SinopecCard) {
  79. $text = "中石化";
  80. }
  81. else {
  82. $text = "中石油";
  83. }
  84. return self::outerr(207,"今日贵司{$amount}{$text}充值库存已经用完.");
  85. }
  86. }
  87. $order_time = time();
  88. $params = ['mchid' => $this->mchid(),
  89. 'buyer_id' => $this->adminid(),
  90. 'amount' => $amount,
  91. 'card_no' => $card_no,
  92. 'mch_order' => $mch_order,
  93. 'notify_url' => $notify_url,
  94. 'org_quality' => $quality,
  95. 'order_time' => $order_time
  96. ];
  97. refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE);
  98. Model('refill_order')->add_detail($this->mchid(),$mch_order,$order_time,$params,ORDER_STATE_QUEUE);
  99. [$can_refill, $period] = refill\util::can_commit($card_no, $card_type);
  100. if ($can_refill === false) {
  101. $state = refill\util::async_add($params, $period);
  102. } else {
  103. $state = refill\util::push_add($params);
  104. }
  105. $mchid = $this->mchid();
  106. if ($state === true) {
  107. Log::record("refill::util::push_add success mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG);
  108. return self::outsuccess(['state' => true]);
  109. } else {
  110. refill\util::del_queue_order($this->mchid(),$mch_order);
  111. Model('refill_order')->del_detail($this->mchid(),$mch_order);
  112. Log::record("refill::util::push_add error mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG);
  113. return self::outerr(208, '提交失败');
  114. }
  115. }
  116. private function check_third($params)
  117. {
  118. if(empty($params['product_code'])) {
  119. return [false,'参数没有包含product_code'];
  120. }
  121. $pcode = $params['product_code'];
  122. if($this->check_pcode($pcode) == false) {
  123. return [false,"对应的产品编码{$pcode}"];
  124. }
  125. if(empty($params['quantity']) || intval($params['quantity']) < 1) {
  126. return [false,"购买数量参数不存在,或者小于0"];
  127. }
  128. if(empty($params['notifyurl'])) {
  129. return [false,'参数没有包含notifyurl'];
  130. }
  131. if(empty($params['order_sn'])) {
  132. return [false,'参数没有包含贵方唯一订单号:order_sn'];
  133. }
  134. return [true,""];
  135. }
  136. private function check_pcode($pcode)
  137. {
  138. $mod_third = Model('thrid_refill');
  139. $product = $mod_third->getProduct(['system_code' => $pcode]);
  140. return !empty($product);
  141. }
  142. public function add_thirdOp()
  143. {
  144. [$success,$error] = $this->check_third($_GET);
  145. if($success === false) {
  146. return self::outerr(201,$error);
  147. }
  148. $amount = refill\util::ThirdRefillAmount;
  149. $notify_url = $_GET['notifyurl'];
  150. $mch_order = $_GET['order_sn']; //对方的order编号
  151. $product_code = $_GET['product_code'];
  152. $card_no = $_GET['cardno'] ?? '';
  153. $quantity = $_GET['quantity'];
  154. $third_card_type = $_GET['card_type'] ?? 1;
  155. //三方充值没有质量
  156. $quality = 1;
  157. $card_type = mtopcard\ThirdRefillCard;
  158. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  159. return self::outerr(205,"客户订单号重复或者为空.");
  160. }
  161. $order_time = time();
  162. $params = ['mchid' => $this->mchid(),
  163. 'buyer_id' => $this->adminid(),
  164. 'amount' => $amount,
  165. 'mch_order' => $mch_order,
  166. 'notify_url' => $notify_url,
  167. 'org_quality' => $quality,
  168. 'card_type' => $card_type,
  169. 'card_no' => $card_no,
  170. 'product_code' => $product_code,
  171. 'quantity' => $quantity,
  172. 'third_card_type' => $third_card_type,
  173. 'third_product_type' => mtopcard\ThirdOnlineProduct,
  174. 'order_time' => $order_time
  175. ];
  176. refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE);
  177. Model('refill_order')->add_detail($this->mchid(), $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
  178. $state = refill\util::push_addthird($params);
  179. if ($state === true) {
  180. return self::outsuccess(['state' => true]);
  181. } else {
  182. refill\util::del_queue_order($this->mchid(),$mch_order);
  183. Model('refill_order')->del_detail($this->mchid(),$mch_order);
  184. return self::outerr(208, '提交失败');
  185. }
  186. }
  187. private function check_electirc($params)
  188. {
  189. if(empty($params['cardno'])) {
  190. return [false,'参数没有包含cardno'];
  191. }
  192. if(empty($params['notifyurl'])) {
  193. return [false,'参数没有包含notifyurl'];
  194. }
  195. if(empty($params['order_sn'])) {
  196. return [false,'参数没有包含贵方唯一订单号:order_sn'];
  197. }
  198. if(empty($params['amount']) || intval($params['amount']) <= 0) {
  199. return [false,'参数没有包含充值金额:amount'];
  200. }
  201. if(empty($params['company_type']) || !in_array($params['company_type'],mtopcard\ElectricCompanyTypes)) {
  202. return [false,'没有电卡类型参数(nation,south)'];
  203. }
  204. if(empty($params['use_type']) || !in_array($params['use_type'],mtopcard\ElectricUseTypes)) {
  205. return [false,"没有电卡用途参数(home,commerce,pedlar)"];
  206. }
  207. if(empty($params['province']) || !array_key_exists($params['province'],mtopcard\ProvinceList)) {
  208. return [false,"请传入省份"];
  209. }
  210. if(empty($params['city'])) {
  211. return [false,"请传入城市名称"];
  212. }
  213. //如果是南方电网需要带身份证后六位
  214. if($params['company_type'] === 'south')
  215. {
  216. $card_id = $params['card_id'];
  217. if(empty($card_id) || strlen($card_id) != mtopcard\CardidVerifyLength || !mtopcard\is_alpha($card_id)) {
  218. return [false,"请传入正确的身份证后6位"];
  219. }
  220. }
  221. return [true,""];
  222. }
  223. public function add_electricOp()
  224. {
  225. [$success,$error] = $this->check_electirc($_GET);
  226. if($success === false) {
  227. return self::outerr(201,$error);
  228. }
  229. $notify_url = $_GET['notifyurl'];
  230. $mch_order = $_GET['order_sn']; //对方的order编号
  231. $card_no = $_GET['cardno'] ?? '';
  232. $quantity = 1;
  233. $third_card_type = mtopcard\ThirdCardElect;
  234. $company_type = $_GET['company_type'];
  235. $use_type = $_GET['use_type'];
  236. $province = intval($_GET['province']);
  237. $city = $_GET['city'];
  238. $amount = intval($_GET['amount']);
  239. if($company_type == 'nation') {
  240. $card_id = '';
  241. } else {
  242. $card_id = $_GET['card_id'] ?? '';
  243. }
  244. $product_code = mtopcard\electric_product_code($company_type,$use_type,$amount);
  245. $amount = refill\util::ThirdRefillAmount;
  246. //三方充值没有质量
  247. $quality = 1;
  248. $card_type = mtopcard\ThirdRefillCard;
  249. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  250. return self::outerr(205,"客户订单号重复或者为空.");
  251. }
  252. $order_time = time();
  253. $params = ['mchid' => $this->mchid(),
  254. 'buyer_id' => $this->adminid(),
  255. 'amount' => $amount,
  256. 'mch_order' => $mch_order,
  257. 'notify_url' => $notify_url,
  258. 'org_quality' => $quality,
  259. 'card_type' => $card_type,
  260. 'card_no' => $card_no,
  261. 'product_code' => $product_code,
  262. 'quantity' => $quantity,
  263. 'third_card_type' => $third_card_type,
  264. 'third_product_type' => mtopcard\ThirdElectricProduct,
  265. 'company_type' => $company_type,
  266. 'use_type' => $use_type,
  267. 'province' => $province,
  268. 'city' => $city,
  269. 'card_id' => $card_id,
  270. 'order_time' => $order_time
  271. ];
  272. refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE);
  273. Model('refill_order')->add_detail($this->mchid(), $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
  274. $state = refill\util::push_addthird($params);
  275. if ($state === true) {
  276. return self::outsuccess(['state' => true]);
  277. } else {
  278. refill\util::del_queue_order($this->mchid(),$mch_order);
  279. Model('refill_order')->del_detail($this->mchid(),$mch_order);
  280. return self::outerr(208, '提交失败');
  281. }
  282. }
  283. private function check_sinopec_coupon($params)
  284. {
  285. if(empty($params['cardno'])) {
  286. return [false,'参数没有包含cardno'];
  287. }
  288. if(empty($params['notifyurl'])) {
  289. return [false,'参数没有包含notifyurl'];
  290. }
  291. if(empty($params['order_sn'])) {
  292. return [false,'参数没有包含贵方唯一订单号:order_sn'];
  293. }
  294. if(empty($params['amount']) || intval($params['amount']) <= 0) {
  295. return [false,'参数没有包含充值金额:amount'];
  296. }
  297. if(empty($params['province']) || !array_key_exists($params['province'],mtopcard\ProvinceList)) {
  298. return [false,"请传入省份"];
  299. }
  300. return [true,""];
  301. }
  302. public function add_sinopec_couponOp()
  303. {
  304. [$success,$error] = $this->check_sinopec_coupon($_GET);
  305. if($success === false) {
  306. return self::outerr(201,$error);
  307. }
  308. $notify_url = $_GET['notifyurl'];
  309. $mch_order = $_GET['order_sn']; //对方的order编号
  310. $card_no = $_GET['cardno'] ?? '';
  311. $quantity = 1;
  312. $third_card_type = mtopcard\ThirdCardPhone;
  313. $province = intval($_GET['province']);
  314. $amount = intval($_GET['amount']);
  315. $product_code = mtopcard\sino_coupon_product_code($amount);
  316. $amount = refill\util::ThirdRefillAmount;
  317. //三方充值没有质量
  318. $quality = 1;
  319. $card_type = mtopcard\ThirdRefillCard;
  320. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  321. return self::outerr(205,"客户订单号重复或者为空.");
  322. }
  323. $order_time = time();
  324. $params = ['mchid' => $this->mchid(),
  325. 'buyer_id' => $this->adminid(),
  326. 'amount' => $amount,
  327. 'mch_order' => $mch_order,
  328. 'notify_url' => $notify_url,
  329. 'org_quality' => $quality,
  330. 'card_type' => $card_type,
  331. 'card_no' => $card_no,
  332. 'product_code' => $product_code,
  333. 'quantity' => $quantity,
  334. 'third_card_type' => $third_card_type,
  335. 'third_product_type' => mtopcard\ThirdSinopecECouponPoroduct,
  336. 'province' => $province,
  337. 'order_time' => $order_time
  338. ];
  339. refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE);
  340. Model('refill_order')->add_detail($this->mchid(), $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
  341. $state = refill\util::push_addthird($params);
  342. if ($state === true) {
  343. return self::outsuccess(['state' => true]);
  344. } else {
  345. refill\util::del_queue_order($this->mchid(),$mch_order);
  346. Model('refill_order')->del_detail($this->mchid(),$mch_order);
  347. return self::outerr(208, '提交失败');
  348. }
  349. }
  350. private function can_refill($cardno)
  351. {
  352. $card_info = refill\util::read_card($cardno);
  353. if(empty($card_info)) return false;
  354. return intval($card_info['black_card']) === 0;
  355. }
  356. private function check_mchorder($mchid,$mch_order)
  357. {
  358. if (empty($mch_order)) {
  359. return false;
  360. } else {
  361. $refill_order = Model('refill_order');
  362. $ret = $refill_order->exist($mchid, $mch_order, refill\util::calc_partition());
  363. return ($ret == false);
  364. }
  365. }
  366. public function balanceOp()
  367. {
  368. $uid = $this->adminid();
  369. $minfo = new member_info($uid);
  370. $available = $minfo->available_predeposit();
  371. return self::outsuccess(['balance' => $available]);
  372. }
  373. public function query_tradeOp()
  374. {
  375. $mchid = $this->mchid();
  376. $trade_no = $_GET['trade_no'];
  377. if(empty($trade_no)) {
  378. return self::outerr(201,"交易号trade_no错误");
  379. }
  380. $mod_refill = Model('refill_order');
  381. $refill_info = $mod_refill->getOrderInfo(['order_sn' => $trade_no,'mchid' => $mchid]);
  382. $vr_order = Model('vr_order');
  383. $order_info = $vr_order->getOrderInfo(['order_sn' => $trade_no]);
  384. if(empty($refill_info) || empty($order_info)) {
  385. return self::outerr(201,"无此交易号");
  386. }
  387. $result = $this->format($order_info,$refill_info);
  388. return self::outsuccess($result);
  389. }
  390. private function format($order_info,$refill_info)
  391. {
  392. $result = [];
  393. $result['mchid'] = $refill_info['mchid'];
  394. $result['trade_no'] = $refill_info['order_sn'];
  395. $result['order_sn'] = $refill_info['mch_order'];
  396. $result['card_no'] = $refill_info['card_no'];
  397. $result['card_type'] = $refill_info['card_type'];
  398. $result['refill_amount'] = $refill_info['refill_amount'];
  399. $result['order_amount'] = $refill_info['mch_amount'];
  400. $result['order_time'] = $refill_info['order_time'];
  401. $result['success_time'] = $refill_info['notify_time'];
  402. $result['official_sn'] = $refill_info['official_sn'] ?? "";
  403. $result['order_state'] = $order_info['order_state'];
  404. return $result;
  405. }
  406. public function queryOp()
  407. {
  408. $mchid = $this->mchid();
  409. $order_sn = $_GET['order_sn']; //用户方的订单号,对应数据库中的mch_order
  410. if(empty($order_sn)) {
  411. return self::outerr(201,"客户订单号order_sn错误");
  412. }
  413. $order_state = refill\util::query_queue_order($mchid,$order_sn);
  414. if ($order_state == ORDER_STATE_QUEUE || $order_state == ORDER_STATE_SEND) {
  415. Log::record("query_state in queue mchid={$mchid} mch_order={$order_sn} order_state={$order_state}" ,Log::DEBUG);
  416. $result['mchid'] = $mchid;
  417. $result['order_sn'] = $order_sn;
  418. $result['order_state'] = $order_state;
  419. return self::outsuccess($result);
  420. }
  421. else
  422. {
  423. $mod_refill = Model('refill_order');
  424. $refill_info = $mod_refill->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid,'inner_status' => 0]);
  425. if(empty($refill_info))
  426. {
  427. Log::record("query_state in db no order mchid={$mchid} mch_order={$order_sn}" ,Log::DEBUG);
  428. return self::outerr(202, "无此订单");
  429. }
  430. else
  431. {
  432. $vr_order = Model('vr_order');
  433. $order_info = $vr_order->getOrderInfo(['order_sn' => $refill_info['order_sn']]);
  434. Log::record("query_state in db mchid={$mchid} mch_order={$order_sn} order_state={$order_info['order_state']}" ,Log::DEBUG);
  435. if (empty($order_info)) {
  436. return self::outerr(203, "无此交易号");
  437. } else {
  438. $result = $this->format($order_info, $refill_info);
  439. return self::outsuccess($result);
  440. }
  441. }
  442. }
  443. }
  444. }