RefillFactory.php 24 KB

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