merchant

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

lookup_pending_webhooks.c (7895B)


      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_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_pending_webhooks.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Context used for lookup_pending_webhooks_cb().
     30  */
     31 struct LookupPendingWebhookContext
     32 {
     33   /**
     34    * Function to call with the results.
     35    */
     36   TALER_MERCHANTDB_PendingWebhooksCallback cb;
     37 
     38   /**
     39    * Closure for @a cb.
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Did database result extraction fail?
     45    */
     46   bool extract_failed;
     47 };
     48 
     49 
     50 /**
     51  * Function to be called with the results of a SELECT statement
     52  * that has returned @a num_results results about webhook.
     53  *
     54  * @param[in,out] cls of type `struct LookupPendingWebhookContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 lookup_pending_webhooks_cb (void *cls,
     60                             PGresult *result,
     61                             unsigned int num_results)
     62 {
     63   struct LookupPendingWebhookContext *pwlc = cls;
     64 
     65   for (unsigned int i = 0; i < num_results; i++)
     66   {
     67     uint64_t webhook_pending_serial;
     68     struct GNUNET_TIME_Absolute next_attempt;
     69     uint32_t retries;
     70     char *url;
     71     char *http_method;
     72     char *header = NULL;
     73     char *body = NULL;
     74     struct GNUNET_PQ_ResultSpec rs[] = {
     75       GNUNET_PQ_result_spec_uint64 ("webhook_pending_serial",
     76                                     &webhook_pending_serial),
     77       GNUNET_PQ_result_spec_absolute_time ("next_attempt",
     78                                            &next_attempt),
     79       GNUNET_PQ_result_spec_uint32 ("retries",
     80                                     &retries),
     81       GNUNET_PQ_result_spec_string ("url",
     82                                     &url),
     83       GNUNET_PQ_result_spec_string ("http_method",
     84                                     &http_method),
     85       GNUNET_PQ_result_spec_allow_null (
     86         GNUNET_PQ_result_spec_string ("header",
     87                                       &header),
     88         NULL),
     89       GNUNET_PQ_result_spec_allow_null (
     90         GNUNET_PQ_result_spec_string ("body",
     91                                       &body),
     92         NULL),
     93       GNUNET_PQ_result_spec_end
     94     };
     95 
     96     if (GNUNET_OK !=
     97         GNUNET_PQ_extract_result (result,
     98                                   rs,
     99                                   i))
    100     {
    101       GNUNET_break (0);
    102       pwlc->extract_failed = true;
    103       return;
    104     }
    105     pwlc->cb (pwlc->cb_cls,
    106               webhook_pending_serial,
    107               next_attempt,
    108               retries,
    109               url,
    110               http_method,
    111               header,
    112               body);
    113     GNUNET_PQ_cleanup_result (rs);
    114   }
    115 }
    116 
    117 
    118 enum GNUNET_DB_QueryStatus
    119 TALER_MERCHANTDB_lookup_pending_webhooks (
    120   struct TALER_MERCHANTDB_PostgresContext *pg,
    121   TALER_MERCHANTDB_PendingWebhooksCallback cb,
    122   void *cb_cls)
    123 {
    124   struct LookupPendingWebhookContext pwlc = {
    125     .cb = cb,
    126     .cb_cls = cb_cls,
    127     .extract_failed = false,
    128   };
    129   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
    130   struct GNUNET_PQ_QueryParam params_null[] = {
    131     GNUNET_PQ_query_param_absolute_time (&now),
    132     GNUNET_PQ_query_param_end
    133   };
    134 
    135   enum GNUNET_DB_QueryStatus qs;
    136 
    137   check_connection (pg);
    138   PREPARE (pg,
    139            "lookup_pending_webhooks",
    140            "SELECT"
    141            " webhook_pending_serial"
    142            ",next_attempt"
    143            ",retries"
    144            ",url"
    145            ",http_method"
    146            ",header"
    147            ",body"
    148            " FROM merchant_pending_webhooks"
    149            " WHERE next_attempt <= $1"
    150            "   ORDER BY next_attempt ASC"
    151            );
    152 
    153   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    154                                              "lookup_pending_webhooks",
    155                                              params_null,
    156                                              &lookup_pending_webhooks_cb,
    157                                              &pwlc);
    158 
    159   if (pwlc.extract_failed)
    160     return GNUNET_DB_STATUS_HARD_ERROR;
    161   return qs;
    162 }
    163 
    164 
    165 enum GNUNET_DB_QueryStatus
    166 TALER_MERCHANTDB_lookup_future_webhook (struct TALER_MERCHANTDB_PostgresContext *pg,
    167                                         TALER_MERCHANTDB_PendingWebhooksCallback cb,
    168                                         void *cb_cls)
    169 {
    170   struct LookupPendingWebhookContext pwlc = {
    171     .cb = cb,
    172     .cb_cls = cb_cls,
    173     .extract_failed = false,
    174   };
    175   struct GNUNET_PQ_QueryParam params_null[] = {
    176     GNUNET_PQ_query_param_end
    177   };
    178 
    179   enum GNUNET_DB_QueryStatus qs;
    180 
    181   check_connection (pg);
    182   PREPARE (pg,
    183            "lookup_future_webhook",
    184            "SELECT"
    185            " webhook_pending_serial"
    186            ",next_attempt"
    187            ",retries"
    188            ",url"
    189            ",http_method"
    190            ",header"
    191            ",body"
    192            " FROM merchant_pending_webhooks"
    193            " ORDER BY next_attempt ASC LIMIT 1"
    194            );
    195 
    196   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    197                                              "lookup_future_webhook",
    198                                              params_null,
    199                                              &lookup_pending_webhooks_cb,
    200                                              &pwlc);
    201 
    202   if (pwlc.extract_failed)
    203     return GNUNET_DB_STATUS_HARD_ERROR;
    204   return qs;
    205 }
    206 
    207 
    208 enum GNUNET_DB_QueryStatus
    209 TALER_MERCHANTDB_lookup_all_webhooks (struct TALER_MERCHANTDB_PostgresContext *pg,
    210                                       const char *instance_id,
    211                                       uint64_t min_row,
    212                                       uint32_t max_results,
    213                                       TALER_MERCHANTDB_PendingWebhooksCallback cb,
    214                                       void *cb_cls)
    215 {
    216   struct LookupPendingWebhookContext pwlc = {
    217     .cb = cb,
    218     .cb_cls = cb_cls,
    219     .extract_failed = false,
    220   };
    221   uint64_t max_results64 = max_results;
    222   struct GNUNET_PQ_QueryParam params[] = {
    223     GNUNET_PQ_query_param_string (instance_id),
    224     GNUNET_PQ_query_param_uint64 (&min_row),
    225     GNUNET_PQ_query_param_uint64 (&max_results64),
    226     GNUNET_PQ_query_param_end
    227   };
    228 
    229   enum GNUNET_DB_QueryStatus qs;
    230 
    231   check_connection (pg);
    232   PREPARE (pg,
    233            "lookup_all_webhooks",
    234            " SELECT"
    235            " webhook_pending_serial"
    236            ",next_attempt"
    237            ",retries"
    238            ",url"
    239            ",http_method"
    240            ",header"
    241            ",body"
    242            " FROM merchant_pending_webhooks"
    243            " JOIN merchant_instances"
    244            "   USING (merchant_serial)"
    245            " WHERE merchant_instances.merchant_id=$1"
    246            " AND webhook_pending_serial > $2"
    247            "  ORDER BY webhook_pending_serial"
    248            "   ASC LIMIT $3");
    249 
    250   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    251                                              "lookup_all_webhooks",
    252                                              params,
    253                                              &lookup_pending_webhooks_cb,
    254                                              &pwlc);
    255 
    256   if (pwlc.extract_failed)
    257     return GNUNET_DB_STATUS_HARD_ERROR;
    258   return qs;
    259 }