Search code examples
androidpaymentwechat

WeChat InApp payment screen shows up only for first time


I'm trying to implement WeChat InApp payments in our app. But we are struggling to make it work.

I will try to sum it up real quick.

  1. Given user is not logged in, WeChat login screen show up every time.
  2. Given user is logged in, when clicked on pay button for a first time, WeChat order info screen shows up, but when clicked back, and clicked on pay button again (in our app), WeChat screen doesn’t show up.

We did implemented WXPayEntryActivity but neither onCreate, onNewIntent nor onResp are called. And yes, this activity is sending broadcast but neither toast nor log shows up.

I tried call registerApp on application started, I tried it just before creating payment req.

Did anybody come across this issue?

Can WeChat help me directly?


Want to see some code?

This is my payment class

public class WXInAppPayment {

    public void startPayment(AppCompatActivity activity, PaymentDataResponse data) {
        IWXAPI api = getApi(activity);
        if (api.isWXAppInstalled()) {
            api.sendReq(getPayRequest(data));
        } else {
            // Showing toast
        }
    }

    public WXReceiver getReceiver() {
        // returning BR for wechat payments
        return new WXReceiver();
    }

    public IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.WE_CHAT_BR_ID);
        return intentFilter;
    }

    private IWXAPI getApi(AppCompatActivity activity) {
        final IWXAPI api = WXAPIFactory.createWXAPI(activity, null);
        api.registerApp(Constants.WE_CHAT_APP_ID);
        return api;
    }

    private PayReq getPayRequest(PaymentDataResponse data) {
        PayReq request = new PayReq();
        request.appId = dataFromAPI.appId;
        request.partnerId = dataFromAPI.partnerId;
        request.prepayId = dataFromAPI.prepayId;
        request.packageValue = dataFromAPI.packageValue;
        request.nonceStr = dataFromAPI.nonceStr;
        request.timeStamp = dataFromAPI.timestimeStampamp;
        request.sign = dataFromAPI.sign;
        return request;
    }
}

And this is WXPayEntryActivity. In manifest:

<activity android:name=".wxapi.WXPayEntryActivity"
          android:label="@string/app_name"
          android:exported="true"/>

And class:

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private final String TAG = getClass().getSimpleName();
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, Constants.WE_CHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

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

    @Override
    public void onReq(BaseReq baseReq) {
        Log.e(TAG, "onReq: " + baseReq.transaction);
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.e(TAG, "onResp: " + baseResp.errStr + " " + baseResp.errCode);
        Intent intent = new Intent(Constants.WE_CHAT_BR_ID);
        intent.putExtra("error_code", baseResp.errCode);
        intent.putExtra("error_string", baseResp.errStr);
        sendBroadcast(intent);
        finish();
    }
}

Solution

  • I went through same issue... Your code look fine. lets cover the scenario:

    • This is normal ... if user is not logged in.. Wechat App will redirect to login screen
    • "Only first time payment passed" happened due to wrong packageName. consider these checks:
    • You need to use ApplicationId not packageName
    • WhiteSpace
    • Debug buildType by default has suffix: .debug to applicatonId
    • Check AppSign which is MD5 of cert you sign with.. Be careful not to use the default one for debug buildType.
    • Try to reassign ApplicationId and AppSign it again.(that was our issue 😞) due to hidden WS not visible.
    • Contact Wechat team support.. they have logs to payment.