h5支付 jsapi支付 扫码支付(场景二)

配置文件

    function config(){
    $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //  支付接口
    $key = ''; //key为商户平台设置的密钥key
    $appid = ''; //appid
    $body = '';// 商品描述
    $mch_id = ''; // 商户号
    $nonce_str = randomkeys(32); // 随机字符串
    $notify_url = ''; //  异步通知地址
    $sign_type = 'MD5';//  签名类型
    $out_trade_no = time();// 商户订单号
    $total_fee = '1'; //  支付金额  以分为单位
    $spbill_create_ip  = getip(); //  终端ip
    $sign_arr = [
        "appid"=>$appid,
        "body"=>$body,
        "mch_id"=>$mch_id,
        "nonce_str"=>$nonce_str,
        "notify_url"=>$notify_url,
        "sign_type"=>$sign_type,
        "out_trade_no"=>$out_trade_no,
        "total_fee"=>$total_fee,
        "spbill_create_ip"=>$spbill_create_ip,
   ];
    return  ["sign_arr"=>$sign_arr,"key"=>$key,"url"=>$url];
     }

微信支付 JSAPI

    function  test_pay(){

        $openid = '';// 用户的openid
        $config = config();  // 公共的配置文件
        $sign_arr = $config['sign_arr'];
        $sign_arr['trade_type'] = 'JSAPI';
        $sign_arr['openid'] = $openid;
        $key = $config['key'];
        $url =$config['url'];
        // 根据键值对数组进行排序
        ksort($sign_arr,SORT_STRING );
        $stringA = ToUrlParams($sign_arr);
        $stringSignTemp = $stringA."&key=".$key;
        $sign= strtoupper(md5($stringSignTemp)); //注:MD5签名方式  签名
        $sign_arr['sign'] = $sign;
        $res = wxpost($url,arrayToXml($sign_arr));
        $pay_arr = xmlToArray($res);  //这里是数组
        $nonceStr = $sign_arr['nonce_str'];
        $package = 'prepay_id='. $pay_arr['prepay_id'];
        $timeStamp = (string)time();
        $signType = 'MD5';
        $json_data = [
        "appId"=>$sign_arr['appid'], // appid
        "nonceStr"=>$nonceStr,// 随机串
        "package"=>$package,//支付id
        "signType"=>$signType,//微信签名方式
        "timeStamp"=>$timeStamp,//  时间戳
        ];
        //  签名
        $stringB = ToUrlParams($json_data);
        $sign= strtoupper(md5($stringB."&key=$key")); //注:MD5签名方式  签名
        $json_data['paySign'] = $sign;
        $data = $json_data;
        $data['jsApiParameters'] = json_encode($json_data);
        echo  json_encode($data);
}

微信外部支付 h5支付

 function  test_pay_h5(){
       $config = config();  // 公共的配置文件
       $scene_info = '{"h5_info": {"type":"Wap","wap_url": "网站域名","wap_name": "支付"}}';//  场景信息
       $sign_arr = $config['sign_arr'];
       $sign_arr['trade_type'] = 'MWEB';
       $sign_arr['scene_info'] = $scene_info; //
       $key = $config['key'];
       $url =$config['url'];
      ksort($sign_arr,SORT_STRING );
      $stringA = ToUrlParams($sign_arr);
      $stringA = $stringA."&key=$key";
      $sign = strtoupper(md5($stringA));
      $sign_arr['sign'] = $sign;
      //  参数组装完之后  转化为xml数据
      $post_data = arrayToXml($sign_arr);
      //  转化完之后  进行请求接口
      $rest = wxpost($url,$post_data);
      $array = xmlToArray($rest);
      $mweb_url = $array['mweb_url']."&redirect_url=".urlencode("想要跳转的地址");
      header("location:".$mweb_url);
  }



扫码支付
     
     function test_pay_QRcode(){
        require_once "./phpqrcode.php";
         $config = config();  // 公共的配置文件
         $sign_arr = $config['sign_arr'];
         $sign_arr['trade_type'] = 'NATIVE';
         $key = $config['key'];
         $url =$config['url'];
        // 根据键值对数组进行排序
        ksort($sign_arr,SORT_STRING );
        $stringA = ToUrlParams($sign_arr);
        $stringSignTemp = $stringA."&key=".$key;
        $sign= strtoupper(md5($stringSignTemp)); //注:MD5签名方式  签名
        $sign_arr['sign'] = $sign;
        $res = wxpost($url,arrayToXml($sign_arr));
        $pay_arr = xmlToArray($res);  //这里是数组
         //  在输出图片之前 不能有任何的内容输出
        QRcode::png($pay_arr['code_url']);

    }

 //异步回调地址
 
  function  test_index(){
     $rst =  file_get_contents("php://input") ;
     $msg = (array)simplexml_load_string($rst, 'SimpleXMLElement', LIBXML_NOCDATA);
     $data = json_encode($msg);
  }

// xml 转为数组

  function xmlToArray($xml,$type='')
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        //simplexml_load_string()解析读取xml数据,然后转成json格式
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

        if ($type == "json") {
            $json = json_encode($xmlstring);
            return $json;
        }
        $arr = json_decode(json_encode($xmlstring), true);
        return $arr;
    }

// curl请求

       function wxpost($url,$post)
        {
            //初始化
            $curl = curl_init();
            $header[] = "Content-type: text/xml";//定义content-type为xml
            //设置抓取的url
            curl_setopt($curl, CURLOPT_URL, $url);
            //设置头文件的信息作为数据流输出
    //        curl_setopt($curl, CURLOPT_HEADER, 1);
            //定义请求类型
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            //设置获取的信息以文件流的形式返回,而不是直接输出。
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            //设置post方式提交
            curl_setopt($curl, CURLOPT_POST, 1);
            //设置post数据
            $post_data = $post;
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
            //执行命令
            $data = curl_exec($curl);
            //关闭URL请求
    
            //显示获得的数据
            if ($data)
            {
                curl_close($curl);
                return $data;
            }else{
                $res = curl_error($curl);
                curl_close($curl);
                return $res;
            }
    
        }

// array 转为xml

   function arrayToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key=>$val){
            if(is_array($val)){
                $xml.="<".$key.">".arrayToXml($val)."</".$key.">";
            }else{
                $xml.="<".$key.">".$val."</".$key.">";
            }
         }
        $xml.="</xml>";
        return $xml;
    }

// 拼装成url参数形式

    function ToUrlParams($data)
    {
        ksort($data,SORT_STRING);
        $buff = "";
        foreach ($data as $k => $v)
        {
        if($k != "sign" && $v != "" && !is_array($v)){
        $buff .= $k . "=" . $v . "&";
        }
        }

        $buff = trim($buff, "&");
        return $buff;
    }

生成随机字符串

    function randomkeys($length)
    {
        $key = '';
        $pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        for($i=0;$i<$length;$i++)
        {
            $key .= $pattern{mt_rand(0,35)};  //生成php随机数
        }
        return $key;
    }

获得客户端的ip

    function getip() {
        $ip = '';
        if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" )) {
            $ip = getenv ( "HTTP_CLIENT_IP" );
        } else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" )) {
            $ip = getenv ( "HTTP_X_FORWARDED_FOR" );
        } else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" )) {
            $ip = getenv ( "REMOTE_ADDR" );
        } else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" )) {
            $ip = $_SERVER ['REMOTE_ADDR'];
        } else {
            $ip = "unknown";
        }
        return ($ip);
    }
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、付费专栏及课程。

余额充值