merchant

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

taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.c (4233B)


      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 src/backend/taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.c
     22  * @brief implementing PATCH /webhooks/$ID request handling
     23  * @author Priscilla HUANG
     24  */
     25 #include "platform.h"
     26 #include "taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.h"
     27 #include "taler-merchant-httpd_helper.h"
     28 #include <taler/taler_json_lib.h>
     29 #include "merchant-database/update_webhook.h"
     30 
     31 
     32 /**
     33  * PATCH configuration of an existing instance, given its configuration.
     34  *
     35  * @param rh context of the handler
     36  * @param connection the MHD connection to handle
     37  * @param[in,out] hc context with further information about the request
     38  * @return MHD result code
     39  */
     40 enum MHD_Result
     41 TMH_private_patch_webhooks_ID (const struct TMH_RequestHandler *rh,
     42                                struct MHD_Connection *connection,
     43                                struct TMH_HandlerContext *hc)
     44 {
     45   struct TMH_MerchantInstance *mi = hc->instance;
     46   const char *webhook_id = hc->infix;
     47   struct TALER_MERCHANTDB_WebhookDetails wb = {0};
     48   enum GNUNET_DB_QueryStatus qs;
     49   struct GNUNET_JSON_Specification spec[] = {
     50     GNUNET_JSON_spec_string ("event_type",
     51                              (const char **) &wb.event_type),
     52     TALER_JSON_spec_web_url ("url",
     53                              (const char **) &wb.url),
     54     GNUNET_JSON_spec_string ("http_method",
     55                              (const char **) &wb.http_method),
     56     GNUNET_JSON_spec_mark_optional (
     57       GNUNET_JSON_spec_string ("header_template",
     58                                (const char **) &wb.header_template),
     59       NULL),
     60     GNUNET_JSON_spec_mark_optional (
     61       GNUNET_JSON_spec_string ("body_template",
     62                                (const char **) &wb.body_template),
     63       NULL),
     64     GNUNET_JSON_spec_end ()
     65   };
     66 
     67   GNUNET_assert (NULL != mi);
     68   GNUNET_assert (NULL != webhook_id);
     69   {
     70     enum GNUNET_GenericReturnValue res;
     71 
     72     res = TALER_MHD_parse_json_data (connection,
     73                                      hc->request_body,
     74                                      spec);
     75     if (GNUNET_OK != res)
     76       return (GNUNET_NO == res)
     77              ? MHD_YES
     78              : MHD_NO;
     79   }
     80 
     81   qs = TALER_MERCHANTDB_update_webhook (TMH_db,
     82                                         mi->settings.id,
     83                                         webhook_id,
     84                                         &wb);
     85   {
     86     enum MHD_Result ret = MHD_NO;
     87 
     88     switch (qs)
     89     {
     90     case GNUNET_DB_STATUS_HARD_ERROR:
     91       GNUNET_break (0);
     92       ret = TALER_MHD_reply_with_error (
     93         connection,
     94         MHD_HTTP_INTERNAL_SERVER_ERROR,
     95         TALER_EC_GENERIC_DB_STORE_FAILED,
     96         NULL);
     97       break;
     98     case GNUNET_DB_STATUS_SOFT_ERROR:
     99       GNUNET_break (0);
    100       ret = TALER_MHD_reply_with_error (
    101         connection,
    102         MHD_HTTP_INTERNAL_SERVER_ERROR,
    103         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
    104         "unexpected serialization problem");
    105       break;
    106     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    107       ret = TALER_MHD_reply_with_error (
    108         connection,
    109         MHD_HTTP_NOT_FOUND,
    110         TALER_EC_MERCHANT_GENERIC_WEBHOOK_UNKNOWN,
    111         webhook_id);
    112       break;
    113     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    114       ret = TALER_MHD_reply_static (connection,
    115                                     MHD_HTTP_NO_CONTENT,
    116                                     NULL,
    117                                     NULL,
    118                                     0);
    119       break;
    120     }
    121     GNUNET_JSON_parse_free (spec);
    122     return ret;
    123   }
    124 }
    125 
    126 
    127 /* end of taler-merchant-httpd_patch-private-webhooks-WEBHOOK_ID.c */