- //去充值的界面
- public function recharge(Request $request){
- $data['money']=$request->input('money');//钱数
- $data['order_id']=$request->input('order_id');//订单号
- $data['card_num']=$request->input('card_num');//元宝数量
- $data['username']=$request->session()->get("username");//用户昵称
- $url = 'http://epay.taidupay.com/api/pay/unifiedOrder';
- $key = '';//<span style="color:#33ff33;">平台申请的app_key</span>
- $datas = [
- 'appId' =>'',//<span style="background-color: rgb(255, 153, 0);">平台申请的app_id</span>
- 'timestamp' => (string)time(),
- 'nonce' => time().rand(),
- 'service' => 'alipay.native',
- 'orderNo' => $data['order_id'],
- 'totalAmount' =>1,
- 'clientIp' => '测试服务器Ip',
- 'attach' => 'ces',
- 'notifyUrl' => '测试服务器IP/city.php',//异步回调地址(必须是外网访问到的地址)
- 'body' => '元宝',
- ];
- ksort($datas);
- $signOStr = '';
- foreach ($datas as $k => $val) {
- if ($signOStr != '') {
- $signOStr .= '&';
- }
- $signOStr .= $k . '=' . $val;
- }
- $signOStr = $signOStr . '&key=' . $key;
- $datas['sign'] = strtoupper(MD5($signOStr));
- $data['rs'] = json_decode($this->json_post($url, json_encode($datas)), true);
- //print_r($data['rs']);die;
- if($data['rs']['success'] == true && $data['rs']['status'] == 0){
- echo json_decode($data['rs']['payInfo'], true);
- }else{
- echo "<script> alert('订单编号重复!');parent.location.href='测试服务器iP/userInfo'; </script>";
- }
- return view('recharge')->with('data', $data);
- }
- public function json_post($url, $data){
- $ch = curl_init();
- $header = [
- 'Accept-Charset: utf-8',
- 'Content-Type: application/json'
- ];
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $tmpInfo = curl_exec($ch);
- $errorno=curl_errno($ch);
- return $tmpInfo;
- }
city.php文件内容
- <?php
- header("Content-type:text/html;charset=utf-8");
- $get_data = file_get_contents('php://input', 'r');
- //file_put_contents('a.txt',$get_data);
- $get_data = json_decode($get_data, true);
- $key = '1bdc6c80b3cb8987e2720bd965748593f07bb3e9';
- $data = $get_data;
- unset($data['sign']);
- ksort($data);
- $signOStr = '';
- foreach ($data as $k => $val) {
- if ($signOStr != '') {
- $signOStr .= '&';
- }
- $signOStr .= $k . '=' . $val;
- }
- $signOStr = $signOStr . '&key=' . $key;
- $sign = strtoupper(MD5($signOStr));
- if($sign == $get_data['sign'] && $data['status'] == 3){
- $dbms='mysql';//数据库类型
- $dbName='nicai';//使用的数据库
- $users='root';//数据库连接用户名
- $pwd='root';//数据库连接密码
- $host='127.0.0.1';//数据库主机名
- $dsn="$dbms:host=$host;port=3306;dbname=$dbName";
- $pdo=new PDO($dsn,$users,$pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo
- //成功
- $out_trade_no=$data['orderNo'];//订单号
- $trade_no=$data['platformOrderNo'];
- $pay_time=$data['finishTime'];
- //通过订单号修改订单id
- $updatesql="UPDATE pay_order set trade_no='$trade_no',pay_time='$pay_time',status=1 WHERE order_id='$out_trade_no'";
- $res=$pdo->exec($updatesql);
- if($res==1){
- //修改完状态之后要给用户加上元宝
- //$order_user=$model_payorder->getorder($out_trade_no);
- //查询修改完订单的信息
- $query1="select * from `pay_order` where order_id='$out_trade_no'";//需要执行的sql语句
- $res1=$pdo->prepare($query1);//准备查询语句
- $res1->execute();//
- $arr=$res1->fetch(PDO::FETCH_ASSOC);
- $uid=$arr['uid'];//用户的id
- $querys="select * from `user` where id='$uid'";//需要执行的sql语句
- $res2=$pdo->prepare($querys);//准备查询语句
- $res2->execute();//
- $user=$res2->fetch(PDO::FETCH_ASSOC);
- $acer_money=$user['acer_money'];
- $card_num=$arr['card_num'];
- $updatesq="UPDATE `user` set acer_money='$acer_money'+'$card_num' WHERE id='$uid'";
- $result=$pdo->exec($updatesq);
- if($result==1){
- echo "success";
- }
- }else{
- echo "fail";
- }
- }else{
- echo "fail";
- }
注意放到充值的页面
- $(document).ready(function () {
- setInterval("ajaxstatus()", 17000);
- });
- function ajaxstatus() {
- var order_id=$("#out_trade_no").val();
- //alert(order_id);
- if ($("#out_trade_no").val() != 0) {
- $.ajax({
- url: "pay/order",
- type: "GET",
- dataType:'jsonp',
- async: false,
- data: {order_id:order_id},
- success: function(msg) {
- if(msg.status == 1) {
- //注册成功调到登录页面
- window.location.href = "/userInfo";
- } else {
- layer.msg('请求订单状态出错!', {icon: 7, time: 2000});
- return false;
- }
- }
- });
- }
- }
- ajax请求的地址pay/order控制器操作
- //跳转页面
- public function paysorder(Request $request){
- $order_id=$request->input('order_id','');
- $callback=$_GET['callback'];
- $model_payorder=new PayOrder();
- $info=$model_payorder->getorder($order_id);
- if($info){
- $response = array(
- 'state' => 200,
- 'message' => '成功',
- 'order_id' => $info['order_id'],
- 'status' => $info['status'],
- );
- exit( $callback.'('.json_encode($response).')');
- }else{
- $response = array(
- 'state' => 202,
- );
- exit( $callback.'('.json_encode($response).')');
- }
- }