Skip to content

Commit

Permalink
Add currency selection logic update.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-signal authored and greyson-signal committed Dec 30, 2022
1 parent 055b469 commit 1e2f7f0
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ fun DonationsConfiguration.getMinimumDonationAmounts(paymentMethodAvailability:
.mapValues { FiatMoney(it.value.minimum, it.key) }
}

fun DonationsConfiguration.getAvailablePaymentMethods(currencyCode: String): Set<String> {
return currencies[currencyCode.lowercase()]?.supportedPaymentMethods ?: emptySet()
}

private fun DonationsConfiguration.getFilteredCurrencies(paymentMethodAvailability: PaymentMethodAvailability): Map<String, DonationsConfiguration.CurrencyConfiguration> {
val userPaymentMethods = paymentMethodAvailability.toSet()
val availableCurrencyCodes = PlatformCurrencyUtil.getAvailableCurrencyCodes()
return currencies.filter { (code, config) ->
val areAllMethodsAvailable = config.supportedPaymentMethods.containsAll(userPaymentMethods)
val areAllMethodsAvailable = config.supportedPaymentMethods.any { it in userPaymentMethods }
availableCurrencyCodes.contains(code.uppercase()) && areAllMethodsAvailable
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class DonateToSignalViewModel(
state.copy(
oneTimeDonationState = state.oneTimeDonationState.copy(
boosts = boostList,
selectedBoost = null,
selectedCurrency = currency,
donationStage = DonateToSignalState.DonationStage.READY,
selectableCurrencyCodes = availableCurrencies.map(Currency::getCurrencyCode),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.thoughtcrime.securesms.components.settings.app.subscription.donate.Do
import org.thoughtcrime.securesms.components.settings.app.subscription.models.GooglePayButton
import org.thoughtcrime.securesms.components.settings.app.subscription.models.PayPalButton
import org.thoughtcrime.securesms.components.settings.configure
import org.thoughtcrime.securesms.components.settings.models.IndeterminateLoadingCircle
import org.thoughtcrime.securesms.payments.FiatMoneyUtil
import org.thoughtcrime.securesms.util.LifecycleDisposable
import org.thoughtcrime.securesms.util.fragments.requireListener
Expand All @@ -42,6 +43,7 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {
BadgeDisplay112.register(adapter)
GooglePayButton.register(adapter)
PayPalButton.register(adapter)
IndeterminateLoadingCircle.register(adapter)

lifecycleDisposable.bindTo(viewLifecycleOwner)

Expand All @@ -65,6 +67,12 @@ class GatewaySelectorBottomSheet : DSLSettingsBottomSheetFragment() {

space(66.dp)

if (state.loading) {
customPref(IndeterminateLoadingCircle)
space(16.dp)
return@configure
}

if (state.isGooglePayAvailable) {
customPref(
GooglePayButton.Model(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.thoughtcrime.securesms.components.settings.app.subscription.donate.gateway

import io.reactivex.rxjava3.core.Single
import org.thoughtcrime.securesms.components.settings.app.subscription.getAvailablePaymentMethods
import org.whispersystems.signalservice.api.services.DonationsService
import java.util.Locale

class GatewaySelectorRepository(
private val donationsService: DonationsService
) {
fun getAvailableGateways(currencyCode: String): Single<Set<GatewayResponse.Gateway>> {
return Single.fromCallable {
donationsService.getDonationsConfiguration(Locale.getDefault())
}.flatMap { it.flattenResult() }
.map { configuration ->
configuration.getAvailablePaymentMethods(currencyCode).map {
when (it) {
"PAYPAL" -> listOf(GatewayResponse.Gateway.PAYPAL)
"CARD" -> listOf(GatewayResponse.Gateway.CREDIT_CARD, GatewayResponse.Gateway.GOOGLE_PAY)
else -> listOf()
}
}.flatten().toSet()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.components.settings.app.subscription.donate.g
import org.thoughtcrime.securesms.badges.models.Badge

data class GatewaySelectorState(
val loading: Boolean = true,
val badge: Badge,
val isGooglePayAvailable: Boolean = false,
val isPayPalAvailable: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package org.thoughtcrime.securesms.components.settings.app.subscription.donate.g

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import io.reactivex.rxjava3.kotlin.subscribeBy
import org.signal.donations.PaymentSourceType
import org.thoughtcrime.securesms.components.settings.app.subscription.InAppDonations
import org.thoughtcrime.securesms.components.settings.app.subscription.StripeRepository
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.util.rx.RxStore

class GatewaySelectorViewModel(
args: GatewaySelectorBottomSheetArgs,
private val repository: StripeRepository
repository: StripeRepository,
gatewaySelectorRepository: GatewaySelectorRepository
) : ViewModel() {

private val store = RxStore(
Expand All @@ -29,33 +32,33 @@ class GatewaySelectorViewModel(
val state = store.stateFlowable

init {
checkIfGooglePayIsAvailable()
val isGooglePayAvailable = repository.isGooglePayAvailable().toSingleDefault(true).onErrorReturnItem(false)
val availabilitySet = gatewaySelectorRepository.getAvailableGateways(currencyCode = args.request.currencyCode)
disposables += Single.zip(isGooglePayAvailable, availabilitySet, ::Pair).subscribeBy { (googlePayAvailable, gatewaysAvailable) ->
SignalStore.donationsValues().isGooglePayReady = googlePayAvailable
store.update {
it.copy(
loading = false,
isCreditCardAvailable = it.isCreditCardAvailable && gatewaysAvailable.contains(GatewayResponse.Gateway.CREDIT_CARD),
isGooglePayAvailable = it.isGooglePayAvailable && googlePayAvailable && gatewaysAvailable.contains(GatewayResponse.Gateway.GOOGLE_PAY),
isPayPalAvailable = it.isPayPalAvailable && gatewaysAvailable.contains(GatewayResponse.Gateway.PAYPAL)
)
}
}
}

override fun onCleared() {
store.dispose()
disposables.clear()
}

private fun checkIfGooglePayIsAvailable() {
disposables += repository.isGooglePayAvailable().subscribeBy(
onComplete = {
SignalStore.donationsValues().isGooglePayReady = true
store.update { it.copy(isGooglePayAvailable = true) }
},
onError = {
SignalStore.donationsValues().isGooglePayReady = false
store.update { it.copy(isGooglePayAvailable = false) }
}
)
}

class Factory(
private val args: GatewaySelectorBottomSheetArgs,
private val repository: StripeRepository
private val repository: StripeRepository,
private val gatewaySelectorRepository: GatewaySelectorRepository = GatewaySelectorRepository(ApplicationDependencies.getDonationsService())
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return modelClass.cast(GatewaySelectorViewModel(args, repository)) as T
return modelClass.cast(GatewaySelectorViewModel(args, repository, gatewaySelectorRepository)) as T
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class DonationsConfigurationExtensionsKtTest {
private val testSubject = JsonUtil.fromJson(testData, DonationsConfiguration::class.java)

@Test
fun `Given all methods are available, when I getSubscriptionAmounts, then I expect BIF`() {
fun `Given all methods are available, when I getSubscriptionAmounts, then I expect all currencies`() {
val subscriptionPrices = testSubject.getSubscriptionAmounts(DonationsConfiguration.SUBSCRIPTION_LEVELS.first(), AllPaymentMethodsAvailability)

assertEquals(1, subscriptionPrices.size)
assertEquals("BIF", subscriptionPrices.first().currency.currencyCode)
assertEquals(3, subscriptionPrices.size)
assertTrue(subscriptionPrices.map { it.currency.currencyCode }.containsAll(setOf("JPY", "BIF", "USD")))
}

@Test
Expand Down Expand Up @@ -84,11 +84,13 @@ class DonationsConfigurationExtensionsKtTest {
}

@Test
fun `Given all methods are available, when I getGiftAmounts, then I expect BIF`() {
fun `Given all methods are available, when I getGiftAmounts, then I expect BIF and JPY and USD`() {
val giftAmounts = testSubject.getGiftBadgeAmounts(AllPaymentMethodsAvailability)

assertEquals(1, giftAmounts.size)
assertEquals(3, giftAmounts.size)
assertNotNull(giftAmounts[Currency.getInstance("BIF")])
assertNotNull(giftAmounts[Currency.getInstance("JPY")])
assertNotNull(giftAmounts[Currency.getInstance("USD")])
}

@Test
Expand All @@ -108,11 +110,13 @@ class DonationsConfigurationExtensionsKtTest {
}

@Test
fun `Given all methods are available, when I getBoostAmounts, then I expect BIF`() {
fun `Given all methods are available, when I getBoostAmounts, then I expect BIF and JPY and USD`() {
val boostAmounts = testSubject.getBoostAmounts(AllPaymentMethodsAvailability)

assertEquals(1, boostAmounts.size)
assertEquals(3, boostAmounts.size)
assertNotNull(boostAmounts[Currency.getInstance("BIF")])
assertNotNull(boostAmounts[Currency.getInstance("JPY")])
assertNotNull(boostAmounts[Currency.getInstance("USD")])
}

@Test
Expand All @@ -132,11 +136,13 @@ class DonationsConfigurationExtensionsKtTest {
}

@Test
fun `Given all methods are available, when I getMinimumDonationAmounts, then I expect BIF`() {
fun `Given all methods are available, when I getMinimumDonationAmounts, then I expect BIF and JPY and USD`() {
val minimumDonationAmounts = testSubject.getMinimumDonationAmounts(AllPaymentMethodsAvailability)

assertEquals(1, minimumDonationAmounts.size)
assertEquals(3, minimumDonationAmounts.size)
assertNotNull(minimumDonationAmounts[Currency.getInstance("BIF")])
assertNotNull(minimumDonationAmounts[Currency.getInstance("JPY")])
assertNotNull(minimumDonationAmounts[Currency.getInstance("USD")])
}

@Test
Expand Down Expand Up @@ -185,6 +191,24 @@ class DonationsConfigurationExtensionsKtTest {
}
}

@Test
fun `Given I want to pay in USD, when I getAvailablePaymentMethods, then I expect CARD`() {
val availablePaymentMethods = testSubject.getAvailablePaymentMethods("UsD")

assertEquals(1, availablePaymentMethods.size)
assertTrue("CARD" in availablePaymentMethods)
}

@Test
fun `Given I want to pay in BIF, when I getAvailablePaymentMethods, then I expect CARD and PAYPAL`() {
val availablePaymentMethods = testSubject.getAvailablePaymentMethods("bIF")

println(testSubject.currencies)
assertEquals(2, availablePaymentMethods.size)
assertTrue("CARD" in availablePaymentMethods)
assertTrue("PAYPAL" in availablePaymentMethods)
}

private object AllPaymentMethodsAvailability : PaymentMethodAvailability {
override fun isPayPalAvailable(): Boolean = true
override fun isGooglePayOrCreditCardAvailable(): Boolean = true
Expand Down
6 changes: 3 additions & 3 deletions app/src/test/resources/donations_configuration_test_data.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"currencies": {
"JPY": {
"jpy": {
"minimum": 300,
"oneTime": {
"1": [
Expand All @@ -24,7 +24,7 @@
"PAYPAL"
]
},
"USD": {
"usd": {
"minimum": 2.5,
"oneTime": {
"1": [
Expand All @@ -48,7 +48,7 @@
"CARD"
]
},
"BIF": {
"bif": {
"minimum": 3000,
"oneTime": {
"1": [
Expand Down

0 comments on commit 1e2f7f0

Please sign in to comment.