123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace refill;
- use Log;
- use rbridge\RBridgeFactory;
- use StatesHelper;
- use mtopcard;
- class policy extends ProviderManager implements IPolicy
- {
- protected $mChannelControl;
- protected $mQuality;
- protected $mPrices;
- protected $mAmountLockTurn;
- protected $mStorageLocker;
- protected $mGroupCtl;
- public function __construct()
- {
- parent::__construct();
- $this->mChannelControl = new chctl();
- $this->mQuality = new quality_ploy();
- $this->mPrices = new merchant_price();
- $this->mStorageLocker = new rstorage();
- $this->mGroupCtl = new rgroup_ctl();
- }
- public function load()
- {
- parent::load();
- $this->mChannelControl->load();
- $this->mQuality->load();
- $this->mPrices->load();
- $turn_name = 'oil_amount_lock_turn';
- $this->mAmountLockTurn = rkcache($turn_name);
- Log::record("AmountLockTurn = {$this->mAmountLockTurn}",Log::DEBUG);
- $this->mStorageLocker->load();
- $this->mGroupCtl->load();
- }
- public function find_providers(int $mchid, int $spec, int $card_type, int $quality, $regin_no): array
- {
- $providers = parent::find_providers($mchid,$spec,$card_type,$quality,$regin_no);
- if(empty($providers)) {
- return [$providers,false];
- }
- $names = [];
- foreach ($providers as $provider) {
- $names[] = $provider->name();
- }
- Log::record(" mGroupCtl find_providers result=" . implode(',',$names),Log::DEBUG);
- [$hasGroup,$can_others,$channels] = $this->mGroupCtl->find_providers($mchid, $spec, $card_type, $quality);
- if($hasGroup)
- {
- if(empty($channels))
- {
- if(!$can_others) {
- return [[],false];
- }
- }
- else
- {
- $ret = array_intersect($names, $channels);
- if (empty($ret)) {
- return [[],false];
- }
- else {
- $names = $ret;
- }
- }
- }
- Log::record("after mGroupCtl find_providers result=" . implode(',',$names),Log::DEBUG);
- $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)
- {
- Log::record("channel {$name} has overloaded {$overload}",Log::DEBUG);
- 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,$pcode)
- {
- return $this->mPrices->price($mchid,$card_type,$spec,$quality,$pcode);
- }
- public function find_quality($mchid,$spec,$card_type,$org_quality,$times,$used_time,$pcode): 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,$pcode);
- if($price === false) {
- Log::record("{$mchid} 没有协商 quality = {$quality} 价格",Log::DEBUG);
- continue;
- }
- [$providers,$overload] = $this->find_providers($mchid,$spec,$card_type,$quality,-1);
- 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
- {
- return $this->mStorageLocker->allow($mchid,$card_type,$amount);
- }
- private function allow_storge($mchid,$card_type,$amount,$quality)
- {
- $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];
- [$has_sms,$sms] = $this->sms($refill_info);
- if($has_sms) {
- $params['sms'] = $sms;
- }
- $secure_key = $mch_info['secure_key'];
- $sign = $this->sign($params, $secure_key);
- return [$params, $sign];
- }
- private function sms($refill_info)
- {
- $mchids = [1090,10116];
- $mchid = $refill_info['mchid'];
- $official_sn = $refill_info['official_sn'] ?? "";
- $card_type = $refill_info['card_type'];
- $card_no = $refill_info['card_no'];
- $amount = intval($refill_info['refill_amount']);
- if(in_array($mchid,$mchids) && !empty($official_sn) && $card_type == mtopcard\SinopecCard && !empty($card_no))
- {
- $short_no = substr($card_no,-6);
- $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);
- if($ret > 0) {
- $sms = "【中国石化】您尾号为{$short_no}的加油卡于{$matches['month']}月{$matches['day']}日 {$matches['hour']}时{$matches['min']}分充值成功,金额{$amount}元,订单号:{$official_sn}";
- return [true,$sms];
- }
- }
- return [false,''];
- }
- 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://");
- }
- }
|