RefillFactory.php 28 KB

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