Collecting payment information through Google Pay

    Allow customers to securely make payments using Google Pay on their Android devices. Google Pay allows customers to make payments in your app or website using any credit or debit card saved to their Google Account, including ones from Google Play, YouTube, Chrome or an Android device.

    To use Google Pay, first enable the Android Pay API by adding the following to the <application> tag of your AndroidManifest.xml:

        android:value="true" />
    You must also be using Google Play Services 11.4.0 or later:
    dependencies {
      compile ''
      // You'll also need the support-v4 and appcompat-v7 libraries
      compile ''
      compile ''

    Next, create an instance of PaymentsClient in your Activity. This allows you to access the Payments APIs. Do this before you start the payment flow, such as in onCreate.

    PaymentsClient paymentsClient =
        new Wallet.WalletOptions.Builder().setEnvironment(WalletConstants.ENVIRONMENT_TEST)

    After creatingPaymentsClient, create an instance of IsReadyToPayRequest to see whether or not to display Google Pay as an option.

    private void isReadyToPay() {
      IsReadyToPayRequest request = IsReadyToPayRequest.newBuilder()
      Task<Boolean> task = paymentsClient.isReadyToPay(request);
          new OnCompleteListener<Boolean>() {
            public void onComplete(Task<Boolean> task) {
              try {
                boolean result =
                if(result == true) {
                  //show Google as payment option
                } else {
                  //hide Google as payment option
              } catch (ApiException exception) { }

    When the user is ready to pay, create a PaymentDataRequest object. The PaymentDataRequest object is a Parcelable representing a request for methods of payment (such as a credit card) and other details. It provides the necessary information to support a payment.

    To create a PaymentDataRequest object using Stripe as your gateway, create a PaymentMethodTokenizationParameters object with your credentials.

    private PaymentMethodTokenizationParameters createTokenizationParameters() {
      return PaymentMethodTokenizationParameters.newBuilder()
        .addParameter("gateway", "stripe")
        .addParameter("stripe:publishableKey", "pk_test_6pRNASCoBOKtIshFeQd4XMUh")
        .addParameter("stripe:version", "5.1.0")

    Once you have your tokenization parameters, create the PaymentDataRequest object with the information relevant to the purchase.

    private PaymentDataRequest createPaymentDataRequest() {
      PaymentDataRequest.Builder request =

    Now that you’ve established a way to create the payment data request, hook up that method to your “Buy” button’s click handler.

      new View.OnClickListener() {
        public void onClick(View view) {
          PaymentDataRequest request = createPaymentDataRequest();
          if (request != null) {
              // LOAD_PAYMENT_DATA_REQUEST_CODE is a constant integer of your choice,
              // similar to what you would use in startActivityForResult

    Invoking the AutoResolveHelper’s resolveTask function with a PaymentDataRequest will launch Google Pay. In order to handle the result of this action, listen for it in the onActivityResult function of your Activity.

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      switch (requestCode) {
          switch (resultCode) {
            case Activity.RESULT_OK:
              PaymentData paymentData = PaymentData.getFromIntent(data);
              // You can get some data on the user's card, such as the brand and last 4 digits
              CardInfo info = paymentData.getCardInfo();
              // You can also pull the user address from the PaymentData object.
              UserAddress address = paymentData.getShippingAddress();
              // This is the raw JSON string version of your Stripe token.
              String rawToken = paymentData.getPaymentMethodToken().getToken();
              // Now that you have a Stripe token object, charge that by using the id
              Token stripeToken = Token.fromString(rawToken);
              if (stripeToken != null) {
                  // This chargeToken function is a call to your own server, which should then connect
                  // to Stripe's API to finish the charge.
            case Activity.RESULT_CANCELED:
            case AutoResolveHelper.RESULT_ERROR:
              Status status = AutoResolveHelper.getStatusFromIntent(data);
               // Log the status for debugging
               // Generally there is no need to show an error to
               // the user as the Google Payment API will do that
              // Do nothing.
        break; // Breaks the case LOAD_PAYMENT_DATA_REQUEST_CODE
          // Handle any other startActivityForResult calls you may have made.
          // Do nothing.