The main content
About enterprise qualified developers to apply for wechat payment, how to adjust wechat payment, the official documents have been very detailed, and there are also a lot of relevant information on Baidu, not introduced here. This article mainly talks about how to adjust wechat payment in your App.
demo
implementation
The front end
In the demonstration, the personal qualification payment interface provided by YunGouOS (wechat payment service provider) is used. Wechat is aroused in the App, mainly by using H5 payment method. H5 payment link is loaded in the WebView of the App, and then the payment link is processed as follows:
webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {if (url.isNullorempty ()) {return false} if (url.contains("https://wx.tenpay.com")) {// Is the paid URL add Referer val extraHeaders = HashMap<String, String>() extraHeaders.put("Referer", BASE_URL) view?.loadUrl(url, extraHeaders) return true } try { if (url.startsWith("weixin://")) { if (url.startsWith("weixin://") && ! Intent.action_view = intent.action_view () intent.action_view () intent.data = Uri.parse(url) startActivity(intent) return true } } catch (e: Exception) { return false } return super.shouldOverrideUrlLoading(view,url) } ... }Copy the code
ShouldOverrideUrlLoading () callback of WebViewClient. If the URL contains **wx.tenpay.com**, it means that it is a wechat H5 payment link. We need to add the Referer for the WebView, which is the domain name that invoks wechat payment. Bind the domain name of your server, which needs to be bound in the wechat background first, you can let [YunGouOS (wechat payment service provider)](https://dwz.cn/8IL…
The back-end
Generate the payment link by calling the H5Pay method in YunGouOS official SDK:
String payBody = "member recharge "; String payUrl = WxPay.H5Pay(orderNum, money, wxMChid, payBody, attach, notifyUrl, returnUrl, "", "", "", wxKey);Copy the code
Parameter Description:
- OrderNum: the order number of the merchant, that is, the order number of its own system;
- Money: the amount of money, yuan as a unit;
- WxMChid: wechat merchant number, which can be obtained after opening payment in YunGouOS (wechat payment service provider)
- PayBody product brief description
- Attach additional data and return to the original path when callback
- NotifyUrl Address of asynchronous callback
- ReturnUrl sync callback address, the demo will jump to the payment result query webpage after the payment is completed, because the sync callback address is passed in;
- The other three blanks are the parameters that need to be filled in for ledger distribution. You can directly fill in the blank string without using ledger distribution.
- WxKey Wechat payment key can be obtained after YunGouOS (wechat payment service provider) opens the payment
Note:
Incoming returnUrl synchronous callback address, does not represent payment success will jump this site, whether or not to pay successful, tune up WeChat pay after 5 seconds will automatically jump this address, so you should do is, in a synchronous callback pages, add check payment results of logic, request your check payment results interface, and then determine whether have been paid, Displays payment success or failure.
conclusion
It is mainly to judge the intercepted URL. If it is the link address of payment, the Referer will be added; if it is the wechat Scheme protocol (Weixin ://), the operation of wechat will be invoked. In order to facilitate your reference, we have uploaded the code to Github for your reference. Below is the corresponding download address of demo, you can download and experience it.
Download experience
Demo source download
Github.com/chaychan/We…