In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of the php WeChat Pay interface development program. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Php WeChat Pay interface development program explanation:
Necessary conditions:
Appid / / official account backend developer Center gets (same as in email)
Available in mchid// mail
Key// merchants set up their own backend settings.
Appsecret / / official account developer Center obtains
Two certificate files, apiclient_cert.pem apiclient_key.pem is obtained in the email
Note:
Official account backstage WeChat Pay-"Development configuration -" add test directory and test personal WeChat account.
Developer Center-"Web page authorization to get basic user information -" is modified to your test domain name. Otherwise, there will be redirect_uri parameter error.
-follow-up to be improved-
WeChat Pay has operated three times at the backend of the ready page:
1. Get openid
/ / use the jsapi interface code to copy the code $jsApi = new JsApi_pub (); / / = step 1: get the user openid= with web authorization / / obtain the openid if through code (! isset ($_ GET ['code'])) {/ / trigger Wechat to return the code $url = $jsApi- > createOauthUrlForCode (WxPayConf_pub::JS_API_CALL_URL); / / echo $url; Header ("Location: $url") } else {/ / get the code to get openid $code = $_ GET ['code']; $jsApi- > setCode ($code); $openid = $jsApi- > getOpenid ();}
At the beginning, I also encountered a problem in the first step. I couldn't get openid, which is related to some servers. Curl is used in demo.
It's strange that my server curl has been unable to get it. Later, it was changed to file_get_contents, which can be obtained normally.
But this is not the solution. Because more curl operations are needed later.
Seeing that there is a place in the development document where writing certificates requires libcurl 7.20.1 or above, I have been trying to improve the php curl version of linux throughout the server. I wish I had switched to another windows server at the end.
Let's do this for the time being, and debug it next time we need it.
Step 2: obtain and pay the order number id
The code is as follows
$unifiedOrder = new UnifiedOrder_pub (); / / var_dump ($unifiedOrder) / / set unified payment interface parameters / / set required parameters / / appid has been filled, merchants do not need to repeat / / mch_id has been filled, merchants do not need to repeat / / noncestr, merchants do not need to repeat / / spbill_create_ip, merchants do not need to repeat / / sign has been filled, merchants do not need to repeat $unifiedOrder- > setParameter ("openid", "$openid") / / Product description $unifiedOrder- > setParameter ("body", "contribute a penny"); / / Product description / / Custom order number, for example only $timeStamp = time (); $out_trade_no = WxPayConf_pub::APPID. "$timeStamp"; $unifiedOrder- > setParameter ("out_trade_no", "$out_trade_no"); / / Merchant order No. $unifiedOrder- > setParameter ("total_fee", "1") / / Total amount $unifiedOrder- > setParameter ("notify_url", WxPayConf_pub::NOTIFY_URL); / / Notification address $unifiedOrder- > setParameter ("trade_type", "JSAPI"); / / transaction type / / optional parameters. Merchants can choose / / $unifiedOrder- > setParameter ("sub_mch_id", "XXXX") according to the actual situation; / / sub-merchant / / $unifiedOrder- > setParameter ("device_info", "XXXX") / / device number / / $unifiedOrder- > setParameter ("attach", "XXXX"); / / additional data / / $unifiedOrder- > setParameter ("time_start", "XXXX"); / / transaction start time / / $unifiedOrder- > setParameter ("time_expire", "XXXX"); / / transaction end time / / $unifiedOrder- > setParameter ("goods_tag", "XXXX"); / / Commodity tag / / $unifiedOrder- > setParameter ("openid", "XXXX") / / user ID / / $unifiedOrder- > setParameter ("product_id", "XXXX"); / / Product ID $prepay_id = $unifiedOrder- > getPrepayId (); / / echo 'prepay_id:'; var_dump ($prepay_id)
This step also encountered a lot of problems.
First of all, the WeChat Pay test is more difficult, only in Wechat can be tested. I just swipe it around with my cell phone.
Secondly, it is not easy to debug with var_dump. Printing some files in xml format shows only the length of characters, not the content. So write debugging on the server in the form of log, and log code:
The code is as follows
/ / print log function log_d ($word) {$log_name= ". / logd.log"; / / log file path $fp = fopen ($log_name, "a"); flock ($fp, LOCK_EX); fwrite ($fp, "execution date:" .strftime ("% Y-%m-%d-%H:%M:%S", time ()). "n". $word. "nn"); flock ($fp, LOCK_UN) Fclose ($fp);}
In the WxPayPubHelper.php in demo, use $this- > log_d (xxx); call.
At the beginning, I always reported an error because of the mismatch between mchid and appid given to me. They gave me the wrong account number. I didn't know how to try at first. Debugging for this step var_dump ($this- > result) in getPrepayId (); you can see the error code.
Step 3: generate the payment front-end js code ready to the web page:
The code is as follows
$jsApi- > setPrepayId ($prepay_id); $jsApiParameters = $jsApi- > getParameters ()
-Click to pay-
There is another problem with this part:
Android returns "System:Access_denied" and ios returns "access_control:not_allowed"
Searched a lot of Baidu. In fact, I saw this thing a long time ago and didn't pay attention to it!
The page that initiates the authorization request must be a page under the authorization directory, not exist in a subdirectory. Otherwise, an error will be returned
I put the payment file on / domain name / pay/demo/
At the beginning, I thought it would be fine until the end of / domain name / pay/. Support subdirectory, the result is not good!
Finally, take a look at the following picture-
Wxpay1
Wxpay3
Wxpay2
-xmljs- in the process
Order for generation and payment to be submitted id:
The code is as follows
1 10012345
Correct is returned after submission, including perpay_id:
Generate the js for payment:
{"appId": "wx8888888888888888", "timeStamp": "1414411784", "nonceStr": "gbwr71b5no6q6ne18c8up1u7l7he2y75", "package": "prepay_id=wx201410272009395522657a690389285100", "signType": "MD5", "paySign": "9C6747193720F851EB876299D59F6C7D"}
Notification xml returned after successful payment:
1 this is the end of the article on "sample Analysis of php WeChat Pay Interface Development Program". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.