123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- namespace refill;
- require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- require_once(BASE_HELPER_PATH . '/refill/suhc/RefillOil.php');
- require_once(BASE_HELPER_PATH . '/refill/suhc/RefillPhone.php');
- require_once(BASE_HELPER_PATH . '/refill/suhc/RefillCallBack.php');
- require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
- require_once(BASE_HELPER_PATH . '/refill/beixt/RefillPhone.php');
- require_once(BASE_HELPER_PATH . '/refill/beixt/RefillCallBack.php');
- require_once(BASE_HELPER_PATH . '/refill/beixts/RefillPhone.php');
- require_once(BASE_HELPER_PATH . '/refill/beixts/RefillCallBack.php');
- use Log;
- use mtopcard;
- use QueueClient;
- use member_info;
- class RefillFactory
- {
- static private $stInstance = null;
- static public function instance()
- {
- if (self::$stInstance == null) {
- self::$stInstance = new RefillFactory();
- }
- return self::$stInstance;
- }
- private $mOilProvider = [];
- private $mPhoneProvider = [];
- private $mProviderNames = [];
- private function __construct()
- {
- $this->load();
- }
- public function goods()
- {
- global $config;
- $oil = $this->combine_goods($config['oil_providers']);
- $phone = $this->combine_goods($config['phone_providers']);
- return array_merge($oil,$phone);
- }
- private function combine_goods($configs)
- {
- $result = [];
- foreach ($configs as $cfg)
- {
- $card_types = $cfg['card_type'];
- $amounts = $cfg['amount'];
- foreach ($card_types as $type)
- {
- if(array_key_exists($type,$result)) {
- $item = $result[$type];
- }
- else {
- $item = [];
- }
- foreach ($amounts as $amount => $val) {
- $item[] = $amount;
- }
- $item = array_unique($item);
- $result[$type] = $item;
- }
- }
- return $result;
- }
- private function load()
- {
- global $config;
- $pro_configs = $config['oil_providers'];
- $names = [];
- foreach ($pro_configs as $cfg) {
- $name = $cfg['name'];
- $names[] = $name;
- if($name == 'suhc') {
- $provider = new suhc\RefillOil($cfg);
- $this->mOilProvider[] = $provider;
- }
- else {
- Log::record("Oil Provider has no name",Log::ERR);
- }
- }
- $pro_configs = $config['phone_providers'];
- foreach ($pro_configs as $cfg) {
- $name = $cfg['name'];
- $names[] = $name;
- if($name == 'beixt') {
- $provider = new beixt\RefillPhone($cfg);
- $this->mPhoneProvider[] = $provider;
- }
- else {
- Log::record("Phone Provider has no name",Log::ERR);
- }
- }
- $this->mProviderNames = array_unique($names);
- }
- public function find_providers(int $amount,int $card_type) : array
- {
- if($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard){
- return $this->find_oil($amount,$card_type);
- }
- elseif($card_type == mtopcard\ChinaMobileCard || $card_type == mtopcard\ChinaUnicomCard || $card_type == mtopcard\ChinaTelecomCard) {
- return $this->find_phone($amount,$card_type);
- }
- else {
- return [];
- }
- }
- private function find_oil(int $amount,int $card_type) : array
- {
- $providers = [];
- foreach ($this->mOilProvider as $provider)
- {
- $name = $provider->name();
- [$success,$err] = $provider->check($amount,$card_type);
- if($success) {
- $providers[] = $provider;
- }
- else {
- Log::record("{$name} provider cannot match check,err:{$err}",Log::DEBUG);
- }
- }
- return $providers;
- }
- private function find_phone(int $amount,int $card_type) : array
- {
- $providers = [];
- foreach ($this->mPhoneProvider as $provider)
- {
- $name = $provider->name();
- [$success,$err] = $provider->check($amount,$card_type);
- if($success) {
- $providers[] = $provider;
- }
- else {
- Log::record("{$name} provider cannot match check,err:{$err}",Log::DEBUG);
- }
- }
- return $providers;
- }
- public function notify($chname,$input)
- {
- if(!in_array($chname,$this->mProviderNames)) {
- return false;
- }
- $class_name = "{$chname}\\RefillCallBack";
- $caller = new $class_name();
- if($caller->verify($input))
- {
- [$order_id,$success,$can_try] = $caller->notify($input);
- if($order_id !== false)
- {
- $mod_order = Model('vr_order');
- $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
- $order_state = intval($order_info['order_state']);
- if($order_state != ORDER_STATE_SEND) {
- return false;
- }
- $mod_refill = Model('refill_order');
- $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id]);
- $logic_vr_order = Logic("vr_order");
- if($success) {
- $logic_vr_order->changeOrderStateSuccess($order_id);
- }
- elseif ($can_try)
- {
- $logic_vr_order->changeOrderStateCancel($order_id);
- if($this->retry($refill_info) === false) {
- }
- else {
- }
- }
- else {
- $logic_vr_order->changeOrderStateCancel($order_id);
- }
- $mod_refill->edit($order_id,['notify_time' => time(),'notify_state' => 1]);
- QueueClient::push("NotifyMerchantComplete",['order_id' => $order_id]);
- }
- else {
- Log::record("系统无此订单ID:{$order_id}",Log::ERR);
- }
- }
- else {
- Log::record("{$chname} 签名失败.");
- }
- return true;
- }
- private function retry(array $refill_info)
- {
- return false;
- }
- public function add($mchid,$buyer_id,$amount,$card_no,$mch_order,$notify_url,$try_times = 0)
- {
- $card_type = mtopcard\card_type($card_no);
- $providers = $this->find_providers($amount,$card_type);
- if(empty($providers)) {
- return [202,"找不到合适的充值通道"];
- }
- $minfo = new member_info($buyer_id);
- $calc = new CalcMerchantPrice($mchid,$amount,$card_type);
- $mch_amount = $calc->calc_vgoods_price([]);
- if($mch_amount > $minfo->available_predeposit()) {
- return [203,"余额不足"];
- }
- $refill_state = false;
- foreach ($providers as $provider)
- {
- $channel_name = $provider->name();
- [$goods_id, $price] = $provider->goods($amount);
- $input['goods_id'] = $goods_id;
- $input['quantity'] = 1;
- $input['price'] = $price;
- $input['buyer_phone'] = $minfo->mobile();
- $input['buyer_name'] = $minfo->truename();
- $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
- $input['order_from'] = 1;
- $input['pd_pay'] = true;
- $logic_buy_virtual = Logic('buy_virtual');
- $result = $logic_buy_virtual->buyStep3($input, $buyer_id,[$calc,'calc_vorder_amount'],true);
- $mod_refill = Model('refill_order');
- if($result['state'] === true)
- {
- $order_sn = $result['data']['order_sn'];
- $order_id = $result['data']['order_id'];
- //虚拟订单表信息扩展
- $orderext = ['order_id' => $order_id,'order_sn' => $order_sn,'mchid' => $mchid,
- 'refill_amount' => $amount,'mch_order' => $mch_order,
- 'notify_url' => $notify_url,'channel_name' => $channel_name,
- 'mch_amount' => $mch_amount,'channel_amount' => $price,'order_time' => time(),
- 'card_type' => $card_type,'card_no' => $card_no];
- $mod_refill->add_refill($orderext);
- }
- else {
- continue;
- }
- $params = ['order_sn' => $order_sn];
- [$state,$err] = $provider->add($card_no,$card_type,$amount,$params);
- if($state) {
- $trade_no = $err;
- $logic_vr_order = Logic("vr_order");
- $logic_vr_order->changeOrderStateSend($order_id);
- $data = ['commit_time' => time(),'ch_trade_no' => $trade_no];
- $mod_refill->edit($order_id,$data);
- $refill_state = true;
- break;
- }
- else {
- Log::record("channel:{$channel_name} err:{$err}");
- $logic_vr_order = Logic("vr_order");
- $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
- $logic_vr_order->changeOrderStateCancel($order_info,'',"调用{$channel_name}接口失败");
- }
- }
- if($refill_state) {
- return [true,$order_sn];
- } else {
- return [204,"充值失败."];
- }
- }
- public function notify_merchant($order_id)
- {
- if($order_id <= 0) {
- return [false,"订单ID小于0"];
- }
- $vr_order = Model('vr_order');
- $refill_order = Model('refill_order');
- $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]);
- $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
- if(empty($order_info) || empty($refill_info)) {
- return [false,"无此订单"];
- }
- if ($refill_info['mch_notify_state'] != 0) {
- return [false,"已经通知客户方"];
- }
- $notify_url = $refill_info['notify_url'];
- if(empty($notify_url)) {
- return [false,"回调地址为空"];
- }
- $order_state = $order_info['order_state'];
- if($order_state == ORDER_STATE_CANCEL) {
- $state = "CANCEL";
- }
- elseif($order_state == ORDER_STATE_SUCCESS) {
- $state = "SUCCESS";
- }
- else {
- return [false,"错误的订单状态,不能通知."];
- }
- $mchid = $refill_info['mchid'];
- $mch_info = Model('merchant')->getMerchantInfo($mchid);
- [$params,$sign] = $this->body($state,$refill_info,$mch_info);
- $params['sign'] = $sign;
- $resp = http_request($notify_url,$params,'POST');
- if($resp == "SUCCESS") {
- $refill_order->edit(['order_id' => $order_id],['mch_notify_state' => 1,'mch_notify_times' => ['exp','mch_notify_times+1']]);
- return [true,""];
- }
- else {
- $refill_order->edit(['order_id' => $order_id],['mch_notify_times' => ['exp','mch_notify_times+1']]);
- $times = $refill_info['mch_notify_times'] + 1;
- if($times > 80) {
- $refill_order->edit(['order_id' => $order_id],['mch_notify_state' => 2]);
- }
- else {
- $N = intval($times / 5);
- $period = intval(pow(2,$N));
- QueueClient::async_push("NotifyMerchantComplete",['order_id' => $order_id],$period);
- }
- return [false,"通知{$times}次,失败."];
- }
- }
- public function OrderQuery($channel_name,$params){
- if($channel_name == "suhc") {
- $query_cls = new suhc\RefillOil([]);
- [$state,$err] = $query_cls->OrderQuery($params['ch_trade_no'],$params['card_no'],$params['order_sn']);
- }elseif ($channel_name == 'beixt'){
- $query_cls = new beixt\RefillPhone([]);
- [$state,$err] = $query_cls->OrderQuery($params['ch_trade_no'],$params['order_sn']);
- }
- else {
- return false;
- }
- }
- private function body($state,$refill_info,$mch_info)
- {
- $params = [
- "mchid" => $refill_info['mchid'],
- "order_sn" => $refill_info['mch_order'],
- "amount" => $refill_info['refill_amount'],
- "cardno" => $refill_info['card_no'],
- "trade_no" => $refill_info['order_sn'],
- "idcard" => $refill_info['idcard'] ?? "",
- "card_name" => $refill_info['card_name'] ?? "",
- "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}";
- 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;
- }
- }
|