taler-android

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

PayTemplateDetails.kt (3969B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2024 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.payment
     18 
     19 import kotlinx.serialization.SerialName
     20 import kotlinx.serialization.Serializable
     21 import net.taler.common.Amount
     22 import net.taler.common.RelativeTime
     23 
     24 @Serializable
     25 enum class TemplateType {
     26     @SerialName("fixed-order")
     27     FixedOrder,
     28 
     29     @SerialName("inventory-cart")
     30     InventoryCart,
     31 
     32     @SerialName("paivana")
     33     Paivana,
     34 
     35     Unknown,
     36 }
     37 
     38 @Serializable
     39 data class TemplateContractDetails(
     40     @SerialName("template_type")
     41     val templateType: TemplateType? = null,
     42 
     43     /**
     44      * Human-readable summary for the template.
     45      */
     46     val summary: String? = null,
     47 
     48     /**
     49      * Required currency for payments to the template. The user may specify
     50      * any amount, but it must be in this currency. This parameter is
     51      * optional and should not be present if "amount" is given.
     52      */
     53     val currency: String? = null,
     54 
     55     /**
     56      * The price is imposed by the merchant and cannot be changed by the
     57      * customer. This parameter is optional.
     58      */
     59     val amount: Amount? = null,
     60 
     61     /**
     62      * Minimum age buyer must have (in years). Default is 0.
     63      */
     64     @SerialName("minimum_age")
     65     val minimumAge: Int? = null,
     66 
     67     /**
     68      * The time the customer need to pay before his order will be deleted. It
     69      * is deleted if the customer did not pay and if the duration is over.
     70      */
     71     @SerialName("pay_duration")
     72     val payDuration: RelativeTime? = null,
     73 )
     74 
     75 @Serializable
     76 data class TemplateContractDetailsDefaults(
     77     val summary: String? = null,
     78     val currency: String? = null,
     79     val amount: Amount? = null,
     80     @SerialName("minimum_age")
     81     val minimumAge: Int? = null,
     82 )
     83 
     84 @Serializable
     85 class WalletTemplateDetails(
     86     /**
     87      * Hard-coded information about the contract terms for this template.
     88      */
     89     @SerialName("template_contract")
     90     val templateContract: TemplateContractDetails,
     91 
     92     /**
     93      * Key-value pairs matching a subset of the fields from template_contract
     94      * that are user-editable defaults for this template.
     95      */
     96     @SerialName("editable_defaults")
     97     val editableDefaults: TemplateContractDetailsDefaults? = null,
     98 ) {
     99     val defaultSummary get() = editableDefaults?.summary
    100         ?: templateContract.summary
    101 
    102     val defaultAmount get() = editableDefaults?.amount
    103         ?: templateContract.amount
    104 
    105     val defaultCurrency get() = editableDefaults?.currency
    106         ?: templateContract.currency
    107 
    108     fun isSummaryEditable() = templateContract.summary == null
    109 
    110     fun isAmountEditable() = templateContract.amount == null
    111 
    112     fun isCurrencyEditable(usableCurrencies: List<String>) = isAmountEditable()
    113             && templateContract.currency == null
    114             && usableCurrencies.size > 1
    115 
    116     fun isTemplateEditable(usableCurrencies: List<String>) = isSummaryEditable()
    117             || isAmountEditable()
    118             || isCurrencyEditable(usableCurrencies)
    119 
    120     // NOTE: it is important to nullify non-editable values!
    121     fun toTemplateParams() = TemplateParams(
    122         amount = if(isAmountEditable()) templateContract.amount else null,
    123         summary = if(isSummaryEditable()) templateContract.summary else null,
    124     )
    125 }
    126 
    127 @Serializable
    128 data class TemplateParams(
    129     val amount: Amount? = null,
    130     val summary: String? = null,
    131 )