


How to implement WeChat enterprise account payment for individuals in PHP
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 )
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; }
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, '8.8.8.8'); //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,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT,$cert); //默认格式为PEM,可以注释 curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); 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; } }
Enterprise payment to individual
//企业向个人付款 public function payToUser($params, $key, $apicent_cert, $apiclient_key) { $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //检测必填参数 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"] == 'FORCE_CHECK' or $params["check_name"] == 'OPTION_CHECK') && ($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['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'] == '::1' ? '192.127.1.1' : $_SERVER['REMOTE_ADDR'];//获取IP $params["sign"] = $this->getSign($params, $key);//签名 $xml = $this->arrayToXml($params); return $this->postXmlSSLCurl($xml, $url, false, $apicent_cert, $apiclient_key); }
Enterprise payment
private function _enterprisePay($number, $member_id, $amount, $desc) { // 获取openid $wxuser_id = M('Member')->where(array('id' => $member_id))->getField('wxuser_id'); $openid = M('Wxuser')->where(array('id' => $wxuser_id))->getField('openid'); $pay = C('PAY_WEIXIN'); import('@.Action.WxDevelop'); $enterprise = new WxEnterprise($pay['appid'], $pay['appsecret'], $pay['mchid']); $params = array( 'partner_trade_no' => $number, 'openid' => $openid, 'check_name' => 'NO_CHECK', 'amount' => $amount, // 总计 'desc' => $desc, ); $result = $enterprise->payToUser($params, $pay['key'], $pay['apiclient_cert'], $pay['apiclient_key']); return $result; }
Processing distributor withdrawals
private function _handle($truename, $price) { // 处理分销商提现 $withdrawid = date("ymdHis") . strval(rand(1000, 9999)); $data = array('withdrawid' => $withdrawid, 'store_id' => $this->store_id, 'member_id' => $this->member_id, 'truename' => $truename, 'price' => $price, 'addtime' => time()); //免审核 if ($price >= C('withdraw_uncheck_value')) { $data['need_check'] = 0; $data['status'] = 1; if ($this->withdrawModel->add($data)) { $result = $this->_enterprisePay($withdrawid, $this->member_id, $price * 100, '分销商(' . $truename . ')提现'); //遇到支付信息出错,转为需审核提现 if ($result['return_code'] != 'SUCCESS') { $this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('need_check' => 1, 'status' => 0)); $this->assign('success', 2); } else { //设置微信交易号 $this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('payment_no' => $result['payment_no'])); //增加佣金流水,待修复 $data = array('store_id' => $this->store_id, 'user_type' => 2, 'user_id' => $this->shop_id, 'trade_type' => 2, 'trade_no' => $withdrawid, 'price' => -$price, 'status'=> 1, 'message' => $truename.'提现', 'addtime' => time()); M('Twitter_log')->add($data); //减少相应可提佣金 M('Member')->where(array('id' => $this->member_id))->setInc('money', -$price); $this->assign('success', 1); //发送佣金变动消息 import('@.Action.Tmplmsg'); $tmplmsg = new Tmplmsg(); $tmplmsg->send(Tmplmsg::PRICE_CHANGE, $this->member_id, array('token' => $this->token, 'intro' => '分销佣金提现转出', 'price' => $price, 'business' => BUSINESS)); } } else { $this->error('提现信息错误!'); } } //需要审核 else { $this->withdrawModel->add($data); $this->assign('success' , 2); } }
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>
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>
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>
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!

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools
