Speaking of WeChat Pay, I believe everyone is familiar with it, but when we want to complete this program, how should our programmers write and set it up? Don’t worry, today the editor of the new technology channel of the bug has brought you an example, let’s go to the following article to learn more!
Introduction: Distributor, how to withdraw cash from WeChat business?
Pay directly with WeChat.
Implementation is as follows:
WeChat Payment Configuration
/*WeChat Payment*/ 'PAY_WEIXIN' => array( 'appid' => 'XXXX', 'appsecret' => 'XXXXXX, 'mchid' => '1283301801', //Merchant number 'key' => 'zhudianbaodiandozhudianbao0527', //Merchant payment key 'apiclient_cert' => 'Conf/cert/apiclient_cert.pem', //Merchant certificate apiclient_cert.pem 'apiclient_key' => 'Conf/cert/apiclient_key.pem', //Merchant certificate apiclient_key.pem )
arrayToXml
/*** array to 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 the xml to the corresponding interface url in post
/*** Function: Use a certificate to submit xml to the corresponding interface url*/function postXmlSSLCurl($xml, $url, $second, $cert, $key){ $ch = curl_init(); //Timeout time curl_setopt($ch,CURLOPT_TIMEOUT,$second ? $second : $this->timeout); //Set the proxy here, if there is one //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); //Set header curl_setopt($ch,CURLOPT_HEADER,FALSE); //Set the result as a string and output to the screen curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); //Set the certificate//Use the certificate: cert and key It belongs to two .pem files respectively //The default format is PEM, you can annotate curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT,$cert); //The default format is PEM, you can annotate curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY, $key); //The post submission method curl_setopt($ch,CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POSTFIELDS,$xml); $data = curl_exec($ch); //Return result if($data){ curl_close($ch); return $this->xmlToArray($data); } else { $error = curl_errno($ch); echo "curl error, error code: $error"."<br>"; curl_close($ch); return false; }}Businesses pay individuals
//The enterprise pays individual public function payToUser($params, $key, $apcent_cert, $apiclient_key) { $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //Detection required parameters if($params["partner_trade_no"] == null) { // exit("Refund application interface, the required parameters partner_trade_no!"."<br>"); }elseif($params["openid"] == null){ exit("Refund application interface, the required parameters openid!"."<br>"); }elseif($params["check_name"] == null){ //NO_CHECK: No real name verification FORCE_CHECK: Strong verification of the real name (users without real name authentication will fail to verify, and the transfer cannot be transferred) OPTION_CHECK: Only users who have real name authenticated are verified (users who have not real name authenticated do not verify, the transfer can be successfully transferred) exit("Refund application interface, the required parameter check_name is missing!"."<br>"); }elseif(($params["check_name"] == 'FORCE_CHECK' or $params["check_name"] == 'OPTION_CHECK') && ($params["re_user_name"] == null)){ //The real name of the payment user. exit("Refund application interface, the required parameter re_user_name is missing!"."<br>"); }elseif($params["amount"] == null){ exit("Refund application interface, the required parameter amount is missing!"."<br>"); }elseif($params["desc"] == null){ exit("Refund application interface, the required parameter desc!"."<br>"); } $params["mch_appid"] = $this->appid;//Public account ID $params["mchid"] = $this->mchid;//Merchant number $params["nonce_str"] = $this->createNoncestr();//Random string $params['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'] == '::1' ? '192.127.1.1' : $_SERVER['REMOTE_ADDR'];//Get IP $params["sign"] = $this->getSign($params, $key);//Signature $xml = $this->arrayToXml($params); return $this->postXmlSSLCurl($xml, $url, false, $apcent_cert, $apiclient_key);}Business payment
private function _enterprisePay($number, $member_id, $amount, $desc){ // Get 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, // Total 'desc' => $desc, ); $result = $enterprise->payToUser($params, $pay['key'], $pay['apiclient_cert'], $pay['apiclient_key']); return $result;}Process distributor withdrawals
private function _handle($truename, $price) { // Handle distributor withdrawal $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()); //No audit 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, 'Distribution(' . $truename . ') Withdraw'); //After an error in payment information, it needs to be reviewed if ($result['return_code'] != 'SUCCESS') { $this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('need_check' => 1, 'status' => 0)); $this->assign('success', 2); } else { //Set WeChat transaction number $this->withdrawModel->where(array('withdrawid' => $withdrawd))->save(array('payment_no' => $result['payment_no'])); //Increase commission flow, to be fixed $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.'Cash withdrawal', 'addtime' => time()); M('Twitter_log')->add($data); //Reduce the corresponding commissions M('Member')->where(array('id' => $this->member_id))->setInc('money', -$price); $this->assign('success', 1); //Send commission change messages import('@.Action.Tmplmsg'); $tmplmsg = new Tmplmsg(); $tmplmsg->send(Tmplmsg::PRICE_CHANGE, $this->member_id, array('token' => $this->token, 'intro' => 'Distribution commission withdrawal transfer', 'price' => $price, 'business' => BUSINESS)); } } else { $this->error('Cash withdrawal information error!'); } } //Review is required else { $this->withdrawModel->add($data); $this->assign('success' , 2); }}Provides the function of enterprises to pay to users, supports enterprises to pay through the API interface, or operate payment through the WeChat payment merchant platform web function.
Kind tips:
◆ Pay to the same real-name user, the daily limit for a single transaction is 2W/2W
◆ Payment to the same non-real user, with a single transaction and a single daily limit of 2000/2000
◆ The total payment limit for a merchant on the same day is 100W
◆ Only APPIDs that have been bound to the merchant number are supported;
◆ For the target users of payment, users who have been authenticated by WeChat Pay can provide the function of verifying their real names. Users who have not authenticated by real names cannot verify. Enterprises can choose the verification type based on the security level of their own business;
◆ The payment amount must be less than or equal to the amount currently available for the merchant;
◆ For paid records, enterprises can view the corresponding data through enterprise payment query.
Arrival
The payment funds will enter the target user's change (WeChat-I-Wallet-Change). WeChat Pay will make a notification of change in the account, and the change income and expenditure details will be displayed.
Kind tips:
For the historical client version of the change account, funds will enter the user's red envelope account, and WeChat Pay will notify users without any messages, and enterprises can choose to reach users by themselves.
Interface link: https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers
Is a certificate required
A two-way certificate is required for a request.
Data example:
<xml><mch_appid>wxe062425f740c30d8</mch_appid><mchid>10000098</mchid><nonce_str>3PG2J4ILTKCH16CQ2502SI8ZNMTM67VS</nonce_str><partner_trade_no>100000982014120919616</partner_trade_no><openid>ohO4Gt7wVPxIT1A9GjFaMYMiZ Y1s</openid><check_name>OPTION_CHECK</check_name><re_user_name>Zhang San</re_user_name><amount>100</amount><desc>Happy holiday!</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[lxuDzMnR jpcXzxLx0q]]></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[system is busy, please try again later.]]></return_msg><result_code><![CDATA[FAIL]]></result_code><err_code><![CDATA[SYSTEMERROR]]></err_code><err_code_des><![CDATA[system is busy, please try again later.]]></err_code_des></xml>
For more information about PHP related content, please check out the topics of this site: "Summary of PHP WeChat Development Skills", "Summary of PHP encoding and transcoding operation techniques", "Summary of PHP network programming techniques", "Introduction to PHP basic syntax tutorial", "Summary of php string usage", "Introduction to PHP database operation tutorial" and "Summary of common php database operation techniques"
I hope that the detailed explanation of the method of implementing WeChat enterprise account payment in PHP described in this article will be helpful to everyone to learn PHP programming. I hope everyone will continue to support the wrong new technology channel!