This time to bring you thinkphp to achieve payment (JSAPI payment) steps in detail, thinkphp realize payment (JSAPI payment) of the attention to what, the following is the actual case, together to see.
At that time, the environment is not using the framework, directly in a domain name to the bottom of the directory after the new directory to access the implementation of the directory, but applied to the framework, there are still some problems, in thinkphp, because the routing rules and payment authorization directory discrepancies, so will error. This article speaks about the process of integrating payments in TP.
Goose Factory out of the SDK and documentation, is to let you do not understand, hard around, this acid cool used to know. Shouldn't the documentation and SDK be as simple as possible? Is it only hard to refactor to show the Goose Factory program ape technology superb? The amount ... is not exposing my rookie properties ... In fact, the SDK is good to use, but also seen in the previous article, in the payment to complete the callback function, really let people around the dizzy.
For those who do not want to be around the official, want to use the TP to pay to see a big God himself in accordance with the official document refactoring streamlined to build a payment SDK for TP, the source I downloaded to read, the code is very elegant introduction, the process is very simple, easy to understand. See blog: http://baijunyao.com/article/78
I am still frowning, using the official SDK, but also successfully realized the payment, the following to share with you the process:
1.SDK Downloads and modifications
This is more than talk, do not know can look at my previous article: PHP implementation of the payment (JSAPI payment) process, the inside details the downloaded files which need to be modified.
2. Public number setting
A. Still need to set up a Web page authorization domain name, this is nothing special;
B. Here to pay attention to the payment authorization directory, using TP Many people are using the rewrite mode (rewrite mode) or while using the rewrite mode, using pseudo-static mode, the resulting link is: HTTP://SERVERNAME/HOME/BLOG/READ/ID/1;
If you are using the PathInfo mode, the generated link is: HTTP://SERVERNAME/INDEX.PHP/HOME/BLOG/READ/ID/1, such as in the Home module under the Blog controller in a method to pay, The authorization directory We are paying for should be http://serverName/Home/Blog/or http://serverName/index.php/Home/Blog/, depending on the URL pattern of your own TP settings.
3. Payment Process
(1) Unification of orders
The payment parameter configuration of the next order, which is the same as the previous one, the key note is to pay the callback verification link, because to be called multiple times, I directly in the application/common/common/ The parameter configuration is encapsulated in function.php, and my SDK is placed in the API directory under the project root, so the SDK is not used when the vendor function is introduced.
/** * Payment * @param string $openId openId * @param string $goods Product Name * @param string $attach Additional parameters, we can choose to pass a parameter, such as order I D * @param string $order _sn Order number * @param string $total _fee amount */function Wxpay ($openId, $goods, $order _sn, $total _fee, $at Tach) {require_once app_root. /api/wxpay/lib/wxpay.api.php "; Require_once app_root. " /api/wxpay/payment/wxpay.jsapipay.php "; Require_once App_root. ' /api/wxpay/payment/log.php '; Initialize log $logHandler = new Clogfilehandler (app_root. ") /api/wxpay/logs/". Date (' y-m-d '). Log '); $log = Log::init ($logHandler, 15); $tools = new Jsapipay (); if (empty ($openId)) $openId = $tools->getopenid (); $input = new Wxpayunifiedorder (); $input->setbody ($goods); Commodity name $input->setattach ($attach); Additional parameters, can be filled, fill in the words, inside the string can not appear space $input->setout_trade_no ($order _sn); Order number $input->settotal_fee ($total _fee); Payment amount, Unit: $input->settime_start (Date ("Ymdhis")); Payment Initiation $input->settime_expire (Date ("Ymdhis", Time () + 600))//Payment Timeout $input-&Gt Setgoods_tag ("Test3"); $input->setnotify_url ("http://". $_server[' Http_host '). " /payment.php "); The payment callback verifies the address $input->setnotify_url ("http://". $_server[' Http_host '). " /payment.php/wexinapi/weixinpay/notify "); $input->settrade_type ("JSAPI"); Payment type $input->setopenid ($openId); User OpenID $order = wxpayapi::unifiedorder ($input); Uniform Order $jsApiParameters = $tools->getjsapiparameters ($order); return $jsApiParameters; }
Attention, attention, knocking the blackboard to the point:
Payment callback verification link, must not have permission to verify, if you access that link yourself, you also need to login registration verification, do not try, you must have access to accessible links, and do not have a series of parameters to pass.
The best is the simple rough http://serverName/xxx.php, I am in the directory, similar to index.php, re-wrote a special for the payment callback of the entry file payment.php, and it corresponds to the application/ Modules (WEXINAPI), controllers (Weixinpay) and methods (notify) in the directory:
Detects the PHP environment if (Version_compare (php_version, ' 5.3.0 ', ' < ') die (' Require PHP > 5.3.0! '); $_get[' m ']= ' Admin '; Open Debug mode suggest the development phase to open the deployment phase comment or set to false define (' App_debug ', True); Specify the module controller and method $_get[' m ']= ' Wexinapi '; $_get[' C ']= ' Weixinpay '; $_get[' A ']= ' notify '; Define the application directory define (' App_path ', './application/'); Define ("App_root", DirName (FILE)); Introduction of the thinkphp entry file require './thinkcore/thinkcore.php '; There's no code behind the pro ^_^, it's so simple.
Now access http://serverName/payment.php, will go directly to the http://serverName/payment.php/WexinApi/WeixinPay/notify, so the callback verification link can write Http://serverName/payment.php, can also write http://serverName/payment.php/WexinApi/WeixinPay/notify.
(2) Initiating payment
So simple:
/** * Payment Test * Access: Http://daoshi.sdxiaochengxu.com/payment.php/WexinApi/WeixinPay/pay */Public Function Pay () {$order _SN = Getrand_num (TRUE); $openId = "; $jsApiParameters = Wxpay ($openId, ' Jiangnan geek ', $order _sn,1); $this->assign (Array (' data ' = $jsApiParameters)); $this->display (); }
But pay the page URL to pay attention to, because the URL of the payment page must have a lot of parameters, just said the TP used in the rewrite mode, your link is similar to [HTTP://SERVERNAME/HOME/BLOG/READ/ID/1], may have more parameters, this time the payment will consider your payment authorization directory is [Http://serverName/Home/Blog/read/id/], but your real authorization directory is [http://serverName/Home/Blog/], So it will be an error. The way to deal with this is, when entering the payment page, the refactoring URL, written in normal mode, that is, [http://serverName/Home/Blog/read?id=1], so it can.
(3) Support for successful callbacks
Now that the payment is complete, it will go to the corresponding method of the previously written link, i.e. [http://serverName/payment.php/WexinApi/WeixinPay/notify]:
The payment callback verifies that the public function notify () {$xml = $GLOBALS [' Http_raw_post_data ']; This sentence file_put_contents is used to view the XML data returned by the server after the test can be deleted file_put_contents ('./api/wxpay/logs/log.txt ', $xml, file_append); Convert the XML data returned by the server to an array//$data = Json_decode (Json_encode (simplexml_load_string ($xml, ' simplexmlelement ', Libxml_ Nocdata)), true); $data = Xmltoarray ($xml); Save the signature signed by the server $data _sign = $data [' sign ']; Sign does not participate in the signature algorithm unset ($data [' signs ']); $sign = $this->makesign ($data); Determine if the signature is correct to determine the payment status if (($sign = = = $data _sign) && ($data [' Return_code ']== ' SUCCESS ') && ($data [' Result_ Code ']== ' SUCCESS ') {$result = $data; This sentence file_put_contents is used to view the XML data returned by the server after the test can be deleted file_put_contents ('./api/wxpay/logs/log1.txt ', $xml, file_append); Gets the data returned by the server $order _sn = $data [' Out_trade_no ']; Order number $order _id = $data [' Attach ']; Additional parameters, select Delivery Order id $openid = $data [' OpenID ']; Payer OpenID $total _fee = $data [' Total_fee ']; Payment amount//Update database $this->updatedb ($order _id, $order _sn, $openid, $total _fee); }else{$result = false; }//Return status to Server if ($result) {$str = ' <xml><return_code><![ cdata[success]]></return_code><return_msg><! [cdata[ok]]></return_msg></xml> '; }else{$str = ' <xml><return_code><![ cdata[fail]]></return_code><return_msg><! [cdata[signature failed]]></return_msg></xml> '; } Echo $str; return $result; }
For security reasons, to re-verify the returned signature:
/** * Generate signature * @return Signature, this function does not overwrite the sign member variable */protected function makesign ($data) { //Get payment key require_once app_root. " /api/wxpay/lib/wxpay.api.php "; $key = \wxpayconfig::key; Go to Empty $data =array_filter ($data); Signature Step One: sort the parameters by dictionary order Ksort ($data); $string _a=http_build_query ($data); $string _a=urldecode ($string _a); Signature Step Two: Add key after string //$config = $this->config; $string _sign_temp= $string _a. " &key= ". $key; Signature Step Three: MD5 encryption $sign = MD5 ($string _sign_temp); Signature Step four: Convert all characters to uppercase $result =strtoupper ($sign); return $result; }
At this point, the TP payment is also done. This is the integration of the official SDK implementation, if you do not use the SDK, you can use the simpler method, see: PHP Implementation payment (JSAPI payment) and refunds (no integration payment SDK required)
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP implementation of one-way hash encryption operation steps
php implementation of single sign-on steps in detail