merchant

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

merchant_api_delete_order.c (4724B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-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_delete_order.c
     19  * @brief Implementation of the DELETE /orders/$ORDER_ID request of the merchant's HTTP API
     20  * @author Jonathan Buchanan
     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  * Handle for a DELETE /orders/$ID operation.
     35  */
     36 struct TALER_MERCHANT_OrderDeleteHandle
     37 {
     38   /**
     39    * The url for this request.
     40    */
     41   char *url;
     42 
     43   /**
     44    * Handle for the request.
     45    */
     46   struct GNUNET_CURL_Job *job;
     47 
     48   /**
     49    * Function to call with the result.
     50    */
     51   TALER_MERCHANT_OrderDeleteCallback cb;
     52 
     53   /**
     54    * Closure for @a cb.
     55    */
     56   void *cb_cls;
     57 
     58   /**
     59    * Reference to the execution context.
     60    */
     61   struct GNUNET_CURL_Context *ctx;
     62 };
     63 
     64 
     65 /**
     66  * Function called when we're done processing the
     67  * HTTP DELETE /orders/$ORDER_ID request.
     68  *
     69  * @param cls the `struct TALER_MERCHANT_OrderDeleteHandle`
     70  * @param response_code HTTP response code, 0 on error
     71  * @param response response body, NULL if not in JSON
     72  */
     73 static void
     74 handle_delete_order_finished (void *cls,
     75                               long response_code,
     76                               const void *response)
     77 {
     78   struct TALER_MERCHANT_OrderDeleteHandle *odh = cls;
     79   struct TALER_MERCHANT_HttpResponse hr = {
     80     .http_status = (unsigned int) response_code,
     81     .reply = NULL,
     82   };
     83 
     84   odh->job = NULL;
     85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     86               "Got /orders/$ID response with status code %u\n",
     87               (unsigned int) response_code);
     88   switch (response_code)
     89   {
     90   case MHD_HTTP_NO_CONTENT:
     91     break;
     92   case MHD_HTTP_UNAUTHORIZED:
     93     hr.ec = TALER_JSON_get_error_code (response);
     94     hr.hint = TALER_JSON_get_error_hint (response);
     95     /* Nothing really to verify, merchant says we need to authenticate. */
     96     break;
     97   case MHD_HTTP_NOT_FOUND:
     98     break;
     99   case MHD_HTTP_CONFLICT:
    100     break;
    101   default:
    102     /* unexpected response code */
    103     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    104                 "Unexpected response code %u\n",
    105                 (unsigned int) response_code);
    106     break;
    107   }
    108   odh->cb (odh->cb_cls,
    109            &hr);
    110   TALER_MERCHANT_order_delete_cancel (odh);
    111 }
    112 
    113 
    114 struct TALER_MERCHANT_OrderDeleteHandle *
    115 TALER_MERCHANT_order_delete (
    116   struct GNUNET_CURL_Context *ctx,
    117   const char *backend_url,
    118   const char *order_id,
    119   bool force,
    120   TALER_MERCHANT_OrderDeleteCallback cb,
    121   void *cb_cls)
    122 {
    123   struct TALER_MERCHANT_OrderDeleteHandle *odh;
    124 
    125   odh = GNUNET_new (struct TALER_MERCHANT_OrderDeleteHandle);
    126   odh->ctx = ctx;
    127   odh->cb = cb;
    128   odh->cb_cls = cb_cls;
    129   {
    130     char *path;
    131 
    132     GNUNET_asprintf (&path,
    133                      "private/orders/%s%s",
    134                      order_id,
    135                      force
    136                      ? "?force=yes"
    137                      : "");
    138 
    139     odh->url = TALER_url_join (backend_url,
    140                                path,
    141                                NULL);
    142     GNUNET_free (path);
    143   }
    144   if (NULL == odh->url)
    145   {
    146     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    147                 "Could not construct request url.\n");
    148     GNUNET_free (odh);
    149     return NULL;
    150   }
    151 
    152   {
    153     CURL *eh;
    154 
    155     eh = TALER_MERCHANT_curl_easy_get_ (odh->url);
    156     GNUNET_assert (CURLE_OK ==
    157                    curl_easy_setopt (eh,
    158                                      CURLOPT_CUSTOMREQUEST,
    159                                      MHD_HTTP_METHOD_DELETE));
    160     odh->job = GNUNET_CURL_job_add (ctx,
    161                                     eh,
    162                                     &handle_delete_order_finished,
    163                                     odh);
    164   }
    165   return odh;
    166 }
    167 
    168 
    169 void
    170 TALER_MERCHANT_order_delete_cancel (
    171   struct TALER_MERCHANT_OrderDeleteHandle *odh)
    172 {
    173   if (NULL != odh->job)
    174     GNUNET_CURL_job_cancel (odh->job);
    175   GNUNET_free (odh->url);
    176   GNUNET_free (odh);
    177 }