In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 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 methods of Android access to Alipay and WeChat Pay. The content of the article is of high quality, so the editor shares it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Preface
A lot of APP need payment function, domestic generally is Alipay and Wechat. At present, these two access methods are more convenient for the app side, because most of the logic such as security verification is on the server side.
To sum up, the APP side takes three steps:
The library connected to the payment accepts the order information from the server, and initiates calls to Alipay and Wechat to receive callbacks from Alipay and Wechat.
Alipay access
The first is to access the aar file of Alipay.
What is more sinister is that Alipay also needs to download aar files to import, rather than relying on a line of gradle to get it done.
We need to download the latest DEMO and SDK from the official website. The official website address is here.
Then put the downloaded aar package into the libs directory under the project directory and rely on it through the following gradle
/ / configuration compile required for Alipay SDK AAR package (name: 'alipaySdk-15.6.0-20190226104104murnoUtdidpayment, ext:' aar')
Call the method of Alipay SDK to initiate payment
If you call Alipay SDK to initiate payment, you only need one parameter, that is, the order information returned by the server. So the payment order here is that we first call the server's API to create an order, and then the server returns the order information to us, and our APP takes this order information to call Alipay.
/ / the orderInfo below is the order information returned by our server, in addition to the order ID, signature and other security information / / basically follow the Alipay DEMO final Runnable payRunnable = new Runnable () {PayTask alipay = new PayTask (PayDemoActivity.this); Map result = alipay.payV2 (orderInfo, true); Log.i ("msp", result.toString ()); Message msg = new Message () Msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage (msg);}}; / / must call Thread payThread = new Thread (payRunnable) asynchronously; payThread.start ()
As can be seen from the above call, Alipay receives the callback through the message mechanism, so we have to receive the callback message in the Handler message.
Private Handler mHandler = new Handler () {@ SuppressWarnings ("unused") public void handleMessage (Message msg) {switch (msg.what) {case SDK_PAY_FLAG: {/ / receive the callback information of Alipay here / / it should be noted that the payment result must be determined by calling your own server, and break;} default: break;} can not be judged by the callback result of Alipay;}
It should be noted that the payment results must be called their own server to determine, can not be judged by Alipay callback results!
Other
In fact, it is necessary to consider whether Alipay has been installed on the user's mobile phone.
Already installed Alipay, will directly call Alipay pay without installing Alipay, will call up Alipay's H5 page payment
The above is the access of Alipay, the steps are still relatively simple, there is no pit. There will be a pit for WeChat Pay below.
WeChat Pay access
Access the SDK of Wechat
The official website is here.
Compared with Alipay, it is relatively easy for Wechat to connect to SDK. One line of code is done.
/ / WeChat Pay SDKcompile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
It should be noted here that payment and social login in Wechat's SDK are integrated and cannot be separated. So if Wechat login has been integrated before in the project, there is no need for repeated integration!
Call Wechat SDK to initiate payment
String content =... (this is the order information returned by the server) / / you need an APPIDIWXAPI api = WXAPIFactory.createWXAPI (mContext, APPID) for registering WeChat Pay; JSONObject json = new JSONObject (content); PayReq req = new PayReq (); req.appId = json.getString ("appid"); req.partnerId = json.getString ("partnerid"); req.prepayId = json.getString ("prepayid"); req.nonceStr = json.getString ("noncestr"); req.timeStamp = json.getString ("timestamp") Req.packageValue = json.getString ("package"); req.sign = json.getString ("sign"); api.sendReq (req); / / here is the call to WeChat Pay
Receive WeChat Pay's callback
You can see that the above code is different from Alipay and does not use Handler.
What is special about WeChat Pay is that we need to use a special Activity to receive callback messages. If this is not handled well, it is easy to encounter a situation in which callbacks are not received.
We need to create a new Activity called WXPayEntryActivity, which can be found in Wechat SDK. There are a few points to pay attention to:
The path of WXPayEntryActivity Activity must meet the requirements. For example, if the package name of APP is com.niubi.company, then the path of this Activity needs to be com.niubi.company.wxapi.WXPayEntryActivity.
Of course, this WXPayEntryActivity needs to be registered in the AndroidManifest file
It is particularly important to note that the above exported attribute and launchMode attribute must be added, otherwise the callback will not be received
After the payment is completed, we generally want to go straight back to our own application. At this point, we need to make sure that the above WXPayEntryActivity is not displayed, otherwise it will flash or stay on the black page.
If it is not displayed, you should pay attention to two points, one is not to have a layout, and the other is to finish the Activity in time.
Public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {... @ Override public void onResp (BaseResp resp) {Log.d (TAG, "onPayFinish, errCode =" + resp.errCode); if (resp.getType () = = ConstantsAPI.COMMAND_PAY_BY_WX) {/ / the dialog box must not pop up like the DEMO above, but inform us of the page that initiated the payment call / / and then finish this page in time and post a pseudo code: sendPayNotice () finish () }
Other
Similarly, whether WeChat Pay is successful or not should also be viewed by calling his own server, rather than relying on the callback status of Wechat, which should be kept in mind.
On Android access to Alipay and WeChat Pay's method to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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.