简单的微信支付TP5,与TP3封装教程案例

浏览:2923 发布日期:2018/02/22 分类:功能实现 关键字: 微信支付 公众号支付
微信公众号支付,类封装解决方案。主要这是基础代码需要结合你直接的业务逻辑进行修改。
注意: 本代码是给微信支付原生代码进行了封装,我给相关的核心类文件都封装到控制器上了,只需要引入或者 POST 到接口数据就,可以进行微信支付了。
如有问题可以联系作者,新手建议下载下来代码首先看一下,学习研究下。
H5,小程序,公众号,等常用技术,如: 退款、提现、模板消息、H5支付、小程序支付、分享,微信扫码支付 ,支付宝支付,等,如需技术协助请联系作者QQ:1139636824,QQ:1139636824发CODE
只需要一个类文件就OK了-----------------下面代码的pay是方法POST入口,比如你前端需要发起一个支付那么你就POST过来数据到这个pay方法,然后pay方法处理相关数据,返回支付相关的必要json数据->发送给前端->支付吊起。
代码的实现原理,与效果(场景)-点击立即支付会立即跳出来支付窗口,是不是很牛X 哈哈 哈哈。

private $app_id='wx4eb29d';                     //appid
    private $app_secret='ce8daeec0';   //app_secret
    private $mch_id ='1465357402';                              //商户号
    private $key='Xe4XRjiA7IENsS';
    private $notify='http://yibhang.com/wxpay.php'; //回调地址
    public $openid;
    public function pay($reannumb,$money,$openid){  //单号(保证每次唯一)  -费用 -openid  res:返回支付的必要json参数(注意查询数据)
            $this->openid=$openid;
            $pays=$money;
            $conf = $this->payconfig($reannumb,$pays*100,'支付程序支付');
            if (!$conf || $conf['return_code'] == 'FAIL') exit("<script>alert('对不起,微信支付接口调用错误!" . $conf['return_msg'] . "');history.go(-1);</script>");
            $this->orderid = $conf['prepay_id'];
            $jsApiObj["appId"] =$conf['appid'];
            $timeStamp = time();
            $jsApiObj["timeStamp"] = "$timeStamp";
            $jsApiObj["nonceStr"] = $this->createNoncestr();
            $jsApiObj["package"] = "prepay_id=".$conf['prepay_id'];
            $jsApiObj["signType"] = "MD5";
            $jsApiObj["paySign"] = $this->MakeSign($jsApiObj);
            echo json_encode(array('parameters'=>$jsApiObj));
            die();
    }
#微信JS支付参数获取-注意下面是支付方法可以不需要管!!!#
    protected function payconfig($no, $fee, $body)
    {
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $data['appid'] =$this->app_id;
        $data['mch_id'] =$this->mch_id;
        $data['device_info'] ='WEB';
        $data['body'] = $body;
        $data['out_trade_no'] =$no;
        $data['total_fee'] = $fee;
        $data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"];
        $data['notify_url'] =$this->notify;
        $data['trade_type'] = 'JSAPI';
        $data['openid']=$this->openid; 
        $data['nonce_str'] = $this->createNoncestr();
        $data['sign'] = $this->MakeSign($data);
        //print_r($data);
        $xml = $this->ToXml($data);
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        //设置header
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_POST, TRUE);       //发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);      // 设置超时限制防止死循环
        $tmpInfo = curl_exec($curl); // 执行操作
        curl_close($curl); //关闭CURL会话
        $arr = $this->FromXml($tmpInfo);
        return $arr;
    }
    /**
     *    作用:产生随机字符串,不长于32位
     */
    public 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;
    }
    /**
     *    作用:产生随机字符串,不长于32位
     */
    public function randomkeys($length)
    {
        $pattern = '1234567890123456789012345678905678901234';
        $key = null;
        for ($i = 0; $i < $length; $i++) {
            $key .= $pattern{mt_rand(0, 30)};    //生成php随机数
        }
        return $key;
    }
    /**
     * 将xml转为array
     * @param string $xml
     * @throws WxPayException
     */
    public function FromXml($xml)
    {
        //将XML转为array
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
    }
    /**
     * 输出xml字符
     * @throws WxPayException
     **/
    public function ToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }
  
不知道为啥代码压RAR不能上传, 联系我QQ我传给大家。
不知道为啥代码压RAR不能上传, 联系我QQ我传给大家。
不知道为啥代码压RAR不能上传, 联系我QQ我传给大家。
最后一句: 技术无价,不喜勿喷。
评论( 相关
后面还有条评论,点击查看>>