Skip to content
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

Add merchant toggle to CustomerSheet example #7687

Merged
merged 1 commit into from
Nov 30, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.stripe.android.customersheet.ExperimentalCustomerSheetApi
import com.stripe.android.customersheet.rememberCustomerSheet
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.example.R
import com.stripe.android.paymentsheet.example.samples.ui.customersheet.playground.CustomerSheetPlaygroundViewAction.UpdateMerchantCountryCode
import com.stripe.android.paymentsheet.example.samples.ui.shared.MultiToggleButton
import com.stripe.android.paymentsheet.example.samples.ui.shared.PaymentSheetExampleTheme
import com.stripe.android.paymentsheet.example.utils.rememberDrawablePainter
Expand Down Expand Up @@ -535,6 +536,23 @@ class CustomerSheetPlaygroundActivity : AppCompatActivity() {
}
)
}
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 2.dp)
) {
Text(
text = "Merchant Country",
color = MaterialTheme.colors.onBackground,
)
MultiToggleButton(
currentSelection = configurationState.merchantCountry,
toggleStates = listOf("US", "FR"),
onToggleChange = { viewActionHandler(UpdateMerchantCountryCode(it)) },
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ data class CustomerSheetPlaygroundConfigurationState(
val attachDefaultBillingAddress: Boolean = true,
val achEnabled: Boolean = true,
val billingCollectionConfiguration: PaymentSheet.BillingDetailsCollectionConfiguration =
PaymentSheet.BillingDetailsCollectionConfiguration()
)
PaymentSheet.BillingDetailsCollectionConfiguration(),
val merchantCountry: String = "US",
val currency: String = "usd",
) {

val customerId: String
get() = if (isExistingCustomer) "returning" else "new"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ sealed class CustomerSheetPlaygroundViewAction {
data class UpdateBillingEmailCollection(val value: String) : CustomerSheetPlaygroundViewAction()
data class UpdateBillingPhoneCollection(val value: String) : CustomerSheetPlaygroundViewAction()
data class UpdateBillingAddressCollection(val value: String) : CustomerSheetPlaygroundViewAction()
data class UpdateMerchantCountryCode(val code: String) : CustomerSheetPlaygroundViewAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ class CustomerSheetPlaygroundViewModel(
)

init {
viewModelScope.launch {
viewModelScope.launch(Dispatchers.IO) {
fetchClientSecret()
}
}

private suspend fun fetchClientSecret(
customerId: String = "returning"
customerId: String = configurationState.value.customerId,
): Result<PlaygroundCustomerSheetResponse, FuelError> {
val request = PlaygroundCustomerSheetRequest(
customerId = customerId,
mode = "payment",
merchantCountryCode = "US",
currency = "usd",
merchantCountryCode = configurationState.value.merchantCountry,
currency = configurationState.value.currency,
)

val requestBody = Json.encodeToString(
Expand Down Expand Up @@ -240,6 +240,9 @@ class CustomerSheetPlaygroundViewModel(
updateBillingNameCollection(viewAction.value)
is CustomerSheetPlaygroundViewAction.UpdateBillingPhoneCollection ->
updateBillingPhoneCollection(viewAction.value)
is CustomerSheetPlaygroundViewAction.UpdateMerchantCountryCode -> {
updateMerchantCountry(viewAction.code)
}
}
}

Expand Down Expand Up @@ -301,14 +304,8 @@ class CustomerSheetPlaygroundViewModel(
)
}

viewModelScope.launch {
fetchClientSecret(
if (configurationState.value.isExistingCustomer) {
"returning"
} else {
"new"
}
)
viewModelScope.launch(Dispatchers.IO) {
fetchClientSecret()
}
}

Expand Down Expand Up @@ -389,6 +386,21 @@ class CustomerSheetPlaygroundViewModel(
}
}

private fun updateMerchantCountry(code: String) {
val currency = if (code == "FR") "eur" else "usd"

updateConfiguration {
it.copy(
merchantCountry = code,
currency = currency,
)
}

viewModelScope.launch(Dispatchers.IO) {
fetchClientSecret()
}
}

private inline fun <reified T : CustomerSheetPlaygroundViewState> updateViewState(transform: (T) -> T) {
(_viewState.value as? T)?.let {
_viewState.update {
Expand Down
Loading