merchant

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

merchant_api_get_webhook.c (6117B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Lesser General Public License as published by the Free Software
      7   Foundation; either version 2.1, 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 Lesser General Public License for more details.
     12 
     13   You should have received a copy of the GNU Lesser General Public License along with
     14   TALER; see the file COPYING.LGPL.  If not, see
     15   <http://www.gnu.org/licenses/>
     16 */
     17 /**
     18  * @file merchant_api_get_webhook.c
     19  * @brief Implementation of the GET /webhooks/$ID request of the merchant's HTTP API
     20  * @author Priscilla HUANG
     21  */
     22 #include "platform.h"
     23 #include <curl/curl.h>
     24 #include <jansson.h>
     25 #include <microhttpd.h> /* just for HTTP status codes */
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include "taler_merchant_service.h"
     29 #include "merchant_api_curl_defaults.h"
     30 #include <taler/taler_json_lib.h>
     31 #include <taler/taler_signatures.h>
     32 
     33 
     34 /**
     35  * Handle for a GET /webhooks/$ID operation.
     36  */
     37 struct TALER_MERCHANT_WebhookGetHandle
     38 {
     39   /**
     40    * The url for this request.
     41    */
     42   char *url;
     43 
     44   /**
     45    * Handle for the request.
     46    */
     47   struct GNUNET_CURL_Job *job;
     48 
     49   /**
     50    * Function to call with the result.
     51    */
     52   TALER_MERCHANT_WebhookGetCallback cb;
     53 
     54   /**
     55    * Closure for @a cb.
     56    */
     57   void *cb_cls;
     58 
     59   /**
     60    * Reference to the execution context.
     61    */
     62   struct GNUNET_CURL_Context *ctx;
     63 
     64 };
     65 
     66 
     67 /**
     68  * Function called when we're done processing the
     69  * HTTP GET /webhooks/$ID request.
     70  *
     71  * @param cls the `struct TALER_MERCHANT_WebhookGetHandle`
     72  * @param response_code HTTP response code, 0 on error
     73  * @param response response body, NULL if not in JSON
     74  */
     75 static void
     76 handle_get_webhook_finished (void *cls,
     77                              long response_code,
     78                              const void *response)
     79 {
     80   struct TALER_MERCHANT_WebhookGetHandle *wgh = cls;
     81   const json_t *json = response;
     82   struct TALER_MERCHANT_HttpResponse hr = {
     83     .http_status = (unsigned int) response_code,
     84     .reply = json
     85   };
     86 
     87   wgh->job = NULL;
     88   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     89               "Got /webhooks/$ID response with status code %u\n",
     90               (unsigned int) response_code);
     91   switch (response_code)
     92   {
     93   case MHD_HTTP_OK:
     94     {
     95       const char *event_type;
     96       const char *url;
     97       const char *http_method;
     98       const char *header_template;
     99       const char *body_template;
    100       bool rst_ok = true;
    101       struct GNUNET_JSON_Specification spec[] = {
    102         GNUNET_JSON_spec_string ("event_type",
    103                                  &event_type),
    104         TALER_JSON_spec_web_url ("url",
    105                                  &url),
    106         GNUNET_JSON_spec_string ("http_method",
    107                                  &http_method),
    108         GNUNET_JSON_spec_string ("header_template",
    109                                  &header_template),
    110         GNUNET_JSON_spec_string ("body_template",
    111                                  &body_template),
    112         GNUNET_JSON_spec_end ()
    113       };
    114 
    115 
    116       if ( (rst_ok) &&
    117            (GNUNET_OK ==
    118             GNUNET_JSON_parse (json,
    119                                spec,
    120                                NULL, NULL)) )
    121       {
    122         wgh->cb (wgh->cb_cls,
    123                  &hr,
    124                  event_type,
    125                  url,
    126                  http_method,
    127                  header_template,
    128                  body_template);
    129         GNUNET_JSON_parse_free (spec);
    130         TALER_MERCHANT_webhook_get_cancel (wgh);
    131         return;
    132       }
    133       hr.http_status = 0;
    134       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    135       GNUNET_JSON_parse_free (spec);
    136       break;
    137     }
    138   case MHD_HTTP_UNAUTHORIZED:
    139     hr.ec = TALER_JSON_get_error_code (json);
    140     hr.hint = TALER_JSON_get_error_hint (json);
    141     /* Nothing really to verify, merchant says we need to authenticate. */
    142     break;
    143   case MHD_HTTP_NOT_FOUND:
    144     hr.ec = TALER_JSON_get_error_code (json);
    145     hr.hint = TALER_JSON_get_error_hint (json);
    146     break;
    147   default:
    148     /* unexpected response code */
    149     hr.ec = TALER_JSON_get_error_code (json);
    150     hr.hint = TALER_JSON_get_error_hint (json);
    151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    152                 "Unexpected response code %u/%d\n",
    153                 (unsigned int) response_code,
    154                 (int) hr.ec);
    155     break;
    156   }
    157   wgh->cb (wgh->cb_cls,
    158            &hr,
    159            NULL,
    160            NULL,
    161            NULL,
    162            NULL,
    163            NULL);
    164   TALER_MERCHANT_webhook_get_cancel (wgh);
    165 }
    166 
    167 
    168 struct TALER_MERCHANT_WebhookGetHandle *
    169 TALER_MERCHANT_webhook_get (
    170   struct GNUNET_CURL_Context *ctx,
    171   const char *backend_url,
    172   const char *webhook_id,
    173   TALER_MERCHANT_WebhookGetCallback cb,
    174   void *cb_cls)
    175 {
    176   struct TALER_MERCHANT_WebhookGetHandle *wgh;
    177   CURL *eh;
    178 
    179   wgh = GNUNET_new (struct TALER_MERCHANT_WebhookGetHandle);
    180   wgh->ctx = ctx;
    181   wgh->cb = cb;
    182   wgh->cb_cls = cb_cls;
    183   {
    184     char *path;
    185 
    186     GNUNET_asprintf (&path,
    187                      "private/webhooks/%s",
    188                      webhook_id);
    189     wgh->url = TALER_url_join (backend_url,
    190                                path,
    191                                NULL);
    192     GNUNET_free (path);
    193   }
    194   if (NULL == wgh->url)
    195   {
    196     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    197                 "Could not construct request URL.\n");
    198     GNUNET_free (wgh);
    199     return NULL;
    200   }
    201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    202               "Requesting URL '%s'\n",
    203               wgh->url);
    204   eh = TALER_MERCHANT_curl_easy_get_ (wgh->url);
    205   wgh->job = GNUNET_CURL_job_add (ctx,
    206                                   eh,
    207                                   &handle_get_webhook_finished,
    208                                   wgh);
    209   return wgh;
    210 }
    211 
    212 
    213 void
    214 TALER_MERCHANT_webhook_get_cancel (
    215   struct TALER_MERCHANT_WebhookGetHandle *wgh)
    216 {
    217   if (NULL != wgh->job)
    218     GNUNET_CURL_job_cancel (wgh->job);
    219   GNUNET_free (wgh->url);
    220   GNUNET_free (wgh);
    221 }