RefillFactory.php 28 KB

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