Skip to content

Commit

Permalink
Add merchant toggle to CustomerSheet example (#7687)
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe authored Nov 30, 2023
1 parent e54144f commit dba77db
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
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

0 comments on commit dba77db

Please sign in to comment.