certified84 / GooglePay

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GooglePay Library

Jit

A wrapper over the Google Pay API

Setup

Add it in your root build.gradle.kts at the end of repositories:

      allprojects {
          repositories {
              //...omitted for brevity
              maven(url = "https://jitpack.io")
          }
      }

Add the dependency

      dependencies {
          implementation("com.github.certified84:GooglePay:$latest_release")
          implementation ("com.google.android.gms:play-services-wallet:19.2.1")
      }

💡 Tech Used

Usage

Sample implementation here

BuyWithGooglePay

  • Add BuyWithGooglePay to your xml layout.
        <com.certified.google_pay.BuyWithGooglePay
            android:id="@+id/btn_google_pay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
  • Align the button properly to suit your needs

Initialize the indicator

      var buyWithGoogleButton: BuyWithGooglePay = findViewById(R.id.btn_google_pay)
      // ViewBinding
      var buyWithGoogleButton = binding.btnGooglePay

Call the initialize() method

      /*
        * This step is required
        * Pass the current to the initialize method
        * You'll need to override onActivityResult too
      /*

      buyWithGoogleButton.initialize(requireActivity())

Set the price and shipping cost

     buyWithGoogleButton.setPrice(200)
     buyWithGoogleButton.setShippingCost(50)
     // OR
     buyWithGoogleButton.price = 200
     buyWithGoogleButton.shippingCost = 50

Set the indicator text color

      buyWithGoogleButton.setTextColor(ResourcesCompat.getColorStateList(resources, R.color.white, null)!!)

Set the text typeface

       buyWithGoogleButton.setTypeface(R.font.space_grotesk_regular)

Override the onActivityResult

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            // Value passed in AutoResolveHelper
            GOOGLE_PAY_REQUEST_CODE -> {
                when (resultCode) {
                    RESULT_OK ->
                        data?.let { intent ->
                            PaymentData.getFromIntent(intent)?.let(::handlePaymentSuccess)
                        }

                    RESULT_CANCELED -> {
                        // The user cancelled the payment attempt
                    }

                    AutoResolveHelper.RESULT_ERROR -> {
                        AutoResolveHelper.getStatusFromIntent(data)?.let {
                            handleError(it.statusCode)
                        }
                    }
                }
            }
        }
    }

Add methods to handle payment success and error

   indicator.stopAnimation()
   
   // Always stop the animation in onPause()
   fun handlePaymentSuccess(paymentData: PaymentData) {
       //....perform an action
   }

   fun handleError(statusCode: Int) {
        //...perform an action e.g
        Log.w("loadPaymentData failed", "Error code: $statusCode")
    }

Misc

Contribute 🤝

Licensed under the Apache-2.0 License


Copyright 2023 Samson Achiaga

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

License:Apache License 2.0


Languages

Language:Kotlin 100.0%