The Android-connected WeChat payment is completely parsed ~

Source: Internet
Author: User

Complete parsing of Android access payment, too full ~

Today, let's talk about the demand for access to payment in android. Some people will say that this is simple. What are you talking about here? Please wash your bed ~~

I'm not convinced. If it's easy to say, do you know the number of the official website payments? If you are one hundred degrees, the following are all advertisements. Haha, you accidentally fell into the trap, in simple cases, as long as you want to do it well, it is actually very complicated ~

Introduction

First, visit the Payment Official Website:

We can see that this is the payment home page. There are several payment methods below, and today our main character is APP payment. We can click in directly, or access guide-APP payment from the upper left corner, the document format is shown in the following figure:

Someone must have said that it is useless to post such a message, and it is still a waste of traffic...

Don't worry, let me tell you what the figure is for. First, you can see from this figure what steps and materials are required from registering a development platform account to completing payment access, in this way, you can ask the relevant personnel to prepare the materials in advance, instead of filling out a step of information. When looking for the next step, remember that time is money, in addition, if your leader tells me what materials are prepared for the payment, and you haven't read this document, then I will have to wait for O ~. Which of the following statements about payment is simple? Some of them know this picture, and others have read it carefully ~~

We can see that 300 RMB is required and some enterprise information is required.

In addition, there are two platforms for payment: The developer platform and the Merchant platform.

Developer platform: mainly for developers, such as creating applications and obtaining appid
Merchant platform: mainly refers to some management of merchants, such as viewing flow and orders.

Create an application

Here I will only demonstrate how to create an application, and I will not use this account at last, because I am a personal account and cannot apply for payment. I just want to give a demo to a friend who will not create it and what information is needed, will be skipped ~

First, let's go to the developer platform and register without an account. I don't want to demonstrate how to create an application. First, after logging on to the account, click "Management Center"> "mobile application:

Click Create mobile app in the upper left corner to go to the following page, because this is a test and all the materials are left blank.

Click Next.

This step allows you to fill in the required platform and platform information. Here I only hold android, fill in the package name and signature, and here he does not want Weibo to fill in multiple signatures, here, we recommend that you enter the debug signature at the beginning. After debugging is passed, enter the formal signature. The signature retrieval method is the same as that for third-party login. Finally, submit the application for review. After the review, click application details. This should be the result.

After the application created by the personal account is approved, only the sharing function is available. If you still need to pay, you can click Apply and then authenticate the account and upload some materials. This is a long process, we can't do that much here. Now let's talk about how to implement it in the code.

Run the official demo

Remember that the payment demo here is downloaded from the help of the merchant platform. The address is here, instead of the demo downloaded from the developer platform (previously, it was okay. Now the demo downloaded here, ), for example, this page:

The first is the basic library. After you click it, it will jump to the developer platform. The second parameter is the payment demo, which also contains sharing and other functions. It can be said that if you want to make both payment and share, you only need this demo. Of course you need this article.

After downloading and importing eclipse, replace debug. keystore and run it, you can see the following interface and finally see the payment. Are you so excited?

Then we can click "Jump to the payment page" to see what the effect is. It's because the scorpion is a horse, and the demo is running ~

The demo has been read and the money has been paid. Now we should start to transfer the payment.

Formal Transfer Payment

First, you have to upload a flowchart. Otherwise, you know how to use the logic?

This is the official address of this document.

This is an official explanation.

Description of interaction between the merchant System and the payment system:

Step 1: select products in the merchant APP, submit the order, and select pay.

Step 2: the merchant's background receives the user's payment order and calls the unified order payment interface. See [Unified order API ].

Step 3: The unified order interface returns the normal prepay_id, and then re-generates the signature according to the signature specification, the data is transmitted to the APP. The fields involved in the signature are appId, partnerId, prepayId, nonceStr, timeStamp, and package. Note: The package Value format is Sign = WXPay.

Step 4: The Merchant APP starts the payment. For more information about APIs, see [app development steps]

Step 5: The Merchant's background receives the payment notification. For more information about the api, see [payment result notification API]

Step 6: query the payment result at the merchant's background ., For api details, see [query order API]

The first payment is exposed to me in two steps. One step is to generate a pre-payment order, and then the pre-payment order id is called for payment. Therefore, there are two implementation methods here, one is that the client processes all these steps, and the other is that the server creates and pays the order and signature, and then returns it to us before we call the payment with these parameters. In actual application, we recommend that you use the service type, but here we will talk about how to implement local payment. If you are paying on the server, then you will tell him what parameters you need, how does he create pre-payment orders and other server-side orders? refer to here

Configuration

This is the official app payment development step. In addition, because there is no available payment, the package name and key used for writing the demo are all demo.

Configure permissions

Configure activity


  

Configure callback activity

Call payment

Public void testWxPay (View view) {new Thread (new Runnable () {@ Override public void run () {String url = "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php? Plat = android "; ToastUtil. define toastinbackgroundthread (getActivity (), "Get order... "); try {byte [] buf = Util. httpGet (url); if (buf! = Null & buf. length> 0) {String content = new String (buf); Log. e ("get server pay params:", content); JSONObject json = new JSONObject (content); if (null! = Json &&! Json. has ("retcode") {req = new PayReq (); // req. appId = "wxf8b4f85f3a794e77"; // appId req is used for testing. 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"); req. extData = "app data"; // optional ToastUtil. optional toastinbackgroundthread (getActivity (), "normal start payment"); toPay ();} else {Log. d ("PAY_GET", "Return Error" + json. getString ("retmsg"); ToastUtil. jsontoastinbackgroundthread (getActivity (), "Return Error" + json. getString ("retmsg") ;}} else {Log. d ("PAY_GET", "server request error"); ToastUtil. except toastinbackgroundthread (getActivity (), "server request error") ;}} catch (Exception e) {Log. e ("PAY_GET", "exception:" + e. getMessage (); ToastUtil. when toastinbackgroundthread (getActivity (), "exception:" + e. getMessage ());}}}). start ();} private void toPay () {// call IWXMsg first if the application is not registered before payment. registerApp registers the application to the api. sendReq (req );}

Now, if you follow my configuration, you can call the payment interface normally. If-1 is displayed, check whether debugging is replaced. if the keystore is replaced, remember to clear the cache.

The above test code is on github, and the officially downloaded sdk package is also under the docs directory of the repository.

If my article is helpful, you can add me, Weibo, QQ, and other friends. In addition, Weibo will occasionally issue some high-quality articles, thank you for your support ~~, The contact information is in my personal profile.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.