https://github.com/google-pay/compose-pay-button
An Android library that provides a Jetpack Compose wrapper on top of the Google Pay Button API.
https://github.com/google-pay/compose-pay-button
google-pay jetpack-compose
Last synced: about 1 year ago
JSON representation
An Android library that provides a Jetpack Compose wrapper on top of the Google Pay Button API.
- Host: GitHub
- URL: https://github.com/google-pay/compose-pay-button
- Owner: google-pay
- License: apache-2.0
- Created: 2023-05-09T00:26:44.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-26T11:48:52.000Z (about 1 year ago)
- Last Synced: 2025-05-26T12:54:53.595Z (about 1 year ago)
- Topics: google-pay, jetpack-compose
- Language: Kotlin
- Homepage:
- Size: 769 KB
- Stars: 38
- Watchers: 4
- Forks: 5
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Google Pay button for Jetpack Compose
[](https://search.maven.org/search?q=g:com.google.pay.button)
An Android library that provides a [Jetpack Compose](https://developer.android.com/jetpack/compose) wrapper on top of the
[Google Pay Button API](https://developers.google.com/pay/api/android/guides/tutorial).
## Installation
The library is hosted on Maven central and can be used by ensuring the following lines exist in each gradle file:
**build.gradle:**
```groovy
repositories {
mavenCentral()
}
```
**app/build.gradle:**
```
dependencies {
implementation "com.google.pay.button:compose-pay-button:"
}
```
## Usage
```kotlin
// other imports omitted for brevity
// see full example in the "app" directory
import com.google.pay.button.ButtonTheme
import com.google.pay.button.ButtonType
import com.google.pay.button.PayButton
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val onClick = { println("Button clicked") }
// as per https://developers.google.com/pay/api/android/reference/request-objects#PaymentMethod
val allowedPaymentMethods = """
[
{
"type": "CARD",
"parameters": {
"allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
"allowedCardNetworks": ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"]
},
"tokenizationSpecification": {
"type": "PAYMENT_GATEWAY",
"parameters": {
"gateway": "example",
"gatewayMerchantId": "exampleGatewayMerchantId"
}
}
}
]
""".trimIndent()
setContent {
// Default
PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods)
// Customized look
PayButton(
onClick = onClick,
allowedPaymentMethods = allowedPaymentMethods,
radius = 1.dp,
modifier = Modifier.width(200.dp),
theme = ButtonTheme.Light
)
// Customized labels
PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods, type = ButtonType.Book)
PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods, type = ButtonType.Subscribe)
PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods, type = ButtonType.Donate)
}
}
}
```
## Error handling and Fallback UI
The `PayButton` composable wraps the underlying `PayButton` Android View, which may encounter errors during initialization. To handle these situations, the `PayButton` composable provides:
* **`onError` Callback:** Invoked when an error occurs during the button's initialization. This callback receives the `Throwable` that caused the error, allowing you to log it or take other actions.
* **`fallbackUi` Composable:** An optional composable function that is displayed in place of the button if an error occurs. If not provided, nothing will be displayed in case of error.
This mechanism ensures that your app can gracefully handle potential issues with the `PayButton` and provide a fallback experience to the user.