-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
cb7a9be
commit 6303e73
Showing
29 changed files
with
1,306 additions
and
1,585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
.../src/main/java/com/braintreepayments/api/googlepay/BraintreeGooglePayWalletConstants.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
...ay/src/main/java/com/braintreepayments/api/googlepay/BraintreeGooglePayWalletConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
59 changes: 0 additions & 59 deletions
59
GooglePay/src/main/java/com/braintreepayments/api/googlepay/GooglePayActivity.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
GooglePay/src/main/java/com/braintreepayments/api/googlepay/GooglePayActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
...ay/src/main/java/com/braintreepayments/api/googlepay/GooglePayActivityResultContract.java
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
...ePay/src/main/java/com/braintreepayments/api/googlepay/GooglePayActivityResultContract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
GooglePay/src/main/java/com/braintreepayments/api/googlepay/GooglePayCapabilities.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
GooglePay/src/main/java/com/braintreepayments/api/googlepay/GooglePayCapabilities.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.