-
Notifications
You must be signed in to change notification settings - Fork 263
using Google Pay button API in demo app #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
saralvasquez
merged 4 commits into
braintree:main
from
dmengelt:demo-app-google-pay-button
Jun 16, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5fe2d0f
using Google Pay button API in demo app
dmengelt 68d3ded
Merge branch 'main' into demo-app-google-pay-button
saralvasquez 2454145
Apply suggestion from @saralvasquez
saralvasquez 07b115e
Merge branch 'main' into demo-app-google-pay-button
saralvasquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
121 changes: 0 additions & 121 deletions
121
Demo/src/main/java/com/braintreepayments/demo/GooglePayFragment.java
This file was deleted.
Oops, something went wrong.
176 changes: 176 additions & 0 deletions
176
Demo/src/main/java/com/braintreepayments/demo/GooglePayFragment.kt
This file contains hidden or 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,176 @@ | ||
| package com.braintreepayments.demo | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.width | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.ComposeView | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.navigation.fragment.findNavController | ||
| import androidx.navigation.fragment.navArgs | ||
| import com.braintreepayments.api.core.PaymentMethodNonce | ||
| import com.braintreepayments.api.core.UserCanceledException | ||
| import com.braintreepayments.api.googlepay.GooglePayBillingAddressFormat | ||
| import com.braintreepayments.api.googlepay.GooglePayCheckoutOption | ||
| import com.braintreepayments.api.googlepay.GooglePayClient | ||
| import com.braintreepayments.api.googlepay.GooglePayLauncher | ||
| import com.braintreepayments.api.googlepay.GooglePayPaymentAuthRequest | ||
| import com.braintreepayments.api.googlepay.GooglePayReadinessResult | ||
| import com.braintreepayments.api.googlepay.GooglePayRequest | ||
| import com.braintreepayments.api.googlepay.GooglePayResult | ||
| import com.braintreepayments.api.googlepay.GooglePayShippingAddressParameters | ||
| import com.braintreepayments.api.googlepay.GooglePayTotalPriceStatus | ||
| import com.google.pay.button.ButtonType | ||
| import com.google.pay.button.PayButton | ||
| import androidx.compose.foundation.layout.Column | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
|
|
||
| class GooglePayFragment : BaseFragment() { | ||
|
|
||
| private lateinit var googlePayClient: GooglePayClient | ||
| private lateinit var googlePayLauncher: GooglePayLauncher | ||
|
|
||
| private val args: GooglePayFragmentArgs by navArgs() | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View { | ||
| googlePayClient = GooglePayClient(requireContext(), args.authString) | ||
| googlePayLauncher = GooglePayLauncher(this) { paymentAuthResult -> | ||
| googlePayClient.tokenize(paymentAuthResult) { googlePayResult -> | ||
| when (googlePayResult) { | ||
| is GooglePayResult.Failure -> handleError(googlePayResult.error) | ||
| is GooglePayResult.Success -> handleGooglePayActivityResult(googlePayResult.nonce) | ||
| is GooglePayResult.Cancel -> handleError(UserCanceledException("User canceled Google Pay")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ComposeView(requireContext()).apply { | ||
| setContent { | ||
| GooglePayScreen() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| fun GooglePayScreen() { | ||
| var isReadyToPay by remember { mutableStateOf(false) } | ||
| var allowedPaymentMethods by remember { mutableStateOf("") } | ||
|
|
||
| LaunchedEffect(Unit) { | ||
| googlePayClient.isReadyToPay(requireActivity()) { result -> | ||
| if (result is GooglePayReadinessResult.ReadyToPay) { | ||
| isReadyToPay = true | ||
| // Note: We avoid createPaymentAuthRequest here to prevent unnecessary analytics noise. | ||
| // Instead, we assume common card networks are supported for the demo. | ||
| val cardParams = JSONObject() | ||
| .put( | ||
| "allowedAuthMethods", | ||
| JSONArray().put("PAN_ONLY").put("CRYPTOGRAM_3DS") | ||
| ) | ||
| .put( | ||
| "allowedCardNetworks", | ||
| JSONArray().put("VISA").put("MASTERCARD").put("AMEX").put("DISCOVER") | ||
| .put("JCB") | ||
| ) | ||
|
|
||
| allowedPaymentMethods = JSONArray().put( | ||
| JSONObject().put("type", "CARD").put("parameters", cardParams) | ||
| ).toString() | ||
| } else { | ||
| showDialog( | ||
| "Google Pay is not available. The following issues could be the cause:\n\n" + | ||
| "No user is logged in to the device.\n\n" + | ||
| "Google Play Services is missing or out of date." | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Box( | ||
| modifier = Modifier | ||
| .fillMaxSize() | ||
| .padding(16.dp), | ||
| contentAlignment = Alignment.Center | ||
| ) { | ||
| Column(horizontalAlignment = Alignment.CenterHorizontally) { | ||
| if (isReadyToPay) { | ||
| if (allowedPaymentMethods.isNotEmpty()) { | ||
| PayButton( | ||
| modifier = Modifier.width(280.dp), | ||
| onClick = { launchGooglePay() }, | ||
| allowedPaymentMethods = allowedPaymentMethods, | ||
| type = ButtonType.Buy | ||
| ) | ||
| } else { | ||
| LaunchedEffect(Unit) { | ||
| showDialog( | ||
| "allowedPaymentMethods cannot be empty.\n\n" + | ||
| "Please configure allowedPaymentMethods (e.g. CARD) " + | ||
| "to display the Google Pay button." | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun launchGooglePay() { | ||
| val activity = requireActivity() | ||
| val googlePayRequest = GooglePayRequest( | ||
| Settings.getGooglePayCurrency(activity), | ||
| "1.00", | ||
| GooglePayTotalPriceStatus.TOTAL_PRICE_STATUS_FINAL | ||
| ).apply { | ||
| totalPriceLabel = "Braintree Demo Payment" | ||
| allowPrepaidCards = Settings.areGooglePayPrepaidCardsAllowed(activity) | ||
| billingAddressFormat = GooglePayBillingAddressFormat.FULL | ||
| isBillingAddressRequired = Settings.isGooglePayBillingAddressRequired(activity) | ||
| isEmailRequired = Settings.isGooglePayEmailRequired(activity) | ||
| isPhoneNumberRequired = Settings.isGooglePayPhoneNumberRequired(activity) | ||
| isShippingAddressRequired = Settings.isGooglePayShippingAddressRequired(activity) | ||
| shippingAddressParameters = GooglePayShippingAddressParameters( | ||
| Settings.getGooglePayAllowedCountriesForShipping(requireContext()) | ||
| ) | ||
| checkoutOption = GooglePayCheckoutOption.COMPLETE_IMMEDIATE_PURCHASE | ||
| } | ||
|
|
||
| googlePayClient.createPaymentAuthRequest(googlePayRequest) { paymentAuthRequest -> | ||
| when (paymentAuthRequest) { | ||
| is GooglePayPaymentAuthRequest.ReadyToLaunch -> { | ||
| googlePayLauncher.launch(paymentAuthRequest) | ||
| } | ||
|
|
||
| is GooglePayPaymentAuthRequest.Failure -> { | ||
| handleError(paymentAuthRequest.error) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun handleGooglePayActivityResult(paymentMethodNonce: PaymentMethodNonce) { | ||
| super.onPaymentMethodNonceCreated(paymentMethodNonce) | ||
|
|
||
| val action = GooglePayFragmentDirections.actionGooglePayFragmentToDisplayNonceFragment( | ||
| paymentMethodNonce | ||
| ) | ||
| findNavController().navigate(action) | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this rename isn't strictly necessary could you please leave this as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can revert this but
playServicesis misleading IMO 🙂 WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree it's not the most intuitive naming. I generally try to keep unnecessary changes to a minimum since it's critical to keep current integrations functioning as expected but I was too hasty here. Renaming something in this file should be 100% safe since it's only internally accessible. Feel free to leave it