(Mobile terminal) Teach you to complete the App terminal - WeChat payment Android

Before starting, I would like to rant to those of you who have never done WeChat payment.

———————— WeChat payment is the third-party payment that is the most difficult to use, the worst documentation, and the most disgusting technical support I have ever used in my life. There is no second. ———–

I hope it didn't scare you, but it's really bad. It also charges 300 service fees every year. It's really bad. The demo and documentation are from three or four years ago. It's really a big bully.

Not much nonsense, let’s first put a wave of the official payment flow chart, or that sentence, you must read the flow chart carefully, which will greatly improve the processing of your business logic. 
write picture description here

Knowing the general process of payment, the next step is to analyze how to pay. In my project, the payment process is like this:

First, select the product and quantity, etc., and click to place an order. At this time, an order form will be generated in the background. Any piece of data in this form will be valid within half an hour. After half an hour, the order data will be invalid. So payment should be done within half an hour. 
When paying after placing an order, the background returns to the mobile phone for prepaid orders, and WeChat is activated at this time to complete the payment. The result after payment is the same as Alipay, and the data in the background still needs to be called to ensure the correctness of the transaction. Although it is very cumbersome, we must be cautious in business involving money. As programmers, we are also responsible for the code we write.

Another disgusting thing about WeChat is that all data exchange is carried out in xml format, which is crazy for half an hour.

Let’s go to the main topic: First of all, the company needs to provide a dedicated account to apply for the merchant platform. There is a pitfall here, that is, when logging in to the merchant platform, the Tenpay control will be downloaded. We’d better use the IE browser or the QQ browser. This pit control does not support other browsers very well. An error will occur.

Official Development Points

After a merchant applies to develop an application on the WeChat open platform, the WeChat open platform will generate the unique identifier APPID of the APP, and I will save it in the global constant. 
public static final String WX_APP_ID = "wxxxxxxxxxxxxx"; 
then add 
compile 'com.tencent.mm .opensdk:wechat-sdk-android-with-mta:1.4.0'  to the gradle file of the project
or go to the official download jar package: click I download 
sync and the gradle compilation is completed. 
Before that, there are some necessary things to do: 
paste the following code into the AndroidManifest file

<activity
            android:name=".wxapi.WXPayEntryActivity"
            android:exported="true"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>

                <data android:scheme="这里填写你的AppId"/>
            </intent-filter>
        </activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

In addition, you also need to copy the wxapi package and the Activity under the package in the Demo to the root directory of your project, as shown in the figure: If 
write picture description here 
there is no accident, you can start the payment.

initiate payment

It is assumed here that we have already obtained the prepaid order, and add the following declaration to the payment Activity:

 private IWXAPI msgApi;
  • 1

Initialize in oncreate:

msgApi = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
        msgApi.registerApp(Constant.WX_APP_ID);
  • 1
  • 2
  • 3

Assuming that the prepaid order has been obtained now, then the payment can be mobilized at this time

if (msg.what == ORDER_WX_PAY)
        {
            WXOrder wxOrder = JSONObject.parseObject(orders.getWxPayOrderString(), WXOrder.class);
            showToast("获取订单中...");
            // 将该app注册到微信
            PayReq request = new PayReq();
            request.appId = Constant.WX_APP_ID;
            request.partnerId = Constant.WX_APP_BUSINESS; //微信支付分配的商户号
            request.prepayId = wxOrder.getPrepayid(); //微信返回的支付交易会话ID
            request.nonceStr = wxOrder.getNoncestr(); // 随机字符串
            request.timeStamp = wxOrder.getTimestamp(); // 北京时间时间戳
            request.packageValue = "Sign=WXPay";
            request.sign = wxOrder.getSign(); //服务端生成的签名
            App app = (App) this.getApplicationContext(); // 将ordersId保存到全局变量中,因为在微信回调页面无法获取它
            app.setWxpayStatus(orders.getId());
            if (wxOrder.getPrepayid() != null || "".equals(wxOrder.getPrepayid()))
            {
                msgApi.sendReq(request);
            } else
            {
                showToast("订单号重复,请联系客服!");
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

At this time, under normal circumstances, it will jump to the WeChat client payment interface. If there is no jump, please confirm whether the AppId and the signature of the app are correct, and then modify it correctly on the merchant platform. 
– There are many reasons for the payment failure, but the official website is very responsible and only gives an error code. Therefore, if we usually get -1, we can only check the error step by step. 
write picture description here

payment completed

After the payment is completed, jump to the WXPayEntryActivity of the WeChat package, 
write picture description here

So far, the mobile terminal has been successfully completed. Predict how to write the background, and listen to the next decomposition.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326122788&siteId=291194637