php 微信h5支付


 function wxPay($appid,$mch_id,$key,$out_trade_no,$money){


    $userip = $_SERVER["REMOTE_ADDR"]; //获得用户设备IP
    $nonce_str=MD5($out_trade_no);//随机字符串
    $total_fee = $money*100; //金额*100
    //$total_fee = 1;
    $spbill_create_ip = $userip; //IP
    $notify_url = "http://".$_SERVER['SERVER_NAME']."/index.php/Home/Back/wx_back"; //回调地址 jishu.whwlhd.com/index.php/Home/Pay/wx/id/
    $trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
    $body="算卦币充值";
//
//
    $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.xinkuizi.com","wap_name":"支付"}}';//场景信息 必要参数
    $signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_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="$appid$body$mch_id$nonce_str$notify_url$out_trade_no$scene_info$spbill_create_ip$total_fee$trade_type$sign
        ";//拼接成XML 格式


    $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址

    $dataxml =http_post($url,$post_data);
    $objectxml = (array)simplexml_load_string($dataxml,'SimpleXMLElement',LIBXML_NOCDATA); //将微信返回的XML 转换成数组


    if($objectxml['return_code'] == 'SUCCESS'){
        $redirect_url = urlencode("http://jishu.whwlhd.com/index.php?a=index&m=Pay&uid=26");
        $url = $objectxml['mweb_url'].'&redirect_url='.$redirect_url;
        echo "";
        exit;

    }
//    $redirect_url = urlencode("http://www.bojuwang.net/");
}


 function http_post($url='',$post_data=array(),$header=array(),$timeout=30) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;
}
?>

你可能感兴趣的:(php)