RefillFactory.php 26 KB

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