This article illustrates the method of php to realize the payment of individuals with WeChat enterprise accounts. , the details are as follows:
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);}