RefillFactory.php 27 KB

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