Problem with custom Capacitor plugin (Android) getting control back from AutoResolveHelper.resolveTask

I’m creating my first custom Capacitor plugin and although (miraculously) I’ve managed to get the functionality I want working on iOS, I’m having problems on the Android side. I want my plugin to present the user with a GooglePay panel where they see their GooglePay wallet and select a card for a payment etc.

On the iOS side I can present the ApplePay dialog and I have full control if the user cancels or selects a card.

On the Android side as soon as the GooglePay screen opens I see AppPause on the logical console in Android studio and if I cancel out of the payment screen I see some errors (as well as the app doing a resume because it has paused initially). I don’t see ANY console logs from my handleOnActivityResult method which is what I am hoping for.

I’m using the AutoResolveHelper.resolveTask method to display the payment screen - not sure of this is the right way of doing It or not?

And I’m trying to trap the Cancel, Payment and Error events overriding the handleOnActivityResult method (although Android Studio is warning me this is deprecated).

Any help or advice would be appreciated! I am not a Java developer (or native Android developer) !

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.wallet.AutoResolveHelper;
import com.google.android.gms.wallet.PaymentData;
import com.google.android.gms.wallet.PaymentDataRequest;
import com.google.android.gms.wallet.PaymentsClient;
import com.google.android.gms.wallet.Wallet;
import com.google.android.gms.wallet.WalletConstants;

import org.json.JSONException;
import org.json.JSONObject;

@CapacitorPlugin(name = "CapacitorPluginNativePayments")
public class CapacitorPluginNativePaymentsPlugin extends Plugin {

    private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 101;
    private static final String TAG = "CapacitorPayments";

    private PaymentsClient paymentsClient;
    private Activity activity;
    private String merchantId;

    private CapacitorPluginNativePayments implementation = new CapacitorPluginNativePayments();


    @Override
    public void load() {
        // Initialize the PaymentsClient
        activity = getActivity();
        paymentsClient = createPaymentsClient(activity);
    }

    @PluginMethod
    public void echo(PluginCall call) {
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", implementation.echo(value));
        call.resolve(ret);
    }

    @PluginMethod
    public void requestPayment(PluginCall call) {
        Log.d("requestPayment", "requestPayment");

        if (activity == null) {
            call.reject("Activity is null");
            return;
        }

        String paymentDataRequestJsonString = """
                {
                    "apiVersion": 2,
                    "apiVersionMinor": 0,
                    "allowedPaymentMethods": [
                    {
                        "type": "CARD",
                        "parameters": {
                            "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
                            "allowedCardNetworks": ["VISA", "MASTERCARD"],
                            "billingAddressRequired": true,
                            "billingAddressParameters": {
                                "format": "FULL",
                                "phoneNumberRequired": true
                        }
                },
                    "tokenizationSpecification": {
                        "type": "PAYMENT_GATEWAY",
                            "parameters": {
                                "gateway": "barclayssmartpayadvance",
                                "gatewayMerchantId": "xyzzy"
                            }
                        }
                    }
                ],
                "transactionInfo": {
                "totalPrice": "10.00",
                "totalPriceStatus": "FINAL",
                 "currencyCode": "USD"
            },
            "merchantInfo": {
                "merchantId": "",
                "merchantName": "Example Merchant"
            }
        }""";

        try {
            JSONObject paymentDataRequestJson = new JSONObject(paymentDataRequestJsonString);
            // Continue with the next step
        } catch (JSONException e) {
            e.printStackTrace();
            // Handle the JSON parsing error
        }

        try {
            JSONObject paymentDataRequestJson = new JSONObject(paymentDataRequestJsonString);
            PaymentDataRequest paymentDataRequest = PaymentDataRequest.fromJson(paymentDataRequestJson.toString());
            Log.d("requestPayment", "AutoResolveHelper.resolveTask");
            AutoResolveHelper.resolveTask(
                paymentsClient.loadPaymentData(paymentDataRequest),
                activity,
                LOAD_PAYMENT_DATA_REQUEST_CODE
        );
            // Use the paymentDataRequest object as needed
        } catch (JSONException e) {
            e.printStackTrace();
            // Handle the JSON parsing error
        }
        JSObject ret = new JSObject();
        ret.put("value", true);
        call.resolve(ret);
    }

    private PaymentsClient createPaymentsClient(Activity activity) {
        Wallet.WalletOptions walletOptions = new Wallet.WalletOptions.Builder()
                .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
                .build();

        return Wallet.getPaymentsClient(activity, walletOptions);
    }
    @Override
    protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
        super.handleOnActivityResult(requestCode, resultCode, data);
        Log.d("handleOnActivityResult","");
        if (requestCode == LOAD_PAYMENT_DATA_REQUEST_CODE) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    Log.d("handleOnActivityResult", "OK");
                    PaymentData paymentData = PaymentData.getFromIntent(data);
                    String paymentInfo = extractPaymentInfo(paymentData);
                    Log.d(" paymentInfo", paymentInfo);
                    // Handle the encrypted payment payload
                    // ...

                    break;
                case Activity.RESULT_CANCELED:
                    Log.d("handleOnActivityResult", "CANCELLED");
                    // The user canceled the payment process
                    break;
                case AutoResolveHelper.RESULT_ERROR:
                    Log.d("handleOnActivityResult", "ERROR");
                    // An error occurred during the payment process
                    if (data != null) {
                        Status status = AutoResolveHelper.getStatusFromIntent(data);
                        if (status != null) {
                            String errorMessage = status.getStatusMessage();
                            if (errorMessage != null) {
                                Log.e(TAG, "Payment failed. Error: " + errorMessage);
                            } else {
                                Log.e(TAG, "Payment failed. Unknown error occurred.");
                            }
                        } else {
                            Log.e(TAG, "Payment failed. Unknown status.");
                        }
                    } else {
                        Log.e(TAG, "Payment failed. No data received.");
                    }
                    break;
                default:
                    // Unknown result code
                    break;
            }
        }
    }

    private String extractPaymentInfo(PaymentData paymentData) {
        // Extract the necessary payment information from PaymentData object
        // ...

        //return paymentInfo;
        return "";
    }

}

There are the errors I see in the LogCat console when I click cancel on the Payment Screen (there are a lot more messages in between these but these are the two errors in red)

updateAcquireFence: Did not find frame.
Error binding to custom tabs service