merchant

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

pg_insert_exchange_keys.c (3247B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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 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 backenddb/pg_insert_exchange_keys.c
     18  * @brief Implementation of the insert_exchange_keys function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "pg_insert_exchange_keys.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 enum GNUNET_DB_QueryStatus
     30 TMH_PG_insert_exchange_keys (
     31   void *cls,
     32   const char *exchange_url,
     33   const struct TALER_EXCHANGE_Keys *keys,
     34   struct GNUNET_TIME_Absolute first_retry,
     35   uint32_t http_status,
     36   enum TALER_ErrorCode ec)
     37 {
     38   struct PostgresClosure *pg = cls;
     39   uint32_t ec32 = (uint32_t) ec;
     40   json_t *jkeys = (NULL == keys)
     41     ? NULL
     42     : TALER_EXCHANGE_keys_to_json (keys);
     43   struct GNUNET_TIME_Timestamp ldid = (NULL == keys)
     44     ? GNUNET_TIME_UNIT_ZERO_TS
     45     : keys->last_denom_issue_date;
     46   struct GNUNET_PQ_QueryParam params[] = {
     47     keys == NULL
     48     ? GNUNET_PQ_query_param_null ()
     49     : TALER_PQ_query_param_json (jkeys),
     50     GNUNET_PQ_query_param_absolute_time (&first_retry),
     51     GNUNET_PQ_query_param_timestamp (&ldid),
     52     GNUNET_PQ_query_param_string (exchange_url),
     53     GNUNET_PQ_query_param_uint32 (&http_status),
     54     GNUNET_PQ_query_param_uint32 (&ec32),
     55     GNUNET_PQ_query_param_end
     56   };
     57   enum GNUNET_DB_QueryStatus qs;
     58 
     59   check_connection (pg);
     60   PREPARE (pg,
     61            "insert_exchange_keys",
     62            "INSERT INTO merchant_exchange_keys"
     63            "(keys_json"
     64            ",first_retry"
     65            ",expiration_time"
     66            ",exchange_url"
     67            ",exchange_http_status"
     68            ",exchange_ec_code"
     69            ") VALUES ("
     70            " $1::TEXT::JSONB, $2, $3, $4, $5, $6"
     71            ");");
     72   PREPARE (pg,
     73            "update_exchange_keys",
     74            "UPDATE merchant_exchange_keys SET"
     75            /* preserve old keys if new ones failed to download */
     76            " keys_json=COALESCE($1::TEXT::JSONB,keys_json)"
     77            ",first_retry=$2"
     78            ",expiration_time=$3"
     79            ",exchange_http_status=$5"
     80            ",exchange_ec_code=$6"
     81            " WHERE"
     82            " exchange_url=$4;");
     83   qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     84                                            "update_exchange_keys",
     85                                            params);
     86   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
     87     qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     88                                              "insert_exchange_keys",
     89                                              params);
     90   json_decref (jkeys);
     91   return qs;
     92 }