123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace refill;
- use Log;
- use rbridge\RBridgeFactory;
- use StatesHelper;
- class policy extends ProviderManager implements IPolicy
- {
- protected $mChannelControl;
- protected $mMchctl;
- protected $mQuality;
- protected $mPrices;
- public function __construct()
- {
- parent::__construct();
- $this->mChannelControl = new chctl();
- $this->mMchctl = new mchctl();
- $this->mQuality = new quality_ploy();
- $this->mPrices = new merchant_price();
- }
- public function load()
- {
- parent::load();
- $this->mChannelControl->load();
- $this->mMchctl->load();
- $this->mQuality->load();
- $this->mPrices->load();
- }
- public function find_providers(int $spec, int $card_type,int $quality): array
- {
- $providers = parent::find_providers($spec,$card_type,$quality);
- if(empty($providers)) {
- return [$providers,false];
- }
- $names = [];
- foreach ($providers as $provider) {
- $names[] = $provider->name();
- }
- $name_overloads = $this->mChannelControl->match($names,$spec,$card_type,$quality);
- Log::record("policy::find_providers match result=" . implode(',',$names),Log::DEBUG);
- $result = [];
- foreach ($name_overloads as $name => $overload)
- {
- if(!isset($first)) {
- $first = $overload;
- }
- if($overload) continue;
- foreach ($providers as $provider)
- {
- if($name == $provider->name()) {
- $result[] = $provider;
- }
- }
- }
- if(!isset($first)) {
- $first = false;
- }
- return [$result,$first];
- }
- public function price($mchid,$spec,$card_type,$quality)
- {
- return $this->mPrices->price($mchid,$card_type,$spec,$quality);
- }
- public function find_quality($mchid,$spec,$card_type,$org_quality,$times,$used_time): array
- {
- [$org_quality,$qualities] = $this->mQuality->find_quality($mchid,$card_type,$org_quality,$times,$used_time);
- if(empty($qualities)) {
- return [$org_quality,0];
- }
- foreach ($qualities as $quality)
- {
- $price = $this->mPrices->price($mchid,$card_type,$spec,$quality);
- if($price === false) {
- Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
- continue;
- }
- [$providers,$overload] = $this->find_providers($spec,$card_type,$quality);
- if (!empty($providers))
- {
- if (!$overload) {
- Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is ok", Log::DEBUG);
- }
- else {
- Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is overload", Log::DEBUG);
- }
- return [$org_quality, $quality];
- } else {
- Log::record("Policy::find_quality:{$quality}-{$spec}-{$card_type} is fail", Log::DEBUG);
- }
- }
- return [$org_quality,0];
- }
- public function allow($mchid,$card_type,$amount,$quality) : bool
- {
- $reader = function () {
- $cache = rcache("refill_able",'merchant-');
- if(!empty($cache)) {
- $result = unserialize($cache['data']);
- }
- else {
- $result = [];
- }
- return $result;
- };
- if(defined('MOBILE_SERVER') && MOBILE_SERVER === true)
- {
- if(StatesHelper::fetch_state('merchant')) {
- $this->mLimits = $reader();
- }
- }
- else {
- $this->mLimits = $reader();
- }
- $key = "{$mchid}-{$card_type}-{$amount}";
- if(empty($this->mLimits)) {
- return true;
- }
- elseif(array_key_exists($key,$this->mLimits)) {
- return $this->mLimits[$key];
- }
- else {
- return true;
- }
- }
- public function notify($order_info, $refill_info) : bool
- {
- $order_state = $order_info['order_state'];
- if ($order_state == ORDER_STATE_CANCEL) {
- $state = "CANCEL";
- } else {
- $state = "SUCCESS";
- }
- $mchid = $refill_info['mchid'];
- $mch_info = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
- [$params, $sign] = $this->body($state, $refill_info, $mch_info);
- $params['sign'] = $sign;
- $notify_url = $refill_info['notify_url'];
- //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
- if ($this->is_url($notify_url)) {
- $resp = http_request($notify_url, $params, 'POST');
- } else {
- $resp = RBridgeFactory::instance()->notify($notify_url, $params);
- }
- return $resp == "SUCCESS";
- }
- private function body($state, $refill_info, $mch_info)
- {
- $params = [
- "mchid" => $refill_info['mchid'],
- "order_sn" => $refill_info['mch_order'],
- "amount" => $refill_info['refill_amount'],//intval($refill_info['refill_amount'] + 0.05),
- "cardno" => $refill_info['card_no'],
- "trade_no" => $refill_info['order_sn'],
- "idcard" => $refill_info['idcard'] ?? "",
- "card_name" => $refill_info['card_name'] ?? "",
- 'official_sn' => $refill_info['official_sn'] ?? "",
- 'message' => $refill_info['err_msg'] ?? "",
- "state" => $state];
- $secure_key = $mch_info['secure_key'];
- $sign = $this->sign($params, $secure_key);
- return [$params, $sign];
- }
- private function sign($params, $key)
- {
- ksort($params);
- $body = "";
- $i = 0;
- foreach ($params as $k => $v) {
- if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
- if ($i == 0) {
- $body .= "{$k}" . "=" . urlencode($v);
- } else {
- $body .= "&" . "{$k}" . "=" . urlencode($v);
- }
- $i++;
- }
- }
- $body .= "&key={$key}";
- Log::record("notify body={$body}",Log::DEBUG);
- return md5($body);
- }
- private function check_empty($value)
- {
- if (!isset($value))
- return true;
- if ($value === null)
- return true;
- if (trim($value) === "")
- return true;
- return false;
- }
- private function is_url($url)
- {
- $checker = function ($haystack, $needle) {
- $length = strlen($needle);
- return (substr($haystack, 0, $length) === $needle);
- };
- return $checker($url, "http://") || $checker($url, "https://");
- }
- }
|