policy.php 13 KB

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