merchant

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

lookup_refunds.c (4291B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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.c
     18  * @brief Implementation of the lookup_refunds 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.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Closure for #lookup_refunds_cb().
     28  */
     29 struct LookupRefundsContext
     30 {
     31   /**
     32    * Function to call for each refund.
     33    */
     34   TALER_MERCHANTDB_RefundCallback 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 closure of type `struct LookupRefundsContext *`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 lookup_refunds_cb (void *cls,
     63                    PGresult *result,
     64                    unsigned int num_results)
     65 {
     66   struct LookupRefundsContext *lrc = cls;
     67 
     68   for (unsigned int i = 0; i<num_results; i++)
     69   {
     70     struct TALER_CoinSpendPublicKeyP coin_pub;
     71     struct TALER_Amount refund_amount;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     74                                             &coin_pub),
     75       TALER_PQ_result_spec_amount_with_currency ("refund_amount",
     76                                                  &refund_amount),
     77       GNUNET_PQ_result_spec_end
     78     };
     79 
     80     if (GNUNET_OK !=
     81         GNUNET_PQ_extract_result (result,
     82                                   rs,
     83                                   i))
     84     {
     85       GNUNET_break (0);
     86       lrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     87       return;
     88     }
     89     lrc->rc (lrc->rc_cls,
     90              &coin_pub,
     91              &refund_amount);
     92     GNUNET_PQ_cleanup_result (rs); /* technically useless here */
     93   }
     94   lrc->qs = num_results;
     95 }
     96 
     97 
     98 enum GNUNET_DB_QueryStatus
     99 TALER_MERCHANTDB_lookup_refunds (
    100   struct TALER_MERCHANTDB_PostgresContext *pg,
    101   const char *instance_id,
    102   const struct TALER_PrivateContractHashP *h_contract_terms,
    103   TALER_MERCHANTDB_RefundCallback rc,
    104   void *rc_cls)
    105 {
    106   struct GNUNET_PQ_QueryParam params[] = {
    107     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
    108     GNUNET_PQ_query_param_end
    109   };
    110   struct LookupRefundsContext lrc = {
    111     .rc = rc,
    112     .rc_cls = rc_cls,
    113     .pg = pg
    114   };
    115   enum GNUNET_DB_QueryStatus qs;
    116 
    117   GNUNET_assert (NULL != pg->current_merchant_id);
    118   GNUNET_assert (0 == strcmp (instance_id,
    119                               pg->current_merchant_id));
    120   /* no preflight check here, run in transaction by caller! */
    121   TALER_LOG_DEBUG ("Looking for refund of h_contract_terms %s at `%s'\n",
    122                    GNUNET_h2s (&h_contract_terms->hash),
    123                    instance_id);
    124   check_connection (pg);
    125   TMH_PQ_prepare_anon (pg,
    126                        "SELECT"
    127                        " coin_pub"
    128                        ",refund_amount"
    129                        " FROM merchant_refunds"
    130                        " WHERE order_serial="
    131                        "  (SELECT order_serial"
    132                        "     FROM merchant_contract_terms"
    133                        "    WHERE h_contract_terms=$1)");
    134   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    135                                              "",
    136                                              params,
    137                                              &lookup_refunds_cb,
    138                                              &lrc);
    139   if (0 >= qs)
    140     return qs;
    141   return lrc.qs;
    142 }