微信扫码支付,适用于pc端

 

<?php


namespace Api\Controller;


class TestPayController
{
/**
     * 微信支付-发起订单.
     *
     * @param float $totalFee 收款总费用 单位元
     * @param string $outTradeNo 唯一的订单号
     * @param string $orderName 订单名称
     *
     * @return array
     */
    public function weChatPay($totalFee, $outTradeNo, $orderName)
    {

        header('Content-type:text/html; Charset=utf-8');

        //获取微信支付参数
        $config = $this->weChatPayConfig();
        $notifyUrl = '';//支付成功异步回调地址

        //订单发起时间
        $timestamp = time();
        $unified = array(
            'appid' => $config['appid'],
            'attach' => 'pay',             //商家数据包,原样返回,如果填写中文,请注意转换为utf-8
            'body' => $orderName,
            'mch_id' => $config['mch_id'],
            'nonce_str' => $this->createNonceStr(),
            'notify_url' => $notifyUrl,
            'out_trade_no' => $outTradeNo,
            'spbill_create_ip' => get_client_ip(),
            'total_fee' => intval($totalFee * 100),       //单位 转为分
            'trade_type' => 'NATIVE',
        );
        $unified['sign'] = $this->getSign($unified, $config['key']);
        $responseXml = $this->curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', $this->arrayToXml($unified));
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);


        if (false === $unifiedOrder) {
            die('parse xml error');
        }
        if ('SUCCESS' != $unifiedOrder->return_code) {
            die($unifiedOrder->return_msg);
        }
        if ('SUCCESS' != $unifiedOrder->result_code) {
            die($unifiedOrder->err_code);
        }
        $codeUrl = (array)($unifiedOrder->code_url);
        if (!$codeUrl[0]) {
            exit('get code_url error');
        }

        echo $this->wePayQRCode($codeUrl[0]);
    }    
public function weChatPayConfig()
{
    //公众号id
    $config['appid'] = '';
    //商户号
    $config['mch_id'] = '';
    //key
    $config['key'] = '';

    return $config;
}

//获取随机字符串    
public function createNonceStr($length = 16)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $str = '';
        for ($i = 0; $i < $length; ++$i) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }


//获取当前服务器时间
    public function get_client_ip()
    {
        if ($_SERVER['REMOTE_ADDR']) {
            $cip = $_SERVER['REMOTE_ADDR'];
        } elseif (getenv('REMOTE_ADDR')) {
            $cip = getenv('REMOTE_ADDR');
        } elseif (getenv('HTTP_CLIENT_IP')) {
            $cip = getenv('HTTP_CLIENT_IP');
        } else {
            $cip = 'unknown';
        }

        return $cip;
    }

//获取签名
    public function getSign($params, $key)
    {
        ksort($params, SORT_STRING);
        $unSignParaString = $this->formatQueryParaMap($params, false);
        $signStr = strtoupper(md5($unSignParaString . '&key=' . $key));

        return $signStr;
    }

//生成签名
    public function formatQueryParaMap($paraMap, $urlEncode = false)
    {
        $buff = '';
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if (null != $v && 'null' != $v) {
                if ($urlEncode) {
                    $v = urlencode($v);
                }
                $buff .= $k . '=' . $v . '&';
            }
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }

        return $reqPar;
    }


//发送微信支付请求 get方式 一般用post方式 不用此方法
    public function curlGet($url = '', $options = array())
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

//将数组转为xml-WeChatPay
    public function arrayToXml($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;
    }

//生成微信支付二维码
    public function wePayQRCode($url)
    {
        require_once VENDOR_PATH . 'WxpayAPI/example/phpqrcode/phpqrcode.php';
        $value = $url;                    //二维码内容
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 5;            //生成图片大小
        //生成二维码图片
        $filename = 'qrcode/' . 'local' . time() . '.png';
        \QRcode::png($value)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值