policy.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. $this->mChannelControl->load();
  28. $this->mQuality->load();
  29. $this->mPrices->load();
  30. $turn_name = 'oil_amount_lock_turn';
  31. $this->mAmountLockTurn = rkcache($turn_name);
  32. Log::record("AmountLockTurn = {$this->mAmountLockTurn}",Log::DEBUG);
  33. $this->mStorageLocker->load();
  34. $this->mGroupCtl->load();
  35. }
  36. public function find_providers(int $mchid, int $spec, int $card_type, int $quality, $regin_no): array
  37. {
  38. $providers = parent::find_providers($mchid,$spec,$card_type,$quality,$regin_no);
  39. if(empty($providers)) {
  40. return [$providers,false];
  41. }
  42. $names = [];
  43. foreach ($providers as $provider) {
  44. $names[] = $provider->name();
  45. }
  46. Log::record(" mGroupCtl find_providers result=" . implode(',',$names),Log::DEBUG);
  47. [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $quality);
  48. if($hasGroup)
  49. {
  50. if(empty($channels))
  51. {
  52. if(!$can_others) {
  53. return [[],false];
  54. }
  55. }
  56. else
  57. {
  58. $ret = array_intersect($names, $channels);
  59. if (empty($ret)) {
  60. return [[],false];
  61. }
  62. else {
  63. $names = $ret;
  64. }
  65. }
  66. }
  67. Log::record("after mGroupCtl find_providers result=" . implode(',',$names),Log::DEBUG);
  68. $name_overloads = $this->mChannelControl->match($names,$spec,$card_type,$quality);
  69. Log::record("policy::find_providers match result=" . implode(',',$names),Log::DEBUG);
  70. $result = [];
  71. foreach ($name_overloads as $name => $overload)
  72. {
  73. Log::record("channel {$name} has overloaded {$overload}",Log::DEBUG);
  74. if(!isset($first)) {
  75. $first = $overload;
  76. }
  77. if($overload) continue;
  78. foreach ($providers as $provider)
  79. {
  80. if($name == $provider->name()) {
  81. $result[] = $provider;
  82. }
  83. }
  84. }
  85. if(!isset($first)) {
  86. $first = false;
  87. }
  88. return [$result,$first];
  89. }
  90. public function price($mchid,$spec,$card_type,$quality,$pcode)
  91. {
  92. return $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  93. }
  94. public function find_quality($mchid,$spec,$card_type,$org_quality,$times,$used_time,$pcode): array
  95. {
  96. [$org_quality,$qualities] = $this->mQuality->find_quality($mchid,$card_type,$org_quality,$times,$used_time);
  97. if(empty($qualities)) {
  98. return [$org_quality,0];
  99. }
  100. foreach ($qualities as $quality)
  101. {
  102. $price = $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
  103. if($price === false) {
  104. Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
  105. continue;
  106. }
  107. [$providers,$overload] = $this->find_providers($mchid,$spec,$card_type,$quality,-1);
  108. if (!empty($providers))
  109. {
  110. if (!$overload) {
  111. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is ok", Log::DEBUG);
  112. }
  113. else {
  114. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is overload", Log::DEBUG);
  115. }
  116. return [$org_quality, $quality];
  117. } else {
  118. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is fail", Log::DEBUG);
  119. }
  120. }
  121. return [$org_quality,0];
  122. }
  123. public function allow($mchid,$card_type,$amount,$quality) : bool
  124. {
  125. return $this->mStorageLocker->allow($mchid,$card_type,$amount);
  126. }
  127. private function allow_storge($mchid,$card_type,$amount,$quality)
  128. {
  129. $reader = function ()
  130. {
  131. $cache = rcache("refill_able",'merchant-');
  132. if(!empty($cache)) {
  133. $result = unserialize($cache['data']);
  134. }
  135. else {
  136. $result = [];
  137. }
  138. return $result;
  139. };
  140. if(defined('MOBILE_SERVER') && MOBILE_SERVER === true)
  141. {
  142. if(StatesHelper::fetch_state('merchant')) {
  143. $this->mLimits = $reader();
  144. }
  145. }
  146. else {
  147. $this->mLimits = $reader();
  148. }
  149. $key = "{$mchid}-{$card_type}-{$amount}";
  150. if(empty($this->mLimits)) {
  151. return true;
  152. }
  153. elseif(array_key_exists($key,$this->mLimits)) {
  154. return $this->mLimits[$key];
  155. }
  156. else {
  157. return true;
  158. }
  159. }
  160. public function notify($order_info, $refill_info) : bool
  161. {
  162. $order_state = $order_info['order_state'];
  163. if ($order_state == ORDER_STATE_CANCEL) {
  164. $state = "CANCEL";
  165. } else {
  166. $state = "SUCCESS";
  167. }
  168. $mchid = $refill_info['mchid'];
  169. $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  170. [$params, $sign] = $this->body($state, $refill_info, $mch_info);
  171. $params['sign'] = $sign;
  172. $notify_url = $refill_info['notify_url'];
  173. //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
  174. if ($this->is_url($notify_url)) {
  175. $resp = http_request($notify_url, $params, 'POST');
  176. } else {
  177. $resp = RBridgeFactory::instance()->notify($notify_url, $params);
  178. }
  179. return $resp == "SUCCESS";
  180. }
  181. private function body($state, $refill_info, $mch_info)
  182. {
  183. $params = [
  184. "mchid" => $refill_info['mchid'],
  185. "order_sn" => $refill_info['mch_order'],
  186. "amount" => $refill_info['refill_amount'],//intval($refill_info['refill_amount'] + 0.05),
  187. "cardno" => $refill_info['card_no'],
  188. "trade_no" => $refill_info['order_sn'],
  189. "idcard" => $refill_info['idcard'] ?? "",
  190. "card_name" => $refill_info['card_name'] ?? "",
  191. 'official_sn' => $refill_info['official_sn'] ?? "",
  192. 'message' => $refill_info['err_msg'] ?? "",
  193. "state" => $state];
  194. [$has_sms,$sms] = $this->sms($refill_info);
  195. if($has_sms) {
  196. $params['sms'] = $sms;
  197. }
  198. $secure_key = $mch_info['secure_key'];
  199. $sign = $this->sign($params, $secure_key);
  200. return [$params, $sign];
  201. }
  202. private function sms($refill_info)
  203. {
  204. $mchids = [1090,10116];
  205. $mchid = $refill_info['mchid'];
  206. $official_sn = $refill_info['official_sn'] ?? "";
  207. $card_type = $refill_info['card_type'];
  208. $card_no = $refill_info['card_no'];
  209. $amount = intval($refill_info['refill_amount']);
  210. if(in_array($mchid,$mchids) && !empty($official_sn) && $card_type == mtopcard\SinopecCard && !empty($card_no))
  211. {
  212. $short_no = substr($card_no,-6);
  213. $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);
  214. if($ret > 0) {
  215. $sms = "【中国石化】您尾号为{$short_no}的加油卡于{$matches['month']}月{$matches['day']}日 {$matches['hour']}时{$matches['min']}分充值成功,金额{$amount}元,订单号:{$official_sn}";
  216. return [true,$sms];
  217. }
  218. }
  219. return [false,''];
  220. }
  221. private function sign($params, $key)
  222. {
  223. ksort($params);
  224. $body = "";
  225. $i = 0;
  226. foreach ($params as $k => $v) {
  227. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  228. if ($i == 0) {
  229. $body .= "{$k}" . "=" . urlencode($v);
  230. } else {
  231. $body .= "&" . "{$k}" . "=" . urlencode($v);
  232. }
  233. $i++;
  234. }
  235. }
  236. $body .= "&key={$key}";
  237. Log::record("notify body={$body}",Log::DEBUG);
  238. return md5($body);
  239. }
  240. private function check_empty($value)
  241. {
  242. if (!isset($value))
  243. return true;
  244. if ($value === null)
  245. return true;
  246. if (trim($value) === "")
  247. return true;
  248. return false;
  249. }
  250. private function is_url($url)
  251. {
  252. $checker = function ($haystack, $needle) {
  253. $length = strlen($needle);
  254. return (substr($haystack, 0, $length) === $needle);
  255. };
  256. return $checker($url, "http://") || $checker($url, "https://");
  257. }
  258. }