merchant_api_patch_product.c (10776B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020, 2021 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_product.c 21 * @brief Implementation of the PATCH /products/$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 /products/$ID operation. 39 */ 40 struct TALER_MERCHANT_ProductPatchHandle 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_ProductPatchCallback 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 /products/$ID request. 79 * 80 * @param cls the `struct TALER_MERCHANT_ProductPatchHandle` 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_product_finished (void *cls, 86 long response_code, 87 const void *response) 88 { 89 struct TALER_MERCHANT_ProductPatchHandle *pph = 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 pph->job = NULL; 97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 98 "PATCH /products/$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 pph->cb (pph->cb_cls, 154 &hr); 155 TALER_MERCHANT_product_patch_cancel (pph); 156 } 157 158 159 struct TALER_MERCHANT_ProductPatchHandle * 160 TALER_MERCHANT_product_patch2 ( 161 struct GNUNET_CURL_Context *ctx, 162 const char *backend_url, 163 const char *product_id, 164 const char *description, 165 const json_t *description_i18n, 166 const char *unit, 167 const struct TALER_Amount *unit_prices, 168 size_t unit_price_len, 169 const char *image, 170 const json_t *taxes, 171 int64_t total_stock, 172 uint32_t total_stock_frac, 173 bool unit_allow_fraction, 174 const uint32_t *unit_precision_level, 175 uint64_t total_lost, 176 const json_t *address, 177 struct GNUNET_TIME_Timestamp next_restock, 178 TALER_MERCHANT_ProductPatchCallback cb, 179 void *cb_cls) 180 { 181 struct TALER_MERCHANT_ProductPatchHandle *pph; 182 json_t *req_obj; 183 char unit_total_stock_buf[64]; 184 185 TALER_MERCHANT_format_stock_string (total_stock, 186 total_stock_frac, 187 unit_total_stock_buf, 188 sizeof (unit_total_stock_buf)); 189 190 { 191 req_obj = GNUNET_JSON_PACK ( 192 /* FIXME: once we move to the new-style API, 193 allow applications to set the product name properly! */ 194 GNUNET_JSON_pack_string ("product_name", 195 description), 196 GNUNET_JSON_pack_string ("description", 197 description), 198 GNUNET_JSON_pack_object_incref ("description_i18n", 199 (json_t *) description_i18n), 200 GNUNET_JSON_pack_string ("unit", 201 unit), 202 TALER_JSON_pack_amount_array ("unit_price", 203 unit_price_len, 204 unit_prices), 205 GNUNET_JSON_pack_string ("image", 206 image), 207 GNUNET_JSON_pack_array_incref ("taxes", 208 (json_t *) taxes), 209 GNUNET_JSON_pack_string ("unit_total_stock", 210 unit_total_stock_buf), 211 GNUNET_JSON_pack_bool ("unit_allow_fraction", 212 unit_allow_fraction), 213 GNUNET_JSON_pack_uint64 ("total_lost", 214 total_lost), 215 GNUNET_JSON_pack_object_incref ("address", 216 (json_t *) address), 217 GNUNET_JSON_pack_timestamp ("next_restock", 218 next_restock)); 219 } 220 if (NULL != unit_precision_level) 221 { 222 GNUNET_assert (0 == 223 json_object_set_new (req_obj, 224 "unit_precision_level", 225 json_integer ( 226 *unit_precision_level))); 227 } 228 if (! unit_allow_fraction) 229 { 230 GNUNET_assert (0 == 231 json_object_del (req_obj, 232 "unit_allow_fraction")); 233 if (NULL != unit_precision_level) 234 GNUNET_assert (0 == 235 json_object_del (req_obj, 236 "unit_precision_level")); 237 } 238 pph = GNUNET_new (struct TALER_MERCHANT_ProductPatchHandle); 239 pph->ctx = ctx; 240 pph->cb = cb; 241 pph->cb_cls = cb_cls; 242 { 243 char *path; 244 245 GNUNET_asprintf (&path, 246 "private/products/%s", 247 product_id); 248 pph->url = TALER_url_join (backend_url, 249 path, 250 NULL); 251 GNUNET_free (path); 252 } 253 if (NULL == pph->url) 254 { 255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 256 "Could not construct request URL.\n"); 257 json_decref (req_obj); 258 GNUNET_free (pph); 259 return NULL; 260 } 261 { 262 CURL *eh; 263 264 eh = TALER_MERCHANT_curl_easy_get_ (pph->url); 265 if (GNUNET_OK != 266 TALER_curl_easy_post (&pph->post_ctx, 267 eh, 268 req_obj)) 269 { 270 GNUNET_break (0); 271 curl_easy_cleanup (eh); 272 json_decref (req_obj); 273 GNUNET_free (pph); 274 return NULL; 275 } 276 json_decref (req_obj); 277 GNUNET_assert (CURLE_OK == 278 curl_easy_setopt (eh, 279 CURLOPT_CUSTOMREQUEST, 280 MHD_HTTP_METHOD_PATCH)); 281 pph->job = GNUNET_CURL_job_add2 (ctx, 282 eh, 283 pph->post_ctx.headers, 284 &handle_patch_product_finished, 285 pph); 286 } 287 return pph; 288 } 289 290 291 struct TALER_MERCHANT_ProductPatchHandle * 292 TALER_MERCHANT_product_patch ( 293 struct GNUNET_CURL_Context *ctx, 294 const char *backend_url, 295 const char *product_id, 296 const char *description, 297 const json_t *description_i18n, 298 const char *unit, 299 const struct TALER_Amount *price, 300 const char *image, 301 const json_t *taxes, 302 int64_t total_stock, 303 uint64_t total_lost, 304 const json_t *address, 305 struct GNUNET_TIME_Timestamp next_restock, 306 TALER_MERCHANT_ProductPatchCallback cb, 307 void *cb_cls) 308 { 309 return TALER_MERCHANT_product_patch2 (ctx, 310 backend_url, 311 product_id, 312 description, 313 description_i18n, 314 unit, 315 price, 316 1, 317 image, 318 taxes, 319 total_stock, 320 0, 321 false, 322 NULL, 323 total_lost, 324 address, 325 next_restock, 326 cb, 327 cb_cls); 328 } 329 330 331 void 332 TALER_MERCHANT_product_patch_cancel ( 333 struct TALER_MERCHANT_ProductPatchHandle *pph) 334 { 335 if (NULL != pph->job) 336 { 337 GNUNET_CURL_job_cancel (pph->job); 338 pph->job = NULL; 339 } 340 TALER_curl_easy_post_finished (&pph->post_ctx); 341 GNUNET_free (pph->url); 342 GNUNET_free (pph); 343 } 344 345 346 /* end of merchant_api_patch_product.c */