In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to develop WeChat Pay on Wechat public platform. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. JS-SDK
WeChat Pay in the official account needs to be realized through JS. Wechat JS-SDK is a web development kit based on Wechat provided by Wechat public platform for web developers.
1) introduce JS script file
2) verify the configuration by injecting permissions through the config API
Wx.config ({debug: false, / / enables debug mode. The returned values of all api called will be displayed in the alert on the client side. To view the parameters passed, you can open them on the PC side, and the parameter information will be typed out through log and printed only on the PC side. AppId:'', / / required, unique ID of official account timestamp:, / / required, timestamp for generating signature nonceStr:', / / required, random string for generating signature signature:', / / required, signature})
AppId is the string of characters that start with ID,wx. Timestamp is obtained by time () in php, while nonceStr is obtained by uniqid (), and signature is obtained according to a specific algorithm.
Protected function getJsapiConfig () {$weixin = new Weixin (); $ticketMongo = new WeixinJsapiTicket (); $data = ['appId' = > $weixin- > getAppId (),' noncestr' = > uniqid (), 'jsapi_ticket' = > $ticketMongo- > getJsapiTicket (),' timestamp' = > time ()] / / assemble the original unsigned string $src = ['noncestr='. $data ['noncestr'],' jsapi_ticket='. $data ['jsapi_ticket'],' timestamp='. $data ['timestamp']]; sort ($src); $data [' signature'] = sha1 (implode ('&', $src)); return $data;}
"jsapi_ticket" is described here. Jsapi_ticket is the temporary ticket used by the official account to call the Wechat JS API. Normally, the jsapi_ticket is valid for 7200 seconds and is obtained through access_token. Due to the time limit and the limited number of api calls to get jsapi_ticket, I will save the acquired jsapi_ticket to MongoDB.
/ * * obtain jsapi_ticket * @ param $access_token * @ return string through access_token | null * / public function getJsapiTicket ($access_token) {$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; $param = [' access_token' = > $access_token, 'type' = >' jsapi'] $res = $this- > request ($url, $param); $result = json_decode ($res, true); if (isset ($result ['errcode']) & & $result [' errcode'] = = 0 & isset ($result ['ticket'])) {return $result;} return null;}
3) successful verification through ready API processing
1) prepay_id is obtained according to the locally generated order number, etc. Each request of the order number is different, otherwise an error will be reported.
2) nonceStr is obtained using md5 (uniqid ('baiaimama'))
3) signType uses MD5
4) paySign is sorted and spliced according to the parameters of the code.
Wx.chooseWXPay ({timestamp: 0, / / payment signature timestamp. Note that all usage timestamp fields in Wechat jssdk are lowercase. However, the latest version of the timeStamp field name used by the payment backend to generate signature needs to capitalize the S character nonceStr:'', / / payment signature random string, no longer than the prepay_id parameter value returned by the 32-bit package:'', / / unified payment API. The submission format, such as: prepay_id=***) signType:', / / signature, defaults to 'SHA1'. To use the new version of payment, you need to input 'MD5' paySign:', / / payment signature success: function (res) {/ / callback function}} after successful payment / * parameters to be called for generating jsapi * / public function getJsapiParam () {$param = ['appId' = > $this- > APPID,' timeStamp' = > time (), 'nonceStr' = > md5 (uniqid (' baiaimama')), 'package' = >' prepay_id='.$this- > param ['prepay_id'],' signType' = > 'MD5'] $str = []; foreach ($param as $k = > $v) {if (! empty ($v)) {$str [] = "{$k} = {$v}";}} sort ($str); $unsignKey = join ('&', $str).'& key='.$this- > KEY; $sign = strtoupper (md5 ($unsignKey)); $param ['paySign'] = $sign Return $param;} II. Asynchronous callback
Do some operations such as modifying order status, sending text messages, pushing messages and so on in asynchronous callback.
/ * * WeChat Pay asynchronous callback API * WeChat Pay successfully receives an asynchronous callback * / public function actionWxpay () {$weixinPay = new WeixinPay (); $weixin = new Weixin (); $xml = file_get_contents ('php://input'); $msg = $weixin- > parseMsg ($xml) / record Wechat push log $notifyMongo = new WeixinPayNotify (); $notifyMongo- > logPayNotify ($xml); if (! $msg | |! is_object ($msg)) {$weixinPay- > notifyXml ('FAIL',' notification is invalid') } if (! isset ($msg- > return_code) | | $msg- > return_code! = 'SUCCESS') {$weixinPay- > notifyXml (' FAIL', 'communication failure');} if (! isset ($msg- > result_code) | | $msg- > result_code! = "SUCCESS") {$weixinPay- > notifyXml ('FAIL',' transaction failed') } / / signature verification failed if (! $weixinPay- > checkSign ($msg)) {$weixinPay- > notifyXml ('FAIL',' signature verification failed');} / / $notifyMongo- > add ($msg); / / this indicates that payment has been made successfully, and there is no need to update the order logic $userOrder = new UserOrder () / record Wechat order number $userOrder- > pay ($msg- > out_trade_no, $msg- > transaction_id);} this is the end of the article on "how to develop WeChat Pay on Wechat Public platform". 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: 251
*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.