merchant

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

pg_lookup_webhook_by_event.c (4843B)


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