微信扫码支付 回调 跳转 带前端 响应代码

微信扫码登录不同的就是 支付成功  只有异步没有,支付成功的跳转。因此我们在支付扫码的时候要一直检测是否支付成功。


微信扫码支付的代码 配置





这几个代码去官方下载就好 


微信配置账号在




别的不用动。下面我们开始请求


前端显示 这个样子的


点击支付 显示效果




前端代码 


<html>
<head>
   <meta charset="utf-8">
   <script type="text/javascript" src="jquery-1.11.3.min.js"></script>
   <title></title>
   <style type="text/css">
      .content{width:80%;margin:200px auto;}
      .hide_box{z-index:999;filter:alpha(opacity=50);background:#666;opacity: 0.5;-moz-opacity: 0.5;left:0;top:0;height:99%;width:100%;position:fixed;display:none;}
      .shang_box{width:340px;height:378px;padding:10px;background-color:#fff;border-radius:10px;position:fixed;z-index:1000;left:50%;top:50%;margin-left:-280px;margin-top:-280px;border:1px dotted #dedede;display:none;}
      .shang_box img{border:none;border-width:0;}
      .dashang{display:block;width:100px;margin:5px auto;height:25px;line-height:25px;padding:10px;background-color:#E74851;color:#fff;text-align:center;text-decoration:none;border-radius:10px;font-weight:bold;font-size:16px;transition: all 0.3s;}
      .dashang:hover{opacity:0.8;padding:15px;font-size:18px;}
      .shang_close{float:right;display:inline-block;}
      .shang_logo{display:block;text-align:center;margin:20px auto;}
      .shang_tit{width: 100%;height: 75px;text-align: center;line-height: 66px;color: #a3a3a3;font-size: 16px;background: url('img/cy-reward-title-bg.jpg');font-family: 'Microsoft YaHei';margin-top: 7px;margin-right:2px;}
      .shang_tit p{color:#a3a3a3;text-align:center;font-size:16px;}
      .shang_payimg{width:185px;padding:10px;border:6px solid #EA5F00;margin:0 auto;border-radius:3px;height:176px;}
      .shang_payimg img{display:block;text-align:center;width:140px;height:140px; }
      .pay_explain{text-align:center;margin:10px auto;font-size:12px;color:#545454;}
      .radiobox{width: 16px;height: 16px;background: url('img/radio2.jpg');display: block;float: left;margin-top: 5px;margin-right: 14px;}
      .checked .radiobox{background:url('img/radio1.jpg');}
      .shang_payselect{text-align:center;margin:0 auto;margin-top:40px;cursor:pointer;height:60px;width:280px;}
      .shang_payselect .pay_item{display:inline-block;margin-right:10px;float:left;}
      .shang_info{clear:both;}
      .shang_info p,.shang_info a{color:#C3C3C3;text-align:center;font-size:12px;text-decoration:none;line-height:2em;}

   </style>
</head>
<body>
<div class="content">
   <p><a href="javascript:void(0)" onClick="show_wx()" class="dashang" title="支付">微信支付</a></p>
   <div class="hide_box"></div>
   <div class="shang_box">
      <a class="shang_close" href="javascript:void(0)" onClick="hide_wx()" title="关闭"><img src="source/plugin/xigua_re/static/close.jpg" alt="取消" /></a>
      <div class="shang_tit">
         <p>微信充值扫码</p>
      </div>
      <div class="shang_payimg">

      </div>
      <div class="shang_payselect">

         <div class="pay_item" id="weipay" style="display: none">
            <span class="radiobox"></span>
            <span class="pay_logo" ><img src="static/wechat.jpg" alt="微信" /><span style="position: absolute;bottom:24px;font-size: 19px; font-weight: 600;">成功</span></span>
         </div>
      </div>

   </div>
</div>
<script type="text/javascript">

   if(typeof jq == 'undefined'){
      var jq = jQuery.noConflict();
   }

   var wechat_checkST1 = null, wechat_checkCount1 = 0;

   function  show_wx(){
      jq.get('act=weixin', function(res) {
//       console.log(res.order_id,res.img);

         var order_id=res.order_id;
//       console.log(order_id);
         if(res.img){
//          console.log(order_id);
            wechat_checkST1 = setInterval(function(){

               check_order(order_id);

            }, 2000);
            jq('.shang_payimg').html(res.img);
         }
      },'json');

      jq(".shang_box").show();
   }

   function hide_wx(){

      clearTimeout(wechat_checkST1);
      jq(".shang_box").hide();
   }

   function check_order(order_id){

      jq.get('check&order_id='+order_id, function(s) {

         if(s=='1'){

            jq("#weipay").show();

            setTimeout(function(){
               hide_wx();
               console.log('支付成功')

            },1200);



         }
      });

   }

</script>
</body>

</html>

act  请求地址是 返回微信的 二维码   就是生成二维码 
后台 act 代码


if($_REQUEST['act']=='weixin'){
    $order_id = C::t('')->get_order_id(
        $fromopenid, $_G['uid'], $touid, $toopenid,
        $price*100,
        $_GET['subject'], $_GET['url'],$_GET['postid'], $_G['cookie']['rewxnickname'], $_G['cookie']['rewxheadimgurl'], 1,1,1);
    if(!$order_id){
        exit('order_id empty!');
    }
    $body = diconv($navtitle, CHARSET, 'UTF-8');
    $body='账号充值';
    $notify = new NativePay();
    $input = new WxPayUnifiedOrder();
    $input->SetBody($body);
    $input->SetAttach($body);
    $input->SetOut_trade_no($order_id);
    $input->SetTotal_fee($price*100);
    $input->SetTime_start(date("YmdHis"));
//    $input->SetTime_expire(date("YmdHis", time() + 3600));
    $input->SetGoods_tag($body);
    $input->SetNotify_url($_G['siteurl']. 'addmoney_notify_wx.php'); //这个是回调很重要
    $input->SetTrade_type("NATIVE");
    $input->SetProduct_id($order_id);
    $result = $notify->GetPayUrl($input);
    $url2 = $result["code_url"];

    if(!$url2){
        $img = diconv($result['return_msg'],'UTF-8',CHARSET );
    }else{
        if(class_exists('QRcode')){
            $qrfile = '/cache/o'.$order_id.'.png';
            QRcode::png($url2, DISCUZ_ROOT.$qrfile, QR_ECLEVEL_L, 3);
            $src = $_G['siteurl'].$qrfile;
        }else{
            $src= 'http://qr.liantu.com/api.php?&w=240&bg=ffffff&fg=333333&text='.urlencode($url2);
        }
        $errorsrc= 'http://qr.liantu.com/api.php?&w=240&bg=ffffff&fg=333333&text='.urlencode($url2);
        $img =  "<img src='$src' οnerrοr=\"this.error=null;this.src='$errorsrc'\" class=\"payqrcode\" /><p class=\"xg1\" style='margin:10px auto!important;'>".lang('plugin/xigua_re', 'sao')."</p>";
    }

   echo json_encode(array('order_id'=>$order_id,'img'=>$img));

    die;

异步回调验证 


addmoney_notify_wx.php


<?php


define('IN_API', true);
define('CURSCRIPT', 'api');
define('DISABLEXSSCHECK', true);

require_once 'class/class_core.php';
$discuz = C::app();
$discuz->init();

$_G['siteurl'] = str_replace('', '',$_G['siteurl'] );
require_once 'common.php';

//ini_set('display_errors', 1);
//error_reporting(E_ALL ^ E_NOTICE);
//$re = WxPayApi::promotion('201602022028434331035', 'ohvWAwxEy3M9CvZB5J4HFDuOk8Nc', 100, 'test');
//$re['return_msg'] = diconv($re['return_msg'],'UTF-8', CHARSET);
//$re['err_code_des'] = diconv($re['err_code_des'],'UTF-8', CHARSET);
//var_dump($re);
//if($re['return_code'] == 'SUCCESS'){

//}else{
//
//}


$notifydata = re_notifycheck();

@file_put_contents('./aaa.text',print_r($_REQUEST,true));

if($notifydata['validator']) {
    
    //这里面 开始写成功之后的业务逻辑 比如 改变订单状态
    $order_id = $notifydata['order_no'];

}
function re_notifycheck() {
    global $_G;

    $msg = '';

    $notify = WxPayApi::notify($msg);

    if(empty($notify)){
        $return = array(
            'return_code'=>'FAIL',
            'return_msg'=>$msg,
        );
        WxPayApi::replyNotify(arr2xml($return));
        exit;
    }

    //checksign
    $sign = $notify['sign'];
    unset($notify['sign']);

    ksort($notify);
    $string = reToUrlParams($notify);
    $string = $string . "&key=".WxPayConfig::KEY;
    $string = md5($string);
    $result = strtoupper($string);
    if($result != $sign){
        $return = array(
            'return_code'=>'FAIL',
            'return_msg'=> 'sign error!',
        );
        WxPayApi::replyNotify(arr2xml($return));
        exit;
    }
    if($notify['result_code'] == 'SUCCESS') {
        return array(
            'validator'    => isset($notify['result_code']) && $notify['result_code'] == 'SUCCESS' ? 1 : 0,
            'order_no'     => $notify['out_trade_no'],
            'trade_no' => isset($notify['transaction_id']) ? $notify['transaction_id'] : '',
            'price'    => $notify['total_fee'],
            'appid' => $notify['appid'],
            'notify'   => arr2xml(array('return_code'=>'SUCCESS')),
            'location' => false,
            'fromopenid' => $notify['openid'],
        );
    }
}


function arr2xml($data){
    $xml = "<xml>";
    foreach ($data as $key=>$val)
    {
        if (is_numeric($val)){
            $xml.="<".$key.">".$val."</".$key.">";
        }else{
            $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
        }
    }
    $xml.="</xml>";
    return $xml;
}

check.php 

检测是否支付成功!

传到前面的是订单号。

拿订单号去 后台验证

<?php

$order_id = $_GET['order_id'];
$order = C::t('a')->fetch_by_order_id($order_id);

$succeed = 0;
if($order && $order['paystatus'] == 1){


   $succeed = 1;
}

echo $succeed;

die;
响应那个一直请求 当验证通过时候 就 跳转 停止请求 支付完成。






©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页