美文网首页
微信支付

微信支付

作者: 涳_de26 | 来源:发表于2019-05-23 17:08 被阅读0次

    下载微信支付SDK包,快速搭建支付 下载地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

    1.微信扫码支付

        /**
         * 生成支付代码
         * @param   array   $order      订单信息
         * @param   array   $config    支付方式信息
         */
        function get_code($order, $config)
        {
            $notify_url = SITE_URL . '/index.php/app/notify_url'; // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input = new WxPayUnifiedOrder();
            $input->SetBody($config['body']); // 商品描述
            $input->SetAttach("weixin"); // 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
            $input->SetOut_trade_no($order['order_sn'] . time()); // 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
            $input->SetTotal_fee($order['order_amount'] * 100); // 订单总金额,单位为分,详见支付金额
            $input->SetNotify_url($notify_url); // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input->SetTrade_type("NATIVE"); // 交易类型   取值如下:JSAPI,NATIVE,APP,详细说明见参数规定    NATIVE--原生扫码支付
            $input->SetProduct_id("123456789"); // 商品ID trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
            $notify = new NativePay();
            $result = $notify->GetPayUrl($input); // 获取生成二维码的地址
            $url2 = $result["code_url"];
            if (empty($url2))
                return '没有获取到支付地址, 请检查支付配置' . print_r($result, true);
            return '<img alt="模式二扫码支付" src="/index.php?m=Home&c=Index&a=qr_code&data='.urlencode($url2).'" style="width:110px;height:110px;"/>';
        }
    

    2微信公众号支付

    function getJSAPI($order)
        {
            if(stripos($order['order_sn'],'recharge') !== false){
                $go_url = U('Mobile/User/points',array('type'=>'recharge'));
                $back_url = U('Mobile/User/recharge',array('order_id'=>$order['order_id']));
            }else{
                $go_url = U('Mobile/Order/order_detail',array('id'=>$order['order_id']));
                $back_url = U('Mobile/Cart/cart4',array('order_id'=>$order['order_id']));
            }
            //①、获取用户openid
            $tools = new JsApiPay();
            //$openId = $tools->GetOpenid();
            $openId = $_SESSION['openid'];
            //②、统一下单
            $input = new WxPayUnifiedOrder();
            $input->SetBody("支付订单:".$order['order_sn']);
            $input->SetAttach("weixin");
            $input->SetOut_trade_no($order['order_sn'].time());
            $input->SetTotal_fee($order['order_amount']*100);
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetGoods_tag("tp_wx_pay");
            $input->SetNotify_url(SITE_URL.'/index.php/Home/Payment/notifyUrl/pay_code/weixin');
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($openId);
            $order2 = WxPayApi::unifiedOrder($input);
            //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
            //printf_info($order);exit;  
            $jsApiParameters = $tools->GetJsApiParameters($order2);
            $html = <<<EOF
        <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:ok") {
                        location.href='$go_url';
                     }else{
                        alert(res.err_code+res.err_desc+res.err_msg);
                        location.href='$back_url';
                     }
                }
            );
        }
    
        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();
            }
        }
        callpay();
        </script>
    EOF;
            
        return $html;
    
        }
    

    2微信小程序支付

    /**
         * 生成支付代码
         * @param   array   $order      订单信息
         * @param   array   $config    支付方式信息
         */
        function get_code($order, $config)
        {
            $user = session('user');
            $openid = $user['openid'];
            $notify_url = SITE_URL . '/api/mini/notify_url'; // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input = new WxPayUnifiedOrder();
            if (empty($config['body'])){
                $config['body'] = '买东西了!';
            }
            $input->SetBody($config['body']);    // 商品描述
            $input->SetOut_trade_no($order['order_sn']);    // 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
            $input->SetTotal_fee($order['order_amount'] * 100);    // 订单总金额,单位为分,详见支付金额
            $input->SetNotify_url($notify_url);     // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input->SetOpenid($openid);    //付款人的openid
            $input->SetTrade_type('JSAPI');
    //        echo $notify_url;exit;
            try {
                $return = WxPayApi::unifiedOrder($input);
            } catch (\Exception $e) {
                throw new JsonErrorException($e->getMessage());
            }
            $time = time();
            $tmp=[];
            if ($return['return_code'] == 'FAIL'  ||  $return['result_code'] == 'FAIL'){
                $msg = $return['return_msg'];
                if ($msg == 'OK'){
                    $msg = $return['err_code_des'];
                }
                throw new JsonErrorException($msg);
            }
            //组装数据给小程序吊起支付
            $tmp['appId'] = WxPayConfig::$appid;
            $tmp['nonceStr'] = $this->getNonceStr();
            $tmp['package'] = 'prepay_id='.$return['prepay_id'];
            $tmp['signType'] = 'MD5';
            $tmp['timeStamp'] = "$time";
            $tmp['paySign'] = $this->MakeSign($tmp,WxPayConfig::$key);
            return $tmp;
        }
    

    3.微信app支付

    /**
         * 生成支付代码
         * @param   array   $order      订单信息
         * @param   array   $config    支付方式信息
         */
        function get_code($order, $config = '')
        {
            $notify_url = SITE_URL . '/api/app/notify_url'; // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input = new WxPayUnifiedOrder();
            if (empty($config['body'])){
                $config['body'] = '支付描述!';
            }
            $input->SetBody($config['body']);    // 商品描述
            $input->SetOut_trade_no($order['order_sn']);    // 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
            $input->SetTotal_fee($order['order_amount'] * 100);     // 订单总金额,单位为分,详见支付金额
            $input->SetNotify_url($notify_url);    // 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
            $input->SetOpenid($openid);    //付款人的openid,开放平台的openid
            $input->SetTrade_type('APP');
            try {
                $return = WxPayApi::unifiedOrder($input);
            } catch (\Exception $e) {
                throw new JsonErrorException($e->getMessage());
            }
            $time = time();
            $tmp=[];
            $result = array();
            if ($return['return_code'] == 'FAIL'){
                throw new JsonErrorException($return['return_msg']);
            }
            //用于重组数据进行签名
            $tmp['appid'] = WxPayConfig::$appid;
            $tmp['noncestr'] = $this->getNonceStr();
            $tmp['prepayid'] = $return['prepay_id'];
            $tmp['package'] = 'Sign=WXPay';
            $tmp['timestamp'] = "$time";
            $tmp['partnerid'] = WxPayConfig::$mchid;
    
            //重新组装数据给app端
            $result['sign'] = $this->MakeSign($tmp,WxPayConfig::$key);
            $result['appid'] = $tmp['appid'];
            $result['noncestr'] = $tmp['noncestr'];
            $result['prepayid'] = $tmp['prepayid'];
            $result['packagePay'] = $tmp['package'];
            $result['timestamp'] = $tmp['timestamp'];
            $result['partnerid'] = $tmp['partnerid'];
            return $result;
        }
    

    4.微信支付之原路返回

         // 微信订单退款原路退回
        public function payment_refund($data){
        /*code_4支付原路退回逻辑*/
            if(!empty($data["transaction_id"])){
                $input = new WxPayRefund();
                $input->SetTransaction_id($data["transaction_id"]);
                $input->SetTotal_fee($data["total_fee"]*100);
                $input->SetRefund_fee($data["refund_fee"]*100);
                $input->SetOut_refund_no(WxPayConfig::$mchid.date("YmdHis"));
                $input->SetOp_user_id(WxPayConfig::$mchid);
                return WxPayApi::refund($input);
            }else{
                return false;
            }
        /*code_4支付原路退回逻辑*/
        }
    

    相关文章

      网友评论

          本文标题:微信支付

          本文链接:https://www.haomeiwen.com/subject/birbzqtx.html