RefillFactory.php 28 KB

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