refill.php 21 KB

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