policy.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace refill;
  3. use Log;
  4. use rbridge\RBridgeFactory;
  5. use StatesHelper;
  6. class policy extends ProviderManager implements IPolicy
  7. {
  8. protected $mChannelControl;
  9. protected $mMchctl;
  10. protected $mQuality;
  11. protected $mPrices;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->mChannelControl = new chctl();
  16. $this->mMchctl = new mchctl();
  17. $this->mQuality = new quality_ploy();
  18. $this->mPrices = new merchant_price();
  19. }
  20. public function load()
  21. {
  22. parent::load();
  23. $this->mChannelControl->load();
  24. $this->mMchctl->load();
  25. $this->mQuality->load();
  26. $this->mPrices->load();
  27. }
  28. public function find_providers(int $spec, int $card_type,int $quality): array
  29. {
  30. $providers = parent::find_providers($spec,$card_type,$quality);
  31. if(empty($providers)) {
  32. return [$providers,false];
  33. }
  34. $names = [];
  35. foreach ($providers as $provider) {
  36. $names[] = $provider->name();
  37. }
  38. $name_overloads = $this->mChannelControl->match($names,$spec,$card_type,$quality);
  39. Log::record("policy::find_providers match result=" . implode(',',$names),Log::DEBUG);
  40. $result = [];
  41. foreach ($name_overloads as $name => $overload)
  42. {
  43. if(!isset($first)) {
  44. $first = $overload;
  45. }
  46. if($overload) continue;
  47. foreach ($providers as $provider)
  48. {
  49. if($name == $provider->name()) {
  50. $result[] = $provider;
  51. }
  52. }
  53. }
  54. if(!isset($first)) {
  55. $first = false;
  56. }
  57. return [$result,$first];
  58. }
  59. public function price($mchid,$spec,$card_type,$quality)
  60. {
  61. return $this->mPrices->price($mchid,$card_type,$spec,$quality);
  62. }
  63. public function find_quality($mchid,$spec,$card_type,$org_quality,$times,$used_time): array
  64. {
  65. [$org_quality,$qualities] = $this->mQuality->find_quality($mchid,$card_type,$org_quality,$times,$used_time);
  66. if(empty($qualities)) {
  67. return [$org_quality,0];
  68. }
  69. foreach ($qualities as $quality)
  70. {
  71. $price = $this->mPrices->price($mchid,$card_type,$spec,$quality);
  72. if($price === false) {
  73. Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
  74. continue;
  75. }
  76. [$providers,$overload] = $this->find_providers($spec,$card_type,$quality);
  77. if (!empty($providers))
  78. {
  79. if (!$overload) {
  80. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is ok", Log::DEBUG);
  81. }
  82. else {
  83. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is overload", Log::DEBUG);
  84. }
  85. return [$org_quality, $quality];
  86. } else {
  87. Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is fail", Log::DEBUG);
  88. }
  89. }
  90. return [$org_quality,0];
  91. }
  92. public function allow($mchid,$card_type,$amount,$quality) : bool
  93. {
  94. $reader = function () {
  95. $cache = rcache("refill_able",'merchant-');
  96. if(!empty($cache)) {
  97. $result = unserialize($cache['data']);
  98. }
  99. else {
  100. $result = [];
  101. }
  102. return $result;
  103. };
  104. if(defined('MOBILE_SERVER') && MOBILE_SERVER === true)
  105. {
  106. if(StatesHelper::fetch_state('merchant')) {
  107. $this->mLimits = $reader();
  108. }
  109. }
  110. else {
  111. $this->mLimits = $reader();
  112. }
  113. $key = "{$mchid}-{$card_type}-{$amount}";
  114. if(empty($this->mLimits)) {
  115. return true;
  116. }
  117. elseif(array_key_exists($key,$this->mLimits)) {
  118. return $this->mLimits[$key];
  119. }
  120. else {
  121. return true;
  122. }
  123. }
  124. public function notify($order_info, $refill_info) : bool
  125. {
  126. $order_state = $order_info['order_state'];
  127. if ($order_state == ORDER_STATE_CANCEL) {
  128. $state = "CANCEL";
  129. } else {
  130. $state = "SUCCESS";
  131. }
  132. $mchid = $refill_info['mchid'];
  133. $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  134. [$params, $sign] = $this->body($state, $refill_info, $mch_info);
  135. $params['sign'] = $sign;
  136. $notify_url = $refill_info['notify_url'];
  137. //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
  138. if ($this->is_url($notify_url)) {
  139. $resp = http_request($notify_url, $params, 'POST');
  140. } else {
  141. $resp = RBridgeFactory::instance()->notify($notify_url, $params);
  142. }
  143. return $resp == "SUCCESS";
  144. }
  145. private function body($state, $refill_info, $mch_info)
  146. {
  147. $params = [
  148. "mchid" => $refill_info['mchid'],
  149. "order_sn" => $refill_info['mch_order'],
  150. "amount" => $refill_info['refill_amount'],//intval($refill_info['refill_amount'] + 0.05),
  151. "cardno" => $refill_info['card_no'],
  152. "trade_no" => $refill_info['order_sn'],
  153. "idcard" => $refill_info['idcard'] ?? "",
  154. "card_name" => $refill_info['card_name'] ?? "",
  155. 'official_sn' => $refill_info['official_sn'] ?? "",
  156. 'message' => $refill_info['err_msg'] ?? "",
  157. "state" => $state];
  158. $secure_key = $mch_info['secure_key'];
  159. $sign = $this->sign($params, $secure_key);
  160. return [$params, $sign];
  161. }
  162. private function sign($params, $key)
  163. {
  164. ksort($params);
  165. $body = "";
  166. $i = 0;
  167. foreach ($params as $k => $v) {
  168. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  169. if ($i == 0) {
  170. $body .= "{$k}" . "=" . urlencode($v);
  171. } else {
  172. $body .= "&" . "{$k}" . "=" . urlencode($v);
  173. }
  174. $i++;
  175. }
  176. }
  177. $body .= "&key={$key}";
  178. Log::record("notify body={$body}",Log::DEBUG);
  179. return md5($body);
  180. }
  181. private function check_empty($value)
  182. {
  183. if (!isset($value))
  184. return true;
  185. if ($value === null)
  186. return true;
  187. if (trim($value) === "")
  188. return true;
  189. return false;
  190. }
  191. private function is_url($url)
  192. {
  193. $checker = function ($haystack, $needle) {
  194. $length = strlen($needle);
  195. return (substr($haystack, 0, $length) === $needle);
  196. };
  197. return $checker($url, "http://") || $checker($url, "https://");
  198. }
  199. }