LCOV - code coverage report
Current view: top level - lib - merchant_api_post_webhooks.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 52.8 % 72 38
Test Date: 2025-10-31 14:20:14 Functions: 100.0 % 3 3

            Line data    Source code
       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
       6              :   it under the terms of the GNU Lesser General Public License as
       7              :   published by the Free Software Foundation; either version 2.1,
       8              :   or (at your option) any later version.
       9              : 
      10              :   TALER is distributed in the hope that it will be useful,
      11              :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13              :   GNU Lesser General Public License for more details.
      14              : 
      15              :   You should have received a copy of the GNU Lesser General
      16              :   Public License along with TALER; see the file COPYING.LGPL.
      17              :   If not, see <http://www.gnu.org/licenses/>
      18              : */
      19              : /**
      20              :  * @file merchant_api_post_webhooks.c
      21              :  * @brief Implementation of the POST /webhooks request
      22              :  *        of the merchant's HTTP API
      23              :  * @author Priscilla HUANG
      24              :  */
      25              : #include "platform.h"
      26              : #include <curl/curl.h>
      27              : #include <jansson.h>
      28              : #include <microhttpd.h> /* just for HTTP status codes */
      29              : #include <gnunet/gnunet_util_lib.h>
      30              : #include "taler_merchant_service.h"
      31              : #include "merchant_api_curl_defaults.h"
      32              : #include "merchant_api_common.h"
      33              : #include <taler/taler_json_lib.h>
      34              : #include <taler/taler_curl_lib.h>
      35              : 
      36              : 
      37              : /**
      38              :  * Handle for a POST /webhooks/$ID operation.
      39              :  */
      40              : struct TALER_MERCHANT_WebhooksPostHandle
      41              : {
      42              : 
      43              :   /**
      44              :    * The url for this request.
      45              :    */
      46              :   char *url;
      47              : 
      48              :   /**
      49              :    * Handle for the request.
      50              :    */
      51              :   struct GNUNET_CURL_Job *job;
      52              : 
      53              :   /**
      54              :    * Function to call with the result.
      55              :    */
      56              :   TALER_MERCHANT_WebhooksPostCallback cb;
      57              : 
      58              :   /**
      59              :    * Closure for @a cb.
      60              :    */
      61              :   void *cb_cls;
      62              : 
      63              :   /**
      64              :    * Reference to the execution context.
      65              :    */
      66              :   struct GNUNET_CURL_Context *ctx;
      67              : 
      68              :   /**
      69              :    * Minor context that holds body and headers.
      70              :    */
      71              :   struct TALER_CURL_PostContext post_ctx;
      72              : 
      73              : };
      74              : 
      75              : 
      76              : /**
      77              :  * Function called when we're done processing the
      78              :  * HTTP POST /webhooks request.
      79              :  *
      80              :  * @param cls the `struct TALER_MERCHANT_WebhooksPostHandle`
      81              :  * @param response_code HTTP response code, 0 on error
      82              :  * @param response response body, NULL if not in JSON
      83              :  */
      84              : static void
      85           10 : handle_post_webhooks_finished (void *cls,
      86              :                                long response_code,
      87              :                                const void *response)
      88              : {
      89           10 :   struct TALER_MERCHANT_WebhooksPostHandle *wph = cls;
      90           10 :   const json_t *json = response;
      91           10 :   struct TALER_MERCHANT_HttpResponse hr = {
      92           10 :     .http_status = (unsigned int) response_code,
      93              :     .reply = json
      94              :   };
      95              : 
      96           10 :   wph->job = NULL;
      97           10 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      98              :               "POST /webhooks completed with response code %u\n",
      99              :               (unsigned int) response_code);
     100           10 :   switch (response_code)
     101              :   {
     102            0 :   case 0:
     103            0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     104            0 :     break;
     105            8 :   case MHD_HTTP_NO_CONTENT:
     106            8 :     break;
     107            0 :   case MHD_HTTP_BAD_REQUEST:
     108            0 :     hr.ec = TALER_JSON_get_error_code (json);
     109            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110              :     /* This should never happen, either us
     111              :      * or the merchant is buggy (or API version conflict);
     112              :      * just pass JSON reply to the application */
     113            0 :     break;
     114            0 :   case MHD_HTTP_UNAUTHORIZED:
     115            0 :     hr.ec = TALER_JSON_get_error_code (json);
     116            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     117              :     /* Nothing really to verify, merchant says we need to authenticate. */
     118            0 :     break;
     119            0 :   case MHD_HTTP_FORBIDDEN:
     120            0 :     hr.ec = TALER_JSON_get_error_code (json);
     121            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     122              :     /* Nothing really to verify, merchant says we tried to abort the payment
     123              :      * after it was successful. We should pass the JSON reply to the
     124              :      * application */
     125            0 :     break;
     126            0 :   case MHD_HTTP_NOT_FOUND:
     127            0 :     hr.ec = TALER_JSON_get_error_code (json);
     128            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     129              :     /* Nothing really to verify, this should never
     130              :        happen, we should pass the JSON reply to the
     131              :        application */
     132            0 :     break;
     133            2 :   case MHD_HTTP_CONFLICT:
     134            2 :     hr.ec = TALER_JSON_get_error_code (json);
     135            2 :     hr.hint = TALER_JSON_get_error_hint (json);
     136            2 :     break;
     137            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     138            0 :     hr.ec = TALER_JSON_get_error_code (json);
     139            0 :     hr.hint = TALER_JSON_get_error_hint (json);
     140              :     /* Server had an internal issue; we should retry,
     141              :        but this API leaves this to the application */
     142            0 :     break;
     143            0 :   default:
     144            0 :     TALER_MERCHANT_parse_error_details_ (json,
     145              :                                          response_code,
     146              :                                          &hr);
     147              :     /* unexpected response code */
     148            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     149              :                 "Unexpected response code %u/%d\n",
     150              :                 (unsigned int) response_code,
     151              :                 (int) hr.ec);
     152            0 :     GNUNET_break_op (0);
     153            0 :     break;
     154              :   }
     155           10 :   wph->cb (wph->cb_cls,
     156              :            &hr);
     157           10 :   TALER_MERCHANT_webhooks_post_cancel (wph);
     158           10 : }
     159              : 
     160              : 
     161              : struct TALER_MERCHANT_WebhooksPostHandle *
     162           10 : TALER_MERCHANT_webhooks_post (
     163              :   struct GNUNET_CURL_Context *ctx,
     164              :   const char *backend_url,
     165              :   const char *webhook_id,
     166              :   const char *event_type,
     167              :   const char *url,
     168              :   const char *http_method,
     169              :   const char *header_template,
     170              :   const char *body_template,
     171              :   TALER_MERCHANT_WebhooksPostCallback cb,
     172              :   void *cb_cls)
     173              : {
     174              :   struct TALER_MERCHANT_WebhooksPostHandle *wph;
     175              :   json_t *req_obj;
     176              : 
     177           10 :   req_obj = GNUNET_JSON_PACK (
     178              :     GNUNET_JSON_pack_string ("webhook_id",
     179              :                              webhook_id),
     180              :     GNUNET_JSON_pack_string ("event_type",
     181              :                              event_type),
     182              :     GNUNET_JSON_pack_string ("url",
     183              :                              url),
     184              :     GNUNET_JSON_pack_string ("http_method",
     185              :                              http_method),
     186              :     GNUNET_JSON_pack_string ("header_template",
     187              :                              header_template),
     188              :     GNUNET_JSON_pack_string ("body_template",
     189              :                              body_template));
     190           10 :   wph = GNUNET_new (struct TALER_MERCHANT_WebhooksPostHandle);
     191           10 :   wph->ctx = ctx;
     192           10 :   wph->cb = cb;
     193           10 :   wph->cb_cls = cb_cls;
     194           10 :   wph->url = TALER_url_join (backend_url,
     195              :                              "private/webhooks",
     196              :                              NULL);
     197           10 :   if (NULL == wph->url)
     198              :   {
     199            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     200              :                 "Could not construct request URL.\n");
     201            0 :     json_decref (req_obj);
     202            0 :     GNUNET_free (wph);
     203            0 :     return NULL;
     204              :   }
     205              :   {
     206              :     CURL *eh;
     207              : 
     208           10 :     eh = TALER_MERCHANT_curl_easy_get_ (wph->url);
     209           10 :     GNUNET_assert (GNUNET_OK ==
     210              :                    TALER_curl_easy_post (&wph->post_ctx,
     211              :                                          eh,
     212              :                                          req_obj));
     213           10 :     json_decref (req_obj);
     214           20 :     wph->job = GNUNET_CURL_job_add2 (ctx,
     215              :                                      eh,
     216           10 :                                      wph->post_ctx.headers,
     217              :                                      &handle_post_webhooks_finished,
     218              :                                      wph);
     219           10 :     GNUNET_assert (NULL != wph->job);
     220              :   }
     221           10 :   return wph;
     222              : }
     223              : 
     224              : 
     225              : void
     226           10 : TALER_MERCHANT_webhooks_post_cancel (
     227              :   struct TALER_MERCHANT_WebhooksPostHandle *wph)
     228              : {
     229           10 :   if (NULL != wph->job)
     230              :   {
     231            0 :     GNUNET_CURL_job_cancel (wph->job);
     232            0 :     wph->job = NULL;
     233              :   }
     234           10 :   TALER_curl_easy_post_finished (&wph->post_ctx);
     235           10 :   GNUNET_free (wph->url);
     236           10 :   GNUNET_free (wph);
     237           10 : }
     238              : 
     239              : 
     240              : /* end of merchant_api_post_webhooks.c */
        

Generated by: LCOV version 2.0-1