RefillFactory.php 28 KB

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