Current location - Quotes Website - Personality signature - How to use PHP to realize WeChat payment, please consult. Novice! Detailed explanation
How to use PHP to realize WeChat payment, please consult. Novice! Detailed explanation
Wechat official account JSAPI payment

One: Configuration parameters

After the application is successful, obtain the interface file, put all the files under the project root directory weixin, and fill in the configuration account information in WxPay.ub.config.php;

Two: Set the authorization

Developer Center-> Web service->; Web page authorization to obtain basic information of users-> Modify;

"The domain name of the authorization callback page can be changed to your domain name address. You must ensure that you have obtained the authorization of this page, otherwise you will report an error in the redirect_uri parameter;

Third, the web page authorizes users to obtain openid.

Js_api_call.php requests changes in the file (all parameters passed to WeChat are received in the portal file).

$ out _ trade _ no = $ _ GET[' out _ trade _ no ']; //The internal order number of the merchant system is within 32 characters.

$ total _ fee = $ _ GET[' total _ fee ']* 100; //The unit of the total order amount is minutes, not decimals, so the price must be multiplied by 100.

Otherwise, an error will be reported when obtaining prepay_id (it is best to set the amount field to 2 decimal places).

$ pay _ status = get _ pay _ status($ out _ trade _ no); //View order payment status

$ return _ URL = get _ return _ URL($ out _ trade _ no); //Set the jump page after successful payment.

//Use jsapi interface

$ jsApi = new jsApi _ pub();

//Get openid through code

If (! Isset($ _ get[' code ']){// Triggers the WeChat return code.

//Set the redirect_uri parameter and return the code address, which is actually to redirect to the current page and get the user code parameters.

$ URL = WxPayConf _ pub::JS _ API _ CALL _ URL。 "? showwxpaytitle = 1 & amp; out _ trade _ no = $ out _ trade _ no & amp; total _ fee = $ total _ fee ";

$ URL = $ jsApi-& gt; createOauthUrlForCode($ URL);

header(" Location:$ URL ");

}else{ // get code to get openid.

$ code = $ _ GET[' code '];

$ jsApi-& gt; setCode($ code);

$ OpenID = $ jsApi-& gt; get OpenID(); //user ID

}

Then there is the method of Create OAuthurrforCode ($ redirection) under the JsApi_pub () class of WxPayHubHelper.php file, and there is a second parameter.

Change to $ urlobj ["redirect _ uri"] = urlcode ($ redirection); //url encodes a $url variable, otherwise the order number and amount cannot be obtained when redirecting the header.

4. Set the unified payment interface parameters and obtain the prepayment _ ID (the prepayment ID generated by WeChat for subsequent interface calls).

Wechat payment-> Development configuration->; Payment test-> Add Test Authorization Directory and Test Whitelist

(The payment authorization directory needs to be accurate to the minimum level. When using it, add the file name directly after the directory name, such as ccc.com/weixin/).

Make sure that the getPrepayId method of the UnifiedOrder_pub class in your WxPayPubHelper.php file can be used normally.

The debugging of this step is in get prepaid () var _ dump ($ this-> Results); You can see the error code;

Call CreateXml after posXml.

Function postXml() {

$ XML = $ this-& gt; create XML();

//* * Check whether the xml file is generated normally here.

include_once("log_。 PHP”);

$ Log = new Log _();

$ log-& gt; log_result("log.txt ",$ XML);

//* * Because some files in xml format are printed, only the character length is displayed, not the content. So write it in the form of a log on the server for debugging.

$ this-& gt; response = $ this-& gt; postmlcurl($ XML,$ this-& gt; url,$ this-& gt; curl _ time out);

return $ this-& gt; Respond;

}

Make sure that the xml variable format is as follows;

PostXmlCurl can only be submitted correctly if the parameters are correct. After submission, you can get the correct prepay_id _ ID _ ID and the payment box will pop up.

Five: Use jsapi to adjust payment.

Generate jsapi payment parameters according to prepay_id.

$ jsApi-& gt; setprepureid($ prepay _ id);

$ jsApi parameters = $ jsApi-& gt; get parameters();

//Call WeChat JS api to pay.

Function jsApiCall() {

WeixinJSBridge.invoke(

getBrandWCPayRequest ',

,

Function (resource)

weixinsbridge . log(RES . err _ msg);

If (res.err _ msg = = "get _ brand _ wcpay _ request: ok") {//After the payment is successful,

Window.location.href="" // Jump address and sequential operation. Orders must also be processed in asynchronous pages to prevent synchronization failure.

} Otherwise {

Alert ("payment failed"+res.err _ code+res.err _ desc+res.err _ msg);

}

}

);

} (You must judge the order status every time you request the callpay method to prevent repeated submission)

Sixth, the general notification interface asynchronously processes the results returned by WeChat.

After the payment is completed, get the callback of WeChat in notify.php.

$ XML = $ GLOBALS[' HTTP _ RAW _ POST _ DATA '];

//You must verify the signature and reply to WeChat.

if($notify-& gt; CheckSign() == TRUE) // After verifying the signature and updating the order status,

$ notify-& gt; setReturnParameter(" return _ code "," SUCCESS "); //Set the return code to ensure that the payment status changes before the return is successful.

//When interacting with the background notification, if WeChat receives the response from the merchant unsuccessfully or overtime, WeChat considers the notification failed.

//Wechat will re-launch the notification regularly through a certain strategy (for example, 30 minutes ***8 times).

$ return XML = $ notify-& gt; return XML();

Echo $ returnXml (returns xml data to WeChat. When return_code is successful, no notification will be made. )

//When receiving the notification that needs to be processed, first check the status of the corresponding business data to judge whether the notification has been processed, and if not, then process it.

If it is processed, the result is returned directly. Data locks should be used for concurrency control before checking and processing business data.

Avoid data confusion caused by function reentry.

//Judge whether to open it in WeChat browser.

Function isWeiXin(){

var ua = window . navigator . user agent . tolowercase();

if(ua . match(/micro messenger/I)= ' micro messenger '){

Return true

} Otherwise {

Returns false

}

}

The steps are detailed enough, I realized it myself, so I wrote a log, and combined with the php version of demo, I can get a general understanding. If you are not clear about the whole process, after carefully reading the WeChat official account payment interface document, you will get it if you apply for WeChat official account payment successfully.