merchant

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

taler-merchant-httpd_private-patch-webhooks-ID.c (6559B)


      1 /*
      2   This file is part of TALER
      3   (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 Affero General Public License as
      7   published by the Free Software Foundation; either version 3,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 
     20 /**
     21  * @file taler-merchant-httpd_private-patch-webhooks-ID.c
     22  * @brief implementing PATCH /webhooks/$ID request handling
     23  * @author Priscilla HUANG
     24  */
     25 #include "platform.h"
     26 #include "taler-merchant-httpd_private-patch-webhooks-ID.h"
     27 #include "taler-merchant-httpd_helper.h"
     28 #include <taler/taler_json_lib.h>
     29 
     30 
     31 /**
     32  * How often do we retry the simple INSERT database transaction?
     33  */
     34 #define MAX_RETRIES 3
     35 
     36 
     37 /**
     38  * Determine the cause of the PATCH failure in more detail and report.
     39  *
     40  * @param connection connection to report on
     41  * @param instance_id instance we are processing
     42  * @param webhook_id ID of the webhook to patch
     43  * @param wb webhook details we failed to set
     44  */
     45 static MHD_RESULT
     46 determine_cause (struct MHD_Connection *connection,
     47                  const char *instance_id,
     48                  const char *webhook_id,
     49                  const struct TALER_MERCHANTDB_WebhookDetails *wb)
     50 {
     51   struct TALER_MERCHANTDB_WebhookDetails wpx;
     52   enum GNUNET_DB_QueryStatus qs;
     53 
     54   qs = TMH_db->lookup_webhook (TMH_db->cls,
     55                                instance_id,
     56                                webhook_id,
     57                                &wpx);
     58   switch (qs)
     59   {
     60   case GNUNET_DB_STATUS_HARD_ERROR:
     61     GNUNET_break (0);
     62     return TALER_MHD_reply_with_error (connection,
     63                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     64                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     65                                        NULL);
     66   case GNUNET_DB_STATUS_SOFT_ERROR:
     67     GNUNET_break (0);
     68     return TALER_MHD_reply_with_error (connection,
     69                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     70                                        TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
     71                                        "unexpected serialization problem");
     72   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     73     return TALER_MHD_reply_with_error (connection,
     74                                        MHD_HTTP_NOT_FOUND,
     75                                        TALER_EC_MERCHANT_GENERIC_WEBHOOK_UNKNOWN,
     76                                        webhook_id);
     77   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     78     break; /* do below */
     79   }
     80 
     81   {
     82     enum TALER_ErrorCode ec;
     83 
     84     ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     85     TALER_MERCHANTDB_webhook_details_free (&wpx);
     86     GNUNET_break (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE != ec);
     87     return TALER_MHD_reply_with_error (connection,
     88                                        MHD_HTTP_CONFLICT,
     89                                        ec,
     90                                        NULL);
     91   }
     92 }
     93 
     94 
     95 /**
     96  * PATCH configuration of an existing instance, given its configuration.
     97  *
     98  * @param rh context of the handler
     99  * @param connection the MHD connection to handle
    100  * @param[in,out] hc context with further information about the request
    101  * @return MHD result code
    102  */
    103 MHD_RESULT
    104 TMH_private_patch_webhooks_ID (const struct TMH_RequestHandler *rh,
    105                                struct MHD_Connection *connection,
    106                                struct TMH_HandlerContext *hc)
    107 {
    108   struct TMH_MerchantInstance *mi = hc->instance;
    109   const char *webhook_id = hc->infix;
    110   struct TALER_MERCHANTDB_WebhookDetails wb = {0};
    111   enum GNUNET_DB_QueryStatus qs;
    112   struct GNUNET_JSON_Specification spec[] = {
    113     GNUNET_JSON_spec_string ("event_type",
    114                              (const char **) &wb.event_type),
    115     TALER_JSON_spec_web_url ("url",
    116                              (const char **) &wb.url),
    117     GNUNET_JSON_spec_string ("http_method",
    118                              (const char **) &wb.http_method),
    119     GNUNET_JSON_spec_mark_optional (
    120       GNUNET_JSON_spec_string ("header_template",
    121                                (const char **) &wb.header_template),
    122       NULL),
    123     GNUNET_JSON_spec_mark_optional (
    124       GNUNET_JSON_spec_string ("body_template",
    125                                (const char **) &wb.body_template),
    126       NULL),
    127     GNUNET_JSON_spec_end ()
    128   };
    129 
    130   GNUNET_assert (NULL != mi);
    131   GNUNET_assert (NULL != webhook_id);
    132   {
    133     enum GNUNET_GenericReturnValue res;
    134 
    135     res = TALER_MHD_parse_json_data (connection,
    136                                      hc->request_body,
    137                                      spec);
    138     if (GNUNET_OK != res)
    139       return (GNUNET_NO == res)
    140              ? MHD_YES
    141              : MHD_NO;
    142   }
    143 
    144 
    145   qs = TMH_db->update_webhook (TMH_db->cls,
    146                                mi->settings.id,
    147                                webhook_id,
    148                                &wb);
    149   {
    150     MHD_RESULT ret = MHD_NO;
    151 
    152     switch (qs)
    153     {
    154     case GNUNET_DB_STATUS_HARD_ERROR:
    155       GNUNET_break (0);
    156       ret = TALER_MHD_reply_with_error (connection,
    157                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
    158                                         TALER_EC_GENERIC_DB_STORE_FAILED,
    159                                         NULL);
    160       break;
    161     case GNUNET_DB_STATUS_SOFT_ERROR:
    162       GNUNET_break (0);
    163       ret = TALER_MHD_reply_with_error (connection,
    164                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
    165                                         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
    166                                         "unexpected serialization problem");
    167       break;
    168     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    169       ret = determine_cause (connection,
    170                              mi->settings.id,
    171                              webhook_id,
    172                              &wb);
    173       break;
    174     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    175       ret = TALER_MHD_reply_static (connection,
    176                                     MHD_HTTP_NO_CONTENT,
    177                                     NULL,
    178                                     NULL,
    179                                     0);
    180       break;
    181     }
    182     GNUNET_JSON_parse_free (spec);
    183     return ret;
    184   }
    185 }
    186 
    187 
    188 /* end of taler-merchant-httpd_private-patch-webhooks-ID.c */