RefillBase.php 24 KB

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