Skip to content

Commit

Permalink
Googlepay kotlin conversion (#1114)
Browse files Browse the repository at this point in the history
* Convert GooglePay module classess to Kotlin

* Fix unit tests

* Suppress warnings as refactoring is required

* Update variable name

* Improve null and emtpy string check

* Remove doulbe exclamations

* Parcelize GooglePayRequest using kotlin-parcelize plugin

* Improve return block in GooglePayCapabilities

---------

Co-authored-by: Ching-Hsiang Lin <[email protected]>
  • Loading branch information
chinghlinn and Ching-Hsiang Lin authored Aug 28, 2024
1 parent cb7a9be commit 6303e73
Show file tree
Hide file tree
Showing 29 changed files with 1,306 additions and 1,585 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
* Change `GooglePayGetTokenizationParametersCallback` parameters
* Rename `GooglePayLauncherCallback#onResult` to
`GooglePayLauncherCallback#onGooglePayLauncherResult`
* Change `GooglePayRequest#isCreditCardsAllowed` to `GooglePayRequest#getAllowCreditCards`
* ThreeDSecure
* Remove `ThreeDSecureListener`
* Add `ThreeDSecureLauncher`, `ThreeDSecurePaymentAuthResult`,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.braintreepayments.api.googlepay

import com.google.android.gms.wallet.WalletConstants

/**
* Collection of constant values used by the Braintree SDK Google Payment module. Extends upon
* com.google.android.gms.wallet.WalletConstants.
*/
internal object BraintreeGooglePayWalletConstants {
/**
* Card network Elo.
*/
const val CARD_NETWORK_ELO: Int = WalletConstants.CARD_NETWORK_OTHER + 1
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.braintreepayments.api.googlepay

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.wallet.AutoResolveHelper
import com.google.android.gms.wallet.PaymentDataRequest
import com.google.android.gms.wallet.Wallet
import com.google.android.gms.wallet.Wallet.WalletOptions
import com.google.android.gms.wallet.WalletConstants

class GooglePayActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (savedInstanceState != null && savedInstanceState.getBoolean(EXTRA_RECREATING)) {
return
}

val paymentsClient = Wallet.getPaymentsClient(
this, WalletOptions.Builder()
.setEnvironment(
intent.getIntExtra(
EXTRA_ENVIRONMENT,
WalletConstants.ENVIRONMENT_TEST
)
)
.build()
)

val request = intent.getParcelableExtra<PaymentDataRequest>(EXTRA_PAYMENT_DATA_REQUEST)
if (request != null) {
AutoResolveHelper.resolveTask(paymentsClient.loadPaymentData(request), this, REQUEST_CODE)
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(EXTRA_RECREATING, true)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

setResult(resultCode, data)
finish()
}

override fun finish() {
super.finish()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}

companion object {
const val EXTRA_ENVIRONMENT: String =
"com.braintreepayments.api.EXTRA_ENVIRONMENT"
const val EXTRA_PAYMENT_DATA_REQUEST: String =
"com.braintreepayments.api.EXTRA_PAYMENT_DATA_REQUEST"

private const val EXTRA_RECREATING = "com.braintreepayments.api.EXTRA_RECREATING"

private const val REQUEST_CODE = 1
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.braintreepayments.api.googlepay

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.braintreepayments.api.core.BraintreeException
import com.braintreepayments.api.core.UserCanceledException
import com.google.android.gms.wallet.AutoResolveHelper
import com.google.android.gms.wallet.PaymentData

internal class GooglePayActivityResultContract :
ActivityResultContract<GooglePayPaymentAuthRequestParams, GooglePayPaymentAuthResult>() {
override fun createIntent(context: Context, input: GooglePayPaymentAuthRequestParams): Intent {
return Intent(context, GooglePayActivity::class.java)
.putExtra(GooglePayClient.EXTRA_ENVIRONMENT, input.googlePayEnvironment)
.putExtra(GooglePayClient.EXTRA_PAYMENT_DATA_REQUEST, input.paymentDataRequest)
}

override fun parseResult(resultCode: Int, intent: Intent?): GooglePayPaymentAuthResult {
when (resultCode) {
Activity.RESULT_OK -> {
if (intent != null) {
return GooglePayPaymentAuthResult(PaymentData.getFromIntent(intent), null)
}
}
Activity.RESULT_CANCELED -> {
return GooglePayPaymentAuthResult(
null,
UserCanceledException("User canceled Google Pay.")
)
}
AutoResolveHelper.RESULT_ERROR -> {
if (intent != null) {
return GooglePayPaymentAuthResult(
null,
GooglePayException(
"An error was encountered during the Google Pay " +
"flow. See the status object in this exception for more details.",
AutoResolveHelper.getStatusFromIntent(intent)
)
)
}
}
}

return GooglePayPaymentAuthResult(null, BraintreeException("An unexpected error occurred."))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.braintreepayments.api.googlepay

import android.content.Context
import com.braintreepayments.api.core.Configuration
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.wallet.Wallet

/**
* Class representing Google Pay payment capabilities
*/
object GooglePayCapabilities {
/**
* @return `true` if Google Pay is enabled and supported in the current environment,
* `false` otherwise. Note: this value only pertains to the Braintree configuration, to check if
* the user has Google Pay setup use [GooglePayClient.isReadyToPay]
*/
@SuppressWarnings("SwallowedException")
fun isGooglePayEnabled(context: Context, configuration: Configuration): Boolean {
return try {
Class.forName(Wallet::class.java.name)

configuration.isGooglePayEnabled && GoogleApiAvailability.getInstance()
.isGooglePlayServicesAvailable(context) ==
ConnectionResult.SUCCESS
} catch (e: ClassNotFoundException) {
false
} catch (e: NoClassDefFoundError) {
false
}
}
}
Loading

0 comments on commit 6303e73

Please sign in to comment.