有乎-价值、共享、信任

What you care about-value, sharing, trust

微信H5手机端调起微信支付-充值封装

| 阅读:2665 发表时间:2019-07-03 17:00:06 微信

第一步:下载官方SDK

第二步:引入包

require_once("wxlib/WxPay.Api.php");        
require_once("wxlib/WxPay.NativePay.php");
require_once("wxlib/WxPay.JsApiPay.php");

第三步:创建订单

 $outTradeNo = date("ymdhis").rand(10000, 999999); 
 $time = date("Y-m-d H:i:s");
  
 $order['order_amount'] = $amount;
 $order['order_sn'] = $outTradeNo;


 WxPayConfig::$appid = "你的appid"; // * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
 WxPayConfig::$mchid = "你的商户号"; // * MCHID:商户号(必须配置,开户邮件中可查看)
 WxPayConfig::$key = "你的key"; // KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
 WxPayConfig::$appsecret = ""; // 公众帐号secert(仅JSAPI支付的时候需要配置)

 
 payH5();



function payh5($order){
         $ip = get_client_ip();
         $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
         $notify = 'http://'.$_SERVER['HTTP_HOST'].'/native_notify.php';
         $nonce_str = createNoncestr();
         
         $appid = '';
         $key  = '';
         $body = "会员充值";
         $mch_id = '1536840651';
         $notify_url = $notify;
         $spbill_create_ip = $ip;
         $total_fee = $order['order_amount']*100;
         $trade_type = 'MWEB';
         $out_trade_no = $order['order_sn'];
         
         $scene_info = '{"h5_info": {"type":"Android","app_name": "CCGG","package_name": "com.ccgg2019"}}';
         $signA = "appid=$appid&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;
         $sign = strtoupper(MD5($strSignTmp));
         $data['sign'] = $sign;
         
         $post_data = "<xml> 
                <appid>$appid</appid> 
                <body>$body</body> 
                <mch_id>$mch_id</mch_id> 
                <nonce_str>$nonce_str</nonce_str> 
                <notify_url>$notify_url</notify_url> 
                <out_trade_no>$out_trade_no</out_trade_no> 
                <spbill_create_ip>$spbill_create_ip</spbill_create_ip> 
                <total_fee>$total_fee</total_fee> 
                <trade_type>$trade_type</trade_type> 
                <scene_info>$scene_info</scene_info> 
                <sign>$sign</sign> 
                </xml>";
        
        $dataxml = http_post($url, $post_data);
        $objectxml = XmlToArr($dataxml);
       
        if( $objectxml['return_code'] == 'SUCCESS' && $objectxml['result_code'] == 'SUCCESS' ){
             $url = $objectxml['mweb_url'];
             exit("<script>location.href='".$url."'</script>");
        }
 }

function createNoncestr(){
  $num = rand(10,20);
  $times = rand(1,9);
  $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  for ($i=0; $i < $times ; $i++) {
    for ($j=0; $j < $num; $j++) {
      $b = rand(0,61);
      $code .= $str[$b];
    }
  }
  return md5($code);
}

function get_client_ip($type = 0) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下,获取客户端真实IP
        $ip=$_SERVER['HTTP_X_REAL_IP'];     
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
    }else{
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

function XmlToArr($xml)
{	
  if($xml == '') return '';
  libxml_disable_entity_loader(true);
  $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);		
  return $arr;
}

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;
}
*文章为作者独立观点,不代表【uuuho有乎】的立场
本文由【uuuho有乎】发表并编辑,转载此文章须经作者同意,并请附上出处及本页链接。如有侵权,请联系本站删除。

Who are we?