request->param(); Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]); $order_no = $param['order_no']; $order_number = $param['order_number']; $updateData = [ 'amount_price' => $param['amount'], 'fanli_price' => $param['fanli'], 'cost_price' => $param['cost_price'], 'arrived_amount'=> $param['arrived_amount'], 'status' => $param['status'], 'modify_time' => time(), ]; $ret = Db::name("order_huafei")->where("order_number", $order_number)->where("order_sn", $order_no)->update($updateData); if($ret){ echo 'success';exit; }else{ Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']); echo 'error';exit; } } //充值话费面额 public function denomination(){ $data = [ ['money'=>50, 'name'=>'50元'], ['money'=>100, 'name'=>'100元'], ['money'=>200, 'name'=>'200元'], ]; return app('json')->success('获取话费面额成功', $data); } //充值话费推送 public function recharge(){ $uid = $this->request->uid(); $params = $this->request->params(['phone', 'money']); if(!$params['phone']) return app('json')->fail('请提交电话号码'); if(!$params['money']) return app('json')->fail('请提交充值金额'); //验证 $verify = $this->verify($params['phone'], $params['money']); if($verify['code'] == '0000'){ //推送 $order_sn = time().$uid; $push = $this->pushOrder($order_sn, $params['phone'], $params['money']); if($push['code'] == '0000'){ //入库记录 $insertdata = [ 'uid'=> $uid, 'mobile' => $params['phone'], 'commission'=> 0, 'commission_share'=> 0, 'order_number' => $push['data']['order_number'], 'order_sn' => $order_sn, 'amount_price' => $push['data']['amount'], 'fanli_price' => $push['data']['fanli'], 'cost_price' => $push['data']['cost_price'], 'pay_money' => $params['money'], 'status' => 0, 'create_time' => time(), 'finish_time' => time(), 'modify_time' => time(), ]; Db::name('order_huafei')->insert($insertdata); return app('json')->success('话费推送成功', $push); }else{ return app('json')->fail($push['msg']); } }else{ return app('json')->fail($verify['msg']); } } //话费充值记录 public function record(){ $uid = $this->request->uid(); [$page, $limit] = $this->getPage(); $where = []; $where[] = ['uid', '=', $uid]; $query = Db::name("order_huafei"); $count = $query->where($where)->count(); $list = $query->page($page, $limit)->where($where)->select()->each(function ($item){ $item['status']=(int)$item['status']; return $item; }); return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count, 'limit'=>$limit, 'page'=>$page]); } //话费充值验证 public function verify($order_number, $money){ $request['mobile'] = $order_number; $request['amount'] = $money; $request['recharge_type'] = 1; return $this->execute('/rest/Recharge/verify', $request); } //话费订单充值推送 public function pushOrder($order_sn, $phone, $money){ $request['store_id'] = '9189'; $request['order_no'] = $order_sn; $request['mobile'] = $phone; $request['money'] = $money; $request['recharge_type'] = 1; $request['notify_url'] = 'https://api.gdjbp.com//api/huafei/notify'; return $this->execute('/rest/Recharge/PushOrder', $request); } //话费订单查询 public function query($order_number){ $request['store_id'] = '9189'; $request['order_number'] = $order_number; return $this->execute('/rest/Recharge/query', $request); } //话费订单取消 public function cancel($order_number){ $request['order_number'] = $order_number; return $this->execute('/rest/Recharge/cancel', $request); } protected function generateSign($params){ ksort($params); $stringToBeSigned = $this->secretKey; foreach ($params as $k => $v) { if (is_array($v)) { $stringToBeSigned .= $k; }else{ if ("@" != substr($v, 0, 1)) { $stringToBeSigned .= "$k$v"; } } } unset($k, $v); $stringToBeSigned .= $this->secretKey; return strtoupper(md5($stringToBeSigned)); } public function curl($url, $postFields = null,$timeOut = 20){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_REFERER, 'https://api.gdjbp.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT,$timeOut); //https 请求 if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (is_array($postFields) && 0 < count($postFields)){ $postBodyString = ""; $postMultipart = false; foreach ($postFields as $k => $v){ if("@" != substr($v, 0, 1))//判断是不是文件上传 { $postBodyString .= "$k=" . urlencode($v) . "&"; } else//文件上传用multipart/form-data,否则用www-form-urlencoded { $postMultipart = true; } $postMultipart = true; } unset($k, $v); curl_setopt($ch, CURLOPT_POST, true); if ($postMultipart){ curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); }else{ curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); } } $reponse = curl_exec($ch); curl_close($ch); return json_decode($reponse, true); } /** * request api接口后缀,如 /recharge/pushOrder * apiParams 业务参数 数组 */ public function execute($request, $apiParams ,$timeOut =20){ //组装系统参数 $sysParams["app_key"] = $this->appkey; $sysParams["v"] = $this->apiVersion; $sysParams["format"] = $this->format; $sysParams["timestamp"] = time(); $sysParams["client"] = ''; //签名 $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams)); //系统参数放入GET请求串 //$requestUrl = $this->gatewayUrl.$sysParams['method']. "?"; $requestUrl = $this->gatewayUrl.$request. "?"; foreach ($sysParams as $sysParamKey => $sysParamValue){ $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; } $requestUrl = substr($requestUrl, 0, -1); return $this->curl($requestUrl, $apiParams ,$timeOut); } }