123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- <?php
- namespace refill;
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
- require_once(BASE_HELPER_PATH . '/queue/monitor.php');
- require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
- use queue;
- use mtopcard;
- use Log;
- use Exception;
- use Cache;
- use QueueClient;
- use refill;
- use trans_wapper;
- class util
- {
- const ThirdRefillAmount = 100;
- static function make_mobile()
- {
- static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
- "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
- "176", "133", "153", "189", "180", "181", "177"];
- $pos = mt_rand(0, count($prefix) - 1);
- $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
- return $no;
- }
- public static function can_refill($card_no, $card_type)
- {
- if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
- $result = rcache('card_expired', '', "{$card_no}");
- if (empty($result)) {
- wcache("card_expired", [$card_no => time()], '');
- return [true, 0];
- } else {
- $latest = current($result);
- $cur = time();
- $success = ($cur - $latest) > 2;
- if ($success) {
- wcache("card_expired", [$card_no => time()], '');
- }
- return [$success, $latest + 2 - $cur];
- }
- } else {
- return [true, 0];
- }
- }
- public static function can_commit($card_no, $card_type)
- {
- if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
- {
- $result = rcache('card_expired', '', "{$card_no}");
- if (empty($result)) {
- wcache("card_expired", [$card_no => time()], '');
- return [true, 0];
- }
- else
- {
- $latest = current($result);
- $cur = time();
- $lowest = 30;
- if ($cur > $latest && ($cur - $latest) >= $lowest) {
- wcache("card_expired", [$card_no => time()], '');
- return [true, 0];
- } else {
- wcache("card_expired", [$card_no => $latest + $lowest], '');
- return [false, $latest + $lowest - $cur];
- }
- }
- }
- else {
- return [true, 0];
- }
- }
- static function write_card($card_no, $card_type,$bind_phone)
- {
- if(empty($bind_phone)) {
- return false;
- }
- if ($card_type !== mtopcard\SinopecCard && $card_type !== mtopcard\PetroChinaCard) {
- return false;
- }
- $mobile_types = [mtopcard\ChinaMobileCard,mtopcard\ChinaUnicomCard,mtopcard\ChinaTelecomCard];
- $ctype = mtopcard\simple_card_type($bind_phone);
- if (!in_array($ctype,$mobile_types)) {
- return false;
- }
- $mod_topcard = Model('topcard');
- $ret = $mod_topcard->get_card($card_no);
- if (empty($ret)) {
- $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
- } else {
- $mod_topcard->edit($card_no,$bind_phone);
- }
- dcache($card_no, 'cardrefill-');
- return true;
- }
- static function read_card($card_no, $card_type = 0)
- {
- if (empty($card_no)) return false;
- $data = rcache($card_no, 'cardrefill-');
- if (empty($data)) {
- $mod_topcard = Model('topcard');
- $ret = $mod_topcard->get_card($card_no);
- if (empty($ret)) {
- if ($card_type === 0) {
- $card_type = mtopcard\card_type($card_no,$regin_no);
- }
- $bind_phone = util::make_mobile();
- $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
- $data['bind_phone'] = $bind_phone;
- $data['refill_time'] = time();
- $data['times'] = 0;
- $data['black_card'] = 0;
- wcache($card_no, $data, 'cardrefill-');
- } else {
- $val = $ret[0];
- $data['bind_phone'] = $val['bind_phone'];
- $data['black_card'] = $val['black_card'];
- $data['refill_time'] = time();
- $data['times'] = 0;
- }
- }
- //之前没加black_card处理,这个字段不存在.
- if (!array_key_exists('black_card', $data)) {
- $data['black_card'] = 0;
- }
- return $data;
- }
- static function inc_card($card_no, $card_info)
- {
- $card_info['times'] += 1;
- $card_info['refill_time'] = time();
- wcache($card_no, $card_info, 'cardrefill-');
- }
- public static function del_card($card_no)
- {
- dcache($card_no, 'cardrefill-');
- }
- public static function set_black($card_no)
- {
- if (empty($card_no)) return false;
- $card_info = util::read_card($card_no);
- if (!empty($card_info)) {
- $card_info['black_card'] = 1;
- $mod_topcard = Model('topcard');
- $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
- wcache($card_no, $card_info, 'cardrefill-');
- return true;
- } else {
- return false;
- }
- }
- private static function black_order($order_sn, $msg)
- {
- static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
- if (empty($msg)) return false;
- if (in_array($msg, $errMsgs)) {
- $refill = Model('refill_order');
- $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
- if (empty($order)) return false;
- $card_no = $order['card_no'];
- return util::set_black($card_no);
- }
- }
- public static function black_from_log($file_name)
- {
- $fn = fopen($file_name, "r");
- if (empty($fn)) {
- Log::record("Open File {$file_name} error.", Log::ERR);
- return false;
- } else {
- Log::record("{$file_name} start woring", Log::DEBUG);
- }
- $errs = [];
- while (!feof($fn)) {
- $line = trim(fgets($fn));
- $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u', $line, $matches);
- if ($ret) {
- $order_sn = $matches['order_sn'];
- $message = $matches['message'];
- self::black_order($order_sn, $message);
- $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
- }
- }
- foreach ($errs as $msg => $count) {
- Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
- }
- fclose($fn);
- return true;
- }
- public static function async_add($params, $period = 10)
- {
- try {
- QueueClient::async_push("AysncAddDispatcher", ['method' => 'add', 'params' => $params], $period);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function async_notify($chname,$data, $period)
- {
- try {
- QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
- return true;
- } catch (Exception $ex) {
- return false;
- }
- }
- public static function push_add($params)
- {
- try
- {
- $ret = self::push_queue('add', $params);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_add_zero($params)
- {
- try
- {
- $ret = self::push_queue('add_zero', $params);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_addthird($params)
- {
- try
- {
- $ret = self::push_queue('addthird', $params);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_notify($chname, $params)
- {
- try
- {
- $ret = self::push_queue('notify', ['channel' => $chname, 'params' => $params]);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_notify_merchant($order_id, $manual)
- {
- try
- {
- $ret = self::push_queue('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_query($order_id)
- {
- try
- {
- $ret = self::push_queue('query', ['order_id' => $order_id]);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_query_net($order_id)
- {
- try {
- $ret = self::push_queue('query_net', ['order_id' => $order_id]);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function manual_success($order_id)
- {
- try
- {
- $ret = self::push_queue('manual_success', ['order_id' => $order_id]);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function manual_cancel($order_id)
- {
- try
- {
- $ret = self::push_queue('manual_cancel', ['order_id' => $order_id]);
- return $ret !== false;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- public static function push_queue($method, $value)
- {
- if (defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP) {
- $queue_name = 'QUEUE_DISPATCHER_CO';
- $ins = Cache::getInstance('cacheredis');
- return $ins->lpush($queue_name, serialize([$method => $value]));
- }
- else {
- return queue\DispatcherClient::instance()->push($method,$value);
- }
- }
- public static function dispatcher_queue_length()
- {
- $ins = Cache::getInstance('cacheredis');
- return $ins->lLen('QUEUE_DISPATCHER_CO');
- }
- public static function monitor_queue_length()
- {
- $ins = Cache::getInstance('cacheredis');
- return $ins->lLen('REFILL_MONITOR_QUEUE');
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function monitor_submit($mchid, $spec, $card_type, $mch_amount, $time)
- {
- $time = intval($time);
- queue\MonitorClient::instance()->onSubmit($mchid,$time,$spec,$card_type,$mch_amount);
- }
- public static function monitor_callback($mchid,$spec,$card_type,$mch_amount,$channel_amount,$succ,$time)
- {
- queue\MonitorClient::instance()->onCallback($mchid,$time,$spec,$card_type,floatval($mch_amount),floatval($channel_amount),$succ);
- }
- public static function monitor_netchk($chname,$succ)
- {
- queue\MonitorClient::instance()->onNetCheck($chname, time(),$succ);
- }
- public static function monitor_commit($chname, $spec, $card_type, $channel_amount,$commit_time)
- {
- queue\MonitorClient::instance()->onCommit($chname, $commit_time, $spec, $card_type, $channel_amount);
- }
- public static function monitor_notify($chname, $spec, $card_type, $channel_amount, $period, $succ,$commit_time)
- {
- queue\MonitorClient::instance()->onNotify($chname, $commit_time, $spec, $card_type, floatval($channel_amount), $period, $succ);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function set_order_channels($mchid,$mchorder,$datas)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_channels';
- $key = "{$mchid}-{$mchorder}";
- $ins->hset($name, '', [$key => serialize($datas)]);
- }
- public static function get_order_channels($mchid, $mchorder)
- {
- //old-name oil_exclude_channels
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_channels';
- $key = "{$mchid}-{$mchorder}";
- $chnames = $ins->hget($name, '', $key);
- $chnames = unserialize($chnames);
- if(is_array($chnames)) {
- return $chnames;
- } else {
- return [];
- }
- }
- public static function del_order_channels($mchid, $mchorder)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_channels';
- $key = "{$mchid}-{$mchorder}";
- $ins->hdel($name, '', $key);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function set_cancel_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_cancel_hash';
- $key = "{$mchid}-{$mch_order}";
- $ins->hset($name, '', [$key=> 1]);
- }
- public static function query_cancel_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_cancel_hash';
- $key = "{$mchid}-{$mch_order}";
- $value = $ins->hget($name,'',$key);
- return $value;
- }
- public static function del_cancel_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_cancel_hash';
- $key = "{$mchid}-{$mch_order}";
- $ins->hdel($name, $key);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function set_next_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_next_hash';
- $key = "{$mchid}-{$mch_order}";
- $ins->hset($name, '', [$key=> 1]);
- }
- public static function query_next_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_next_hash';
- $key = "{$mchid}-{$mch_order}";
- $value = $ins->hget($name,'',$key);
- return $value;
- }
- public static function del_next_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'order_success_hash';
- $key = "{$mchid}-{$mch_order}";
- $ins->hdel($name, $key);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function merchant_debt_stoped($mchid)
- {
- if($mchid > 0)
- {
- $ret = rcache('merchant-debt-judge', 'refill-',"{$mchid}");
- if(empty($ret)) {
- return false;
- }
- $stoped = intval($ret[$mchid]);
- return ($stoped === 1);
- }
- else {
- return false;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function push_queue_order($mchid,$mch_order,$order_state)
- {
- if(empty($mch_order)) return;
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $ins->hset($name, '', [$key => $order_state]);
- }
- public static function del_queue_order($mchid,$mch_order)
- {
- if(empty($mch_order)) return;
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $ret = $ins->hdel($name, '', $key);
- }
- public static function pop_queue_order($mchid,$mch_order)
- {
- util::del_order_channels($mchid,$mch_order);
- Model('refill_order')->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]);
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $ret = $ins->hdel($name, '', $key);
- }
- public static function query_queue_order($mchid,$mch_order)
- {
- $ins = Cache::getInstance('cacheredis');
- $name = 'merchant_order_query';
- $key = "{$mchid}-{$mch_order}";
- $value = $ins->hget($name,'',$key);
- return $value;
- }
- public static function need_check($net_errno)
- {
- if(empty($net_errno)) return false;
- [$type,$code] = explode('-',$net_errno);
- $code = intval($code);
- if($type == "CURL") {
- static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR];
- return in_array($code,$errors);
- } elseif($type == "HTTP") {
- static $excludes = [404];
- return !in_array($code,$excludes);
- } else {
- return false;
- }
- }
- public static function order_errflag($net_errno) {
- return ($net_errno === 'ORDER_CREATE_FAIL');
- }
- public static function onOrderSuccess($refill_info,$order_info)
- {
- $data = store_member::instance()->get_member($order_info['store_id']);
- if(empty($data)) {
- Log::record("cannot find member when store_id={$order_info['store_id']}",Log::ERR);
- return false;
- }
- $data['order_sn'] = $refill_info['order_sn'];
- $data['amount'] = $refill_info['channel_amount'];
- $model_pd = Model('predeposit');
- $model_pd->changePd('order_pay',$data,true);
- return true;
- }
- public static function getProvider($name,$type = 'RefillPhone')
- {
- $file = BASE_HELPER_RAPI_PATH . "/$name/{$type}.php";
- if(!file_exists($file)){
- Log::record("provider api file={$file} not exist.",Log::DEBUG);
- return false;
- } else {
- require_once($file);
- Log::record("file={$file} load success.",Log::DEBUG);
- }
- $class_name = "refill\\{$name}\\{$type}";
- if (class_exists($class_name, false)) {
- $caller = new $class_name([]);
- return $caller;
- } else {
- $error = "Base Error: class {$class_name} isn't exists!";
- Log::record($error, Log::ERR);
- return false;
- }
- }
- public static function xmlToArray($xml)
- {
- $object = simplexml_load_string($xml);
- $val = json_decode(json_encode($object), true);
- $msg = json_encode($val);
- Log::record("xmlToArray result={$msg}", Log::DEBUG);
- return $val;
- }
-
- //for tester
- public static function send_normal($order_sn)
- {
- $status = mt_rand(1,100);
- if($status > 97) {
- $status = 1;
- } else {
- $status = 0;
- }
- $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
- go(function () use ($url, $status,$order_sn)
- {
- sleep(3);
- while (true)
- {
- $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
- if($resp == 'SUCCESS') {
- break;
- }
- }
- Log::record("resp = {$resp}",Log::DEBUG);
- });
- }
- public static function send_quick($order_sn)
- {
- $status = mt_rand(1,10);
- if($status > 3) {
- $status = 1;
- } else {
- $status = 0;
- }
- $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
- go(function () use ($url, $status,$order_sn)
- {
- sleep(3);
- while (true)
- {
- $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
- if($resp == 'SUCCESS') {
- break;
- }
- }
- Log::record("resp = {$resp}",Log::DEBUG);
- });
- }
- public static function retry_canceled_order($order_id, $skip)
- {
- $mod_order = Model('vr_order');
- $mod_refill = Model('refill_order');
- $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
- $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
- if(empty($refill_info) || empty($order_info)) {
- return [false,'无此订单或者订单已经重试中了...'];
- }
- $tran = new trans_wapper($mod_order,'notify change order state trans');
- try
- {
- $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
- $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
- $order_state = intval($order_info['order_state']);
- if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
- $tran->commit();
- return [false,'订单已经在重试'];
- }
- $mod_refill->edit($order_id, ['is_retrying' => 1]);
- $tran->commit();
- $order = refill\order::from_db($refill_info,$order_info);
- $params = $order->queue_params();
- $params['order_time'] = time();
- if ($skip) {
- $mchid = $refill_info['mchid'];
- $mch_order = $refill_info['mch_order'];
- refill\util::set_next_order($mchid, $mch_order);
- }
- if(util::push_add($params)) {
- return [true,''];
- } else {
- return [false,'加入队列出错'];
- }
- }
- catch (Exception $ex) {
- $tran->rollback();
- Log::record($ex->getMessage(),Log::ERR);
- return [false,"{$ex->getMessage()}"];
- }
- }
- public function transfer_success_order($order_id, $manual_recharge_amount)
- {
- $mod_order = Model('vr_order');
- $mod_refill = Model('refill_order');
- $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
- $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
- if(empty($refill_info) || empty($order_info)) {
- return [false,'无此订单'];
- }
- $tran = new trans_wapper($mod_order,'notify change order state trans');
- try
- {
- $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
- $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
- $order_state = intval($order_info['order_state']);
- if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
- $tran->commit();
- return [false,'订单已经在重试'];
- }
- $mod_refill->edit($order_id, ['is_retrying' => 1]);
- $order = refill\order::from_db($refill_info,$order_info);
- $mchid = $order->mchid();
- [$success,$success_order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
- if(!$success) {
- $tran->rollback();
- return [false, $errmsg];
- }
- $mod_refill->edit($success_order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
- $mod_refill->edit($order_id, ['is_retrying' => 0]);
- if($manual_recharge_amount > 0) {
- $mod_refill->edit($success_order_id, ['channel_amount' => $manual_recharge_amount]);
- }
- $tran->commit();
- return [true,''];
- }
- catch (Exception $ex) {
- $tran->rollback();
- Log::record($ex->getMessage(),Log::ERR);
- return [false,"{$ex->getMessage()}"];
- }
- }
- }
|