Use PHP to realize WeChat refund function

Source: Internet
Author: User
Tags using git
This time to share a small function, many people need to use PHP to achieve the refund function, the code is as follows, can be used directly, but note that units are divided into

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  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 ();


Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!

Related reading:

PHP deployment Environment using GIT

Some use cases of git

JavaScript data types and git use code explained

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.