As usual, first read through the official document.

WeChat pay

preparation

Wechat open platform

Register an account, create an app, open wechat pay, these are all familiar.

Wechat merchant platform

APP payment document pay.weixin.qq.com/wiki/doc/ap… If you just follow the documentation, you’re guaranteed to pay -1 every time, right, just -1, without any other information, I have to say, pit ratio.

APP payment sequence diagram to have a general understanding of the payment process:

Access to the SDK

The introduction of libs

The SDK download: pay.weixin.qq.com/wiki/doc/ap… Put libammsdK. jar in the libs folder

Configuring debug Signatures

To create an application, you need to fill in the application signature and configure the formal key directly used under debug, so that you don’t need to package each time to call the wechat client

signingConfigs {
        debug {
            storeFile file("Your keystore path")
            storePassword "xxx"
            keyAlias "xxx"
            keyPassword "xxx"
        }

        release {
            storeFile file("Your keystore path")
            storePassword "xxx"
            keyAlias "xxx"
            keyPassword "xxx"
        }
}Copy the code

WXPayEntryActivity



There’s nothing in the documentation

Androidmanifest.xml register

<! -- wechat Pay -->
<activity
    android:name=".wxapi.WXPayEntryActivity"
    android:exported="true"
    android:launchMode="singleTop"/>Copy the code

Network permission registration is unnecessary.

Payment interface call

/** * wechat Pay *@paramThe data interface returns */
private void weixinPay(String data) {
    // Get an instance of IWXAPI from the WXAPIFactory
    IWXAPI api = WXAPIFactory.createWXAPI(this, WEIXIN_APP_ID);
    // Register the app with wechat
    api.registerApp(WEIXIN_APP_ID);
    LogUtil.d("data=" + data);
    try {
        JSONObject json = new JSONObject(data);
        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");
        req.extData = "app data"; // optional
        // Before payment, if the app is not registered with wechat, you should first call iwxmsg. registerApp to register the app with wechat
        api.sendReq(req);
    } catch(JSONException e) { e.printStackTrace(); }}Copy the code

For safety, APP end up paying the parameters of the need to merchant backend system to provide the interface returns, parameter documentation: pay.weixin.qq.com/wiki/doc/ap…

The problem

Thought that you can adjust the wechat client payment, did not think has been returned to pay the result of -1, no redundant information tips, not loss is wechat, Android code is simple! We see that the document returns -1 “Possible causes: signature error, unregistered APPID, incorrect project setting APPID, registered APPID does not match the setting, other exceptions, etc.” I repeatedly checked the signature and APPID, and the background also confirmed that the sign information was correct. Alas, I felt very helpless. IOS also called it, and they reminded me that “the payment scene is illegal”.

Alipay access

preparation

Never mind signing up for an account, creating an app, and enabling payment.

Access to the document

Doc.open.alipay.com/docs/doc.ht…

Payment interaction process

Access to the SDK

The SDK download: doc.open.alipay.com/docs/doc.ht… , put the alipaySDK-xxxxXXXX. jar package into the libs folder

AndroidManifest.xml

Add permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Copy the code

Add the statement

<! -- Alipay SDK begin -->
<activity
    android:name="com.alipay.sdk.app.H5PayActivity"
    android:configChanges="orientation|keyboardHidden|navigation"
    android:exported="false"
    android:screenOrientation="behind">
</activity>
<activity
    android:name="com.alipay.sdk.auth.AuthActivity"
    android:configChanges="orientation|keyboardHidden|navigation"
    android:exported="false"
    android:screenOrientation="behind">
</activity>Copy the code

Payment interface call

The payment behavior needs to be executed in a separate, non-UI thread, as follows:

private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /** For the payment result, please rely on the asynchronous notification result of the server. Synchronizes notification results as notification of end of payment only. * /
                    String resultInfo = payResult.getResult();// Synchronization returns the information to be validated
                    String resultStatus = payResult.getResultStatus();
                    // If resultStatus is 9000, the payment is successful
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // Whether the order is actually paid successfully depends on the asynchronous notification from the server.
                        Toast.makeText(mActivity, "Payment successful", Toast.LENGTH_SHORT).show();
                    } else {
                        // The actual payment result of this order depends on the asynchronous notification from the server.
                        Toast.makeText(mActivity, payResult.getMemo(), Toast.LENGTH_SHORT).show();
                    }
                    break;
                }

                default:
                    break; }}; };/** ** alipay */
    private void alipay(final String orderInfo) {
        Runnable payRunnable = new Runnable() {

            @Override
            public void run(a) {
                PayTask alipay = new PayTask(mActivity);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = newMessage(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); }}; Thread payThread =new Thread(payRunnable);
        payThread.start();
    }Copy the code

Also for the sake of safety, orderInfo is backend interface returned here, parameter documentation: doc.open.alipay.com/docs/doc.ht…

This is over. Compared to wechat payment, simple cannot be simpler.

To contact me

My wechat official account: Wu Xiaolong, welcome to follow and exchange ~