Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ build/
*.apk

*.pem
.kotlin
.kotlin
kotlin-js-store/
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

plugins {
alias(libs.plugins.nexusPublishingPlugin)
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.compose.multiplatform) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.androidApplication) apply false
}

apply from: "${rootDir}/scripts/publish-root.gradle"
56 changes: 47 additions & 9 deletions compose-pay-button/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/

plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.compose.multiplatform)
}

ext {
Expand Down Expand Up @@ -50,9 +51,6 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}

buildFeatures {
compose true
Expand All @@ -71,10 +69,50 @@ android {
targetSdk 37
}
}
kotlin {
androidTarget {
publishLibraryVariants("release")
}
js(IR) {
browser()
}
wasmJs {
browser()
}

dependencies {
implementation libs.playServices.wallet
implementation libs.compose.ui
implementation libs.compose.material
implementation libs.androidx.core
sourceSets {
commonMain {
dependencies {
implementation compose.runtime
implementation compose.ui
implementation compose.foundation
implementation compose.material
implementation compose.components.resources
}
}
androidMain {
dependencies {
implementation libs.playServices.wallet
implementation libs.androidx.core
}
}
jsMain {
dependencies {
implementation compose.ui
}
}
wasmJsMain {
dependencies {
implementation compose.ui
}
}
}
}

tasks.register("printWasmJsClasspath") {
doLast {
configurations.getByName("wasmJsCompileClasspath").files.forEach {
println(it.absolutePath)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,44 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.gms.wallet.button.ButtonConstants
import com.google.android.gms.wallet.button.ButtonOptions
import com.google.android.gms.wallet.button.PayButton as GmsPayButton

enum class ButtonTheme(val value: Int) {
Dark(ButtonConstants.ButtonTheme.DARK),
Light(ButtonConstants.ButtonTheme.LIGHT),
}
private const val FULL_ALPHA = 1f
private const val HALF_ALPHA = 0.5f

enum class ButtonType(val value: Int) {
Book(ButtonConstants.ButtonType.BOOK),
Buy(ButtonConstants.ButtonType.BUY),
Checkout(ButtonConstants.ButtonType.CHECKOUT),
Donate(ButtonConstants.ButtonType.DONATE),
Order(ButtonConstants.ButtonType.ORDER),
Pay(ButtonConstants.ButtonType.PAY),
Plain(ButtonConstants.ButtonType.PLAIN),
Subscribe(ButtonConstants.ButtonType.SUBSCRIBE),
PIX(ButtonConstants.ButtonType.PIX),
EWALLET(ButtonConstants.ButtonType.EWALLET)
private fun ButtonTheme.toAndroidValue(): Int = when (this) {
ButtonTheme.Dark -> ButtonConstants.ButtonTheme.DARK
ButtonTheme.Light -> ButtonConstants.ButtonTheme.LIGHT
}

private const val FULL_ALPHA = 1f
private const val HALF_ALPHA = 0.5f
private fun ButtonType.toAndroidValue(): Int = when (this) {
ButtonType.Book -> ButtonConstants.ButtonType.BOOK
ButtonType.Buy -> ButtonConstants.ButtonType.BUY
ButtonType.Checkout -> ButtonConstants.ButtonType.CHECKOUT
ButtonType.Donate -> ButtonConstants.ButtonType.DONATE
ButtonType.Order -> ButtonConstants.ButtonType.ORDER
ButtonType.Pay -> ButtonConstants.ButtonType.PAY
ButtonType.Plain -> ButtonConstants.ButtonType.PLAIN
ButtonType.Subscribe -> ButtonConstants.ButtonType.SUBSCRIBE
ButtonType.PIX -> ButtonConstants.ButtonType.PIX
ButtonType.EWALLET -> ButtonConstants.ButtonType.EWALLET
}

@Composable
fun PayButton(
actual fun PayButton(
onClick: () -> Unit,
allowedPaymentMethods: String,
modifier: Modifier = Modifier,
theme: ButtonTheme = ButtonTheme.Dark,
type: ButtonType = ButtonType.Buy,
radius: Dp = 100.dp,
enabled: Boolean = true,
onError: (Throwable) -> Unit = {},
fallbackUi: @Composable (() -> Unit)? = null,
modifier: Modifier,
theme: ButtonTheme,
type: ButtonType,
radius: Dp,
enabled: Boolean,
onError: (Throwable) -> Unit,
fallbackUi: @Composable (() -> Unit)?,
) {

var showFallback by remember { mutableStateOf(false) }

val radiusPixelValue = with(LocalDensity.current) { radius.toPx().toInt() }
Expand All @@ -76,8 +74,8 @@ fun PayButton(
kotlin.runCatching {
this.initialize(
ButtonOptions.newBuilder()
.setButtonTheme(theme.value)
.setButtonType(type.value)
.setButtonTheme(theme.toAndroidValue())
.setButtonType(type.toAndroidValue())
.setCornerRadius(radiusPixelValue)
.setAllowedPaymentMethods(allowedPaymentMethods)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 Google LLC.
*
* 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.
*/

package com.google.pay.button

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

enum class ButtonTheme {
Dark,
Light,
}

enum class ButtonType {
Book,
Buy,
Checkout,
Donate,
Order,
Pay,
Plain,
Subscribe,
PIX,
EWALLET
}

@Composable
expect fun PayButton(
onClick: () -> Unit,
allowedPaymentMethods: String,
modifier: Modifier = Modifier,
theme: ButtonTheme = ButtonTheme.Dark,
type: ButtonType = ButtonType.Buy,
radius: Dp = 100.dp,
enabled: Boolean = true,
onError: (Throwable) -> Unit = {},
fallbackUi: @Composable (() -> Unit)? = null,
)
Loading