PHP implements the WeChat refund application process instance code and refund process

Source: Internet
Author: User

PHP implements refund application process instance code and refund process

I have discussed how to implement payment. For details, refer to the blog post: PHP implementation of the payment (jsapi payment) process and the implementation of the payment (jsapi payment) process in ThinkPHP. Due to business needs, a refund is required. After research and exploration, the refund is finally completed.

Preparations:

Of course, the payment is done. Otherwise, the official demo is used this time. Of course, there may also be many great gods on the Internet who have rewritten and encapsulated the demo. It may be easier to use and simplify, but I still do not advocate the use of the demo for the following reasons:

(1) It may not be fully functional. Maybe it only implements payment, but you may need to apply for a refund, query a refund, query an order, and revoke an order, if you rely on the convenience of the SDK, and have new business needs, You will be forced;

(2) For security considerations, money is required for payment, which must be extremely secure. Although I spoke about the official SDK, it will at least be relatively secure. I will rewrite it again. Although I haven't seen any problems yet, it won't be good if there is a vulnerability.

This article uses the most important class file WxPay in the official SDK. api. the refund () method provided in php is implemented in WxPay. api. the code for line 2 of the PHP file is as follows:

/***** Apply for a refund, at least one out_trade_no, transaction_id in outputs and * required, total_fee, refund_fee, and op_user_id are required. * appid, mchid, delimiter, nonce_str, and * @ param limit $ inputObj *@ param int $ timeOut * @ throws WxPayException * @ return returns if return is successful, other throwing exceptions */public static function refund ($ inputObj, $ timeOut = 6) {$ url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; // detect required parameters if (! $ InputObj-> IsOut_trade_noSet ()&&! $ InputObj-> IsTransaction_idSet () {throw new WxPayException ("in the refund request interface, set out_trade_no and transaction_id to at least one! ");} Else if (! $ InputObj-> IsOut_refund_noSet () {throw new WxPayException ("the required parameter out_refund_no is missing in the refund request interface! ");} Else if (! $ InputObj-> IsTotal_feeSet () {throw new WxPayException ("the required parameter total_timeout is missing in the refund request interface! ");} Else if (! $ InputObj-> IsRefund_feeSet () {throw new WxPayException ("the required parameter refund_refund is missing in the refund request interface! ");} Else if (! $ InputObj-> IsOp_user_idSet () {throw new WxPayException ("the required parameter op_user_id is missing in the refund request interface! ") ;}$ InputObj-> SetAppid (WxPayConfig: APPID); // public account ID $ inputObj-> SetMch_id (WxPayConfig: MCHID ); // merchant ID $ inputObj-> SetNonce_str (self: getNonceStr (); // random string $ inputObj-> SetSign (); // signature $ xml = $ inputObj-> ToXml (); $ startTimeStamp = self: getMillisecond (); // request start time $ response = self: postXmlCurl ($ xml, $ url, true, $ timeOut); $ result = WxPayResults: Init ($ response); self: reportCostTime ($ url, $ startTimeStamp, $ result ); // request reporting time return $ result ;}

The official method clearly states the required parameters, and some required parameter sdks have already helped us complete them. I will re-encapsulate this method to facilitate calls in the project:

/*** Refund * @ param string $ order_id order ID * @ return (array type), other throwing exceptions */function wxRefund ($ order_id) {// put my SDK In the Api directory under the project root directory require_once APP_ROOT. "/Api/wxpay/lib/WxPay. api. php "; // query the order and refund the order according to the data in the order $ order = M ('order')-> where (array ('id' => $ order_id, 'Is _ refund '=> 2, 'order _ status' => 1)-> find (); $ merchid = WxPayConfig: MCHID; if (! $ Order) return false; $ input = new WxPayRefund (); $ input-> SetOut_trade_no ($ order ['order _ sn ']); // your own order number $ input-> SetTransaction_id ($ order ['transaction _ id']); // The order sequential number officially generated, $ input-> SetOut_refund_no (getrand_num (true) is returned when the payment is successful; // refund Ticket No. $ input-> settotal_amount ($ order ['total _ price']); // order price, in the unit of $ input-> setrefund_refund ($ order ['total _ price']); // total refund amount, total order amount, in the unit of points, it can only be an integer $ input-> SetOp_user_id ($ merchid); $ result = WxPayApi: refund ($ input ); // refund operation // file_put_contents is used to view the refund result returned by the server. After the test is complete, you can delete it. // file_put_contents (APP_ROOT. '/Api/wxpay/logs/log3.txt', arrayToXml ($ result), FILE_APPEND); return $ result ;}

I need to speak out here, not to mention the type of the returned value. XML data is returned during payment. Here, an array is returned, which makes me unprepared. Haha, but it is better to return an array, you can directly determine the processing.

Method call is simpler:

// Refund $ result = wxRefund ($ order_id); // The file_put_contents statement is used to view the refund result returned by the server. You can delete it after the test. // file_put_contents (APP_ROOT. '/Api/wxpay/logs/log4.txt', arrayToXml ($ result), FILE_APPEND); if ($ result ['Return _ Code'] = 'success ') & ($ result ['result _ Code'] = 'success ')) {// refund successful} else if ($ result ['Return _ Code'] = 'fail ') | ($ result ['result _ Code'] = 'fail ')) {// refund failed // cause $ reason = (empty ($ result ['err _ code_des '])? $ Result ['Return _ msg ']: $ result ['err _ code_des']);} else {// Failed}

If the refund is successful, the following is returned:

The test is correct: This is integrated with the official SDK. If you do not use the SDK, you can use a simpler method. For details, see: PHP implementation of payment (jsapi payment) and refund (no need to integrate the payment SDK)

Summary

The above is an example code of PHP implementation refund application process introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.