policy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace refill;
  3. use Log;
  4. use rbridge\RBridgeFactory;
  5. use StatesHelper;
  6. use mtopcard;
  7. class policy extends ProviderManager implements IPolicy
  8. {
  9. protected $mChannelControl;
  10. protected $mQuality;
  11. protected $mPrices;
  12. protected $mAmountLockTurn;
  13. protected $mStorageLocker;
  14. protected $mGroupCtl;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->mChannelControl = new chctl();
  19. $this->mQuality = new quality_ploy();
  20. $this->mPrices = new merchant_price();
  21. $this->mStorageLocker = new rstorage();
  22. $this->mGroupCtl = new rgroup_ctl();
  23. }
  24. public function load()
  25. {
  26. parent::load();
  27. $opened_names = $this->mOpenedProviderNames;
  28. sort($opened_names);
  29. $opened_merchants = $this->opened_merchants();
  30. $this->mChannelControl->load($opened_names);
  31. $this->mChannelControl->update_price($this);
  32. $this->mQuality->load();
  33. $this->mPrices->load($opened_merchants);
  34. $turn_name = 'oil_amount_lock_turn';
  35. $this->mAmountLockTurn = rkcache($turn_name);
  36. Log::record("AmountLockTurn = {$this->mAmountLockTurn}",Log::DEBUG);
  37. $this->mStorageLocker->load();
  38. $this->mGroupCtl->load($opened_names,$opened_merchants);
  39. }
  40. private function opened_merchants()
  41. {
  42. $mchids = [];
  43. $i = 0;
  44. while (true)
  45. {
  46. $start = $i * 1000;
  47. $items = Model()->table('merchant')->field('*')->where(['merchant_state' => 1])->order('mchid asc')->limit("{$start},1000")->select();
  48. if(empty($items)) {
  49. break;
  50. }
  51. $i++;
  52. foreach ($items as $item) {
  53. $mchids[] = intval($item['mchid']);
  54. }
  55. }
  56. sort($mchids);
  57. return $mchids;
  58. }
  59. public function find_providers(order $order): array
  60. {
  61. $mchid = $order->mchid();
  62. $spec = $order->spec();
  63. $card_type = $order->card_type();
  64. $org_quality = $order->org_quality();
  65. $cur_quality = $order->cur_quality();
  66. $commit_times = $order->commit_times();
  67. $elapse_secs = $order->elapse_secs();
  68. $pcode = $order->pcode();
  69. $regin_no = $order->region_no();
  70. $order_time = $order->order_time();
  71. $providers = parent::get_providers($mchid,$spec,$card_type,$cur_quality,$regin_no);
  72. if(empty($providers)) {
  73. return [$providers,false];
  74. }
  75. $names = [];
  76. foreach ($providers as $provider) {
  77. $names[] = $provider->name();
  78. }
  79. Log::record("ProviderManager::get_providers result=" . implode(',',$names),Log::DEBUG);
  80. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $cur_quality);
  81. if($hasGroup)
  82. {
  83. if(empty($channels))
  84. {
  85. if(!$can_others) {
  86. return [[],false];
  87. }
  88. }
  89. else
  90. {
  91. $ret = array_intersect($names, $channels);
  92. if (empty($ret)) {
  93. return [[],false];
  94. }
  95. else {
  96. $names = $ret;
  97. }
  98. }
  99. }
  100. Log::record("GroupControl result=" . implode(',',$names),Log::DEBUG);
  101. if(PolicyUtil::mixed_quality($org_quality)) {
  102. $fQuality = $org_quality;
  103. }
  104. else {
  105. $fQuality = $cur_quality;
  106. }
  107. $price = $this->mPrices->price($mchid,$card_type,$spec,$fQuality,$pcode);
  108. if($price === false) {
  109. return [[],false];
  110. }
  111. $extra_price = $this->mPrices->extra_price($mchid,$card_type,$spec,$fQuality,$pcode);
  112. $extra_price = $extra_price == false ? 0.00 : $extra_price;
  113. global $config;
  114. $auto_find = $config['auto_find_channels'];
  115. $mobile_types = [mtopcard\ChinaMobileCard,mtopcard\ChinaUnicomCard,mtopcard\ChinaTelecomCard];
  116. $qualities = [Quality::Normal];
  117. if($auto_find && in_array($card_type,$mobile_types) && in_array($cur_quality,$qualities))
  118. {
  119. $names = $this->mChannelControl->auto_match($names, $spec, $card_type, $cur_quality, $price - $extra_price, time() - $order_time);
  120. Log::record("policy::find_providers ChannelControl auto_match quality={$cur_quality} spec={$spec} card_type={$card_type} result=" . implode(',', $names), Log::DEBUG);
  121. $name_provider = [];
  122. foreach ($providers as $provider) {
  123. $name = $provider->name();
  124. $name_provider[$name] = $provider;
  125. }
  126. $result = [];
  127. foreach ($names as $name)
  128. {
  129. if(array_key_exists($name,$name_provider)) {
  130. $result[] = $name_provider[$name];
  131. }
  132. }
  133. return [$result,false];
  134. }
  135. else
  136. {
  137. $name_overloads = $this->mChannelControl->match($names,$spec,$card_type,$cur_quality);
  138. Log::record("policy::find_providers ChannelControl match result=" . implode(',',array_keys($name_overloads)),Log::DEBUG);
  139. $result = [];
  140. foreach ($name_overloads as $name => $overload)
  141. {
  142. foreach ($providers as $provider)
  143. {
  144. if($name == $provider->name()) {
  145. $result[] = $provider;
  146. }
  147. }
  148. }
  149. return [$result,false];
  150. }
  151. }
  152. public function price($mchid,$spec,$card_type,$quality,$pcode)
  153. {
  154. return $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  155. }
  156. public function channeles(int $mchid, int $spec, int $card_type, int $quality, $regin_no)
  157. {
  158. $providers = parent::get_providers($mchid, $spec, $card_type, $quality, $regin_no);
  159. return count($providers);
  160. }
  161. public function find_quality(order $order, bool $skip_pre = false): array
  162. {
  163. $mchid = $order->mchid();
  164. $spec = $order->spec();
  165. $card_type = $order->card_type();
  166. $org_quality = $order->org_quality();
  167. $cur_quality = $order->cur_quality();
  168. $commit_times = $order->commit_times();
  169. $elapse_secs = $order->elapse_secs();
  170. $pcode = $order->pcode();
  171. $regin_no = $order->region_no();
  172. if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  173. $caller = new times_caller($mchid,$spec,$card_type,-1,$this);
  174. } else {
  175. $caller = null;
  176. }
  177. [$org_quality,$qualities] = $this->mQuality->find_quality($mchid,$card_type,$org_quality,$cur_quality,$commit_times,$elapse_secs,$caller);
  178. if(empty($qualities)) {
  179. return [$org_quality,0];
  180. }
  181. $namer = function ($providers)
  182. {
  183. $result = [];
  184. foreach ($providers as $provider) {
  185. $result[] = $provider->name();
  186. }
  187. return $result;
  188. };
  189. $start = false;
  190. foreach ($qualities as $quality)
  191. {
  192. if(!$start && $skip_pre)
  193. {
  194. if($quality == $cur_quality) {
  195. $start = true;
  196. }
  197. continue;
  198. }
  199. $price = $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  200. if($price === false) {
  201. Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
  202. continue;
  203. }
  204. $providers = parent::get_providers($mchid, $spec, $card_type, $quality, $regin_no);
  205. if(empty($providers)) continue;
  206. $names = $namer($providers);
  207. $names_overload = $this->mChannelControl->match($names, $spec, $card_type, $quality);
  208. if (!empty($names_overload)) {
  209. return [$org_quality, $quality];
  210. } else {
  211. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is fail", Log::DEBUG);
  212. }
  213. }
  214. return [$org_quality,0];
  215. }
  216. public function allow($mchid,$card_type,$amount,$quality) : bool
  217. {
  218. return $this->mStorageLocker->allow($mchid,$card_type,$amount);
  219. }
  220. private function allow_storge($mchid,$card_type,$amount,$quality)
  221. {
  222. $reader = function ()
  223. {
  224. $cache = rcache("refill_able",'merchant-');
  225. if(!empty($cache)) {
  226. $result = unserialize($cache['data']);
  227. }
  228. else {
  229. $result = [];
  230. }
  231. return $result;
  232. };
  233. if(defined('MOBILE_SERVER') && MOBILE_SERVER === true)
  234. {
  235. if(StatesHelper::fetch_state('merchant')) {
  236. $this->mLimits = $reader();
  237. }
  238. }
  239. else {
  240. $this->mLimits = $reader();
  241. }
  242. $key = "{$mchid}-{$card_type}-{$amount}";
  243. if(empty($this->mLimits)) {
  244. return true;
  245. }
  246. elseif(array_key_exists($key,$this->mLimits)) {
  247. return $this->mLimits[$key];
  248. }
  249. else {
  250. return true;
  251. }
  252. }
  253. public function notify($order_info, $refill_info) : bool
  254. {
  255. $order_state = $order_info['order_state'];
  256. if ($order_state == ORDER_STATE_CANCEL) {
  257. $state = "CANCEL";
  258. } else {
  259. $state = "SUCCESS";
  260. }
  261. $mchid = $refill_info['mchid'];
  262. $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  263. [$params, $sign] = $this->body($state, $refill_info, $mch_info);
  264. $params['sign'] = $sign;
  265. $notify_url = $refill_info['notify_url'];
  266. //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
  267. if ($this->is_url($notify_url)) {
  268. $resp = http_request($notify_url, $params, 'POST');
  269. } else {
  270. $resp = RBridgeFactory::instance()->notify($notify_url, $params);
  271. }
  272. return $resp == "SUCCESS" || $resp == 'FAILED';
  273. }
  274. private function body($state, $refill_info, $mch_info)
  275. {
  276. $params = [
  277. "mchid" => $refill_info['mchid'],
  278. "order_sn" => $refill_info['mch_order'],
  279. "amount" => $refill_info['refill_amount'],//intval($refill_info['refill_amount'] + 0.05),
  280. "cardno" => $refill_info['card_no'],
  281. "trade_no" => $refill_info['order_sn'],
  282. "idcard" => $refill_info['idcard'] ?? "",
  283. "card_name" => $refill_info['card_name'] ?? "",
  284. 'official_sn' => $refill_info['official_sn'] ?? "",
  285. 'message' => $refill_info['err_msg'] ?? "",
  286. "state" => $state];
  287. [$has_sms,$sms] = $this->sms($refill_info);
  288. if($has_sms) {
  289. $params['sms'] = $sms;
  290. }
  291. $secure_key = $mch_info['secure_key'];
  292. $sign = $this->sign($params, $secure_key);
  293. return [$params, $sign];
  294. }
  295. private function sms($refill_info)
  296. {
  297. $mchids = [1090,10116];
  298. $mchid = $refill_info['mchid'];
  299. $official_sn = $refill_info['official_sn'] ?? "";
  300. $card_type = $refill_info['card_type'];
  301. $card_no = $refill_info['card_no'];
  302. $amount = intval($refill_info['refill_amount']);
  303. if(in_array($mchid,$mchids) && !empty($official_sn) && $card_type == mtopcard\SinopecCard && !empty($card_no))
  304. {
  305. $short_no = substr($card_no,-6);
  306. $ret = preg_match('/\d{4}(?P<month>\d{2})(?P<day>\d{2})(?P<hour>\d{2})(?P<min>\d{2})\d{4}/u', $official_sn, $matches);
  307. if($ret > 0) {
  308. $sms = "【中国石化】您尾号为{$short_no}的加油卡于{$matches['month']}月{$matches['day']}日 {$matches['hour']}时{$matches['min']}分充值成功,金额{$amount}元,订单号:{$official_sn}";
  309. return [true,$sms];
  310. }
  311. }
  312. return [false,''];
  313. }
  314. private function sign($params, $key)
  315. {
  316. ksort($params);
  317. $body = "";
  318. $i = 0;
  319. foreach ($params as $k => $v) {
  320. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  321. if ($i == 0) {
  322. $body .= "{$k}" . "=" . urlencode($v);
  323. } else {
  324. $body .= "&" . "{$k}" . "=" . urlencode($v);
  325. }
  326. $i++;
  327. }
  328. }
  329. $body .= "&key={$key}";
  330. Log::record("notify body={$body}",Log::DEBUG);
  331. return md5($body);
  332. }
  333. private function check_empty($value)
  334. {
  335. if (!isset($value))
  336. return true;
  337. if ($value === null)
  338. return true;
  339. if (trim($value) === "")
  340. return true;
  341. return false;
  342. }
  343. private function is_url($url)
  344. {
  345. $checker = function ($haystack, $needle) {
  346. $length = strlen($needle);
  347. return (substr($haystack, 0, $length) === $needle);
  348. };
  349. return $checker($url, "http://") || $checker($url, "https://");
  350. }
  351. public function update_ratios($ratios)
  352. {
  353. $this->mChannelControl->update_ratios($ratios);
  354. }
  355. public function update_mchratios($ratios)
  356. {
  357. $this->mQuality->update_mchratios($ratios);
  358. }
  359. }