Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of integrating WeChat Pay's Native Code scanning payment Model into ThinkPHP Framework

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the ThinkPHP framework to integrate WeChat Pay's Native code scan payment model example analysis, I hope you will gain something after reading this article, let's discuss it together!

The details are as follows:

Hello, everyone, this article is the second in a series of WeChat Pay tutorials after WeChat Pay's jsapi article: scan the mode of payment. At present, there are two modes of payment. Mode one is a little more complicated than mode two. As for the specific content, process, and Wechat development documents of pattern one and mode two, there is a detailed introduction. There is no more nonsense here, and then hurry up on the tutorial!

First of all, let's just import WeChat Pay's class library:

Here are the files under Public:

The configuration here is the same as JSAPI payment and does not need to be changed.

For more information about the document, please refer to JSAPI payment. The link address is no longer stated here: / / www.jb51.net/article/159351.htm

Next, look directly at the code in the controller section:

Step1: again, initialize and introduce the WxPayPubHelper class library first

/ * initialize * / public function _ initialize () {/ / introduce WxPayPubHelper vendor ('WxPayPubHelper.WxPayPubHelper');}

Step2: to display the scan page, you need to survive the QR code link

Public function native_pay () {/ / set static link $nativeLink = new\ NativeLink_pub () / / set static link 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 has been filled, merchants do not need to repeat / / time_stamp has been filled in, merchants do not need to repeat / / sign has been filled in Merchants do not need to repeat $product_id = C ('WxPayConf_pub.APPID'). "static" / / Custom product id $nativeLink- > setParameter ("product_id", $product_id); / / Product id / / get link $product_url = $nativeLink- > getUrl (); / / use short link conversion interface $shortUrl = new\ ShortUrl_pub () / / 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 / / sign has been filled in, merchants do not need to repeat $shortUrl- > setParameter ("long_url", $product_url) / / URL links / / get short links $codeUrl = $shortUrl- > getShortUrl (); $this- > assign ('product_url',$product_url); $this- > assign (' codeUrl',$codeUrl); $this- > display ();}

The above code corresponds to the native_pay.html page

Wechat security payment sweep me, sweep me

Return home page var url = ""; / / Parameter 1 indicates the image size, with a value range of 1-10 Parameter 2 indicates the quality. The range of values is' var qr = qrcode (10,'M'); qr.addData (url); qr.make (); var dom=document.createElement ('DIV'); [XSS _ clean] = qr.createImgTag (); var element=document.getElementById ("qrcode"); element.appendChild (dom)

Here, pay attention to the js address of the survival QR code. I put it in the JS directory under Public.

Step3: after scanning the code, it will be submitted to our public platform native configuration corresponding to the address to deal with the public platform navtive configuration: configure the address http:// your domain name / Pay/index.php/Home/WxNative/todoPost

The corresponding todoPost method is in the controller:

Public function todoPost () {/ / record callback information as a log file for debugging $log_name = _ ROOT__. "/ Public/native_call.log"; / / use the native notification interface $nativeCall = new\ NativeCall the callback information); / / receive Wechat requests $xml = $GLOBALS ['HTTP_RAW_POST_DATA'] Log_result ($log_name, "[received native notifications]:\ n". $xml. "\ n"); $nativeCall- > saveData ($xml); if ($nativeCall- > checkSign () = = FALSE) {$nativeCall- > setReturnParameter ("return_code", "FAIL"); / / return status code $nativeCall- > setReturnParameter ("return_msg", "signature failed") / / return information} else {/ / extract product_id $product_id = $nativeCall- > getProductId (); / / use the unified payment interface $unifiedOrder = new\ UnifiedOrder_pub () / / set the corresponding order parameters according to different $product_id Here is only one example of switch ($product_id) {case C ('WxPayConf_pub.APPID'). "static": / / corresponding to the static link QR code in native_call_qrcode.php / / 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 has been filled, merchants do not need to repeat / / spbill_create_ip has been filled, merchants do not need to repeat / / sign has been filled in, merchants do not need to repeat $unifiedOrder- > setParameter ("body") "contribute a penny") / / Product description / / Custom order number, for example only $timeStamp = time (); $out_trade_no = C ('WxPayConf_pub.APPID'). $timeSt $unifiedOrder- > setParameter ("out_trade_no", $out_trade_no) / / merchant order number $unifiedOrder- > setParameter ("product_id", "$product_id"); / / ID $unifiedOrder- > setParameter ("total_fee", "1"); / / Total amount $unifiedOrder- > setParameter ("notify_url", C ('WxPayConf_pub.NOTIFY_URL')); / / Notification address $unifiedOrder- > setParameter ("trade_type", "NATIVE") / / transaction type $unifiedOrder- > setParameter ("product_id", $product_id); / / user ID / / optional parameter. Merchants can choose / / $unifiedOrder- > setParameter ("sub_mch_id", "XXXX") according to the actual situation; / / sub-merchant number / / $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 / / get prepay_id $prepay_id = $unifiedOrder- > getPrepayId () / / set the return code / / set the required parameters / / appid has been filled, merchants do not need to repeat / / mch_id has been filled, merchants do not need to repeat / / noncestr has been filled, merchants do not need to repeat / / sign has been filled in Merchants do not need to repeat $nativeCall- > setReturnParameter ("return_code", "SUCCESS") / / return status code $nativeCall- > setReturnParameter ("result_code", "SUCCESS"); / / Business result $nativeCall- > setReturnParameter ("prepay_id", $prepay_id); / / prepaid ID break Default: / / set error code / / set required parameters / / appid has been filled, merchants do not need to repeat / / mch_id has been filled in, merchants do not need to repeat / / noncestr has been filled in Merchants do not need to repeat / / sign has been filled in, merchants do not need to repeat $nativeCall- > setReturnParameter ("return_code", "SUCCESS") / / return status code $nativeCall- > setReturnParameter ("result_code", "FAIL"); / / Business result $nativeCall- > setReturnParameter ("err_code_des", "this product is invalid"); / / Business result break;}} / / return the result to Wechat $returnXml = $nativeCall- > returnXml () Log_result ($log_name, "[returns a native response from Wechat]:\ n". $returnXml. "\ n"); echo $returnXml;}

In fact, you have completed the function of scanning code payment mode one by now.

Step4: next, write about asynchronous notification processing, which is the same as jsapi payment:

Public function notify () {/ / use the generic notification interface $notify = new\ Notify_pub manger; the callback of xml / Storage Wechat $xml = $GLOBALS ['HTTP_RAW_POST_DATA']; $notify- > saveData ($xml); / / verify the signature and respond to Wechat. / / when a pair of backend notifications interact with each other, if Wechat receives a response from a merchant that is not successful or timed out, Wechat believes that the notification failed. / / Wechat will regularly re-initiate the notification through certain strategies (for example, 8 times in 30 minutes), and / / improve the success rate of the notification as much as possible, but Wechat does not guarantee that the notification will be successful in the end. If ($notify- > checkSign () = = FALSE) {$notify- > setReturnParameter ("return_code", "FAIL"); / / return status code $notify- > setReturnParameter ("return_msg", "signature failed"); / / return message} else {$notify- > setReturnParameter ("return_code", "SUCCESS"); / / set return code} $returnXml = $notify- > returnXml (); echo $returnXml / / = the merchant sets the corresponding processing flow according to the actual situation. Here is an example only = / / record callback information in log file / / $log_ = new Log_ (); $log_name= _ _ ROOT__. "/ Public/notify_url.log" / / log file path $this- > log_result ($log_name, "[received notify notification]:\ n" .$ xml. "\ n") If ($notify- > checkSign () = = TRUE) {if ($notify- > data ["return_code"] = = "FAIL") {/ / the order status should be updated here, and the merchant should add and delete log_result ($log_name, "[communication error]:\ n". $xml. "\ n") } elseif ($notify- > data ["result_code"] = "FAIL") {/ / the order status should be updated here, and the merchant should add and delete the operation log_result ($log_name, "[business error]:\ n". $xml. "\ n") } else {/ / the order status should be updated here, and merchants add and delete log_result ($log_name, "[payment succeeded]:\ n". $xml. "\ n") } / / merchants add their own processing flow, / / for example: update order status / / for example: database operation / / for example: push payment completion information}}

Native scanning payment mode one demo will be fine.

The screenshot of the test is shown below:

Scan the code interface:

Scan result:

After reading this article, I believe you have a certain understanding of "the example analysis of ThinkPHP framework integration WeChat Pay's Native code scanning payment mode". If you want to know more about it, welcome to follow the industry information channel, thank you for your reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report