ThinkPHP框架整合微信支付之Native 扫码支付模式一(2)

以上代码对应native_pay.html页面

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>微信安全支付</title>
</head>
<body>
    <div align="center" id="qrcode">
        <p >扫我,扫我</p>
    </div>
    <div align="center">
        <a href="#" rel="external nofollow" >返回首页</a>
    </div>
</body>
    <script src="__PUBLIC__/js/qrcode.js"></script>
    <script>
        var url = "<?php echo $product_url;?>";
        //参数1表示图像大小,取值范围1-10;参数2表示质量,取值范围'L','M','Q','H'
        var qr = qrcode(10, 'M');
        qr.addData(url);
        qr.make();
        var dom=document.createElement('DIV');
        dom.innerHTML = qr.createImgTag();
        var element=document.getElementById("qrcode");
        element.appendChild(dom);
    </script>
</html>

这里注意生存二维码的js地址,我放在了Public下的JS目录下

step3:扫码之后,就会提交给我们公众平台native配置的地址对应的去方法处理公众平台navtive配置:配置地址 http://您的域名/Pay/index.php/Home/WxNative/todoPost

 

对应的todoPost方法在控制器中:

public function todoPost()
    {
        //以log文件形式记录回调信息,用于调试
        $log_name = __ROOT__."/Public/native_call.log";
        //使用native通知接口
        $nativeCall = new \NativeCall_pub();
        
        //接收微信请求
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        log_result($log_name,"【接收到的native通知】:\n".$xml."\n");
        $nativeCall->saveData($xml);
        
        if($nativeCall->checkSign() == FALSE){
            $nativeCall->setReturnParameter("return_code","FAIL");//返回状态码
            $nativeCall->setReturnParameter("return_msg","签名失败");//返回信息
        }
        else
        {
            //提取product_id
            $product_id = $nativeCall->getProductId();
        
            //使用统一支付接口
            $unifiedOrder = new \UnifiedOrder_pub();
        
            //根据不同的$product_id设定对应的下单参数,此处只举例一种
            switch ($product_id)
            {
                
                case C('WxPayConf_pub.APPID')."static"://与native_call_qrcode.php中的静态链接二维码对应
                    //设置统一支付接口参数
                    //设置必填参数
                    //appid已填,商户无需重复填写
                    //mch_id已填,商户无需重复填写
                    //noncestr已填,商户无需重复填写
                    //spbill_create_ip已填,商户无需重复填写
                    //sign已填,商户无需重复填写
                    $unifiedOrder->setParameter("body","贡献一分钱");//商品描述
                    //自定义订单号,此处仅作举例
                    $timeStamp = time();
                    $out_trade_no = C('WxPayConf_pub.APPID').$timeStamp;
                    $unifiedOrder->setParameter("out_trade_no",$out_trade_no);//商户订单号             $unifiedOrder->setParameter("product_id","$product_id");//商品ID
                    $unifiedOrder->setParameter("total_fee","1");//总金额
                    $unifiedOrder->setParameter("notify_url",C('WxPayConf_pub.NOTIFY_URL'));//通知地址
                    $unifiedOrder->setParameter("trade_type","NATIVE");//交易类型
                    $unifiedOrder->setParameter("product_id",$product_id);//用户标识
                    //非必填参数,商户可根据实际情况选填
                    //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
                    //$unifiedOrder->setParameter("device_info","XXXX");//设备号
                    //$unifiedOrder->setParameter("attach","XXXX");//附加数据
                    //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
                    //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
                    //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
                    //$unifiedOrder->setParameter("openid","XXXX");//用户标识
        
                    //获取prepay_id
                    $prepay_id = $unifiedOrder->getPrepayId();
                    //设置返回码
                    //设置必填参数
                    //appid已填,商户无需重复填写
                    //mch_id已填,商户无需重复填写
                    //noncestr已填,商户无需重复填写
                    //sign已填,商户无需重复填写
                    $nativeCall->setReturnParameter("return_code","SUCCESS");//返回状态码
                    $nativeCall->setReturnParameter("result_code","SUCCESS");//业务结果
                    $nativeCall->setReturnParameter("prepay_id",$prepay_id);//预支付ID
        
                    break;
                default:
                    //设置返回码
                    //设置必填参数
                    //appid已填,商户无需重复填写
                    //mch_id已填,商户无需重复填写
                    //noncestr已填,商户无需重复填写
                    //sign已填,商户无需重复填写
                    $nativeCall->setReturnParameter("return_code","SUCCESS");//返回状态码
                    $nativeCall->setReturnParameter("result_code","FAIL");//业务结果
                    $nativeCall->setReturnParameter("err_code_des","此商品无效");//业务结果
                    break;
            }
        
        }
        
        //将结果返回微信
        $returnXml = $nativeCall->returnXml();
        log_result($log_name,"【返回微信的native响应】:\n".$returnXml."\n");

        echo $returnXml;
    }
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5956.html