merchant

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

lookup_webhook_by_event.c (4701B)


      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_webhook_by_event.c
     18  * @brief Implementation of the lookup_webhook_by_event 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_webhook_by_event.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Context used for lookup_webhook_by_event_cb().
     28  */
     29 struct LookupWebhookDetailContext
     30 {
     31   /**
     32    * Function to call with the results.
     33    */
     34   TALER_MERCHANTDB_WebhookDetailCallback 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  * Function to be called with the results of a SELECT statement
     49  * that has returned @a num_results results about webhook.
     50  *
     51  * @param[in,out] cls of type `struct LookupPendingWebhookContext *`
     52  * @param result the postgres result
     53  * @param num_results the number of results in @a result
     54  */
     55 static void
     56 lookup_webhook_by_event_cb (void *cls,
     57                             PGresult *result,
     58                             unsigned int num_results)
     59 {
     60   struct LookupWebhookDetailContext *wlc = cls;
     61 
     62   for (unsigned int i = 0; i < num_results; i++)
     63   {
     64     uint64_t webhook_serial;
     65     char *event_type;
     66     char *url;
     67     char *http_method;
     68     char *header_template;
     69     char *body_template;
     70     struct GNUNET_PQ_ResultSpec rs[] = {
     71       GNUNET_PQ_result_spec_uint64 ("webhook_serial",
     72                                     &webhook_serial),
     73       GNUNET_PQ_result_spec_string ("event_type",
     74                                     &event_type),
     75       GNUNET_PQ_result_spec_string ("url",
     76                                     &url),
     77       GNUNET_PQ_result_spec_string ("http_method",
     78                                     &http_method),
     79       GNUNET_PQ_result_spec_allow_null (
     80         GNUNET_PQ_result_spec_string ("header_template",
     81                                       &header_template),
     82         NULL),
     83       GNUNET_PQ_result_spec_allow_null (
     84         GNUNET_PQ_result_spec_string ("body_template",
     85                                       &body_template),
     86         NULL),
     87       GNUNET_PQ_result_spec_end
     88     };
     89 
     90     if (GNUNET_OK !=
     91         GNUNET_PQ_extract_result (result,
     92                                   rs,
     93                                   i))
     94     {
     95       GNUNET_break (0);
     96       wlc->extract_failed = true;
     97       return;
     98     }
     99     wlc->cb (wlc->cb_cls,
    100              webhook_serial,
    101              event_type,
    102              url,
    103              http_method,
    104              header_template,
    105              body_template);
    106     GNUNET_PQ_cleanup_result (rs);
    107   }
    108 }
    109 
    110 
    111 enum GNUNET_DB_QueryStatus
    112 TALER_MERCHANTDB_lookup_webhook_by_event (
    113   struct TALER_MERCHANTDB_PostgresContext *pg,
    114   const char *instance_id,
    115   const char *event_type,
    116   TALER_MERCHANTDB_WebhookDetailCallback cb,
    117   void *cb_cls)
    118 {
    119   struct LookupWebhookDetailContext wlc = {
    120     .cb = cb,
    121     .cb_cls = cb_cls,
    122     .extract_failed = false,
    123   };
    124 
    125   struct GNUNET_PQ_QueryParam params[] = {
    126     GNUNET_PQ_query_param_string (event_type),
    127     GNUNET_PQ_query_param_end
    128   };
    129   enum GNUNET_DB_QueryStatus qs;
    130 
    131   GNUNET_assert (NULL != pg->current_merchant_id);
    132   GNUNET_assert (0 == strcmp (instance_id,
    133                               pg->current_merchant_id));
    134   check_connection (pg);
    135   TMH_PQ_prepare_anon (pg,
    136                        "SELECT"
    137                        " webhook_serial"
    138                        ",event_type"
    139                        ",url"
    140                        ",http_method"
    141                        ",header_template"
    142                        ",body_template"
    143                        " FROM merchant_webhook"
    144                        " WHERE event_type=$1");
    145 
    146   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    147                                              "",
    148                                              params,
    149                                              &lookup_webhook_by_event_cb,
    150                                              &wlc);
    151 
    152   if (wlc.extract_failed)
    153     return GNUNET_DB_STATUS_HARD_ERROR;
    154   return qs;
    155 }