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