merchant

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

lookup_pending_webhooks.c (5963B)


      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_pending_webhooks.c
     18  * @brief Implementation of the lookup_pending_webhooks 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_pending_webhooks.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Context used for lookup_pending_webhooks_cb().
     28  */
     29 struct LookupPendingWebhookContext
     30 {
     31   /**
     32    * Function to call with the results.
     33    */
     34   TALER_MERCHANTDB_PendingWebhooksCallback cb;
     35 
     36   /**
     37    * Closure for @a cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Did database result extraction fail?
     43    */
     44   bool extract_failed;
     45 };
     46 
     47 
     48 /**
     49  * Function to be called with the results of a SELECT statement
     50  * that has returned @a num_results results about webhook.
     51  *
     52  * @param[in,out] cls of type `struct LookupPendingWebhookContext *`
     53  * @param result the postgres result
     54  * @param num_results the number of results in @a result
     55  */
     56 static void
     57 lookup_pending_webhooks_cb (void *cls,
     58                             PGresult *result,
     59                             unsigned int num_results)
     60 {
     61   struct LookupPendingWebhookContext *pwlc = cls;
     62 
     63   for (unsigned int i = 0; i < num_results; i++)
     64   {
     65     uint64_t webhook_pending_serial;
     66     struct GNUNET_TIME_Absolute next_attempt;
     67     uint32_t retries;
     68     char *url;
     69     char *http_method;
     70     char *header = NULL;
     71     char *body = NULL;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_uint64 ("webhook_pending_serial",
     74                                     &webhook_pending_serial),
     75       GNUNET_PQ_result_spec_absolute_time ("next_attempt",
     76                                            &next_attempt),
     77       GNUNET_PQ_result_spec_uint32 ("retries",
     78                                     &retries),
     79       GNUNET_PQ_result_spec_string ("url",
     80                                     &url),
     81       GNUNET_PQ_result_spec_string ("http_method",
     82                                     &http_method),
     83       GNUNET_PQ_result_spec_allow_null (
     84         GNUNET_PQ_result_spec_string ("header",
     85                                       &header),
     86         NULL),
     87       GNUNET_PQ_result_spec_allow_null (
     88         GNUNET_PQ_result_spec_string ("body",
     89                                       &body),
     90         NULL),
     91       GNUNET_PQ_result_spec_end
     92     };
     93 
     94     if (GNUNET_OK !=
     95         GNUNET_PQ_extract_result (result,
     96                                   rs,
     97                                   i))
     98     {
     99       GNUNET_break (0);
    100       pwlc->extract_failed = true;
    101       return;
    102     }
    103     pwlc->cb (pwlc->cb_cls,
    104               webhook_pending_serial,
    105               next_attempt,
    106               retries,
    107               url,
    108               http_method,
    109               header,
    110               body);
    111     GNUNET_PQ_cleanup_result (rs);
    112   }
    113 }
    114 
    115 
    116 enum GNUNET_DB_QueryStatus
    117 TALER_MERCHANTDB_lookup_pending_webhooks (
    118   struct TALER_MERCHANTDB_PostgresContext *pg,
    119   TALER_MERCHANTDB_PendingWebhooksCallback cb,
    120   void *cb_cls)
    121 {
    122   struct LookupPendingWebhookContext pwlc = {
    123     .cb = cb,
    124     .cb_cls = cb_cls,
    125     .extract_failed = false,
    126   };
    127   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
    128   struct GNUNET_PQ_QueryParam params[] = {
    129     GNUNET_PQ_query_param_absolute_time (&now),
    130     GNUNET_PQ_query_param_end
    131   };
    132   enum GNUNET_DB_QueryStatus qs;
    133 
    134   check_connection (pg);
    135   PREPARE (pg,
    136            "lookup_pending_webhooks",
    137            "SELECT"
    138            "  webhook_pending_serial"
    139            " ,next_attempt"
    140            " ,retries"
    141            " ,url"
    142            " ,http_method"
    143            " ,header"
    144            " ,body"
    145            "  FROM merchant.merchant_pending_webhooks"
    146            " WHERE next_attempt <= $1"
    147            " ORDER BY next_attempt ASC");
    148   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    149                                              "lookup_pending_webhooks",
    150                                              params,
    151                                              &lookup_pending_webhooks_cb,
    152                                              &pwlc);
    153   if (pwlc.extract_failed)
    154     return GNUNET_DB_STATUS_HARD_ERROR;
    155   return qs;
    156 }
    157 
    158 
    159 enum GNUNET_DB_QueryStatus
    160 TALER_MERCHANTDB_lookup_future_webhook (
    161   struct TALER_MERCHANTDB_PostgresContext *pg,
    162   TALER_MERCHANTDB_PendingWebhooksCallback cb,
    163   void *cb_cls)
    164 {
    165   struct LookupPendingWebhookContext pwlc = {
    166     .cb = cb,
    167     .cb_cls = cb_cls,
    168     .extract_failed = false,
    169   };
    170   struct GNUNET_PQ_QueryParam params_null[] = {
    171     GNUNET_PQ_query_param_end
    172   };
    173   enum GNUNET_DB_QueryStatus qs;
    174 
    175   check_connection (pg);
    176   PREPARE (pg,
    177            "lookup_future_webhook",
    178            "SELECT"
    179            "  webhook_pending_serial"
    180            " ,next_attempt"
    181            " ,retries"
    182            " ,url"
    183            " ,http_method"
    184            " ,header"
    185            " ,body"
    186            "  FROM merchant.merchant_pending_webhooks"
    187            " ORDER BY next_attempt ASC"
    188            " LIMIT 1");
    189   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    190                                              "lookup_future_webhook",
    191                                              params_null,
    192                                              &lookup_pending_webhooks_cb,
    193                                              &pwlc);
    194   if (pwlc.extract_failed)
    195     return GNUNET_DB_STATUS_HARD_ERROR;
    196   return qs;
    197 }