微信H5支付浏览器支付

//微信支付
Route::get('wechat', 'ZhifuController@wechat');
//微信支付成功完成后回调地址
Route::post('wenotify','ZhifuController@wenotify');
//微信支付成功后跳转页面
Route::get('wechat_ok', 'ZhifuController@wechat_ok');

==================================================

$.ajax({
    type:'get',
    dataType:'html',
    //async : true,
    data: {
        'zhifujiage': $('#zhifujiage').val(),
        'erweima' : $('#erweima').val(),
        'zhifufangshi': 'wechat'
    },    
    url:'{{url('wechat')}}',
    success:function(data) {
        //alert(data);
        window.location.href = data;
    }
});

=====================================================

public function wechat()
    {
        //微信支付
        $outTradeNum = 'wechat'.time();
        $payAmount = request::input('zhifujiage');
        $subject = '商品内容';

        $appid  = "wx72703c3036123456";     //应用 APPID
        $mch_id = "1549212345";                    //微信支付商户号     
        $out_trade_no = $outTradeNum;        //平台内部订单号
        $nonce_str = $this->createNoncestr();   //随机字符串
        $body = $subject;    //内容
        $total_fee = $payAmount * 100;   //金额,转化成分单位
        $spbill_create_ip = $this->Ip();     //IP
        $notify_url = "http://www.xxxxx.com/wenotify"; //回调地址
        $trade_type = 'MWEB';//交易类型 具体看 API 里面有详细介绍
        $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.xxxxx.com","wap_name":"创咖寄卖"}}';//场景信息 必要参数
        //拼接字符串  注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面 XML  是否正确
        $signA ="appid=$appid&attach=$out_trade_no&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
        //微信商户 API 密钥, 申请支付后有给予一个商户账号和密码,登陆后自己设置key
        $key = "wwwxxxxxjimaicom000000000000000";                 
        $strSignTmp = $signA . "&key = $key";
        // MD5 后转换成大写
        $sign = strtoupper(MD5($strSignTmp));
        //拼接成 XML 格式
        $post_data = "<xml>
                            <appid>$appid</appid>
                            <mch_id>$mch_id</mch_id>
                            <body>$body</body>
                            <out_trade_no>$out_trade_no</out_trade_no>
                            <total_fee>$total_fee</total_fee>
                            <spbill_create_ip>$spbill_create_ip</spbill_create_ip>
                            <notify_url>$notify_url</notify_url>
                            <trade_type>$trade_type</trade_type>
                            <scene_info>$scene_info</scene_info>
                            <attach>$out_trade_no</attach>
                            <nonce_str>$nonce_str</nonce_str>
                            <sign>$sign</sign>
                    </xml>";
        //微信传参地址
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        //后台 POST 微信传参地址  同时取得微信返回的参数
        $dataxml = $this->postXmlCurl($post_data, $url);
        //将微信返回的 XML 转换成数组        
        $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);
        // 可以在 MWEB_URL 后拼接上 redirect_url 参数,来指定回调页面
        $returnUrl = "http://www.xxxxx.com/wechat_ok";
        $return_Url = urlencode($returnUrl);
        return $objectxml['mweb_url'] . '&redirect_url=' . $return_Url;
    }

=====================================================

    public function wenotify()
    {
        //微信支付回调页面,静默执行
        //回调地址接收的数据
//{"appid":"wx72703c3036a8197b","bank_type":"CFT","cash_fee":"1","fee_type":"CNY","is_subscribe":"Y","mch_id":"1549232891","nonce_str":"vhJeE1k7lxz5Ankz","openid":"ouzAowGMNiks9R62cw5NZas9BvlQ","out_trade_no":"wechat1566399864","result_code":"SUCCESS","return_code":"SUCCESS","sign":"E5E1040F24C88E48130F5275BD19EA7C","time_end":"20190821230428","total_fee":"1","trade_type":"MWEB","transaction_id":"4200000374201908217238359011"}    

        //获得返回的XML数据
        $xml = file_get_contents("php://input");
        //将服务器返回的XML数据转化为数组,引用格式 $data['appid'];
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        // 保存微信服务器返回的签名sign
           $data_sign = $data['sign'];
           // sign不参与签名算法
           unset($data['sign']);
           $sign = $this->signs($data);
        $result='';
           // 判断签名是否正确  判断支付状态
           if ( ($sign===$data_sign) && ($data['return_code']=='SUCCESS') && ($data['result_code']=='SUCCESS') )
           {
               //$data为获取服务器返回的数据
                $result = $data;
              //这里进行业务逻辑处理。。。。。。
           }else{
            $str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
           }
           // 返回状态给微信服务器
           if ($result) {
               $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
           }else{
               $str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
           }       
           return $str;         
    }

==============================================================

public function wechat_ok()
      {
          //跳转后的处理。。。。
      }

==============================================================

public function Ip()
    {
        //获得ip
           if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
               $ip = getenv('HTTP_CLIENT_IP');
           } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
               $ip = getenv('HTTP_X_FORWARDED_FOR');
           } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
               $ip = getenv('REMOTE_ADDR');
           } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
               $ip = $_SERVER['REMOTE_ADDR'];
           }
           return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    }

-----------------------------------------------------

public function createNoncestr( $length = 32 )
    {
        //随机32位长度的字符串
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }

----------------------------------------------------------

function postXmlCurl($xml, $url, $second = 30){
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //设置 header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post 提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //运行 curl
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        }else{
            $error = curl_errno($ch);
            curl_close($ch);
            echo "curl 出错,错误码:$error"."<br>";
        }
    }

-----------------------------------------

private function signs($data)
    {
        //签名算法
        $stringA = '';
        foreach ($data as $key=>$value){
            if(!$value) continue;
            if($stringA) $stringA .= '&'.$key."=".$value;
            else $stringA = $key."=".$value;
        }
        //申请支付后有给予一个商户账号和密码,登陆后自己设置key
        $wx_key = 'wwwxxxxxjimaicom1234567891234';
        $stringSignTemp = $stringA.'&key='.$wx_key;
        return strtoupper(md5($stringSignTemp));
      }

-------------------------------------------

转载于:https://my.oschina.net/u/2444569/blog/3097315

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2018-09-03 php服务端微信支付整理SDK,封装,如果喜欢请给个好评!谢谢 说明: 配置在 WeChatConfig文件下 环境:php5.6,基于tp5开发 调用统一下单接口: include_once EXTEND_PATH . 'WeChatSDK/WeChatSDK.php'; $data = $this->getOrderInfo($pay_sn); if (!$data) { return $this->resultCode(-2019, '订单不存在或已支付'); } $WeixinPay = new \WeChatSDK(); if ($trade_type == 'JSAPI') { //目前未有此功能 $openid = ''; $product_id = ''; } if ($trade_type == 'NATIVE') { $openid = ''; $product_id = $pay_sn; } if ($trade_type == 'MWEB') { $openid = ''; $product_id = $pay_sn; } if ($trade_type == 'APP') { $openid = ''; $product_id = $pay_sn; } $out_trade_no = $pay_sn; $result = $WeixinPay->setWeiXinPay($data['pay_body'], $data['pay_detail'], $data['pay_money'] * 100, $out_trade_no, $red_url, $trade_type, $openid, $product_id); APP加密:$WeChatSDK->GetAppParameters($result['data']); web编码 $WeChatSDK->GetMwebApiParameters(); jsapi:WeChatSDK-> GetJsApiParameters(); 回调调用: include_once EXTEND_PATH . 'WeChatSDK/WeChatSDK.php'; Log::write("gwgwgwgw---------------------------------进入异步回掉"); $postStr = file_get_contents('php://input'); Log::write("gwgwgwgw---------------------------------" . $postStr); $WeChatSDK = new \WeChatSDK(); if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $check_sign = $WeChatSDK->checkSign($postObj, $postObj->sign); Log::write('-----check_sign-------' . $check_sign . '------------check_sign--------------'); if ($postObj->result_code == 'SUCCESS' && $check_sign == 1) { model('order', 'service')->affirmPayment($postObj->out_trade_no); $xml = "<xml> <![CDATA[SUCCESS]]></return_co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值