Teach you to use PHP development to achieve WeChat refund function

Source: Internet
Author: User
This article explains the knowledge about using PHP to develop a refund function.

This article describes the PHP development to achieve the refund function. Share to everyone for your reference, 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 = "500"; $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 ' + $ou T_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,//TotalAmount ' 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, dir Name (__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 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);//error when the error code is returned. $result [' errnum '] = $error; return $result; }} function Arraytoxml ($arr) {$xml = ""; foreach ($arr as $key = + $val) {if (Is_array ($val)) {$xml. = "<". $key. ">". Arraytoxml ($val). "{c}<!--"? $key--"; } else {$xml. = "<". $key. ">". $val. "{c}<!--"? $key--"; }} $xml. = ""; return $xml; } function Object_to_array ($obj) {$obj = (array) $obj; foreach ($obj as $k + = $v) {if (GetType ($v) = = ' Resource ') {R Eturn; } 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 ();

This article explains the use of PHP development to realize the refund function of the relevant knowledge, more relevant content please focus on the PHP Chinese web.

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.