php微信h5支付demo

 

在网上找了很多关于微信h5支付的demo都不可靠,后来,在找到的demo上面做了修改,

代码如下:

<?php
$money= 1;//充值金额
$userip = get_client_ip();          //获得用户设备IP
$appid  = "xxxxxxxxxxxxxxxxxx";                  //应用APPID
$mch_id = "xxxxxxxxxxx";                  //微信支付商户号
$key    = "xxxxxxxxxxxxxx";                 //微信商户API密钥

$rand = rand(00000,99999);
$out_trade_no = '20170804'.$rand;//平台内部订单号
$nonce_str = createNoncestr();//随机字符串
$body = "H5充值";//内容
$total_fee = $money; //金额
$spbill_create_ip = $userip; //IP
$notify_url = "http://www.xxx.com/"; //回调地址
$trade_type = 'MWEB';//交易类型 因为是h5支付所以交易类型必须是MWEB
$scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.baidu.com","wap_name":"支付"}}';//场景信息 必要参数

//以下信息可以不改
$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";
$strSignTmp = $signA."&key=$key"; //拼接字符串  注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面XML  是否正确

$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写

$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>";//拼接成XML 格式

$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
$dataxml = postXmlCurl($post_data,$url); //后台POST微信传参地址  同时取得微信返回的参数    POST 方法我写下面了
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组

function createNoncestr( $length = 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>";
    }
}
function get_client_ip() {
    static $realip;
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>H5支付测试</title>
    <link rel="stylesheet" href="">
    <style>
        body{
            padding:0px;
            margin:0px auto;
        }
        h3{
            text-align:center;
            color:#e0e0e0;
            margin:0px;
            background:#1c6a9e;
            padding:5px;
        }
        p{
            margin:20px auto;
            text-align:center;
            font-size:20px;
            color: #165565;
        }
        p span{
            font-size:17px;
            color:#00CC33;
        }
        a{
            text-decoration:none;
            display:block;
            text-align:center;
            color:#fff;
            font-size:14px;
            font-weight:bold;
            background:#00CC00;
            padding:5px;
        }
    </style>
</head>
<body>
    <h3>H5支付测试</h3>
    <p>支付金额:<span><?php echo $total_fee/100 ?></span></p>
    <a href="<?php echo $objectxml['mweb_url'] ?>">去支付</a>
</body>
</html>

我以后会亲自去实践java的微信h5支付。想要的小伙伴可以稍微等等!~!

转载于:https://my.oschina.net/xiaoaimiao/blog/1570702

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值