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://"); } }