merchant

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

lookup_refunds_detailed.c (5991B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023 Taler Systems SA
      4 
      5    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    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/backenddb/lookup_refunds_detailed.c
     18  * @brief Implementation of the lookup_refunds_detailed function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_refunds_detailed.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Closure for #lookup_refunds_detailed_cb().
     28  */
     29 struct LookupRefundsDetailedContext
     30 {
     31   /**
     32    * Function to call for each refund.
     33    */
     34   TALER_MERCHANTDB_RefundDetailCallback rc;
     35 
     36   /**
     37    * Closure for @e rc.
     38    */
     39   void *rc_cls;
     40 
     41   /**
     42    * Plugin context.
     43    */
     44   struct TALER_MERCHANTDB_PostgresContext *pg;
     45 
     46   /**
     47    * Transaction result.
     48    */
     49   enum GNUNET_DB_QueryStatus qs;
     50 };
     51 
     52 
     53 /**
     54  * Function to be called with the results of a SELECT statement
     55  * that has returned @a num_results results.
     56  *
     57  * @param cls of type `struct GetRefundsContext *`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 lookup_refunds_detailed_cb (void *cls,
     63                             PGresult *result,
     64                             unsigned int num_results)
     65 {
     66   struct LookupRefundsDetailedContext *lrdc = cls;
     67 
     68   for (unsigned int i = 0; i<num_results; i++)
     69   {
     70     uint64_t refund_serial;
     71     struct GNUNET_TIME_Timestamp timestamp;
     72     struct TALER_CoinSpendPublicKeyP coin_pub;
     73     uint64_t rtransaction_id;
     74     struct TALER_Amount refund_amount;
     75     char *reason;
     76     char *exchange_url;
     77     uint8_t pending8;
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_uint64 ("refund_serial",
     80                                     &refund_serial),
     81       GNUNET_PQ_result_spec_timestamp ("refund_timestamp",
     82                                        &timestamp),
     83       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     84                                             &coin_pub),
     85       GNUNET_PQ_result_spec_string ("exchange_url",
     86                                     &exchange_url),
     87       GNUNET_PQ_result_spec_uint64 ("rtransaction_id",
     88                                     &rtransaction_id),
     89       GNUNET_PQ_result_spec_string ("reason",
     90                                     &reason),
     91       TALER_PQ_result_spec_amount_with_currency ("refund_amount",
     92                                                  &refund_amount),
     93       GNUNET_PQ_result_spec_auto_from_type ("pending",
     94                                             &pending8),
     95       GNUNET_PQ_result_spec_end
     96     };
     97 
     98     if (GNUNET_OK !=
     99         GNUNET_PQ_extract_result (result,
    100                                   rs,
    101                                   i))
    102     {
    103       GNUNET_break (0);
    104       lrdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    105       return;
    106     }
    107     lrdc->rc (lrdc->rc_cls,
    108               refund_serial,
    109               timestamp,
    110               &coin_pub,
    111               exchange_url,
    112               rtransaction_id,
    113               reason,
    114               &refund_amount,
    115               0 != pending8);
    116     GNUNET_PQ_cleanup_result (rs);
    117   }
    118   lrdc->qs = num_results;
    119 }
    120 
    121 
    122 enum GNUNET_DB_QueryStatus
    123 TALER_MERCHANTDB_lookup_refunds_detailed (
    124   struct TALER_MERCHANTDB_PostgresContext *pg,
    125   const char *instance_id,
    126   const struct TALER_PrivateContractHashP *
    127   h_contract_terms,
    128   TALER_MERCHANTDB_RefundDetailCallback rc,
    129   void *rc_cls)
    130 {
    131   struct GNUNET_PQ_QueryParam params[] = {
    132     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
    133     GNUNET_PQ_query_param_end
    134   };
    135   struct LookupRefundsDetailedContext lrdc = {
    136     .rc  = rc,
    137     .rc_cls = rc_cls,
    138     .pg = pg
    139   };
    140   enum GNUNET_DB_QueryStatus qs;
    141 
    142   GNUNET_assert (NULL != pg->current_merchant_id);
    143   GNUNET_assert (0 == strcmp (instance_id,
    144                               pg->current_merchant_id));
    145   /* no preflight check here, run in transaction by caller! */
    146   TALER_LOG_DEBUG ("Looking for refund %s + %s\n",
    147                    GNUNET_h2s (&h_contract_terms->hash),
    148                    instance_id);
    149 
    150   check_connection (pg);
    151   TMH_PQ_prepare_anon (pg,
    152                        "SELECT"
    153                        " ref.refund_serial"
    154                        ",ref.refund_timestamp"
    155                        ",dep.coin_pub"
    156                        ",mcon.exchange_url"
    157                        ",ref.rtransaction_id"
    158                        ",ref.reason"
    159                        ",ref.refund_amount"
    160                        ",merchant_refund_proofs.exchange_sig IS NULL AS pending"
    161                        " FROM merchant_deposit_confirmations mcon"
    162                        " JOIN merchant_deposits dep"
    163                        "   USING (deposit_confirmation_serial)"
    164                        " JOIN merchant_refunds ref"
    165                        "   USING (order_serial, coin_pub)"
    166                        " LEFT JOIN merchant_refund_proofs"
    167                        "   USING (refund_serial)"
    168                        " WHERE mcon.order_serial="
    169                        "  (SELECT order_serial"
    170                        "     FROM merchant_contract_terms"
    171                        "    WHERE h_contract_terms=$1)");
    172 
    173   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    174                                              "",
    175                                              params,
    176                                              &lookup_refunds_detailed_cb,
    177                                              &lrdc);
    178   if (0 >= qs)
    179     return qs;
    180   return lrdc.qs;
    181 }