refill.php 19 KB

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