IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> php微信支付商户免充值代金券接口升级 -> 正文阅读

[PHP知识库]php微信支付商户免充值代金券接口升级

//todo 如果没安装该插件可删除该行,并使用参数requestType改为‘1’
use GuzzleHttp\Client;

/**
 * Class Demo
 * @package ShopEM\Services\WechatPay
 * 微信支付商户免充值代金券接口升级验收
 */
class RechargeFreeVoucherDemo
{
    /**
     * @var int 请求方式
     * 1 使用curl
     * 2 使用插件 GuzzleHttp\Client
     */
    protected $requestType = 1;

    /**
     * @var string 32随机数
     */
    protected $nonceStr = '5K8264ILTKCH16CQ2502SI8ZNMTM67VS';

    /**
     * @var string 微信支付密钥
     */
    protected $signKey = '你的微信支付密钥'; //todo

    /**
     * @var string 商户号
     */
    protected $mchId = '你的商户号'; //todo

    /**
     * @var string 通过sign方法获取沙箱验签秘钥
     */
    protected $sandBoxKey = '沙箱验签秘钥'; //todo
    /**
     * @var bool 返回数据是否xml转化array
     */
    protected $openXmlArr = true;


    /**
     * DateTime : 2021/8/24 19:19
     * @Author : sun
     * 字段拼接
     */
    public static function getSignContent($data): string
    {
        $buff = '';
        foreach ($data as $k => $v) {
            $buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k . '=' . $v . '&' : '';
        }
        return trim($buff, '&');
    }

    /**
     *  作用:array转xml
     */
    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;
    }

    /**
     *  作用:将xml转为array
     */
    public function xmlToArray($xml)
    {
        //将XML转为array
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array_data;
    }

    #签名
    public function getsignkey($data, $box = true)
    {
        $key = $this->sandBoxKey;
        #填写你的秘钥 ,后面几步用沙盒密钥
        if (!$box) $key = $this->signKey;

        ksort($data);
        $string = md5(self::getSignContent($data) . '&key=' . $key);
        return strtoupper($string);
    }

    #第一步 获取沙箱验签秘钥
    public function sign()
    {
        $data = [
            'mch_id'    => $this->mchId,
            'nonce_str' => $this->nonceStr
        ];
        $data['sign'] = $this->getsignkey($data, false);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey';
        $result = $this->post($data, $url);
        if ($this->openXmlArr) if ($this->openXmlArr) $result = $this->xmlToArray($result);
        return $result;
    }

    #第二步 「1003-可选用例-公众号/APP/扫码正常支付」验收
    #1.统一下单
    public function pay()
    {
        //total_fee 必须为551
        $data = [
            'appid'            => 'wxd678efh567hg6787',
            'body'             => '测试商品',
            'mch_id'           => $this->mchId,
            'nonce_str'        => $this->nonceStr,
            'notify_url'       => 'http://www.weixin.qq.com/wxpay/pay.php',
            'out_trade_no'     => '201208241848',
            'spbill_create_ip' => '192.168.10.10',
            'total_fee'        => '552',
            'trade_type'       => 'JSAPI'
        ];
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder';
        $data['sign'] = $this->getsignkey($data);
        $result = $this->post($data, $url);
        if ($this->openXmlArr) $result = $this->xmlToArray($result);
        return $result;
    }

    #2.查询订单
    public function query()
    {
        $data = [
            'appid'        => 'wxd678efh567hg6787',
            'mch_id'       => $this->mchId,
            'nonce_str'    => $this->nonceStr,
            'out_trade_no' => '201208241848',
        ];
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery';
        $data['sign'] = $this->getsignkey($data);
        $result = $this->post($data, $url);
        if ($this->openXmlArr) $result = $this->xmlToArray($result);
        return $result;
    }

    #第三步 「1003-可选用例-公众号/APP/扫码支付退款」验收
    #1.申请退款
    public function refund()
    {
        //refund_fee 必须为552
        $data = [
            'appid'         => 'wxd678efh567hg6787',
            'mch_id'        => $this->mchId,
            'nonce_str'     => $this->nonceStr,
            'out_trade_no'  => '201208241848',
            'out_refund_no' => 'TM201208241848',
            'refund_fee'    => '552',
            'total_fee'     => '552',
        ];
        $data['sign'] = $this->getsignkey($data);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refund';
        $result = $this->post($data, $url);
        if ($this->openXmlArr) $result = $this->xmlToArray($result);
        return $result;
    }

    #2.查询退款
    public function refundquery()
    {
        $data = [
            'appid'        => 'wxd678efh567hg6787',
            'mch_id'       => $this->mchId,
            'nonce_str'    => $this->nonceStr,
            'out_trade_no' => '201208241848',
        ];
        $data['sign'] = $this->getsignkey($data);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery';
        $result = $this->post($data, $url);
        if ($this->openXmlArr) $result = $this->xmlToArray($result);
        return $result;
    }

    #第四步 「1005-必选用例-交易对账单下载」 验收
    public function downloadbill()
    {
        $data = [
            'appid'        => 'wxd678efh567hg6787',
            'bill_date'    => '20120824',
            'bill_type'    => 'ALL',
            'mch_id'       => $this->mchId,
            'nonce_str'    => $this->nonceStr,
            'out_trade_no' => '201208241848',
        ];
        $data['sign'] = $this->getsignkey($data);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill';
        $result = $this->post($data, $url);
        return $result;
    }

    /**
     * DateTime : 2021/8/24 19:06
     * @Author : sun
     * @param $data
     * @param $url
     * post 请求
     */
    public function post($data, $url)
    {
        #两种请求方式
        if ($this->requestType === 1) {
            return $this->curlPost($data, $url);
        }
        if ($this->requestType === 2) {
            return $this->postXmlCurl($data, $url);
        }
        print_r('请求参数错误');
        exit;
    }

    /**
     *  作用:以post方式提交xml到对应的接口url
     */
    public function postXmlCurl($data, $url)
    {
        $xml = $this->arrayToXml($data);
        $httpClient = new Client();
        $response = $httpClient->request('POST', $url, ['body' => $xml]);
        $return = $response->getBody()->getContents();
        return $return;
    }

    /**
     * DateTime : 2021/8/24 19:03
     * @Author : sun
     * curl 请求方式
     */
    public function curlPost($data, $url)
    {
        $xmlData = $this->arrayToXml($data);
        $header[] = "Content-type: text/xml";        //定义content-type为xml,注意是数组
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        curl_close($ch);
        return $response;
    }
}
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-26 11:53:54  更:2021-08-26 11:55:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 5:42:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码