tp3.2中微信支付中将调用支付处简单封装

前提:微信公众平台 微信支付商户平台要配置好.

第一步:将目录WxpayAPI放入项目Vendor目录下,里面有这些文件如下图:

tp3.2中微信支付中将调用支付处简单封装

第二步:将目录Weixin放入项目如下图Common/Libs所示目录:

tp3.2中微信支付中将调用支付处简单封装

第三步:在Util目录里新建Wxpay.class.php文件:

tp3.2中微信支付中将调用支付处简单封装

第四步:Wxpay.class.php里面内容为:

<?php

namespace Org\Util;
import('Common.Libs.Weixin.WechatAuth');//JSSDK 需要用到accessToken
import('Common.Libs.Weixin.JSSDK');//JSSDK
import('Common.Libs.Weixin.ComPay');//红包与企业支付
class Wxpay
    { 

 public function pay($ocode,$money,$openid,$notify){
                $http=$_SERVER['HTTP_HOST'];
                import('Common.Libs.Weixin.JSAPI');
                $tools = new \JsApiPay();
                $Out_trade_no   = $ocode;
                $Body           = '订单号:'.$Out_trade_no;
                $Total_fee      = $money* 100;
                //设置支付
                $input = new \WxPayUnifiedOrder();
                $input->SetBody($Body);
                $input->SetOut_trade_no($Out_trade_no);
                $input->SetTotal_fee($Total_fee);
                $input->SetNotify_url($http.$notify);
                $input->SetTrade_type("JSAPI");
                $input->SetOpenid($openid);
                $order = \WxPayApi::unifiedOrder($input);
                $this->jsApiParameters = $tools->GetJsApiParameters($order);
       }

    }



?>

第五步:控制器里:

上面引入:use Org\Util\Wxpay;

方法recharge里面为:

 public function recharge()
        {  
            $http=$_SERVER['HTTP_HOST'];
            $notify='/Weixin/Notify/index';
            $ocode=$_POST['ordercode'];//商户订单号
            $money=$_POST['money'];//总金额
            $pmethod=$_POST['pay-method'];
            if($pmethod==1)
            {   
                $usermobile=$_SESSION['usermobile'];//当前登录者用户手机号
                $user=M('user');
                $userdata=$user->where(array('tellphone'=>$usermobile))->find();
                $user_id=$userdata['id'];
                $yue_before=$userdata['zbqianbao'];
                $yue_after=$yue_before+$money;
                //支付前生成预订单
                $data=[
                    "user_id"=>$user_id,
                    "change"=>$money,
                    "ctype"=>2,
                    "cmethod"=>3,
                    "yue_before"=>$yue_before,
                    "yue_after"=>$yue_after,
                    "changetime"=>time(),
                    "ocode"=>$ocode,
                    "status"=>0,
                ];
                $addlog=M('log')->add($data);
                $fans=M('fans');
                $fansdata=$fans->where(array('uid'=>$user_id))->find();
                $openid=$fansdata['openid'];

               Wxpay::pay($ocode,$money,$openid,$notify);
                $this->display('Person/anzhuo');
               
             }
            
        }

第六步:在视图目录Person里新建anzhuo.html文件,里面内容为:


<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>微信支付</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="format-detection" content="telephone=no"/>
<script src="__ROOT__/Application/Weixin/Public/bootstrap/js/jquery.min.js"></script>

<style type="text/css">
    body{
        margin: 0;
        padding: 0;
    }
    .box{
        max-width: 640px;
        padding: 10px;
    }
    .btn{
        background: #0072E3;
        line-height: 3rem;
        font-size: 1rem;
        color: #fff;
        text-align: center;
    }
</style>

<script type="text/javascript">
    //JS api 支付
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            {$jsApiParameters},
            function(res){
                WeixinJSBridge.log(res.err_msg);
                if(res.err_msg == 'get_brand_wcpay_request:cancel') {
                    alert("您已取消了此次支付");
                    return;
                } else if(res.err_msg == 'get_brand_wcpay_request:fail') {

alert(res.err_code);  //如果此处弹出错误码3,则微信支付授权目录路径写的不对(极可能是最后少一个参数)
                    alert("支付失败");
                    return;
                } else if(res.err_msg == 'get_brand_wcpay_request:ok') {
                    //alert("支付成功!");
                    location.href="{:U('index/index')}";

                } else {
                    alert("未知错误"+res.error_msg);
                    return;
                }
            }
            );
    }
    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall();
        }
    }
    setTimeout(function(){callpay()},1000);
</script>
</head>
<body>
</body>
</html>