Development is now very hot, this article describes the PHP development to achieve the refund function. Share to everyone for your reference, hope to help everyone.
Specific as follows:
Recently in the transfer refund interface, found that there are many pits, and more people to share a bit
① If you are testing, the Web page suggests that Curl 58 indicates a problem with the path to the certificate (the physical path, the absolute path, is required)
② Web page Tip Curl 52 indicates that your certificate introduction is less, on the official demo only two certificates Apiclient_cert.pem and APICLIENT_KEY.PEM you also need to introduce a certificate ROOTCA.PEM, this certificate requires you to log on to
Your merchant platform to download
③ If the page prompts "http/1.1-ok Server:nginx date:tue, June 08:08:01 GMT Content-type:text/plain content-length:852 C Onnection:keep-alive keep-alive:timeout=8
Explain your code is not a problem, it should be the parameter error I was wrong before the place is the Out_refund_no and Out_trade_no write the same, the official web is also written, but there is a problem, I put out_refund_no modified a bit to find out
Note: The unit of the amount is divided into
Here's the interface I wrote.
function Home_index () {Date_default_timezone_set ("Asia/shanghai"); $date = Date ("Ymdhis"); $appid = ""; $mch _id = ""; $out _trade_no = "14487658021497944120"; $op _user_id = ""; $out _refund_no = $date; $total _fee = "500"; $refund _fee = "$";//$transaction _id = "4009542001201706206596667604"; $key = ""; $nonce _str = Noncestr (); $ref = Strtoupper (MD5 ("appid= $appid &mch_id= $mch _id&nonce_str= $nonce _str&op_user_id= $op _user_id". "&out_refund_no= $out _refund_no&out_trade_no= $out _trade_no&refund_fee= $refund _fee&total_fee=$ Total_fee ". "&key= $key")); Sign encryption MD5 $refund = Array (' appid ' = = $appid,//Application ID, fixed ' mch_id ' + $mch _id,//merchant number, fixed ' nonce_str ' = $nonce _ STR,//random string ' op_user_id ' = $op _user_id,//Operator ' out_refund_no ' + $out _refund_no,//merchant Internal unique refund number ' out_trade_no ' =& Gt $out _trade_no,//Merchant order number, PAY_SN code 1.1 Two Select one, generate the order number, in the payment notice there is return//' transaction_id ' = ' 1 ',//order number 1.2 Two select one, merchant side pass the order number ' Refund_ Fee ' = $refund _fee,//Refund Amount ' total_fee ' = + $total _fee,//Total amount ' sign ' + $ref//signature); $url = "Https://api.mch.weixin.qq.com/secapi/pay/refund"; ; Refund address, POST request $xml = Arraytoxml ($refund); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_header, 1); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_ssl_verifypeer, 1); Certificate Check if ($useCert = = true) {//Set certificate curl_setopt ($ch, Curlopt_sslcerttype, ' Pem '); curl_setopt ($ch, Curlopt_sslcert, DirName (__file__). '/wxpay/cert/apiclient_cert.pem '); curl_setopt ($ch, Curlopt_sslcerttype, ' Pem '); curl_setopt ($ch, Curlopt_sslkey, DirName (__file__). '/wxpay/cert/apiclient_key.pem '); curl_setopt ($ch, Curlopt_sslcerttype, ' Pem '); curl_setopt ($ch, Curlopt_cainfo, DirName (__file__). '/wxpay/cert/rootca.pem '); } curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, $xml); $xml = curl_exec ($ch); Return results 0 can only indicate that the program is normal return does not necessarily indicate the success of the refund if ($xml) {curl_close ($ch); // Convert XML to an array of libxml_disable_entity_loader (true); $xmlstring = simplexml_load_string ($xml, ' simplexmlelement ', libxml_nocdata);//Var_dump ($xmlstring); $result [' errnum '] = 0; $result [' info '] = Object_to_array ($xmlstring);//Var_dump ($result); return $result; } else {$error = Curl_errno ($ch); Curl_close ($ch); The error code is returned when the error occurred. $result [' errnum '] = $error; return $result; }}function Arraytoxml ($arr) {$xml = "<root>"; foreach ($arr as $key = + $val) {if (Is_array ($val)) {$xml. = "<". $key. ">". Arraytoxml ($val). "</". $key. ">"; } else {$xml. = "<". $key. ">". $val. "</". $key. ">"; }} $xml. = "</root>"; return $xml;} function Object_to_array ($obj) {$obj = (array) $obj; foreach ($obj as $k = + $v) {if (GetType ($v) = = ' Resource ') {return; } if (GetType ($v) = = ' object ' | | GetType ($v) = = ' array ') {$obj [$k] = (array) object_to_array ($v); }} return $obj;} function Noncestr () {$chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; $length = 32; for ($i = 0; $i < $length; $i + +) {$str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); }//random string return $str;} Home_index ();
Have you learned, quickly add to your project.