This article mainly introduces the implementation of PHP enterprise number of personal payment method, detailed analysis of the PHP enterprise number of payment configuration, certificate use and payment process related operating skills, the need for friends can refer to the next
The example of this article describes how to implement the Enterprise number payment method in PHP. Share to everyone for your reference, as follows:
Introductory Remarks: Distributors, micro-business withdrawals how to mention?
Directly with payment.
The implementation is as follows:
Payment Configuration
/* Pay * /' pay_weixin ' + Array ( ' appid ' = ' XXXX ', ' appsecret ' = ' XXXXXXX ' , ' Mchid ' = ' 1283301801 ', //Merchant number ' key ' = ' zhudianbaodiandodozhudianbao0527 ', //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;}
Submit XML to the corresponding interface URL using a certificate, post
/*** function: Use the certificate, post the XML to the corresponding interface url*/function postxmlsslcurl ($xml, $url, $second, $cert, $key) {$ch = Curl_init (); Time-out curl_setopt ($ch, Curlopt_timeout, $second? $second: $this->timeout); Here set the agent, if any,//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); Requires the result to be a string and output to the screen curl_setopt ($ch, curlopt_returntransfer,true); Set certificate//Use Certificate: Cert and key belong to two. Pem files//default format is PEM, can be annotated curl_setopt ($ch, Curlopt_sslcerttype, ' PEM '); curl_setopt ($ch, Curlopt_sslcert, $cert); The default format is PEM, which can be annotated with curl_setopt ($ch, Curlopt_sslkeytype, ' PEM '); curl_setopt ($ch, Curlopt_sslkey, $key); POST Submission Mode curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_postfields, $xml); $data = curl_exec ($ch); Returns the 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; }}
Enterprise payments to individuals
Enterprise to personal payment public function Paytouser ($params, $key, $apicent _cert, $apiclient _key) {$url = ' https://api.mch.weixin.qq.co M/mmpaymkttransfers/promotion/transfers '; Check for Required parameters if ($params ["partner_trade_no"] = = NULL) {//exit ("Missing required parameters in the refund request interface partner_trade_no! "." <br> "); }elseif ($params ["OpenID"] = = null) {exit ("Missing required parameters in the refund request interface openid! "." <br> "); }elseif ($params ["check_name"] = = null) {//no_check: Do not verify real name Force_check: Strong verification of real names (unauthenticated users will fail to verify, unable to transfer) Option_ Check: For the authenticated user is verified real name (non-real names authenticated users do not check, can be transferred successfully) exit ("Missing required parameters in the refund request interface check_name! "." <br> "); }elseif ($params ["check_name"] = = ' Force_check ' or $params ["check_name"] = = ' Option_check ') && ($params ["Re_ User_name "] = = null)) {//beneficiary user's real name. Exit ("Missing required parameters in the refund request interface re_user_name! "." <br> "); }elseif ($params ["amount"] = = null) {exit ("Missing required parameters in the refund request interface amount! "." <br> "); }elseif ($params ["desc"] = = null) {exit ("Missing required parameters in the refund request interface desc! "." <br> "); } $params ["mch_appid"] = $this->appid;//Public Account ID $params ["mchid"] = $this->mchid;//merchant number $params ["nonce_str"] = $this->createnoncestr ();//random character 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, $apicent _cert, $apiclient _key);}
Corporate Payments
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;}
Handling of Reseller Withdrawals
Private Function _handle ($truename, $price) {//processing reseller Withdrawal $withdrawid = Date ("Ymdhis"). Strval (rand (1000, 9999)); $data = Array (' withdrawid ' = $withdrawid, ' store_id ' = = $this->store_id, ' member_id ' = = $this->member_id , ' Truename ' and $truename, ' price ' and $price, ' addtime ' = Time ()); Audit-free 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, ' Reseller ('. $truename. '); An error occurred when the payment information was encountered, to be audited for withdrawal if ($result [' return_code ']! = ' SUCCESS ') {$this->withdrawmodel->where (' Wit Hdrawid ' + $withdrawid))->save (Array (' need_check ' = 1, ' status ' = 0)); $this->assign (' Success ', 2); } else {//SET transaction number $this->withdrawmodel->where (' withdrawid ' = $withdrawid))->sav E (Array (' PayMent_no ' = $result [' payment_no ']); Added Commission water, to be repaired $data = Array (' store_id ' = = $this->store_id, ' user_type ' = 2, ' user_id ' + $this->sh op_id, ' Trade_type ' and 2, ' trade_no ' and $withdrawid, ' price ' and '-$price, ' status ' and ' 1, ' message ' + $truena Me. ' Withdraw ', ' addtime ' + Time ()); M (' Twitter_log ')->add ($data); Reduce the corresponding Commission M (' Member ')->where (array (' id ' = $this->member_id))->setinc (' Money ',-$price); $this->assign (' success ', 1); Send the Commission change message import (' @. Action.tmplmsg '); $tmplmsg = new Tmplmsg (); $tmplmsg->send (tmplmsg::P rice_change, $this->member_id, Array (' token ' = $this->token, ' intro ' = ') Distribution Commission withdrawal ', ' price ' and $price, ' business ' = business]); }} else {$this->error (' Withdrawal information error! '); }}//need to audit else {$this->withdrawmodel->add ($data); $this->assign (' Success ', 2); }}
Provides the ability of the enterprise to pay the user, support the enterprise through the API interface payment, or through the Payment Merchant Platform webpage function operation payment.
Warm tips:
Pay for the same real-name user with a single daily limit of 2w/2w
Pay for the same non-real-name user with a single daily limit of 2000/2000
One merchant same day total payment limit 100W
Supports only AppID that the merchant number is bound to;
For the target users of the payment, the user who has paid real name authentication can provide the function of verifying the real names, the user who is not verified by the real certificate cannot verify, the enterprise can choose the authentication type according to its own business security level;
Payment amount must be less than or equal to the amount of the current available balance of the merchant;
Records that have been paid, the enterprise can view the data through the Enterprise payment query.
To account
The payment funds will go to the target user's change (-i-purse-change). The payment will be made in the notice of change, the details of the change will show the corresponding record.
Warm tips:
For the history client version without change account, the funds will enter the user's red envelope account, pay no message to notify the user, the enterprise can choose to reach the user by themselves.
Interface Link: https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers
Whether a certificate is required
The request requires a two-way certificate.
Data examples:
<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> Zhang San </re_user_name><amount>100</amount><desc> Happy Holidays !</desc><spbill_create_ip>10.2.3.10</spbill_create_ip><sign> C97bdbacf37622775366f38b629f45e3</sign></xml>
Success 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[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>
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!