merchant

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

pg_lookup_webhooks.c (3849B)


      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_webhooks.c
     18  * @brief Implementation of the lookup_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 "pg_lookup_webhooks.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Context used for postgres_lookup_webhooks().
     30  */
     31 struct LookupWebhookContext
     32 {
     33   /**
     34    * Function to call with the results.
     35    */
     36   TALER_MERCHANTDB_WebhooksCallback 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 LookupWebhookContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 lookup_webhooks_cb (void *cls,
     60                     PGresult *result,
     61                     unsigned int num_results)
     62 {
     63   struct LookupWebhookContext *wlc = cls;
     64 
     65   for (unsigned int i = 0; i < num_results; i++)
     66   {
     67     char *webhook_id;
     68     char *event_type;
     69     struct GNUNET_PQ_ResultSpec rs[] = {
     70       GNUNET_PQ_result_spec_string ("webhook_id",
     71                                     &webhook_id),
     72       GNUNET_PQ_result_spec_string ("event_type",
     73                                     &event_type),
     74       GNUNET_PQ_result_spec_end
     75     };
     76 
     77     if (GNUNET_OK !=
     78         GNUNET_PQ_extract_result (result,
     79                                   rs,
     80                                   i))
     81     {
     82       GNUNET_break (0);
     83       wlc->extract_failed = true;
     84       return;
     85     }
     86     wlc->cb (wlc->cb_cls,
     87              webhook_id,
     88              event_type);
     89     GNUNET_PQ_cleanup_result (rs);
     90   }
     91 }
     92 
     93 
     94 enum GNUNET_DB_QueryStatus
     95 TMH_PG_lookup_webhooks (void *cls,
     96                         const char *instance_id,
     97                         TALER_MERCHANTDB_WebhooksCallback cb,
     98                         void *cb_cls)
     99 {
    100   struct PostgresClosure *pg = cls;
    101   struct LookupWebhookContext wlc = {
    102     .cb = cb,
    103     .cb_cls = cb_cls,
    104     /* Can be overwritten by the lookup_webhook_cb */
    105     .extract_failed = false,
    106   };
    107   struct GNUNET_PQ_QueryParam params[] = {
    108     GNUNET_PQ_query_param_string (instance_id),
    109     GNUNET_PQ_query_param_end
    110   };
    111   enum GNUNET_DB_QueryStatus qs;
    112 
    113   check_connection (pg);
    114   PREPARE (pg,
    115            "lookup_webhooks",
    116            "SELECT"
    117            " webhook_id"
    118            ",event_type"
    119            " FROM merchant_webhook"
    120            " JOIN merchant_instances"
    121            "   USING (merchant_serial)"
    122            " WHERE merchant_instances.merchant_id=$1");
    123 
    124   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    125                                              "lookup_webhooks",
    126                                              params,
    127                                              &lookup_webhooks_cb,
    128                                              &wlc);
    129   /* If there was an error inside lookup_webhook_cb, return a hard error. */
    130   if (wlc.extract_failed)
    131     return GNUNET_DB_STATUS_HARD_ERROR;
    132   return qs;
    133 }