Skip to content

Commit

Permalink
Tighten up a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrant committed Aug 29, 2024
1 parent 18971be commit 583ede2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
22 changes: 17 additions & 5 deletions app/src/extra/java/org/wikipedia/donate/GooglePayComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import com.google.android.gms.wallet.IsReadyToPayRequest
import com.google.android.gms.wallet.PaymentsClient
import com.google.android.gms.wallet.Wallet
import com.google.android.gms.wallet.WalletConstants
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import org.json.JSONArray
import org.json.JSONObject
import org.wikipedia.dataclient.donate.DonationConfigHelper
import org.wikipedia.settings.Prefs
import org.wikipedia.util.GeoUtil
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale
Expand Down Expand Up @@ -59,11 +63,19 @@ internal object GooglePayComponent {
}

suspend fun isGooglePayAvailable(activity: Activity): Boolean {
val readyToPayRequest = IsReadyToPayRequest.fromJson(googlePayBaseConfiguration.toString())
val paymentsClient = createPaymentsClient(activity)
val readyToPayTask = paymentsClient.isReadyToPay(readyToPayRequest)
readyToPayTask.await()
return readyToPayTask.result
var available: Boolean
withContext(Dispatchers.IO) {
val readyToPayRequest = IsReadyToPayRequest.fromJson(googlePayBaseConfiguration.toString())
val paymentsClient = createPaymentsClient(activity)
val readyToPayTask = paymentsClient.isReadyToPay(readyToPayRequest)
available = readyToPayTask.await()
if (available) {
DonationConfigHelper.getConfig()?.let { config ->
available = config.countryCodeGooglePayEnabled.contains(GeoUtil.geoIPCountry.orEmpty())
}
}
}
return available
}

fun getDonateActivityIntent(activity: Activity, campaignId: String? = null, donateUrl: String? = null): Intent {
Expand Down
24 changes: 22 additions & 2 deletions app/src/extra/java/org/wikipedia/donate/GooglePayViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.wikipedia.settings.Prefs
import org.wikipedia.util.GeoUtil
import org.wikipedia.util.Resource
import org.wikipedia.util.log.L
import java.text.NumberFormat
import java.time.Instant
import java.util.Locale
import java.util.concurrent.TimeUnit
Expand All @@ -32,7 +33,9 @@ class GooglePayViewModel : ViewModel() {
val uiState = MutableStateFlow(Resource<DonationConfig>())
private var donationConfig: DonationConfig? = null
private val currentCountryCode get() = GeoUtil.geoIPCountry.orEmpty()
val currencyFormat get() = GeoUtil.currencyFormat(Locale.getDefault())

val currencyFormat: NumberFormat = NumberFormat.getCurrencyInstance(Locale.Builder()
.setLocale(Locale.getDefault()).setRegion(currentCountryCode).build())
val currencyCode get() = currencyFormat.currency?.currencyCode ?: GooglePayComponent.CURRENCY_FALLBACK
val currencySymbol get() = currencyFormat.currency?.symbol ?: "$"
val decimalFormat = GooglePayComponent.getDecimalFormat(currencyCode)
Expand Down Expand Up @@ -84,7 +87,24 @@ class GooglePayViewModel : ViewModel() {
disclaimerMonthlyCancel = response.query?.allmessages?.find { it.name == MSG_DISCLAIMER_MONTHLY_CANCEL }?.content?.replace("$1", WikipediaApp.instance.getString(R.string.donate_email))
}

async { updatePaymentMethodsPreferences() }.await()
// The paymentMethods API is rate limited, so we cache it manually.
val now = Instant.now().epochSecond
if (abs(now - Prefs.paymentMethodsLastQueryTime) > TimeUnit.DAYS.toSeconds(7)) {
Prefs.paymentMethodsMerchantId = ""
Prefs.paymentMethodsGatewayId = ""

val paymentMethodsCall = async {
ServiceFactory.get(WikiSite(GooglePayComponent.PAYMENTS_API_URL))
.getPaymentMethods(currentCountryCode)
}
paymentMethodsCall.await().response?.let { response ->
Prefs.paymentMethodsLastQueryTime = now
response.paymentMethods.find { it.type == GooglePayComponent.PAYMENT_METHOD_NAME }?.let {
Prefs.paymentMethodsMerchantId = it.configuration?.merchantId.orEmpty()
Prefs.paymentMethodsGatewayId = it.configuration?.gatewayMerchantId.orEmpty()
}
}
}

if (Prefs.paymentMethodsMerchantId.isEmpty() ||
Prefs.paymentMethodsGatewayId.isEmpty() ||
Expand Down
19 changes: 1 addition & 18 deletions app/src/main/java/org/wikipedia/donate/DonateViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import android.app.Activity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.wikipedia.dataclient.donate.DonationConfigHelper
import org.wikipedia.settings.Prefs
import org.wikipedia.util.GeoUtil
import org.wikipedia.util.Resource
import java.util.Locale

class DonateViewModel : ViewModel() {
private val _uiState = MutableStateFlow<Resource<Boolean>>(Resource.Loading())
Expand All @@ -24,19 +19,7 @@ class DonateViewModel : ViewModel() {
}) {
_uiState.value = Resource.Loading()

GooglePayViewModel.updatePaymentMethodsPreferences()
val donationConfig = async { DonationConfigHelper.getConfig() }
var googlePayAvailable = GooglePayComponent.isGooglePayAvailable(activity)
donationConfig.await()?.let { config ->
val currentCountryCode = GeoUtil.geoIPCountry.orEmpty()
val currencyCode = GeoUtil.currencyFormat(Locale.getDefault()).currency?.currencyCode ?: GooglePayComponent.CURRENCY_FALLBACK
googlePayAvailable = !(Prefs.paymentMethodsMerchantId.isEmpty() ||
Prefs.paymentMethodsGatewayId.isEmpty() ||
!config.countryCodeGooglePayEnabled.contains(currentCountryCode) ||
!config.currencyAmountPresets.containsKey(currencyCode))
}

_uiState.value = Resource.Success(googlePayAvailable)
_uiState.value = Resource.Success(GooglePayComponent.isGooglePayAvailable(activity))
}
}
}
5 changes: 0 additions & 5 deletions app/src/main/java/org/wikipedia/util/GeoUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.wikipedia.R
import org.wikipedia.feed.announcement.GeoIPCookieUnmarshaller
import org.wikipedia.settings.Prefs
import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Locale
import kotlin.math.abs

Expand Down Expand Up @@ -58,8 +57,4 @@ object GeoUtil {
val tolerance = 0.0000001
return abs(startLat - endLat) < tolerance && abs(startLon - endLon) < tolerance
}

fun currencyFormat(locale: Locale): NumberFormat {
return NumberFormat.getCurrencyInstance(Locale.Builder().setLocale(locale).setRegion(geoIPCountry.orEmpty()).build())
}
}

0 comments on commit 583ede2

Please sign in to comment.