RefillFactory.php 23 KB

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