taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

payment-poll-crashes-when-the-session-ends.diff (4267B)


      1 diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
      2 index 91672e4f..e39c6a78 100644
      3 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
      4 +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
      5 @@ -27,6 +27,7 @@ import androidx.lifecycle.MutableLiveData
      6  import net.taler.lib.android.assertUiThread
      7  import net.taler.merchantlib.CheckPaymentResponse
      8  import net.taler.merchantlib.MerchantApi
      9 +import net.taler.merchantlib.MerchantConfig
     10  import net.taler.merchantlib.OrderHistoryEntry
     11  import net.taler.merchantlib.PostOrderRequest
     12  import net.taler.merchantpos.MainActivity.Companion.TAG
     13 @@ -91,8 +92,8 @@ class PaymentManager(
     14          order: Order,
     15          includeProducts: Boolean = true,
     16      ) = scope.launch {
     17 -        val merchantConfig = configManager.merchantConfig!!
     18 -        val payment = Payment(order, order.summary, configManager.currency!!)
     19 +        val merchantConfig = liveConfig() ?: return@launch
     20 +        val payment = Payment(order, order.summary, configManager.currency ?: return@launch)
     21          mPayment.value = payment
     22          val parts = order.toOrderParts(includeProducts)
     23          val request =
     24 @@ -235,7 +236,7 @@ class PaymentManager(
     25  
     26      private fun checkPayment(orderId: String) =
     27          scope.launch {
     28 -            val merchantConfig = configManager.merchantConfig!!
     29 +            val merchantConfig = liveConfig() ?: return@launch
     30              api.checkOrder(merchantConfig, orderId).handle(
     31                  { error ->
     32                      // don't call onNetworkError() to not cancel payment, just keep trying
     33 @@ -369,10 +370,13 @@ class PaymentManager(
     34      fun cancelPayment(error: String? = null)
     35      {
     36          stopPaymentChecks()
     37 -        val merchantConfig = configManager.merchantConfig!!
     38 +        // Not required: cancelling has to work even when the session is the
     39 +        // thing that ended, and then there is nobody to delete the order at.
     40 +        val merchantConfig = configManager.merchantConfig
     41          mPayment.value?.let { payment ->
     42              // A checkout order records cash or card already taken, so it stays for resuming.
     43 -            if (!payment.paid &&
     44 +            if (merchantConfig != null &&
     45 +                !payment.paid &&
     46                  payment.checkoutId == null)
     47              {
     48                  payment.orderId?.let { orderId ->
     49 @@ -398,7 +402,7 @@ class PaymentManager(
     50              mDeleteNeedsForce.value = false
     51              return
     52          }
     53 -        val merchantConfig = configManager.merchantConfig!!
     54 +        val merchantConfig = liveConfig() ?: return
     55          val orderId = mPayment.value?.orderId ?: return
     56          scope.launch {
     57              val result = api.deleteOrder(merchantConfig, orderId)
     58 @@ -419,7 +423,7 @@ class PaymentManager(
     59      @UiThread
     60      fun forceDeleteOrder()
     61      {
     62 -        val merchantConfig = configManager.merchantConfig!!
     63 +        val merchantConfig = liveConfig() ?: return
     64          val orderId = mPayment.value?.orderId ?: return
     65          Log.d(TAG, "Force deleting claimed order $orderId")
     66          scope.launch {
     67 @@ -437,6 +441,27 @@ class PaymentManager(
     68          mDeleteNeedsForce.value = null
     69      }
     70  
     71 +    /**
     72 +     * The merchant configuration, or null once the session has ended.
     73 +     *
     74 +     * `ConfigManager` clears it the moment a request comes back 401, and the
     75 +     * payment screen polls once a second -- so between two ticks of
     76 +     * [checkTimer] the field can go from a configuration to nothing, and
     77 +     * every `!!` on it here was a crash on the main thread waiting for a
     78 +     * token to expire.  Where there is no session left there is also nothing
     79 +     * useful to ask the backend, so the polling is stopped as well.
     80 +     */
     81 +    private fun liveConfig(): MerchantConfig?
     82 +    {
     83 +        val merchantConfig = configManager.merchantConfig
     84 +        if (merchantConfig == null)
     85 +        {
     86 +            Log.d(TAG, "no merchant configuration left; stopping payment checks")
     87 +            stopPaymentChecks()
     88 +        }
     89 +        return merchantConfig
     90 +    }
     91 +
     92      private fun stopPaymentChecks()
     93      {
     94          checkTimer.cancel()