wap端微信h5支付,用于非微信浏览器

做项目碰到了wap端实现微信支付,听朋友说微信有wap端的h5支付,就看了下这块的接口,记录下供以后方便查看
(具体的接口可以查看商户平台说明:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1)
<?php
/**
 *非微信浏览器 调用微信h5支付
 */
namespace app\mobile\controller;

use think\Controller;
use think\Loader;
use think\Request;
use think\Url;
use think\Config;
use think\Page;

class Wxpay extends MobileBase
{

    /**
     * @return array  下单方法
     */
    public function wxpaymoney(Request $request)
    {
        $param=$request->param();
        if (!empty($param)){
            $param = json_decode(json_encode(simplexml_load_string($param, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            echo '-------';
            p($param);
            echo '*******';
        }

        $url='https://api.mch.weixin.qq.com/pay/unifiedorder';  //下单地址
        $appid='appid';//公众号appid
        $appsecret='appsecret';
        $mch_id='商户平台id';//商户平台id
        $nonce_str='qyzf'.rand(100000, 999999);//随机数
        $out_trade_no='wap'.date('YmdHis').rand(100000,999999);
        $ip=$this->getClientIp();
        $scene_info='{"h5_info": {"type":"Wap","app_name": "project info","package_name": "wap订单微信支付"}}';
//        $appidInfo=$this->getTokens($appid,$appsecret);
//        echo '=======';
//        p($appidInfo);
//        echo '*******';die;
//        $openid=$appidInfo['openid'];
        $total_fee=1;
        $trade_type='MWEB';
        $attach='wap支付测试';
        $body='H5支付测试';
        $notify_url='http://m.xxxxx.com/Mobile/Wxpay/wxnotify.html';
        $arr=[
            'appid'=>$appid,
            'mch_id'=>$mch_id,
            'nonce_str'=>$nonce_str,
            'out_trade_no'=>$out_trade_no,
            'spbill_create_ip'=>$ip,
            'scene_info'=>$scene_info,
//            'openid'=>$openid,
            'total_fee'=>$total_fee,
            'trade_type'=>$trade_type,
            'attach'=>$attach,
            'body'=>$body,
            'notify_url'=>$notify_url
        ];
        $sign=$this->getSign($arr);
        //<openid>'.$openid.'</openid>
        $data='<xml>
 <appid>'.$appid.'</appid>
 <attach>'.$attach.'</attach>
 <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>'.$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>';
        $result=$this->https_request($url,$data);
        echo '====================';
        var_dump($result);
        echo '*******************';
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result_info = json_decode(json_encode(simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        p($result_info);
        echo '<a href="'.$result_info['mweb_url'].'">测试微信支付</a>';die;
    }

    /**
     *curl请求
     */
    private function https_request($url, $data = null) {
        p($data);
        $curl = curl_init ();
        curl_setopt ( $curl, CURLOPT_URL, $url );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
        if (! empty ( $data )) {
            curl_setopt ( $curl, CURLOPT_POST, 1 );
            curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
        }
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
        $output = curl_exec ( $curl );
        curl_close ( $curl );
        return $output;
    }

    /**
     * @return 获取ip
     */
    private function getClientIp()
    {
        $cip='unknown';
        if ($_SERVER['REMOTE_ADDR']){
            $cip=$_SERVER['REMOTE_ADDR'];
        }elseif (getenv($_SERVER['REMOTE_ADDR'])){
            $cip=getenv($_SERVER['REMOTE_ADDR']);
        }
        return $cip;
    }

    /**
     * @return 异步通知
     */
    public function wxnotify(Request $request)
    {
        Loader::import('payment.wxwap.PayNotifyCallBacks');
        $notify=new \PayNotifyCallBacks();
        $notify->Handle(false);
        die;
    }

    /**
     * @param Request 
     */
    public function wxcallback(Request $request)
    {
        $data=$request->param();
        file_put_contents('./testwx1.txt','888888888888888'.$data['orderid'].'-----'.$data['transaction_id'].'******');

    }
    /**
     *     作用:格式化参数,签名过程需要使用
     */
    public function formatBizQueryParaMap($paraMap, $urlencode)
    {
//        var_dump($paraMap);//die;
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)
            {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar='';
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        p($reqPar);//die;
        return $reqPar;
    }

    /**
     *     作用:生成签名
     */
    public function getSign($Obj)
    {
        p($Obj);//die;
        foreach ($Obj as $k => $v)
        {
            $Parameters[$k] = $v;
        }
        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //echo '【string1】'.$String.'</br>';
        //签名步骤二:在string后加入KEY   5a02bd8ecxxxxxxxxxxxxc1aae7d199  这里的秘钥是 商户平台设置的一定要改不然报签名错误
        $String = $String."&key=5a02bd8ecxxxxxxxxxxxxc1aae7d199";
        //echo "【string2】".$String."</br>";
        //签名步骤三:MD5加密
        $String = md5($String);
        //echo "【string3】 ".$String."</br>";
        //签名步骤四:所有字符转为大写
        $result_ = strtoupper($String);
        //echo "【result】 ".$result_."</br>";
        return $result_;
    }


    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }
}

测试demo(PHP版): http://download.csdn.net/download/u010533511/10155017


          
展开阅读全文

没有更多推荐了,返回首页