Home > Backend Development > PHP Tutorial > How to implement WeChat enterprise payment in PHP (code example)

How to implement WeChat enterprise payment in PHP (code example)

不言
Release: 2023-04-04 07:06:01
Original
1808 people have browsed it

The content of this article is about how to implement WeChat enterprise payment (code example) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Encapsulate the WeChat enterprise payment class WeiXinPayToUser, as shown in the following code:

class WeixinPayToUser
{
    /**
     * API 参数
     * @var array
     * 'mch_appid'         # 公众号APPID
     * 'mchid'             # 商户号
     * 'device_info'       # 设备号
     * 'nonce_str'         # 随机字符串
     * 'partner_trade_no'  # 商户订单号
     * 'openid'            # 收款用户openid
     * 'check_name'        # 校验用户姓名选项 针对实名认证的用户
     * 're_user_name'      # 收款用户姓名
     * 'amount'            # 付款金额
     * 'desc'              # 企业付款描述信息
     * 'spbill_create_ip'  # Ip地址
     * 'sign'              # 签名
     */
    public $parameters = [];
    public $SSLROOTCA_PATH='';
    public $SSLCERT_PATH='';
    public $SSLKEY_PATH='';
    public $appid='';
    public $secret='';
    public $mchid='';
    public $key='';//商户密钥

    public function __construct()
    {

        $this->url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $this->curl_timeout = 10;
        $this->SSLROOTCA_PATH=dirname(__FILE__).'/weixin/cert/rootca.pem';
        $this->SSLCERT_PATH=dirname(__FILE__).'/weixin/cert/apiclient_cert.pem';
        $this->SSLKEY_PATH=dirname(__FILE__).'/weixin/cert/apiclient_key.pem';
    }

    public function setParameter($key,$value){
        $this->parameters[$key]=$value;
    }

    function arrayToXml($arr,$dom=0,$item=0){
        if (!$dom){
            $dom = new DOMDocument("1.0");
        }
        if(!$item){
            $item = $dom->createElement("xml");
            $dom->appendChild($item);
        }
        foreach ($arr as $key=>$val){
            $itemx = $dom->createElement(is_string($key)?$key:"item");
            $item->appendChild($itemx);
            if (!is_array($val)){
                $text = $dom->createTextNode($val);
                $itemx->appendChild($text);

            }else {
                $this->arrayToXml($val,$dom,$itemx);
            }
        }
        $dom->encoding = 'UTF-8'; // insert proper
        return $dom->saveXML();
    }

    public function getSign($paramArr){//print_r($paramArr);
        ksort($paramArr);
        $paramStr = http_build_query($paramArr);
        $paramStr=urldecode($paramStr);
        $param_temp=$paramStr.&#39;&key=&#39;.$this->key;//echo $param_temp.&#39;<br>&#39;;
        $signValue=strtoupper(md5($param_temp));//echo $signValue.&#39;<br>&#39;;
        return $signValue;

    }

    /**
     * 生成请求xml数据
     * @return string
     */
    public function createXml()
    {
        $this->parameters[&#39;mch_appid&#39;] = $this->appid;
        $this->parameters[&#39;mchid&#39;]     = $this->mchid;
       // $this->parameters[&#39;nonce_str&#39;] = md5(time());
        $this->parameters[&#39;nonce_str&#39;] = &#39;dddfff&#39;;
        $this->parameters[&#39;sign&#39;]      = $this->getSign($this->parameters);
        $a= $this->arrayToXml($this->parameters);
        //echo $a;
        return $a;
    }

    public function pay(){
        $xml=$this->createXml();
        $url=$this->url;
        return $this->postXmlSSLCurl($xml,$url,$second=30);
    }

    /**
     *     作用:使用证书,以post方式提交xml到对应的接口url
     */
    function postXmlSSLCurl($xml,$url,$second=30)
    {

        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        //这里设置代理,如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, &#39;8.8.8.8&#39;);
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //设置header
        curl_setopt($ch,CURLOPT_HEADER,FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
        //设置证书
        //curl_setopt($ch,CURLOPT_CAINFO, $this->SSLROOTCA_PATH);
        //使用证书:cert 与 key 分别属于两个.pem文件
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,&#39;PEM&#39;);
        curl_setopt($ch,CURLOPT_SSLCERT, $this->SSLCERT_PATH);
        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,&#39;PEM&#39;);
        curl_setopt($ch,CURLOPT_SSLKEY, $this->SSLKEY_PATH);

        //post提交方式
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            echo "curl出错,错误码:$error"."<br>";
            echo "<a href=&#39;http://curl.haxx.se/libcurl/c/libcurl-errors.html&#39;>错误原因查询</a></br>";
            curl_close($ch);
            return false;
        }
    }


}
?>
Copy after login

2. Call WeiXinPayToUser, as shown below Shown:

 /**
     * 企业付款测试
     */
    public function payToUser()
    {
   
        $mchPay = new WeiXinPayToUser();
        // 用户openid
        $mchPay->setParameter(&#39;openid&#39;, &#39;oy2lbszskklaslEKThrzqEziKEBzqU&#39;);
        // 商户订单号
        $mchPay->setParameter(&#39;partner_trade_no&#39;, &#39;test-&#39;.time());
        // 校验用户姓名选项
        $mchPay->setParameter(&#39;check_name&#39;, &#39;NO_CHECK&#39;);
        // 企业付款金额  单位为分
        $mchPay->setParameter(&#39;amount&#39;, 100);
        // 企业付款描述信息
        $mchPay->setParameter(&#39;desc&#39;, &#39;开发测试&#39;);
        // 调用接口的机器IP地址  自定义
        $mchPay->setParameter(&#39;spbill_create_ip&#39;, &#39;127.0.0.1&#39;); # getClientIp()
        // 收款用户姓名
        // $mchPay->setParameter(&#39;re_user_name&#39;, &#39;Max wen&#39;);
        // 设备信息
        // $mchPay->setParameter(&#39;device_info&#39;, &#39;dev_server&#39;);

        $response = $mchPay->postXmlSSL();
        if( !empty($response) ) {
            $data = simplexml_load_string($response, null, LIBXML_NOCDATA);
            echo json_encode($data);
        }else{
            echo json_encode( array(&#39;return_code&#39; => &#39;FAIL&#39;, &#39;return_msg&#39; => &#39;transfers_接口出错&#39;, &#39;return_ext&#39; => array()) );
        }
    }
}
Copy after login

Related recommendations:

WeChat development enterprise payment PHP code implementation

Code example of how to implement WeChat enterprise payment to users in php

The above is the detailed content of How to implement WeChat enterprise payment in PHP (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template