merchant

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

lookup_webhooks.c (3900B)


      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_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 "merchant-database/lookup_webhooks.h"
     26 #include "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 TALER_MERCHANTDB_lookup_webhooks (struct TALER_MERCHANTDB_PostgresContext *pg,
     96                                   const char *instance_id,
     97                                   TALER_MERCHANTDB_WebhooksCallback cb,
     98                                   void *cb_cls)
     99 {
    100   struct LookupWebhookContext wlc = {
    101     .cb = cb,
    102     .cb_cls = cb_cls,
    103     /* Can be overwritten by the lookup_webhook_cb */
    104     .extract_failed = false,
    105   };
    106   struct GNUNET_PQ_QueryParam params[] = {
    107     GNUNET_PQ_query_param_string (instance_id),
    108     GNUNET_PQ_query_param_end
    109   };
    110   enum GNUNET_DB_QueryStatus qs;
    111 
    112   check_connection (pg);
    113   PREPARE (pg,
    114            "lookup_webhooks",
    115            "SELECT"
    116            " webhook_id"
    117            ",event_type"
    118            " FROM merchant_webhook"
    119            " JOIN merchant_instances"
    120            "   USING (merchant_serial)"
    121            " WHERE merchant_instances.merchant_id=$1");
    122 
    123   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    124                                              "lookup_webhooks",
    125                                              params,
    126                                              &lookup_webhooks_cb,
    127                                              &wlc);
    128   /* If there was an error inside lookup_webhook_cb, return a hard error. */
    129   if (wlc.extract_failed)
    130     return GNUNET_DB_STATUS_HARD_ERROR;
    131   return qs;
    132 }