merchant

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

merchant_api_patch_otp_device.c (7260B)


      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_patch_otp_device.c
     21  * @brief Implementation of the PATCH /otp-devices/$ID request
     22  *        of the merchant's HTTP API
     23  * @author Christian Grothoff
     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_common.h"
     32 #include "merchant_api_curl_defaults.h"
     33 #include <taler/taler_json_lib.h>
     34 #include <taler/taler_curl_lib.h>
     35 
     36 
     37 /**
     38  * Handle for a PATCH /otp-devices/$ID operation.
     39  */
     40 struct TALER_MERCHANT_OtpDevicePatchHandle
     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_OtpDevicePatchCallback 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 PATCH /otp-devices/$ID request.
     79  *
     80  * @param cls the `struct TALER_MERCHANT_OtpDevicePatchHandle`
     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 handle_patch_otp_device_finished (void *cls,
     86                                   long response_code,
     87                                   const void *response)
     88 {
     89   struct TALER_MERCHANT_OtpDevicePatchHandle *tph = cls;
     90   const json_t *json = response;
     91   struct TALER_MERCHANT_HttpResponse hr = {
     92     .http_status = (unsigned int) response_code,
     93     .reply = json
     94   };
     95 
     96   tph->job = NULL;
     97   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     98               "PATCH /otp-devices/$ID completed with response code %u\n",
     99               (unsigned int) response_code);
    100   switch (response_code)
    101   {
    102   case 0:
    103     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    104     break;
    105   case MHD_HTTP_NO_CONTENT:
    106     break;
    107   case MHD_HTTP_BAD_REQUEST:
    108     hr.ec = TALER_JSON_get_error_code (json);
    109     hr.hint = TALER_JSON_get_error_hint (json);
    110     GNUNET_break_op (0);
    111     /* This should never happen, either us
    112      * or the merchant is buggy (or API version conflict);
    113      * just pass JSON reply to the application */
    114     break;
    115   case MHD_HTTP_UNAUTHORIZED:
    116     hr.ec = TALER_JSON_get_error_code (json);
    117     hr.hint = TALER_JSON_get_error_hint (json);
    118     /* Nothing really to verify, merchant says we need to authenticate. */
    119     break;
    120   case MHD_HTTP_FORBIDDEN:
    121     hr.ec = TALER_JSON_get_error_code (json);
    122     hr.hint = TALER_JSON_get_error_hint (json);
    123     /* Nothing really to verify, merchant says we tried to abort the payment
    124      * after it was successful. We should pass the JSON reply to the
    125      * application */
    126     break;
    127   case MHD_HTTP_NOT_FOUND:
    128     hr.ec = TALER_JSON_get_error_code (json);
    129     hr.hint = TALER_JSON_get_error_hint (json);
    130     break;
    131   case MHD_HTTP_CONFLICT:
    132     hr.ec = TALER_JSON_get_error_code (json);
    133     hr.hint = TALER_JSON_get_error_hint (json);
    134     break;
    135   case MHD_HTTP_INTERNAL_SERVER_ERROR:
    136     hr.ec = TALER_JSON_get_error_code (json);
    137     hr.hint = TALER_JSON_get_error_hint (json);
    138     /* Server had an internal issue; we should retry,
    139        but this API leaves this to the application */
    140     break;
    141   default:
    142     TALER_MERCHANT_parse_error_details_ (json,
    143                                          response_code,
    144                                          &hr);
    145     /* unexpected response code */
    146     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    147                 "Unexpected response code %u/%d\n",
    148                 (unsigned int) response_code,
    149                 (int) hr.ec);
    150     GNUNET_break_op (0);
    151     break;
    152   }
    153   tph->cb (tph->cb_cls,
    154            &hr);
    155   TALER_MERCHANT_otp_device_patch_cancel (tph);
    156 }
    157 
    158 
    159 struct TALER_MERCHANT_OtpDevicePatchHandle *
    160 TALER_MERCHANT_otp_device_patch (
    161   struct GNUNET_CURL_Context *ctx,
    162   const char *backend_url,
    163   const char *otp_device_id,
    164   const char *otp_device_description,
    165   const char *otp_key,
    166   enum TALER_MerchantConfirmationAlgorithm mca,
    167   uint64_t otp_ctr,
    168   TALER_MERCHANT_OtpDevicePatchCallback cb,
    169   void *cb_cls)
    170 {
    171   struct TALER_MERCHANT_OtpDevicePatchHandle *tph;
    172   json_t *req_obj;
    173 
    174   req_obj = GNUNET_JSON_PACK (
    175     GNUNET_JSON_pack_string ("otp_device_description",
    176                              otp_device_description),
    177     GNUNET_JSON_pack_uint64 ("otp_algorithm",
    178                              (uint32_t) mca),
    179     GNUNET_JSON_pack_allow_null (
    180       GNUNET_JSON_pack_string ("otp_key",
    181                                otp_key)),
    182     GNUNET_JSON_pack_uint64 ("otp_ctr",
    183                              otp_ctr));
    184   tph = GNUNET_new (struct TALER_MERCHANT_OtpDevicePatchHandle);
    185   tph->ctx = ctx;
    186   tph->cb = cb;
    187   tph->cb_cls = cb_cls;
    188   {
    189     char *path;
    190 
    191     GNUNET_asprintf (&path,
    192                      "private/otp-devices/%s",
    193                      otp_device_id);
    194     tph->url = TALER_url_join (backend_url,
    195                                path,
    196                                NULL);
    197     GNUNET_free (path);
    198   }
    199   if (NULL == tph->url)
    200   {
    201     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    202                 "Could not construct request URL.\n");
    203     json_decref (req_obj);
    204     GNUNET_free (tph);
    205     return NULL;
    206   }
    207   {
    208     CURL *eh;
    209 
    210     eh = TALER_MERCHANT_curl_easy_get_ (tph->url);
    211     if (GNUNET_OK !=
    212         TALER_curl_easy_post (&tph->post_ctx,
    213                               eh,
    214                               req_obj))
    215     {
    216       GNUNET_break (0);
    217       curl_easy_cleanup (eh);
    218       json_decref (req_obj);
    219       GNUNET_free (tph);
    220       return NULL;
    221     }
    222     json_decref (req_obj);
    223     GNUNET_assert (CURLE_OK ==
    224                    curl_easy_setopt (eh,
    225                                      CURLOPT_CUSTOMREQUEST,
    226                                      MHD_HTTP_METHOD_PATCH));
    227     tph->job = GNUNET_CURL_job_add2 (ctx,
    228                                      eh,
    229                                      tph->post_ctx.headers,
    230                                      &handle_patch_otp_device_finished,
    231                                      tph);
    232   }
    233   return tph;
    234 }
    235 
    236 
    237 void
    238 TALER_MERCHANT_otp_device_patch_cancel (
    239   struct TALER_MERCHANT_OtpDevicePatchHandle *tph)
    240 {
    241   if (NULL != tph->job)
    242   {
    243     GNUNET_CURL_job_cancel (tph->job);
    244     tph->job = NULL;
    245   }
    246   TALER_curl_easy_post_finished (&tph->post_ctx);
    247   GNUNET_free (tph->url);
    248   GNUNET_free (tph);
    249 }
    250 
    251 
    252 /* end of merchant_api_patch_otp_device.c */