In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve WeChat Pay php, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Php to achieve WeChat Pay's methods: 1, download the official demo; 2, modify the curl network request configuration code; 3, complete the basic information settings; 4, modify the WxPay.Notify.php file; 5, set the official account background; 6, complete the payment process.
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer
How to realize WeChat Pay with php?
PHP implements WeChat Pay (jsapi payment) process
Recently, I came into contact with a project involving WeChat Pay. I haven't made a payment since I have been engaged in Wechat development for such a long time. I have also worked on an official account to send red envelopes before. I thank my predecessors for their exploration. I read their blog posts, which saved me a lot of detours.
Preliminary preparation:
1. Wechat authentication service number, and activated WeChat Pay
two。 WeChat Pay SDK, download address: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
3. Log in to WeChat Pay platform https://pay.weixin.qq.com/index.php/account/api_cert to download the payment certificate
Methods and steps:
1.demo file processing
(1) download the official demo, which is called WxpayAPI_php_v3, and rename the file to wxpay, so that the directory can be written later.
(2) Open the WxPay.Api.php file under the lib folder, and there is a curl network request configuration code on line 537:
Curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST,2); / / strict check
To be replaced by:
Curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST,2); / / strict check
To prevent cURL from verifying peer certificates (peer's certificate).
(3) Open the WxPay.Config.php file under the lib folder, start on line 25, and complete the basic information settings according to your account number.
Const APPID = 'public account APPID';const MCHID =' merchant number'; const KEY = 'merchant payment key'; const APPSECRET = 'public account secert''
(4) Open the WxPay.Notify.php file under the lib folder, line 79 of the code:
If ($needSign = = true & & $this- > GetReturn_code ($return_code) = = "SUCCESS") {$this- > SetSign ();}
Change it to:
If ($needSign = = true & & $this- > GetReturn_code () = = "SUCCESS") {$this- > SetSign ();}
(5) Open the cert certificate directory and replace the two certificates with your own payment certificate.
two。 Official account background settings
(1) configure web page authorized domain name. My domain name is (xy.chuyin.ren)
(1) configure the payment authorization directory. The domain name is (xy.chuyin.ren). I put demo under the weixinopen/ folder of the directory pointed to by this domain name. The jsapi.php file in demo is located under the example/ directory, so the payment authorization directory is: xy.chuyin.ren/weixinopen/wxpay/example/.
3. Payment process
Open the jsapi.php file in the example directory, and the payment initiation and processing are all done here.
(1) obtain user openid
You've configured your own APPID and APPSecert before, so you don't have to deal with it here.
/ / ①, get user openid$tools = new JsApiPay (); $openId = $tools- > GetOpenid ()
Here, the first initialized JsApiPay () class gets an object. The file corresponds to the WxPay.JsApiPay.php in the example/ directory. If you call the GetOpenid () method, you will automatically get your own openID.
(2) issue orders uniformly
/ / ②, Unified order $input = new WxPayUnifiedOrder (); $input- > SetBody ("test"); $input- > SetAttach ("test"); $input- > SetOut_trade_no (WxPayConfig::MCHID.date ("YmdHis")); $input- > SetTotal_fee ("1"); $input- > SetTime_start (date ("YmdHis")); $input- > SetTime_expire (date ("YmdHis", time () + 600); $input- > SetGoods_tag ("test") $input- > SetNotify_url ("http://paysdk.weixin.qq.com/example/notify.php");$input->SetTrade_type("JSAPI");$input->SetOpenid($openId);$order = WxPayApi::unifiedOrder ($input); echo 'Unified order payment Information'; printf_info ($order); $jsApiParameters = $tools- > GetJsApiParameters ($order)
Configure the order information and the payment callback function corresponding to the unifiedOrder () method in line 24 of WxPay.Api.php. Here, you need to modify several parameters:
a. Product name:
$input- > SetBody ("test")
b. Order number
$input- > SetOut_trade_no (WxPayConfig::MCHID.date ("YmdHis"))
c. Payment amount
$input- > SetTotal_fee ("1")
d. Payment verification link
Set it to the location of your notify.php file, so here I set it to: http://xy.chuyin.ren/weixinopen/wxpay/example/notify.php
You can also write other addresses, of course, under the payment authorization domain name, after the payment is successful, it will be automatically called back to the method specified by the link, where you can judge and operate the database.
$input- > SetNotify_url ("http://paysdk.weixin.qq.com/example/notify.php");"
e. Additional parameters
$input- > SetAttach ("test")
Additional parameters, can be filled in or not, if you fill in, it is best not to appear spaces in the string.
At this time, click pay should be able to pay successfully.
(3) initiate payment
/ call Wechat JS api to pay function jsApiCall () {WeixinJSBridge.invoke ('getBrandWCPayRequest', function (res) {WeixinJSBridge.log (res.err_msg); alert (res.err_code+res.err_desc+res.err_msg);}) } function callpay () {if (typeof WeixinJSBridge = = "undefined") {if (document.addEventListener) {document.addEventListener ('WeixinJSBridgeReady', jsApiCall, false);} else if (document.attachEvent) {document.attachEvent (' WeixinJSBridgeReady', jsApiCall); document.attachEvent ('onWeixinJSBridgeReady', jsApiCall) Else {jsApiCall ();}}
Click the pay now button to call the callpay () function, which will call the jsApiCall () function to open the payment program.
The jsApiCall () function listens for every step:
Res.err_msg indicates the cancellation of payment judged by the front end for get_brand_wcpay_request:cancel, and es.err_msg indicates that the payment judged by the front end is successful. We can redirect the payment to the success page based on this.
(4) support successful callback
You can listen to the payment result through the front-end jsApiCall () function, but this is not credible. Confirm whether the payment is successful or whether the business logic should be handled through notify.php. The payment verification link SetNotify_url () is configured above. After the payment is completed, the Wechat server will automatically request your notify.php file according to the link and open the file. In fact, the main code of this file is only two lines:
$notify = new PayNotifyCallBack (); $notify- > Handle (false)
This traces the Handle () function of the WxPay.Notify.php class file:
/ * callback entry * @ param bool $needSign whether signature output is required * / final public function Handle ($needSign = true) {$msg = "OK"; / / when false is returned, it means that calling NotifyCallBack callback in notify failed to obtain signature verification, and direct reply failed $result = WxpayApi::notify (array ($this, 'NotifyCallBack'), $msg) If ($result = = false) {$this- > SetReturn_code ("FAIL"); $this- > SetReturn_msg ($msg); $this- > ReplyNotify (false); return;} else {/ / the branch is successfully called back to the NotifyCallBack method. After processing, the process $this- > SetReturn_code ("SUCCESS") $this- > SetReturn_msg ("OK");} $this- > ReplyNotify ($needSign);}
Main code:
$result = WxpayApi::notify (array ($this, 'NotifyCallBack'), $msg)
Then come to line 411 of the WxPay.Api.php file, the notify () function:
/ * General notice of payment result * @ param function $callback * Direct callback function usage: notify (you_function); * callback class member function method: notify (array ($this, you_function)); * $callback prototype: function function_name ($data) {} * / public static function notify ($callback, & $msg) {/ / get notification data $xml = $GLOBALS ['HTTP_RAW_POST_DATA'] / / file_put_contents ('log.txt',$xml,FILE_APPEND); / / verify the signature try {$result = WxPayResults::Init ($xml) if returned successfully;} catch (WxPayException $e) {$msg = $e-> errorMessage (); return false;} return call_user_func ($callback, $result);}
The $xml=$GLOBALS ['HTTP_RAW_POST_DATA'] in this is a result returned to you by the user after the payment is successful, which is a string in xml format.
We can record the xml data returned here, and then open it to see that $out_trade_no is the order number I set before payment, and $attach is the additional parameter set.
I got the order number, and then I wrote the logic of the successful payment directly below, such as changing the data in the database and so on.
In this way, WeChat Pay's JsApi payment is roughly analyzed. [recommended study: "PHP Video tutorial"]
This is integrated with the official SDK implementation, if you do not use SDK, you can use a simpler method, see: SDK implementation of WeChat Pay (jsapi payment) and refund (no need to integrate payment SDK)
The above is all the contents of the article "how to achieve WeChat Pay in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.