merchant

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

merchant_api_post_using_templates.c (4898B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2022-2023 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_post_using_templates.c
     21  * @brief Implementation of the POST /using_templates request
     22  *        of the merchant's HTTP API
     23  * @author Priscilla HUANG
     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 POST /templates/$ID operation.
     39  */
     40 struct TALER_MERCHANT_UsingTemplatesPostHandle
     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_PostOrdersCallback 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  * Function called when we're done processing the
     76  * HTTP POST /using-templates request.
     77  *
     78  * @param cls the `struct TALER_MERCHANT_UsingTemplatesPostHandle`
     79  * @param response_code HTTP response code, 0 on error
     80  * @param response response body, NULL if not in JSON
     81  */
     82 static void
     83 handle_post_using_templates_finished (void *cls,
     84                                       long response_code,
     85                                       const void *response)
     86 {
     87   struct TALER_MERCHANT_UsingTemplatesPostHandle *utph = cls;
     88   const json_t *json = response;
     89 
     90   utph->job = NULL;
     91   TALER_MERCHANT_handle_order_creation_response_ (utph->cb,
     92                                                   utph->cb_cls,
     93                                                   response_code,
     94                                                   json);
     95   TALER_MERCHANT_using_templates_post_cancel (utph);
     96 }
     97 
     98 
     99 struct TALER_MERCHANT_UsingTemplatesPostHandle *
    100 TALER_MERCHANT_using_templates_post (
    101   struct GNUNET_CURL_Context *ctx,
    102   const char *backend_url,
    103   const char *template_id,
    104   const char *summary,
    105   const struct TALER_Amount *amount,
    106   TALER_MERCHANT_PostOrdersCallback cb,
    107   void *cb_cls)
    108 {
    109   struct TALER_MERCHANT_UsingTemplatesPostHandle *utph;
    110   json_t *req_obj;
    111 
    112   req_obj = GNUNET_JSON_PACK (
    113     GNUNET_JSON_pack_allow_null (
    114       GNUNET_JSON_pack_string ("summary",
    115                                summary)),
    116     GNUNET_JSON_pack_allow_null (
    117       TALER_JSON_pack_amount ("amount",
    118                               amount)));
    119   utph = GNUNET_new (struct TALER_MERCHANT_UsingTemplatesPostHandle);
    120   utph->ctx = ctx;
    121   utph->cb = cb;
    122   utph->cb_cls = cb_cls;
    123   {
    124     char *path;
    125 
    126     GNUNET_asprintf (&path,
    127                      "templates/%s",
    128                      template_id);
    129     utph->url = TALER_url_join (backend_url,
    130                                 path,
    131                                 NULL);
    132     GNUNET_free (path);
    133   }
    134   if (NULL == utph->url)
    135   {
    136     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    137                 "Could not construct request URL.\n");
    138     json_decref (req_obj);
    139     GNUNET_free (utph);
    140     return NULL;
    141   }
    142   {
    143     CURL *eh;
    144 
    145     eh = TALER_MERCHANT_curl_easy_get_ (utph->url);
    146     GNUNET_assert (GNUNET_OK ==
    147                    TALER_curl_easy_post (&utph->post_ctx,
    148                                          eh,
    149                                          req_obj));
    150     json_decref (req_obj);
    151     utph->job = GNUNET_CURL_job_add2 (ctx,
    152                                       eh,
    153                                       utph->post_ctx.headers,
    154                                       &handle_post_using_templates_finished,
    155                                       utph);
    156     GNUNET_assert (NULL != utph->job);
    157   }
    158   return utph;
    159 }
    160 
    161 
    162 void
    163 TALER_MERCHANT_using_templates_post_cancel (
    164   struct TALER_MERCHANT_UsingTemplatesPostHandle *utph)
    165 {
    166   if (NULL != utph->job)
    167   {
    168     GNUNET_CURL_job_cancel (utph->job);
    169     utph->job = NULL;
    170   }
    171   TALER_curl_easy_post_finished (&utph->post_ctx);
    172   GNUNET_free (utph->url);
    173   GNUNET_free (utph);
    174 }
    175 
    176 
    177 /* end of merchant_api_post_using_templates.c */