merchant

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

merchant_api_delete-private-orders-ORDER_ID.c (6536B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2020-2026 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-private-orders-ORDER_ID.c
     19  * @brief Implementation of the DELETE /private/orders/$ORDER_ID request
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/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/taler-merchant/delete-private-orders-ORDER_ID.h>
     29 #include "merchant_api_curl_defaults.h"
     30 #include <taler/taler_json_lib.h>
     31 
     32 
     33 /**
     34  * Handle for a DELETE /private/orders/$ORDER_ID operation.
     35  */
     36 struct TALER_MERCHANT_DeletePrivateOrderHandle
     37 {
     38   /**
     39    * Base URL of the merchant backend.
     40    */
     41   char *base_url;
     42 
     43   /**
     44    * The full 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_DeletePrivateOrderCallback cb;
     57 
     58   /**
     59    * Closure for @a cb.
     60    */
     61   TALER_MERCHANT_DELETE_PRIVATE_ORDER_RESULT_CLOSURE *cb_cls;
     62 
     63   /**
     64    * Reference to the execution context.
     65    */
     66   struct GNUNET_CURL_Context *ctx;
     67 
     68   /**
     69    * Identifier of the order to delete.
     70    */
     71   char *order_id;
     72 
     73   /**
     74    * Whether to force deletion even if order has payments.
     75    */
     76   bool force;
     77 };
     78 
     79 
     80 /**
     81  * Function called when we're done processing the
     82  * HTTP DELETE /private/orders/$ORDER_ID request.
     83  *
     84  * @param cls the `struct TALER_MERCHANT_DeletePrivateOrderHandle`
     85  * @param response_code HTTP response code, 0 on error
     86  * @param response response body, NULL if not in JSON
     87  */
     88 static void
     89 handle_delete_order_finished (void *cls,
     90                               long response_code,
     91                               const void *response)
     92 {
     93   struct TALER_MERCHANT_DeletePrivateOrderHandle *doh = cls;
     94   const json_t *json = response;
     95   struct TALER_MERCHANT_DeletePrivateOrderResponse dor = {
     96     .hr.http_status = (unsigned int) response_code,
     97     .hr.reply = json
     98   };
     99 
    100   doh->job = NULL;
    101   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    102               "Got /private/orders/$ORDER_ID DELETE response with status code %u\n",
    103               (unsigned int) response_code);
    104   switch (response_code)
    105   {
    106   case MHD_HTTP_NO_CONTENT:
    107     break;
    108   case MHD_HTTP_UNAUTHORIZED:
    109     dor.hr.ec = TALER_JSON_get_error_code (json);
    110     dor.hr.hint = TALER_JSON_get_error_hint (json);
    111     break;
    112   case MHD_HTTP_NOT_FOUND:
    113     dor.hr.ec = TALER_JSON_get_error_code (json);
    114     dor.hr.hint = TALER_JSON_get_error_hint (json);
    115     break;
    116   case MHD_HTTP_CONFLICT:
    117     dor.hr.ec = TALER_JSON_get_error_code (json);
    118     dor.hr.hint = TALER_JSON_get_error_hint (json);
    119     break;
    120   default:
    121     dor.hr.ec = TALER_JSON_get_error_code (json);
    122     dor.hr.hint = TALER_JSON_get_error_hint (json);
    123     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    124                 "Unexpected response code %u/%d for DELETE /private/orders/$ORDER_ID\n",
    125                 (unsigned int) response_code,
    126                 (int) dor.hr.ec);
    127     break;
    128   }
    129   doh->cb (doh->cb_cls,
    130            &dor);
    131   TALER_MERCHANT_delete_private_order_cancel (doh);
    132 }
    133 
    134 
    135 struct TALER_MERCHANT_DeletePrivateOrderHandle *
    136 TALER_MERCHANT_delete_private_order_create (
    137   struct GNUNET_CURL_Context *ctx,
    138   const char *url,
    139   const char *order_id)
    140 {
    141   struct TALER_MERCHANT_DeletePrivateOrderHandle *doh;
    142 
    143   doh = GNUNET_new (struct TALER_MERCHANT_DeletePrivateOrderHandle);
    144   doh->ctx = ctx;
    145   doh->base_url = GNUNET_strdup (url);
    146   doh->order_id = GNUNET_strdup (order_id);
    147   doh->force = false;
    148   return doh;
    149 }
    150 
    151 
    152 enum GNUNET_GenericReturnValue
    153 TALER_MERCHANT_delete_private_order_set_options_ (
    154   struct TALER_MERCHANT_DeletePrivateOrderHandle *handle,
    155   unsigned int num_options,
    156   const struct TALER_MERCHANT_DeletePrivateOrderOptionValue options[])
    157 {
    158   for (unsigned int i = 0; i < num_options; i++)
    159   {
    160     switch (options[i].option)
    161     {
    162     case TALER_MERCHANT_DELETE_PRIVATE_ORDER_OPTION_END:
    163       return GNUNET_OK;
    164     case TALER_MERCHANT_DELETE_PRIVATE_ORDER_OPTION_FORCE:
    165       handle->force = true;
    166       continue;
    167     }
    168     GNUNET_break (0);
    169     return GNUNET_SYSERR;
    170   }
    171   return GNUNET_OK;
    172 }
    173 
    174 
    175 enum TALER_ErrorCode
    176 TALER_MERCHANT_delete_private_order_start (
    177   struct TALER_MERCHANT_DeletePrivateOrderHandle *doh,
    178   TALER_MERCHANT_DeletePrivateOrderCallback cb,
    179   TALER_MERCHANT_DELETE_PRIVATE_ORDER_RESULT_CLOSURE *cb_cls)
    180 {
    181   CURL *eh;
    182 
    183   doh->cb = cb;
    184   doh->cb_cls = cb_cls;
    185   {
    186     char *path;
    187 
    188     GNUNET_asprintf (&path,
    189                      "private/orders/%s",
    190                      doh->order_id);
    191     doh->url = TALER_url_join (doh->base_url,
    192                                path,
    193                                "force",
    194                                (doh->force) ? "yes" : NULL,
    195                                NULL);
    196     GNUNET_free (path);
    197   }
    198   if (NULL == doh->url)
    199     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    200   eh = TALER_MERCHANT_curl_easy_get_ (doh->url);
    201   if (NULL == eh)
    202     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    203   GNUNET_assert (CURLE_OK ==
    204                  curl_easy_setopt (eh,
    205                                    CURLOPT_CUSTOMREQUEST,
    206                                    MHD_HTTP_METHOD_DELETE));
    207   doh->job = GNUNET_CURL_job_add (doh->ctx,
    208                                   eh,
    209                                   &handle_delete_order_finished,
    210                                   doh);
    211   if (NULL == doh->job)
    212     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    213   return TALER_EC_NONE;
    214 }
    215 
    216 
    217 void
    218 TALER_MERCHANT_delete_private_order_cancel (
    219   struct TALER_MERCHANT_DeletePrivateOrderHandle *doh)
    220 {
    221   if (NULL != doh->job)
    222   {
    223     GNUNET_CURL_job_cancel (doh->job);
    224     doh->job = NULL;
    225   }
    226   GNUNET_free (doh->url);
    227   GNUNET_free (doh->order_id);
    228   GNUNET_free (doh->base_url);
    229   GNUNET_free (doh);
    230 }
    231 
    232 
    233 /* end of merchant_api_delete-private-orders-ORDER_ID.c */