request->param(); Db::name('log')->insert(['data'=>'话费充值回调数据'.json_encode($param)]); $order_no = $param['order_no']; $order_number = $param['order_number']; $huafeiInfo = Db::name('order_huafei')->where("order_sn", $order_no)->find(); if(!$huafeiInfo){ return 'error'; } $updateData = [ 'amount_price' => $param['amount'], 'fanli_price' => $param['fanli'], 'cost_price' => $param['cost_price'], 'arrived_amount'=> $param['arrived_amount'], 'status' => $param['status'], 'modify_time' => date('Y-m-d H:i:s'), ]; $ret = Db::name("order_huafei")->where("order_sn", $order_no)->update($updateData); if($ret){ if($param['status'] == 2) { // //判断数据库有无此订单 // $type_shop = 9; // $orderThird = app()->make(UserThirdRepository::class); // //预估养老金 // $orderThird->yuguOrder( // $huafeiInfo['order_huafei_id'], // $huafeiInfo['commission'], // $huafeiInfo['uid'], // $huafeiInfo['order_sn'], // $type_shop, // 'order_huafei'); // //预估积分 // $orderThird->yuguJifen( // $huafeiInfo['commission'] , // $huafeiInfo['uid'], // $huafeiInfo['order_sn'], // $type_shop // ); // //复购金 // $orderThird->yuguFugou( // $huafeiInfo['commission'] , // $huafeiInfo['uid'], // $huafeiInfo['order_sn'], // $type_shop // ); } if($param['status'] == 3) { $order_data = Db::name('order_huafei_hf')->where('order_no', $order_no)->find(); $pay_wx = systemConfig('pay_wx'); //推送失败 给新支付的这一笔退款 Db::name('log')->insert(['data' => '给新话费充值支付的这一笔退款']); if ($pay_wx == 3){ $key='api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871'; }else{ $key='api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f'; } $refund_traits = [ 'payment_id' => $order_data['payment_id'], 'app_id' => $order_data['app_id'], 'order_no' => app()->make(StoreRefundOrderRepository::class)->getNewOrderId(), 'reverse_amt' => $order_data['reverse_amt'], 'notify_url' => env('APP_URL') . "/api/hf_notify/reverse_order", ]; $hf_refund_res = app()->make(StoreRefundOrderRepository::class)->payment_Reverse($refund_traits, $order_data['pay_type'] == '1' ? 2 : 1, $pay_wx, $key); Db::name('log')->insert(['data'=>'话费充值汇付支付撤销返回结果:'.json_encode($hf_refund_res)]); //把赠送出去的养老金 积分 贡献值 复购金 改为已失效 Db::name('user_bill')->where('order_sn', $huafeiInfo['order_no'])->update(['status'=> -1]); Db::name('user_sign_score')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]); Db::name('user_sign_gongxian')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]); Db::name('user_sign_fugou')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]); Db::name('user_sign_hongbao')->where('order_id', $huafeiInfo['order_no'])->update(['status'=> 0]); } echo 'success'; return; }else{ Db::name('log')->insert(['data'=>'话费充值回调数据处理失败']); echo 'error'; return; } }catch (\Exception $e) { Db::name('log')->insert(['data'=>'充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']); return app('json')->fail('充值话费回调错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】'); } } //充值话费面额 public function denomination(){ $data = [ ['money'=>50, 'name'=>'50元'], ['money'=>100, 'name'=>'100元'], ['money'=>200, 'name'=>'200元'], ]; foreach($data as $k=>$v){ $commissionMoney = $v['money']*0.02; $systemvalueObj = $goods_obj = Db::name('system_config_value')->where('config_key','sanfangfenpei')->find(); $sanfangfenpei=json_decode($systemvalueObj['value'],true); $pv=$commissionMoney*$sanfangfenpei['pv']; //PV $yanglaojin=$commissionMoney*$sanfangfenpei['yanglaojin']; //新的养老金 $gongxian=$pv; //新的养老金 $jifen=$pv; //新的养老金 $data[$k]['pv']=round($pv,2); $data[$k]['yanglaojin']=round($yanglaojin,2); $data[$k]['gongxian']=round($gongxian,2); $data[$k]['jifen']=round($jifen,2); } return app('json')->success('获取话费面额成功', $data); } //充值话费推送补发 public function bufa(){ try{ $order_no = $this->request->param('order_no'); $order_data = Db::name('order_huafei_hf') -> where('order_no', $order_no)->find(); $RES = $this->pushHf($order_data['uid'], $order_data['order_no'], $order_data['mobile'], $order_data['pay_price'], $order_data['order_id']); return app('json')->success('充值话费推送补发成功', $RES); }catch (\Exception $e) { Db::name('log')->insert(['data'=>'充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】']); return app('json')->fail('充值话费推送补发错误:'.$e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】'); } } //充值话费推送 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.mt_rand(1000,9999); //创建预支付订单 $insertdata = [ 'uid'=> $uid, 'mobile' => $params['phone'], 'order_no' => $order_sn, 'pay_price' => $params['money'], 'status' => 0, 'create_time'=> date('Y-m-d H:i:s'), ]; $oid = Db::name('order_huafei_hf')->insertGetId($insertdata); if($oid){ return app('json')->success('订单创建成功', ['oid'=>$oid, 'order_sn'=>$order_sn]); }else{ return app('json')->fail('创建话费充值预支付订单失败'); } }else{ return app('json')->fail($verify['msg'].'MSG'); } } //话费充值推送 public function pushHf($uid, $order_sn, $phone, $money, $order_id = 0){ $commissionMoney = $money*0.02; //判断数据库有无此订单 $type_shop = 9; $orderThird = app()->make(UserThirdRepository::class); //预估养老金 $orderThird->yuguOrder( $order_id, $commissionMoney, $uid, $order_sn, $type_shop, 'order_huafei'); //预估积分&贡献值 $orderThird->yuguJifen( $commissionMoney, $uid, $order_sn, $type_shop ); // //复购金 // $orderThird->yuguFugou( // $commissionMoney, // $uid, // $order_sn, // $type_shop // ); //推送订单 $push = $this->pushOrder($order_sn, $phone, $money); Db::name('log')->insert(['data'=>'话费充值推送订单返回数据'.json_encode($push)]); if($push['code'] == '0000'){ $commissionMoney = $money*0.02; //成功入库记录 $insertdata = [ 'uid'=> $uid, 'mobile' => $phone, 'commission'=> $commissionMoney, 'commission_share'=> 0.02, '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' => $money, 'status' => 1, 'create_time' => date('Y-m-d H:i:s'), 'finish_time' => date('Y-m-d H:i:s'), 'modify_time' => date('Y-m-d H:i:s'), ]; Db::name('order_huafei')->insert($insertdata); return $push; }else{ $commissionMoney = $money*0.02; //失败入库记录 $insertdata = [ 'uid'=> $uid, 'mobile' => $phone, 'commission'=> $commissionMoney, 'commission_share'=> 0.02, 'order_number' => '', 'order_sn' => $order_sn, 'amount_price' => $money, 'fanli_price' => 0, 'cost_price' => 0, 'pay_money' => $money, 'status' => 3, 'create_time' => date('Y-m-d H:i:s'), 'finish_time' => date('Y-m-d H:i:s'), 'modify_time' => date('Y-m-d H:i:s'), ]; Db::name('order_huafei')->insert($insertdata); return $push; } } //话费充值记录 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){ return $item; }); return app('json')->success('获取话费充值记录成功', ['list'=>$list, 'count'=>$count, 'uid'=>$uid]); } //话费充值验证 public function verify($order_number, $money){ $request['mobile'] = $order_number; $request['amount'] = $money; $request['recharge_type'] = 0; 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'] = 0; $request['notify_url'] = 'https://' . request()->host() . '/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.hxxfb.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); } }