#Haresh-googlepay

1 messages · Page 1 of 1 (latest)

brittle ivy
#

that means your googlePayButton variable is null.

delicate current
#

yes

brittle ivy
#

you can see in the example code that you're likely using that it gets it from findViewById<Button>(R.id.google_pay_button) , so you have to add a button to your layout XMLs for that.

delicate current
#

Already added my XML code i am using AppCompatButton but i click to call googlePayLauncher.present("gbp", 25) that time showing error

brittle ivy
#

can you share the full code you have? including where you assign something to initialise the googlePayButton variable?

delicate current
#

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/dasboad_layout_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/googlePayButton"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/welcome_button_color"
    android:fontFamily="@font/hindvadodara_bold"
    android:gravity="center"
    android:text="Pay Now"
    android:textAllCaps="false"
    android:textColor="@color/white"
    android:textSize="@dimen/_15sdp" />

</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

#

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getTheme().applyStyle(R.style.OverlayThemeLime, true);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
setContentView(R.layout.activity_google_payment);
/---------------------------hind actionbar----------------------------------------------------/
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
overridePendingTransition(R.anim.enter, R.anim.exit);

    PaymentConfiguration.init(Googlepay_Activity.this, "pk_test_9CnYE16SY0ju0M4GcnOBHzku00gC8VQDPF");

    stripe = new Stripe(
            getApplicationContext(),
            Objects.requireNonNull("pk_test_9CnYE16SY0ju0M4GcnOBHzku00gC8VQDPF")
    );
    googlePayButton = findViewById(R.id.google_pay_button);


    final GooglePayPaymentMethodLauncher googlePayLauncher = new GooglePayPaymentMethodLauncher(
            Googlepay_Activity.this,
            new GooglePayPaymentMethodLauncher.Config(
                    GooglePayEnvironment.Test,
                    "US",
                    "Widget Store"
            ),
            Googlepay_Activity.this::onGooglePayReady,
            Googlepay_Activity.this::onGooglePayResult
    );

    googlePayButton.setOnClickListener(
            v -> googlePayLauncher.present("gbp", 25)
    );


}
#

private void onGooglePayReady(boolean isReady) {
// googlePayButton.setEnabled(isReady);
}

private void onGooglePayResult(@NotNull GooglePayPaymentMethodLauncher.Result result) {

    Log.e("paymentresult", "" + result);


    if (result instanceof GooglePayPaymentMethodLauncher.Result.Completed) {
        // Payment details successfully captured.
        // Send the paymentMethodId to your server to finalize payment.
        final String paymentMethodId =
                ((GooglePayPaymentMethodLauncher.Result.Completed) result).getPaymentMethod().id;
        Log.e("paymentMethodId", "" + paymentMethodId);

    } else if (result instanceof GooglePayPaymentMethodLauncher.Result.Canceled) {
        // User cancelled the operation
    } else if (result instanceof GooglePayPaymentMethodLauncher.Result.Failed) {
        // Operation failed; inspect `result.getError()` for the exception
    }
}
brittle ivy
#

android:id="@+id/googlePayButton"

#

in your XML

#

that should either be android:id="@+id/google_pay_button" instead

#

or change the Kotlin to be googlePayButton = findViewById(R.id.googlePayButton); to match