merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-httpd_amounts.c (2642B)


      1 /*
      2   This file is part of TALER
      3   (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Lesser General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   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   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file src/backend/taler-merchant-httpd_amounts.c
     18  * @brief amount/fee arithmetic helpers for the merchant HTTP daemon
     19  */
     20 #include "platform.h"
     21 #include <gnunet/gnunet_util_lib.h>
     22 #include <taler/taler_json_lib.h>
     23 #include "taler-merchant-httpd_helper.h"
     24 
     25 
     26 enum GNUNET_GenericReturnValue
     27 TMH_compute_expected_transfer (const struct TALER_Amount *contract_amount,
     28                                const struct TALER_Amount *refund_amount,
     29                                const struct TALER_Amount *deposit_fees_total,
     30                                const struct TALER_Amount *deposit_fees_refunded_total,
     31                                const struct TALER_Amount *max_fee,
     32                                bool has_max_fee,
     33                                struct TALER_Amount *expected_total)
     34 {
     35   struct TALER_Amount net_fee;
     36   struct TALER_Amount fee_merchant_pays;
     37 
     38   if (0 >
     39       TALER_amount_subtract (expected_total,
     40                              contract_amount,
     41                              refund_amount))
     42   {
     43     /* Refunds exceed the contract value. */
     44     return GNUNET_SYSERR;
     45   }
     46   if (0 >
     47       TALER_amount_subtract (&net_fee,
     48                              deposit_fees_total,
     49                              deposit_fees_refunded_total))
     50   {
     51     /* Refunded deposit fees exceed total deposit fees. */
     52     return GNUNET_SYSERR;
     53   }
     54 
     55   /* The merchant only ever pays up to max_fee; the customer covers any
     56      excess.  If the contract did not specify a max_fee, fall back to the
     57      previous strict behavior. */
     58   if (has_max_fee &&
     59       (1 == TALER_amount_cmp (&net_fee,
     60                               max_fee)))
     61     fee_merchant_pays = *max_fee;
     62   else
     63     fee_merchant_pays = net_fee;
     64 
     65   if (0 >
     66       TALER_amount_subtract (expected_total,
     67                              expected_total,
     68                              &fee_merchant_pays))
     69   {
     70     /* Deposit fees exceed the amount minus refunds. */
     71     return GNUNET_NO;
     72   }
     73   return GNUNET_OK;
     74 }