Before Unity can connect to wechat Pay, it first needs to solve the problem of connecting to the wechat SDK. I won’t go into detail about this part of this article. For details, see the article I wrote before: Unity access to the third party Android SDK — wechat sharing, but first read this article and then go back to the wechat sharing article, so that the process down.

  1. The first half of Android Library’s package name should be the same as Unity’s package name. For example, if Unity’s package name is com.aaa.bbb, Android Library’s package name should be com.aaa.bbb.ccc.

  2. Regarding the payment callback, wechat requires that the WXPayEntryActivity class must be implemented in the com.aaA.bbB. wxAPI package path (inconsistent package name or class name will cause no callback), and the onResp function should be implemented in the WXPayEntryActivity class. After the payment is completed, The wechat APP returns to the merchant APP and calls back the onResp function.

    The process for creating com.aaA.bbB. wxAPI path is as follows:

Uncheck Campact Middle Packages firstAnd then it looks like thisCreate a new Package on BBB and name it wxAPI

  1. Be sure to include the following in the Library’s AndroidManifest.xml
<activity android:name="com.titianai.hanzi.wxapi.WXPayEntryActivity"
            android:exported="true"
            android:launchMode="singleTop"></activity>
Copy the code

Android: Exported =”true”; otherwise, the callback will not be received. Set it to true, indicating that the Activity supports cross-process communication, so that wechat can communicate with the APP. 支那

  1. The rest is the implementation of WXPayEntryActivity, this part just refer to the source code of wechat SDK to do it, do not make big changes, the following can post my run successful code, not necessarily the most concise, fortunately, the final function to achieve normal:
package com.aaa.bbb.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.titianai.hanzi.wechat.MainActivity;
import com.titianai.hanzi.wechat.WeChatController;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, appid);// This appID should be filled according to the actual project
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {}@Override
    public void onResp(BaseResp resp) {
        int result = 0;
        if (resp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){
            if (resp.errCode==0){
                Toast.makeText(this."Successful payment",Toast.LENGTH_LONG).show();
            }else if (resp.errCode==-2){
                Toast.makeText(this."Cancel payment",Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(this."Payment failure",Toast.LENGTH_LONG).show();
            }
            // Customize the operation after getting the callback result of wechat Pay, please implement according to your own projectMainActivity.WXPayResult(resp.errCode); finish(); }}}Copy the code