goods(); $sorter = function (array $items) { $ret = []; foreach ($items as $key => $val) { sort($val); $ret[$key] = $val; } return $ret; }; $goods_list = $sorter($goods_list); $result = []; $mchid = $this->mchid(); $merchant_goods = rcache('merchant_goods', 'refill-', $mchid); $goods = $merchant_goods[$mchid] ?? serialize([]); $goods = unserialize($goods); foreach ($goods_list as $card_type => $value) { $key = mtopcard\scard_type($card_type); if (array_key_exists($key, $goods)) { $result[$key] = $goods[$key]; } else { $result[$key] = $value; } } $thrid_refill = Model('thrid_refill'); $pcode = $thrid_refill->getMerchantPcode($mchid); $result['third'] = $pcode; return self::outsuccess($result); } private function check_params($params) { if(empty($params['cardno'])) { return [false,'参数没有包含cardno']; } if(empty($params['amount'])) { return [false,'参数没有包含充值金额:amount']; } $card_no = $_GET['cardno']; $card_type = mtopcard\simple_card_type($card_no); if($card_type == mtopcard\UnknownCard) { return [false,'卡类型无法识别']; } if(empty($params['notifyurl'])) { return [false,'参数没有包含notifyurl']; } if(empty($params['order_sn'])) { return [false,'参数没有包含贵方唯一订单号:order_sn']; } return [true,""]; } public function addOp() { [$success,$error] = $this->check_params($_GET); if($success === false) { return self::outerr(201,$error); } $amount = intval($_GET['amount']); $card_no = trim($_GET['cardno']); $notify_url = $_GET['notifyurl']; $mch_order = $_GET['order_sn']; //对方的order编号 $quality = $_GET['quality'] ?? 0; $bind_phone = $_GET['bind_phone'] ?? ''; if(!$this->check_mchorder($this->mchid(),$mch_order)) { return self::outerr(205,"客户订单号重复或者为空."); } $card_type = mtopcard\simple_card_type($card_no); if($card_type === mtopcard\SinopecCard || $card_type === mtopcard\PetroChinaCard) { refill\util::write_card($card_no,$card_type,$bind_phone); if(!$this->can_refill($card_no)) { return self::outerr(206,"平台不支持该卡充值."); } $allow = true; if(!$allow) { if ($card_type === mtopcard\SinopecCard) { $text = "中石化"; } else { $text = "中石油"; } return self::outerr(207,"今日贵司{$amount}{$text}充值库存已经用完."); } } $order_time = time(); $params = ['mchid' => $this->mchid(), 'buyer_id' => $this->adminid(), 'amount' => $amount, 'card_no' => $card_no, 'mch_order' => $mch_order, 'notify_url' => $notify_url, 'org_quality' => $quality, 'order_time' => $order_time ]; refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE); Model('refill_order')->add_detail($this->mchid(),$mch_order,$order_time,$params,ORDER_STATE_QUEUE); [$can_refill, $period] = refill\util::can_commit($card_no, $card_type); if ($can_refill === false) { $state = refill\util::async_add($params, $period); } else { $state = refill\util::push_add($params); } $mchid = $this->mchid(); if ($state === true) { Log::record("refill::util::push_add success mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG); return self::outsuccess(['state' => true]); } else { refill\util::del_queue_order($this->mchid(),$mch_order); Model('refill_order')->del_detail($this->mchid(),$mch_order); Log::record("refill::util::push_add error mchid={$mchid} mch_order={$mch_order} state={$state}",Log::DEBUG); return self::outerr(208, '提交失败'); } } private function check_third($params) { if(empty($params['product_code'])) { return [false,'参数没有包含product_code']; } $pcode = $params['product_code']; if($this->check_pcode($pcode) == false) { return [false,"对应的产品编码{$pcode}"]; } if(empty($params['quantity']) || intval($params['quantity']) < 1) { return [false,"购买数量参数不存在,或者小于0"]; } if(empty($params['notifyurl'])) { return [false,'参数没有包含notifyurl']; } if(empty($params['order_sn'])) { return [false,'参数没有包含贵方唯一订单号:order_sn']; } return [true,""]; } private function check_pcode($pcode) { $mod_third = Model('thrid_refill'); $product = $mod_third->getProduct(['system_code' => $pcode]); return !empty($product); } private function add_third_product($third_product_type) { [$success,$error] = $this->check_third($_GET); if($success === false) { return self::outerr(201,$error); } $amount = refill\util::ThirdRefillAmount; $notify_url = $_GET['notifyurl']; $mch_order = $_GET['order_sn']; //对方的order编号 $product_code = $_GET['product_code']; $card_no = $_GET['cardno'] ?? ''; $quantity = $_GET['quantity']; $third_card_type = $_GET['card_type'] ?? 1; //三方充值没有质量 $quality = 1; $card_type = mtopcard\ThirdRefillCard; if(!$this->check_mchorder($this->mchid(),$mch_order)) { return self::outerr(205,"客户订单号重复或者为空."); } $order_time = time(); $params = ['mchid' => $this->mchid(), 'buyer_id' => $this->adminid(), 'amount' => $amount, 'mch_order' => $mch_order, 'notify_url' => $notify_url, 'org_quality' => $quality, 'card_type' => $card_type, 'card_no' => $card_no, 'product_code' => $product_code, 'quantity' => $quantity, 'third_card_type' => $third_card_type, 'third_product_type' => $third_product_type, 'order_time' => $order_time ]; refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE); Model('refill_order')->add_detail($this->mchid(), $mch_order, $order_time, $params, ORDER_STATE_QUEUE); $state = refill\util::push_addthird($params); if ($state === true) { return self::outsuccess(['state' => true]); } else { refill\util::del_queue_order($this->mchid(),$mch_order); Model('refill_order')->del_detail($this->mchid(),$mch_order); return self::outerr(208, '提交失败'); } } public function add_thirdOp() { return $this->add_third_product(mtopcard\ThirdOnlineProduct); } public function add_netOp() { return $this->add_third_product(mtopcard\ThirdNetTrafficPoroduct); } private function check_electirc($params) { if(empty($params['cardno'])) { return [false,'参数没有包含cardno']; } if(empty($params['notifyurl'])) { return [false,'参数没有包含notifyurl']; } if(empty($params['order_sn'])) { return [false,'参数没有包含贵方唯一订单号:order_sn']; } if(empty($params['amount']) || intval($params['amount']) <= 0) { return [false,'参数没有包含充值金额:amount']; } if(empty($params['company_type']) || !in_array($params['company_type'],mtopcard\ElectricCompanyTypes)) { return [false,'没有电卡类型参数(nation,south)']; } if(empty($params['use_type']) || !in_array($params['use_type'],mtopcard\ElectricUseTypes)) { return [false,"没有电卡用途参数(home,commerce,pedlar)"]; } if(empty($params['province']) || !array_key_exists($params['province'],mtopcard\ProvinceList)) { return [false,"请传入省份"]; } if(empty($params['city'])) { return [false,"请传入城市名称"]; } //如果是南方电网需要带身份证后六位 if($params['company_type'] === 'south') { $card_id = $params['card_id']; if(empty($card_id) || strlen($card_id) != mtopcard\CardidVerifyLength || !mtopcard\is_alpha($card_id)) { return [false,"请传入正确的身份证后6位"]; } } return [true,""]; } public function add_electricOp() { [$success,$error] = $this->check_electirc($_GET); if($success === false) { return self::outerr(201,$error); } $notify_url = $_GET['notifyurl']; $mch_order = $_GET['order_sn']; //对方的order编号 $card_no = $_GET['cardno'] ?? ''; $quantity = 1; $third_card_type = mtopcard\ThirdCardElect; $company_type = $_GET['company_type']; $use_type = $_GET['use_type']; $province = intval($_GET['province']); $city = $_GET['city']; $amount = intval($_GET['amount']); if($company_type == 'nation') { $card_id = ''; } else { $card_id = $_GET['card_id'] ?? ''; } $product_code = mtopcard\electric_product_code($company_type,$use_type,$amount); $amount = refill\util::ThirdRefillAmount; //三方充值没有质量 $quality = 1; $card_type = mtopcard\ThirdRefillCard; if(!$this->check_mchorder($this->mchid(),$mch_order)) { return self::outerr(205,"客户订单号重复或者为空."); } $order_time = time(); $params = ['mchid' => $this->mchid(), 'buyer_id' => $this->adminid(), 'amount' => $amount, 'mch_order' => $mch_order, 'notify_url' => $notify_url, 'org_quality' => $quality, 'card_type' => $card_type, 'card_no' => $card_no, 'product_code' => $product_code, 'quantity' => $quantity, 'third_card_type' => $third_card_type, 'third_product_type' => mtopcard\ThirdElectricProduct, 'company_type' => $company_type, 'use_type' => $use_type, 'province' => $province, 'city' => $city, 'card_id' => $card_id, 'order_time' => $order_time ]; refill\util::push_queue_order($this->mchid(), $mch_order, ORDER_STATE_QUEUE); Model('refill_order')->add_detail($this->mchid(), $mch_order, $order_time, $params, ORDER_STATE_QUEUE); $state = refill\util::push_addthird($params); if ($state === true) { return self::outsuccess(['state' => true]); } else { refill\util::del_queue_order($this->mchid(),$mch_order); Model('refill_order')->del_detail($this->mchid(),$mch_order); return self::outerr(208, '提交失败'); } } private function check_sinopec_coupon($params) { if(empty($params['cardno'])) { return [false,'参数没有包含cardno']; } if(empty($params['notifyurl'])) { return [false,'参数没有包含notifyurl']; } if(empty($params['order_sn'])) { return [false,'参数没有包含贵方唯一订单号:order_sn']; } if(empty($params['amount']) || intval($params['amount']) <= 0) { return [false,'参数没有包含充值金额:amount']; } if(empty($params['province']) || !array_key_exists($params['province'],mtopcard\ProvinceList)) { return [false,"请传入省份"]; } return [true,""]; } public function add_sinopec_couponOp() { [$success,$error] = $this->check_sinopec_coupon($_GET); if($success === false) { return self::outerr(201,$error); } $notify_url = $_GET['notifyurl']; $mch_order = $_GET['order_sn']; //对方的order编号 $card_no = $_GET['cardno'] ?? ''; $quantity = 1; $third_card_type = mtopcard\ThirdCardPhone; $province = intval($_GET['province']); $amount = intval($_GET['amount']); $product_code = mtopcard\sino_coupon_product_code($amount); $amount = refill\util::ThirdRefillAmount; //三方充值没有质量 $quality = 1; $card_type = mtopcard\ThirdRefillCard; if(!$this->check_mchorder($this->mchid(),$mch_order)) { return self::outerr(205,"客户订单号重复或者为空."); } $order_time = time(); $params = ['mchid' => $this->mchid(), 'buyer_id' => $this->adminid(), 'amount' => $amount, 'mch_order' => $mch_order, 'notify_url' => $notify_url, 'org_quality' => $quality, 'card_type' => $card_type, 'card_no' => $card_no, 'product_code' => $product_code, 'quantity' => $quantity, 'third_card_type' => $third_card_type, 'third_product_type' => mtopcard\ThirdSinopecECouponPoroduct, 'province' => $province, 'order_time' => $order_time ]; refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE); Model('refill_order')->add_detail($this->mchid(), $mch_order, $order_time, $params, ORDER_STATE_QUEUE); $state = refill\util::push_addthird($params); if ($state === true) { return self::outsuccess(['state' => true]); } else { refill\util::del_queue_order($this->mchid(),$mch_order); Model('refill_order')->partition(refill\util::part_query($order_time))->del_detail($this->mchid(),$mch_order); return self::outerr(208, '提交失败'); } } private function can_refill($cardno) { $card_info = refill\util::read_card($cardno); if(empty($card_info)) return false; return intval($card_info['black_card']) === 0; } private function check_mchorder($mchid,$mch_order) { if (empty($mch_order)) { return false; } else { $refill_order = Model('refill_order'); $ret = $refill_order->exist($mchid, $mch_order, refill\util::part_query()); return ($ret == false); } } public function balanceOp() { $uid = $this->adminid(); $minfo = new member_info($uid); $available = $minfo->available_predeposit(); $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $this->mchid()]); $available = ncPriceFormat($available - $mchinfo['credit_bonus']); $credit = ncPriceFormat($mchinfo['credit_bonus']); return self::outsuccess(['balance' => $available,'credit' => $credit]); } public function query_tradeOp() { $mchid = $this->mchid(); $trade_no = $_GET['trade_no']; if(empty($trade_no)) { return self::outerr(201,"交易号trade_no错误"); } $mod_refill = Model('refill_order'); $refill_info = $mod_refill->getOrderInfo(['order_sn' => $trade_no,'mchid' => $mchid]); $vr_order = Model('vr_order'); $order_info = $vr_order->getOrderInfo(['order_sn' => $trade_no]); if(empty($refill_info) || empty($order_info)) { return self::outerr(201,"无此交易号"); } $result = $this->format($order_info,$refill_info); return self::outsuccess($result); } private function format($order_info,$refill_info) { $result = []; $result['mchid'] = $refill_info['mchid']; $result['trade_no'] = $refill_info['order_sn']; $result['order_sn'] = $refill_info['mch_order']; $result['card_no'] = $refill_info['card_no']; $result['card_type'] = $refill_info['card_type']; $result['refill_amount'] = $refill_info['refill_amount']; $result['order_amount'] = $refill_info['mch_amount']; $result['order_time'] = $refill_info['order_time']; $result['success_time'] = $refill_info['notify_time']; $result['official_sn'] = $refill_info['official_sn'] ?? ""; $result['order_state'] = $order_info['order_state']; return $result; } public function queryOp() { $mchid = $this->mchid(); $order_sn = $_GET['order_sn']; //用户方的订单号,对应数据库中的mch_order if(empty($order_sn)) { return self::outerr(201,"客户订单号order_sn错误"); } $order_state = refill\util::query_queue_order($mchid,$order_sn); if ($order_state == ORDER_STATE_QUEUE || $order_state == ORDER_STATE_SEND) { Log::record("query_state in queue mchid={$mchid} mch_order={$order_sn} order_state={$order_state}" ,Log::DEBUG); $result['mchid'] = $mchid; $result['order_sn'] = $order_sn; $result['order_state'] = $order_state; return self::outsuccess($result); } else { $mod_refill = Model('refill_order'); $refill_info = $mod_refill->partition(refill\util::part_query())->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid,'inner_status' => 0]); if(empty($refill_info)) { $mod_qerr = Model('refill_query_err'); $insert = ['mch_order' => $order_sn,'mchid' => $mchid]; $items = $mod_refill->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid]); if (empty($items)) { Log::record("query_state in db no order mchid={$mchid} mch_order={$order_sn}", Log::DEBUG); $insert['code'] = 202; $insert['msg'] = "检索充值中的单子,查不到任何订单信息."; $mod_qerr->add($insert); return self::outerr(202, "无此订单,请人工确认处理."); } else { Log::record("DEBUG_TAG: query_state in db mchid={$mchid} mch_order={$order_sn} order_state not completed.", Log::DEBUG); $insert['code'] = 200; $insert['msg'] = "检索充值中的单子能查到,但inner_status=0时查不到,回调充值中状态."; $mod_qerr->add($insert); $result['mchid'] = $mchid; $result['order_sn'] = $order_sn; $result['order_state'] = ORDER_STATE_SEND; return self::outsuccess($result); } } else { $vr_order = Model('vr_order'); $order_info = $vr_order->partition(refill\util::part_query())->getOrderInfo(['order_sn' => $refill_info['order_sn']]); Log::record("query_state in db mchid={$mchid} mch_order={$order_sn} order_state={$order_info['order_state']}" ,Log::DEBUG); if (empty($order_info)) { $mod_qerr = Model('refill_query_err'); $insert = ['mch_order' => $order_sn,'mchid' => $mchid]; $insert['code'] = 203; $insert['msg'] = "检索充值中的单子能查到,但inner_status=0时查不到vr_order中的订单."; $mod_qerr->add($insert); return self::outerr(203, "无此交易,请人工确认处理."); } else { $result = $this->format($order_info, $refill_info); return self::outsuccess($result); } } } } }