LCOV - code coverage report
Current view: top level - lib - merchant_api_post_order_refund.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 84 0.0 %
Date: 2022-08-25 06:17:04 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2021 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_post_order_refund.c
      19             :  * @brief Implementation of the POST /orders/ID/refund request
      20             :  * @author Christian Grothoff
      21             :  * @author Marcello Stanisci
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include "merchant_api_curl_defaults.h"
      31             : #include <taler/taler_json_lib.h>
      32             : #include <taler/taler_signatures.h>
      33             : #include <taler/taler_curl_lib.h>
      34             : 
      35             : 
      36             : /**
      37             :  * Handle for a POST /orders/ID/refund operation.
      38             :  */
      39             : struct TALER_MERCHANT_OrderRefundHandle
      40             : {
      41             :   /**
      42             :    * Complete URL where the backend offers /refund
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Minor context that holds body and headers.
      48             :    */
      49             :   struct TALER_CURL_PostContext post_ctx;
      50             : 
      51             :   /**
      52             :    * The CURL context to connect to the backend
      53             :    */
      54             :   struct GNUNET_CURL_Context *ctx;
      55             : 
      56             :   /**
      57             :    * The callback to pass the backend response to
      58             :    */
      59             :   TALER_MERCHANT_RefundCallback cb;
      60             : 
      61             :   /**
      62             :    * Clasure to pass to the callback
      63             :    */
      64             :   void *cb_cls;
      65             : 
      66             :   /**
      67             :    * Handle for the request
      68             :    */
      69             :   struct GNUNET_CURL_Job *job;
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Callback to process POST /orders/ID/refund response
      75             :  *
      76             :  * @param cls the `struct TALER_MERCHANT_OrderRefundHandle`
      77             :  * @param response_code HTTP response code, 0 on error
      78             :  * @param response response body, NULL if not JSON
      79             :  */
      80             : static void
      81           0 : handle_refund_finished (void *cls,
      82             :                         long response_code,
      83             :                         const void *response)
      84             : {
      85           0 :   struct TALER_MERCHANT_OrderRefundHandle *orh = cls;
      86           0 :   const json_t *json = response;
      87           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      88           0 :     .http_status = (unsigned int) response_code,
      89             :     .reply = json
      90             :   };
      91             : 
      92           0 :   orh->job = NULL;
      93           0 :   switch (response_code)
      94             :   {
      95           0 :   case 0:
      96           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      97           0 :     orh->cb (orh->cb_cls,
      98             :              &hr,
      99             :              NULL,
     100             :              NULL);
     101           0 :     break;
     102           0 :   case MHD_HTTP_OK:
     103             :     {
     104             :       const char *taler_refund_uri;
     105             :       struct TALER_PrivateContractHashP h_contract;
     106             :       struct GNUNET_JSON_Specification spec[] = {
     107           0 :         GNUNET_JSON_spec_string ("taler_refund_uri",
     108             :                                  &taler_refund_uri),
     109           0 :         GNUNET_JSON_spec_fixed_auto ("h_contract",
     110             :                                      &h_contract),
     111           0 :         GNUNET_JSON_spec_end ()
     112             :       };
     113             : 
     114           0 :       if (GNUNET_OK !=
     115           0 :           GNUNET_JSON_parse (json,
     116             :                              spec,
     117             :                              NULL, NULL))
     118             :       {
     119           0 :         GNUNET_break_op (0);
     120           0 :         hr.http_status = 0;
     121           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     122           0 :         orh->cb (orh->cb_cls,
     123             :                  &hr,
     124             :                  NULL,
     125             :                  NULL);
     126           0 :         break;
     127             :       }
     128           0 :       orh->cb (orh->cb_cls,
     129             :                &hr,
     130             :                taler_refund_uri,
     131             :                &h_contract);
     132           0 :       GNUNET_JSON_parse_free (spec);
     133             :     }
     134           0 :     break;
     135           0 :   case MHD_HTTP_UNAUTHORIZED:
     136           0 :     hr.ec = TALER_JSON_get_error_code (json);
     137           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     138             :     /* Nothing really to verify, merchant says we need to authenticate. */
     139           0 :     break;
     140           0 :   case MHD_HTTP_FORBIDDEN:
     141           0 :     hr.ec = TALER_JSON_get_error_code (json);
     142           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     143             :     /* Nothing really to verify, merchant says we need to authenticate. */
     144           0 :     break;
     145           0 :   case MHD_HTTP_NOT_FOUND:
     146             :   case MHD_HTTP_CONFLICT:
     147           0 :     hr.ec = TALER_JSON_get_error_code (json);
     148           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     149           0 :     orh->cb (orh->cb_cls,
     150             :              &hr,
     151             :              NULL,
     152             :              NULL);
     153           0 :     break;
     154           0 :   default:
     155           0 :     GNUNET_break_op (0); /* unexpected status code */
     156           0 :     TALER_MERCHANT_parse_error_details_ (json,
     157             :                                          response_code,
     158             :                                          &hr);
     159           0 :     orh->cb (orh->cb_cls,
     160             :              &hr,
     161             :              NULL,
     162             :              NULL);
     163           0 :     break;
     164             :   }
     165           0 :   TALER_MERCHANT_post_order_refund_cancel (orh);
     166           0 : }
     167             : 
     168             : 
     169             : void
     170           0 : TALER_MERCHANT_post_order_refund_cancel (
     171             :   struct TALER_MERCHANT_OrderRefundHandle *orh)
     172             : {
     173           0 :   if (NULL != orh->job)
     174             :   {
     175           0 :     GNUNET_CURL_job_cancel (orh->job);
     176           0 :     orh->job = NULL;
     177             :   }
     178           0 :   TALER_curl_easy_post_finished (&orh->post_ctx);
     179           0 :   GNUNET_free (orh->url);
     180           0 :   GNUNET_free (orh);
     181           0 : }
     182             : 
     183             : 
     184             : struct TALER_MERCHANT_OrderRefundHandle *
     185           0 : TALER_MERCHANT_post_order_refund (struct GNUNET_CURL_Context *ctx,
     186             :                                   const char *backend_url,
     187             :                                   const char *order_id,
     188             :                                   const struct TALER_Amount *refund,
     189             :                                   const char *reason,
     190             :                                   TALER_MERCHANT_RefundCallback cb,
     191             :                                   void *cb_cls)
     192             : {
     193             :   struct TALER_MERCHANT_OrderRefundHandle *orh;
     194             :   json_t *req;
     195             :   CURL *eh;
     196             : 
     197           0 :   orh = GNUNET_new (struct TALER_MERCHANT_OrderRefundHandle);
     198           0 :   orh->ctx = ctx;
     199           0 :   orh->cb = cb;
     200           0 :   orh->cb_cls = cb_cls;
     201             :   {
     202             :     char *path;
     203             : 
     204           0 :     GNUNET_asprintf (&path,
     205             :                      "private/orders/%s/refund",
     206             :                      order_id);
     207           0 :     orh->url = TALER_url_join (backend_url,
     208             :                                path,
     209             :                                NULL);
     210           0 :     GNUNET_free (path);
     211             :   }
     212           0 :   if (NULL == orh->url)
     213             :   {
     214           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     215             :                 "Could not construct request URL.\n");
     216           0 :     GNUNET_free (orh);
     217           0 :     return NULL;
     218             :   }
     219           0 :   req = GNUNET_JSON_PACK (
     220             :     TALER_JSON_pack_amount ("refund",
     221             :                             refund),
     222             :     GNUNET_JSON_pack_string ("reason",
     223             :                              reason));
     224           0 :   GNUNET_assert (NULL != req);
     225           0 :   eh = TALER_MERCHANT_curl_easy_get_ (orh->url);
     226           0 :   if (GNUNET_OK !=
     227           0 :       TALER_curl_easy_post (&orh->post_ctx,
     228             :                             eh,
     229             :                             req))
     230             :   {
     231           0 :     GNUNET_break (0);
     232           0 :     curl_easy_cleanup (eh);
     233           0 :     json_decref (req);
     234           0 :     GNUNET_free (orh->url);
     235           0 :     GNUNET_free (orh);
     236           0 :     return NULL;
     237             :   }
     238           0 :   json_decref (req);
     239           0 :   orh->job = GNUNET_CURL_job_add2 (ctx,
     240             :                                    eh,
     241           0 :                                    orh->post_ctx.headers,
     242             :                                    &handle_refund_finished,
     243             :                                    orh);
     244           0 :   if (NULL == orh->job)
     245             :   {
     246           0 :     GNUNET_free (orh->url);
     247           0 :     GNUNET_free (orh);
     248           0 :     return NULL;
     249             :   }
     250           0 :   return orh;
     251             : }
     252             : 
     253             : 
     254             : /* end of merchant_api_post_order_refund.c */

Generated by: LCOV version 1.14