Home Backend Development PHP Tutorial How to implement WeChat enterprise account payment for individuals in PHP

How to implement WeChat enterprise account payment for individuals in PHP

Jun 21, 2018 pm 02:35 PM
php personal WeChat pay

This article mainly introduces the method of using PHP to implement WeChat enterprise account payment for individuals, and analyzes in detail the payment configuration, certificate usage and payment process-related operating techniques of PHP WeChat enterprise account. Friends in need can refer to the following

This article describes the example of how to use PHP to implement WeChat enterprise account payment for individuals. Share it with everyone for your reference, the details are as follows:

Introduction: Distributors, how to withdraw cash from micro-business?

Pay directly with WeChat.

The implementation is as follows:

WeChat payment configuration

/*微信支付*/
  'PAY_WEIXIN'        => array(
    'appid'         => 'XXXX',
    'appsecret'       => 'XXXXXXX',
    'mchid'         => '1283301801',                        //商户号
    'key'          => 'zhudianbaodiandodozhudianbao0527',             //商户支付秘钥
    'apiclient_cert'    => 'Conf/cert/apiclient_cert.pem',               //商户证书apiclient_cert.pem
    'apiclient_key'     => 'Conf/cert/apiclient_key.pem',                //商户证书apiclient_key.pem
  )
Copy after login

arrayToXml

/**
* 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;
}
Copy after login

Use the certificate to submit xml to the corresponding Interface url

/**
*  作用:使用证书,以post方式提交xml到对应的接口url
*/
function postXmlSSLCurl($xml, $url, $second, $cert, $key)
{
    $ch = curl_init();
    //超时时间
    curl_setopt($ch,CURLOPT_TIMEOUT,$second ? $second : $this->timeout);
    //这里设置代理,如果有的话
    //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);
    //设置证书
    //使用证书:cert 与 key 分别属于两个.pem文件
    //默认格式为PEM,可以注释
    curl_setopt($ch,CURLOPT_SSLCERTTYPE,&#39;PEM&#39;);
    curl_setopt($ch,CURLOPT_SSLCERT,$cert);
    //默认格式为PEM,可以注释
    curl_setopt($ch,CURLOPT_SSLKEYTYPE,&#39;PEM&#39;);
    curl_setopt($ch,CURLOPT_SSLKEY, $key);
    //post提交方式
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
    $data = curl_exec($ch);
    //返回结果
    if($data){
      curl_close($ch);
      return $this->xmlToArray($data);
    }
    else {
      $error = curl_errno($ch);
      echo "curl出错,错误码:$error"."<br>";
      curl_close($ch);
      return false;
    }
}
Copy after login

Enterprise payment to individual

//企业向个人付款
public function payToUser($params, $key, $apicent_cert, $apiclient_key) {
    $url = &#39;https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers&#39;;
    //检测必填参数
    if($params["partner_trade_no"] == null) {  //
      exit("退款申请接口中,缺少必填参数partner_trade_no!"."<br>");
    }elseif($params["openid"] == null){
      exit("退款申请接口中,缺少必填参数openid!"."<br>");
    }elseif($params["check_name"] == null){       //NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
      exit("退款申请接口中,缺少必填参数check_name!"."<br>");
    }elseif(($params["check_name"] == &#39;FORCE_CHECK&#39; or $params["check_name"] == &#39;OPTION_CHECK&#39;) && ($params["re_user_name"] == null)){ //收款用户真实姓名。
      exit("退款申请接口中,缺少必填参数re_user_name!"."<br>");
    }elseif($params["amount"] == null){
      exit("退款申请接口中,缺少必填参数amount!"."<br>");
    }elseif($params["desc"] == null){
      exit("退款申请接口中,缺少必填参数desc!"."<br>");
    }
    $params["mch_appid"] = $this->appid;//公众账号ID
    $params["mchid"] = $this->mchid;//商户号
    $params["nonce_str"] = $this->createNoncestr();//随机字符串
    $params[&#39;spbill_create_ip&#39;] = $_SERVER[&#39;REMOTE_ADDR&#39;] == &#39;::1&#39; ? &#39;192.127.1.1&#39; : $_SERVER[&#39;REMOTE_ADDR&#39;];//获取IP
    $params["sign"] = $this->getSign($params, $key);//签名
    $xml = $this->arrayToXml($params);
    return $this->postXmlSSLCurl($xml, $url, false, $apicent_cert, $apiclient_key);
}
Copy after login

Enterprise payment

private function _enterprisePay($number, $member_id, $amount, $desc)
{
    // 获取openid
    $wxuser_id = M(&#39;Member&#39;)->where(array(&#39;id&#39; => $member_id))->getField(&#39;wxuser_id&#39;);
    $openid  = M(&#39;Wxuser&#39;)->where(array(&#39;id&#39; => $wxuser_id))->getField(&#39;openid&#39;);
    $pay = C(&#39;PAY_WEIXIN&#39;);
    import(&#39;@.Action.WxDevelop&#39;);
    $enterprise = new WxEnterprise($pay[&#39;appid&#39;], $pay[&#39;appsecret&#39;], $pay[&#39;mchid&#39;]);
    $params = array(
      &#39;partner_trade_no&#39; => $number,
      &#39;openid&#39; => $openid,
      &#39;check_name&#39; => &#39;NO_CHECK&#39;,
      &#39;amount&#39; => $amount, // 总计
      &#39;desc&#39; => $desc,
    );
    $result = $enterprise->payToUser($params, $pay[&#39;key&#39;], $pay[&#39;apiclient_cert&#39;], $pay[&#39;apiclient_key&#39;]);
    return $result;
}
Copy after login

Processing distributor withdrawals

private function _handle($truename, $price) { // 处理分销商提现
    $withdrawid = date("ymdHis") . strval(rand(1000, 9999));
    $data = array(&#39;withdrawid&#39; => $withdrawid, &#39;store_id&#39; => $this->store_id, &#39;member_id&#39; => $this->member_id, &#39;truename&#39; => $truename, &#39;price&#39; => $price, &#39;addtime&#39; => time());
    //免审核
    if ($price >= C(&#39;withdraw_uncheck_value&#39;)) {
      $data[&#39;need_check&#39;] = 0;
      $data[&#39;status&#39;] = 1;
      if ($this->withdrawModel->add($data)) {
        $result = $this->_enterprisePay($withdrawid, $this->member_id, $price * 100, &#39;分销商(&#39; . $truename . &#39;)提现&#39;);
        //遇到支付信息出错,转为需审核提现
        if ($result[&#39;return_code&#39;] != &#39;SUCCESS&#39;) {
          $this->withdrawModel->where(array(&#39;withdrawid&#39; => $withdrawid))->save(array(&#39;need_check&#39; => 1, &#39;status&#39; => 0));
          $this->assign(&#39;success&#39;, 2);
        }
        else {
          //设置微信交易号
          $this->withdrawModel->where(array(&#39;withdrawid&#39; => $withdrawid))->save(array(&#39;payment_no&#39; => $result[&#39;payment_no&#39;]));
          //增加佣金流水,待修复
          $data = array(&#39;store_id&#39; => $this->store_id, &#39;user_type&#39; => 2, &#39;user_id&#39; => $this->shop_id, &#39;trade_type&#39; => 2, &#39;trade_no&#39; => $withdrawid, &#39;price&#39; => -$price, &#39;status&#39;=> 1, &#39;message&#39; => $truename.&#39;提现&#39;, &#39;addtime&#39; => time());
          M(&#39;Twitter_log&#39;)->add($data);
          //减少相应可提佣金
          M(&#39;Member&#39;)->where(array(&#39;id&#39; => $this->member_id))->setInc(&#39;money&#39;, -$price);
          $this->assign(&#39;success&#39;, 1);
          //发送佣金变动消息
          import(&#39;@.Action.Tmplmsg&#39;);
          $tmplmsg = new Tmplmsg();
          $tmplmsg->send(Tmplmsg::PRICE_CHANGE, $this->member_id, array(&#39;token&#39; => $this->token, &#39;intro&#39; => &#39;分销佣金提现转出&#39;, &#39;price&#39; => $price, &#39;business&#39; => BUSINESS));
        }
      }
      else {
        $this->error(&#39;提现信息错误!&#39;);
      }
    }
    //需要审核
    else {
      $this->withdrawModel->add($data);
      $this->assign(&#39;success&#39; , 2);
    }
}
Copy after login

Provides the function of enterprise payment to users and supports enterprises Pay through the API interface, or operate the payment through the WeChat Pay merchant platform web function.

Warm reminder:

◆ For payments to the same real-name user, the single daily limit is 2W/2W
◆ For payments to the same non-real-name user, the single The daily limit for a single transaction is 2000/2000
◆ The total payment limit for a merchant on the same day is 100W
◆ Only the APPID bound to the merchant account is supported;
◆ For the target users of the payment, users who have been authenticated by WeChat Pay real-name It can provide the function of verifying the real name. Users without real-name authentication cannot verify. Enterprises can choose the verification type according to the security level of their own business;
◆ The payment amount must be less than or equal to the amount of the merchant's current available balance;
◆ For paid records, enterprises can view corresponding data through enterprise payment query.

Arrived

The payment funds will enter the target user’s change (WeChat-Me-Wallet-Change). WeChat Pay will notify you of the change being credited into your account, and the change receipt and expenditure details will display corresponding records.

Warm reminder:

For the historical client version without change account, the funds will enter the user's red envelope account. There is no message from WeChat Pay to notify the user, and the enterprise can choose to reach the user on its own.

Interface link: https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers

Whether a certificate is required
The request requires a two-way certificate.

Data example:

<xml>
<mch_appid>wxe062425f740c30d8</mch_appid>
<mchid>10000098</mchid>
<nonce_str>3PG2J4ILTKCH16CQ2502SI8ZNMTM67VS</nonce_str>
<partner_trade_no>100000982014120919616</partner_trade_no>
<openid>ohO4Gt7wVPxIT1A9GjFaMYMiZY1s</openid>
<check_name>OPTION_CHECK</check_name>
<re_user_name>张三</re_user_name>
<amount>100</amount>
<desc>节日快乐!</desc>
<spbill_create_ip>10.2.3.10</spbill_create_ip>
<sign>C97BDBACF37622775366F38B629F45E3</sign>
</xml>
Copy after login

Successful example:

<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[]]></return_msg>
<mch_appid><![CDATA[wxec38b8ff840bd989]]></mch_appid>
<mchid><![CDATA[10013274]]></mchid>
<device_info><![CDATA[]]></device_info>
<nonce_str><![CDATA[lxuDzMnRjpcXzxLx0q]]></nonce_str>
<result_code><![CDATA[SUCCESS]]></result_code>
<partner_trade_no><![CDATA[10013574201505191526582441]]></partner_trade_no>
<payment_no><![CDATA[1000018301201505190181489473]]></payment_no>
<payment_time><![CDATA[2015-05-19 15:26:59]]></payment_time>
</xml>
Copy after login

Error example:

<xml>
<return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg>
<result_code><![CDATA[FAIL]]></result_code>
<err_code><![CDATA[SYSTEMERROR]]></err_code>
<err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des>
</xml>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s learning Help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

PHP implements WeChat public platform enterprise account verification interface

How to use PHP to export data to Taobao Assistant CSV

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

gateio Chinese official website gate.io trading platform website gateio Chinese official website gate.io trading platform website Feb 21, 2025 pm 03:06 PM

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

See all articles