RefillFactory.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/rbridge/RBridgeFactory.php');
  5. require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
  6. require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
  7. require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
  8. require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
  9. require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
  10. require_once(BASE_HELPER_PATH . '/refill/suhc/RefillOil.php');
  11. require_once(BASE_HELPER_PATH . '/refill/suhc/RefillPhone.php');
  12. require_once(BASE_HELPER_PATH . '/refill/suhc/RefillCallBack.php');
  13. require_once(BASE_HELPER_PATH . '/refill/suhctm/RefillOil.php');
  14. require_once(BASE_HELPER_PATH . '/refill/suhctm/RefillPhone.php');
  15. require_once(BASE_HELPER_PATH . '/refill/suhctm/RefillCallBack.php');
  16. require_once(BASE_HELPER_PATH . '/refill/suhcpdd/RefillOil.php');
  17. require_once(BASE_HELPER_PATH . '/refill/suhcpdd/RefillPhone.php');
  18. require_once(BASE_HELPER_PATH . '/refill/suhcpdd/RefillCallBack.php');
  19. require_once(BASE_HELPER_PATH . '/refill/beixt/RefillPhone.php');
  20. require_once(BASE_HELPER_PATH . '/refill/beixt/RefillCallBack.php');
  21. require_once(BASE_HELPER_PATH . '/refill/bxtwt/RefillPhone.php');
  22. require_once(BASE_HELPER_PATH . '/refill/bxtwt/RefillCallBack.php');
  23. require_once(BASE_HELPER_PATH . '/refill/bjb/RefillPhone.php');
  24. require_once(BASE_HELPER_PATH . '/refill/bjb/RefillCallBack.php');
  25. require_once(BASE_HELPER_PATH . '/refill/bdt/RefillPhone.php');
  26. require_once(BASE_HELPER_PATH . '/refill/bdt/RefillCallBack.php');
  27. require_once(BASE_HELPER_PATH . '/refill/zzx/RefillOil.php');
  28. require_once(BASE_HELPER_PATH . '/refill/zzx/RefillPhone.php');
  29. require_once(BASE_HELPER_PATH . '/refill/zzx/RefillCallBack.php');
  30. require_once(BASE_HELPER_PATH . '/refill/lx/RefillOil.php');
  31. require_once(BASE_HELPER_PATH . '/refill/lx/RefillPhone.php');
  32. require_once(BASE_HELPER_PATH . '/refill/lx/RefillCallBack.php');
  33. require_once(BASE_HELPER_PATH . '/refill/saihu/RefillOil.php');
  34. require_once(BASE_HELPER_PATH . '/refill/saihu/RefillPhone.php');
  35. require_once(BASE_HELPER_PATH . '/refill/saihu/RefillCallBack.php');
  36. require_once(BASE_HELPER_PATH . '/refill/yifa/RefillPhone.php');
  37. require_once(BASE_HELPER_PATH . '/refill/yifa/RefillCallBack.php');
  38. require_once(BASE_HELPER_PATH . '/refill/jiec/RefillPhone.php');
  39. require_once(BASE_HELPER_PATH . '/refill/jiec/RefillCallBack.php');
  40. require_once(BASE_HELPER_PATH . '/refill/xc/RefillPhone.php');
  41. require_once(BASE_HELPER_PATH . '/refill/xc/RefillCallBack.php');
  42. require_once(BASE_HELPER_PATH . '/refill/gftd/RefillOil.php');
  43. require_once(BASE_HELPER_PATH . '/refill/gftd/RefillCallBack.php');
  44. require_once(BASE_HELPER_PATH . '/refill/gftdsinop/RefillOil.php');
  45. require_once(BASE_HELPER_PATH . '/refill/gftdsinop/RefillCallBack.php');
  46. require_once(BASE_HELPER_PATH . '/refill/weit/RefillPhone.php');
  47. require_once(BASE_HELPER_PATH . '/refill/weit/RefillCallBack.php');
  48. use Log;
  49. use mtopcard;
  50. use QueueClient;
  51. use member_info;
  52. use Exception;
  53. use rbridge\RBridgeFactory;
  54. use trans_wapper;
  55. class RefillFactory
  56. {
  57. private static $stInstance = null;
  58. public static function instance()
  59. {
  60. if (self::$stInstance == null) {
  61. self::$stInstance = new RefillFactory();
  62. }
  63. return self::$stInstance;
  64. }
  65. private $mOilProvider = [];
  66. private $mPhoneProvider = [];
  67. private $mProviderNames = [];
  68. private function __construct()
  69. {
  70. $this->load();
  71. }
  72. public function goods()
  73. {
  74. global $config;
  75. $oil = $this->combine_goods($config['oil_providers'], 1);
  76. $phone = $this->combine_goods($config['phone_providers'], 2);
  77. return array_merge($oil, $phone);
  78. }
  79. public function providers()
  80. {
  81. return ['oil' => $this->mOilProvider,'phone' => $this->mPhoneProvider];
  82. }
  83. private function combine_goods($configs,$type)
  84. {
  85. $mod_prov = Model('refill_provider');
  86. $provider_items = $mod_prov->getProviderList(['type' => $type]);
  87. foreach ($provider_items as $item) {
  88. $providers[$item['name']] = $item;
  89. }
  90. $result = [];
  91. foreach ($configs as $item)
  92. {
  93. if($providers[$item['name']]['opened'] != 1) {
  94. continue;
  95. }
  96. $cfg = $item['cfg'];
  97. $card_types = $cfg['card_type'] ?? [];
  98. $amounts = $cfg['amount'] ?? [];
  99. foreach ($card_types as $type) {
  100. if (array_key_exists($type, $result)) {
  101. $item = $result[$type];
  102. } else {
  103. $item = [];
  104. }
  105. foreach ($amounts as $amount => $val) {
  106. $item[] = $amount;
  107. }
  108. $item = array_unique($item);
  109. $result[$type] = $item;
  110. }
  111. }
  112. return $result;
  113. }
  114. private function load()
  115. {
  116. global $config;
  117. $oil_configs = $config['oil_providers'];
  118. $names = [];
  119. foreach ($oil_configs as $item)
  120. {
  121. $name = $item['name'];
  122. $cfg = $item['cfg'];
  123. $opened = $item['opened'] ?? true;
  124. $sort = $item['sort'] ?? 65536;
  125. $names[] = $name;
  126. try {
  127. $class = "refill\\{$name}\\RefillOil";
  128. if (class_exists($class, false)) {
  129. $provider = new $class($cfg);
  130. $provider->setOpened($opened);
  131. $provider->setSort($sort);
  132. $this->mOilProvider[] = $provider;
  133. } else {
  134. $error = "Base Error: class {$class} isn't exists!";
  135. throw new Exception($error);
  136. }
  137. } catch (Exception $ex) {
  138. Log::record($ex->getMessage(), Log::ERR);
  139. }
  140. }
  141. $pho_configs = $config['phone_providers'];
  142. foreach ($pho_configs as $item)
  143. {
  144. $name = $item['name'];
  145. $cfg = $item['cfg'];
  146. $opened = $item['opened'] ?? true;
  147. $sort = $item['sort'] ?? 65536;
  148. $names[] = $name;
  149. try {
  150. $class = "refill\\{$name}\\RefillPhone";
  151. if (class_exists($class, false)) {
  152. $provider = new $class($cfg);
  153. $provider->setOpened($opened);
  154. $provider->setSort($sort);
  155. $this->mPhoneProvider[] = $provider;
  156. } else {
  157. $error = "Base Error: class {$class} isn't exists!";
  158. throw new Exception($error);
  159. }
  160. } catch (Exception $ex) {
  161. Log::record($ex->getMessage(), Log::ERR);
  162. }
  163. }
  164. $this->mProviderNames = array_unique($names);
  165. }
  166. public function find_providers(int $amount, int $card_type): array
  167. {
  168. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  169. return $this->find_oil($amount, $card_type);
  170. } elseif ($card_type == mtopcard\ChinaMobileCard || $card_type == mtopcard\ChinaUnicomCard || $card_type == mtopcard\ChinaTelecomCard) {
  171. return $this->find_phone($amount, $card_type);
  172. } else {
  173. return [];
  174. }
  175. }
  176. private function find_oil(int $amount, int $card_type): array
  177. {
  178. $providers = [];
  179. foreach ($this->mOilProvider as $provider) {
  180. $name = $provider->name();
  181. [$success, $err] = $provider->check($amount, $card_type);
  182. if ($success) {
  183. $providers[] = $provider;
  184. } else {
  185. Log::record("{$name} provider cannot match check,err:{$err}", Log::DEBUG);
  186. }
  187. }
  188. return $providers;
  189. }
  190. public function provider(string $chname, int $card_type)
  191. {
  192. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  193. $providers = $this->mOilProvider;
  194. } elseif ($card_type == mtopcard\ChinaMobileCard || $card_type == mtopcard\ChinaUnicomCard || $card_type == mtopcard\ChinaTelecomCard) {
  195. $providers = $this->mPhoneProvider;
  196. } else {
  197. return null;
  198. }
  199. foreach ($providers as $provider) {
  200. if ($provider->name() == $chname) {
  201. return $provider;
  202. }
  203. }
  204. return null;
  205. }
  206. private function find_phone(int $amount, int $card_type): array
  207. {
  208. $providers = [];
  209. foreach ($this->mPhoneProvider as $provider) {
  210. $name = $provider->name();
  211. [$success, $err] = $provider->check($amount, $card_type);
  212. if ($success) {
  213. $providers[] = $provider;
  214. } else {
  215. Log::record("{$name} provider cannot match check,err:{$err}", Log::DEBUG);
  216. }
  217. }
  218. return $providers;
  219. }
  220. public function notify($chname, $input)
  221. {
  222. try {
  223. $class_name = "refill\\{$chname}\\RefillCallBack";
  224. if (class_exists($class_name, false)) {
  225. $caller = new $class_name();
  226. } else {
  227. $error = "Base Error: class {$class_name} isn't exists!";
  228. throw new Exception($error);
  229. }
  230. } catch (Exception $ex) {
  231. Log::record($ex->getMessage(), Log::ERR);
  232. return false;
  233. }
  234. if ($caller->verify($input))
  235. {
  236. [$order_id, $success, $can_try, $need_handle] = $caller->notify($input);
  237. if (!$need_handle) {
  238. return true;
  239. }
  240. if ($order_id !== false)
  241. {
  242. $mod_order = Model('vr_order');
  243. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  244. $order_state = intval($order_info['order_state']);
  245. if ($order_state != ORDER_STATE_SEND) {
  246. return false;
  247. }
  248. $logic_vr_order = Logic("vr_order");
  249. if ($success) {
  250. $logic_vr_order->changeOrderStateSuccess($order_id);
  251. }
  252. elseif ($can_try)
  253. {
  254. try
  255. {
  256. $mod_refill = Model('refill_order');
  257. $trans = new trans_wapper($mod_refill, __METHOD__);
  258. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  259. if(!empty($refill_info))
  260. {
  261. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试");
  262. if ($this->retry($refill_info, $order_info)) {
  263. $mod_refill->edit($order_id, ['inner_status' => 1, 'notify_time' => time(), 'notify_state' => 1]);
  264. $trans->commit();
  265. return true;
  266. }
  267. }
  268. $trans->commit();
  269. }
  270. catch (Exception $ex) {
  271. $trans->rollback();
  272. Log::record("Error:" . $ex->getMessage(),Log::ERR);
  273. }
  274. }
  275. else {
  276. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.");
  277. }
  278. $mod_refill = Model('refill_order');
  279. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  280. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id]);
  281. } else {
  282. Log::record("系统无此订单ID:{$order_id}", Log::ERR);
  283. }
  284. } else {
  285. Log::record("{$chname} 签名失败.");
  286. }
  287. return true;
  288. }
  289. private function retry(array $refill_info, array $order_info)
  290. {
  291. $checker = function ($refill, $order) {
  292. $state = intval($order['order_state']);
  293. if ($state !== ORDER_STATE_SEND) {
  294. return false;
  295. }
  296. // $times = intval($refill['commit_times']);
  297. // if ($times > 5) {
  298. // return false;
  299. // }
  300. $period = time() - $refill['order_time'];
  301. if ($period >= 10 * 60) {
  302. return false;
  303. }
  304. return true;
  305. };
  306. $can_try = $checker($refill_info, $order_info);
  307. if (!$can_try) {
  308. return false;
  309. }
  310. $mchid = $refill_info['mchid'];
  311. $buyer_id = $order_info['buyer_id'];
  312. $amount = $refill_info['refill_amount'];
  313. $card_no = $refill_info['card_no'];
  314. $mch_order = $refill_info['mch_order'];
  315. $notify_url = $refill_info['notify_url'];
  316. $commit_times = $refill_info['commit_times'] + 1;
  317. $order_time = $refill_info['order_time'];
  318. $idcard = $refill_info['idcard'] ?? '';
  319. $card_name = $refill_info['card_name'] ?? '';
  320. [$success, $err] = $this->add($mchid, $buyer_id, $amount, $card_no, $mch_order, $idcard, $card_name, $notify_url, $order_time, $commit_times);
  321. return ($success === true);
  322. }
  323. public function add($mchid, $buyer_id, $amount, $card_no, $mch_order, $idcard, $card_name, $notify_url, $order_time = 0, $commit_times = 0)
  324. {
  325. $card_type = mtopcard\card_type($card_no);
  326. $providers = $this->find_providers($amount, $card_type);
  327. if (empty($providers)) {
  328. return [202, "找不到合适的充值通道"];
  329. }
  330. if (empty($notify_url)) {
  331. $notify_url = "";
  332. }
  333. $minfo = new member_info($buyer_id);
  334. $calc = new CalcMerchantPrice($mchid, $amount, $card_type);
  335. $mch_amount = $calc->calc_vgoods_price([]);
  336. $available = $minfo->available_predeposit();
  337. if ($mch_amount > $available) {
  338. Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG);
  339. return [203, "余额不足"];
  340. }
  341. if ($order_time === 0) {
  342. $order_time = time();
  343. }
  344. $ascending = function ($l, $r) use ($amount) {
  345. [$lid, $lprice] = $l->goods($amount);
  346. [$rid, $rprice] = $r->goods($amount);
  347. $lsort = $l->sort();
  348. $rsort = $r->sort();
  349. if($lprice == $rprice) {
  350. return $lsort < $rsort ? -1 : 1;
  351. }
  352. else {
  353. return $lprice < $rprice ? -1 : 1;
  354. }
  355. };
  356. usort($providers, $ascending);
  357. $refill_state = false;
  358. foreach ($providers as $provider)
  359. {
  360. if(!$provider->opened()) continue;
  361. $channel_name = $provider->name();
  362. [$goods_id, $price] = $provider->goods($amount);
  363. if ($price > $mch_amount) continue;
  364. $input['goods_id'] = $goods_id;
  365. $input['quantity'] = 1;
  366. $input['price'] = $price;
  367. $input['buyer_phone'] = $minfo->mobile();
  368. $input['buyer_name'] = $minfo->truename();
  369. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  370. $input['order_from'] = 1;
  371. $input['pd_pay'] = true;
  372. $logic_buy_virtual = Logic('buy_virtual');
  373. $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true);
  374. $mod_refill = Model('refill_order');
  375. if ($result['state'] === true)
  376. {
  377. $order_sn = $result['data']['order_sn'];
  378. $order_id = $result['data']['order_id'];
  379. if(!$this->pay_completed($order_sn)) {
  380. $logic_vr_order = Logic("vr_order");
  381. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  382. $logic_vr_order->changeOrderStateCancel($order_info, '', "预存款不足以支付该订单");
  383. continue;
  384. }
  385. if (empty($mch_order)) {
  386. $mch_order = $order_sn;
  387. }
  388. //虚拟订单表信息扩展
  389. $orderext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'mchid' => $mchid,
  390. 'refill_amount' => $amount, 'mch_order' => $mch_order,
  391. 'idcard' => $idcard, 'card_name' => $card_name,
  392. 'notify_url' => $notify_url, 'channel_name' => $channel_name,
  393. 'mch_amount' => $mch_amount, 'channel_amount' => $price,
  394. 'order_time' => $order_time, 'commit_times' => $commit_times,
  395. 'card_type' => $card_type, 'card_no' => $card_no];
  396. $mod_refill->add_refill($orderext);
  397. } else {
  398. continue;
  399. }
  400. $params = ['order_sn' => $order_sn, 'idcard' => $idcard, 'card_name' => $card_name];
  401. [$state, $err] = $provider->add($card_no, $card_type, $amount, $params);
  402. if ($state)
  403. {
  404. $trade_no = $err;
  405. if ($provider->refill_type() == 'api') {
  406. $logic_vr_order = Logic("vr_order");
  407. $logic_vr_order->changeOrderStateSend($order_id);
  408. }
  409. $data = ['commit_time' => time(), 'ch_trade_no' => $trade_no];
  410. $mod_refill->edit($order_id, $data);
  411. $refill_state = true;
  412. //如果对方没有回调能力,则启动主动查询.
  413. if($provider->callback() === false) {
  414. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],180);
  415. }
  416. break;
  417. } else {
  418. Log::record("channel:{$channel_name} err:{$err}");
  419. $logic_vr_order = Logic("vr_order");
  420. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  421. $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败");
  422. $mod_refill->edit($order_id, ['commit_time' => time(), 'inner_status' => 1]);
  423. }
  424. }
  425. if ($refill_state) {
  426. return [true, $order_sn];
  427. } else {
  428. return [204, "充值失败."];
  429. }
  430. }
  431. private function pay_completed($order_sn)
  432. {
  433. $logic_payment = Logic('payment');
  434. $order = $logic_payment->getVrOrderInfo($order_sn);
  435. $api_pay_amount = $order['data']['api_pay_amount'];
  436. return ($api_pay_amount == ncPriceFormat(0.00));
  437. }
  438. private function is_url($url)
  439. {
  440. $checker = function ($haystack, $needle) {
  441. $length = strlen($needle);
  442. return (substr($haystack, 0, $length) === $needle);
  443. };
  444. return $checker($url, "http://") || $checker($url, "https://");
  445. }
  446. public function notify_merchant($order_id)
  447. {
  448. if ($order_id <= 0) {
  449. return [false, "订单ID小于0"];
  450. }
  451. $vr_order = Model('vr_order');
  452. $refill_order = Model('refill_order');
  453. $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]);
  454. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  455. if (empty($order_info) || empty($refill_info)) {
  456. return [false, "无此订单"];
  457. }
  458. if ($refill_info['mch_notify_state'] != 0) {
  459. return [false, "已经通知客户方"];
  460. }
  461. $notify_url = $refill_info['notify_url'];
  462. if (empty($notify_url)) {
  463. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]);
  464. return [false, "回调地址为空"];
  465. }
  466. $order_state = $order_info['order_state'];
  467. if ($order_state == ORDER_STATE_CANCEL) {
  468. $state = "CANCEL";
  469. } elseif ($order_state == ORDER_STATE_SUCCESS) {
  470. $state = "SUCCESS";
  471. } else {
  472. return [false, "错误的订单状态,不能通知."];
  473. }
  474. $mchid = $refill_info['mchid'];
  475. $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  476. [$params, $sign] = $this->body($state, $refill_info, $mch_info);
  477. $params['sign'] = $sign;
  478. //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
  479. if ($this->is_url($notify_url)) {
  480. $resp = http_request($notify_url, $params, 'POST');
  481. } else {
  482. $resp = RBridgeFactory::instance()->notify($notify_url, $params);
  483. }
  484. if ($resp == "SUCCESS") {
  485. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  486. return [true, ""];
  487. } else {
  488. $refill_order->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  489. $times = $refill_info['mch_notify_times'] + 1;
  490. if ($times > 80) {
  491. $refill_order->edit($order_id, ['mch_notify_state' => 2]);
  492. } else {
  493. $N = intval($times / 5);
  494. $period = intval(pow(2, $N));
  495. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id], $period);
  496. }
  497. return [false, "通知{$times}次,失败."];
  498. }
  499. }
  500. public function query($order_id)
  501. {
  502. $mod_refill = Model('refill_order');
  503. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  504. $chname = $refill_info['channel_name'];
  505. $card_type = intval($refill_info['card_type']);
  506. $pthis = $this;
  507. $finder = function ($chname, $card_type) use ($pthis)
  508. {
  509. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  510. $providers = $pthis->mOilProvider;
  511. } else {
  512. $providers = $pthis->mPhoneProvider;
  513. }
  514. foreach ($providers as $provider) {
  515. if ($chname == $provider->name()) {
  516. return $provider;
  517. }
  518. }
  519. return NULL;
  520. };
  521. $provider = $finder($chname, $card_type);
  522. [$state, $order_state] = $provider->query($refill_info);
  523. if ($state === true)
  524. {
  525. $notify_state = $refill_info['notify_state'];
  526. if($notify_state == 0)
  527. {
  528. $modify_able = true;
  529. if(!$provider->callback())
  530. {
  531. $logic_vr_order = Logic("vr_order");
  532. if ($order_state == ORDER_STATE_SUCCESS) {
  533. $logic_vr_order->changeOrderStateSuccess($order_id);
  534. } elseif ($order_state == ORDER_STATE_CANCEL) {
  535. $mod_order = Model('vr_order');
  536. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  537. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口查询失败,不再重试.");
  538. } else {
  539. $modify_able = false;
  540. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],180);
  541. }
  542. }
  543. if($modify_able) {
  544. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  545. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id]);
  546. }
  547. }
  548. return true;
  549. }
  550. else {
  551. return false;
  552. }
  553. }
  554. private function body($state, $refill_info, $mch_info)
  555. {
  556. $params = [
  557. "mchid" => $refill_info['mchid'],
  558. "order_sn" => $refill_info['mch_order'],
  559. "amount" => $refill_info['refill_amount'],
  560. "cardno" => $refill_info['card_no'],
  561. "trade_no" => $refill_info['order_sn'],
  562. "idcard" => $refill_info['idcard'] ?? "",
  563. "card_name" => $refill_info['card_name'] ?? "",
  564. 'official_sn' => $refill_info['official_sn'] ?? "",
  565. "state" => $state];
  566. $secure_key = $mch_info['secure_key'];
  567. $sign = $this->sign($params, $secure_key);
  568. return [$params, $sign];
  569. }
  570. private function sign($params, $key)
  571. {
  572. ksort($params);
  573. $body = "";
  574. $i = 0;
  575. foreach ($params as $k => $v) {
  576. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  577. if ($i == 0) {
  578. $body .= "{$k}" . "=" . urlencode($v);
  579. } else {
  580. $body .= "&" . "{$k}" . "=" . urlencode($v);
  581. }
  582. $i++;
  583. }
  584. }
  585. $body .= "&key={$key}";
  586. return md5($body);
  587. }
  588. private function check_empty($value)
  589. {
  590. if (!isset($value))
  591. return true;
  592. if ($value === null)
  593. return true;
  594. if (trim($value) === "")
  595. return true;
  596. return false;
  597. }
  598. }