LCOV - code coverage report
Current view: top level - lib - merchant_api_post_order_refund.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 66 0.0 %
Date: 2020-08-15 06:12:35 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, 2015, 2016, 2017, 2019, 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 lib/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 <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a POST /orders/ID/refund operation.
      37             :  */
      38             : struct TALER_MERCHANT_OrderRefundHandle
      39             : {
      40             :   /**
      41             :    * Complete URL where the backend offers /refund
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * Minor context that holds body and headers.
      47             :    */
      48             :   struct TALER_CURL_PostContext post_ctx;
      49             : 
      50             :   /**
      51             :    * The CURL context to connect to the backend
      52             :    */
      53             :   struct GNUNET_CURL_Context *ctx;
      54             : 
      55             :   /**
      56             :    * The callback to pass the backend response to
      57             :    */
      58             :   TALER_MERCHANT_RefundCallback cb;
      59             : 
      60             :   /**
      61             :    * Clasure to pass to the callback
      62             :    */
      63             :   void *cb_cls;
      64             : 
      65             :   /**
      66             :    * Handle for the request
      67             :    */
      68             :   struct GNUNET_CURL_Job *job;
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Callback to process POST /orders/ID/refund response
      74             :  *
      75             :  * @param cls the `struct TALER_MERCHANT_OrderRefundHandle`
      76             :  * @param response_code HTTP response code, 0 on error
      77             :  * @param json response body, NULL if not JSON
      78             :  */
      79             : static void
      80           0 : handle_refund_finished (void *cls,
      81             :                         long response_code,
      82             :                         const void *response)
      83             : {
      84           0 :   struct TALER_MERCHANT_OrderRefundHandle *orh = cls;
      85           0 :   const json_t *json = response;
      86           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      87           0 :     .http_status = (unsigned int) response_code,
      88             :     .reply = json
      89             :   };
      90             : 
      91           0 :   orh->job = NULL;
      92           0 :   switch (response_code)
      93             :   {
      94           0 :   case 0:
      95           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
      96           0 :     orh->cb (orh->cb_cls,
      97             :              &hr);
      98           0 :     break;
      99           0 :   case MHD_HTTP_OK:
     100           0 :     orh->cb (orh->cb_cls,
     101             :              &hr);
     102           0 :     break;
     103           0 :   case MHD_HTTP_CONFLICT:
     104             :   case MHD_HTTP_NOT_FOUND:
     105           0 :     hr.ec = TALER_JSON_get_error_code (json);
     106           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     107           0 :     orh->cb (orh->cb_cls,
     108             :              &hr);
     109           0 :     break;
     110           0 :   default:
     111           0 :     GNUNET_break_op (0); /* unexpected status code */
     112           0 :     TALER_MERCHANT_parse_error_details_ (json,
     113             :                                          response_code,
     114             :                                          &hr);
     115           0 :     orh->cb (orh->cb_cls,
     116             :              &hr);
     117           0 :     break;
     118             :   }
     119           0 :   TALER_MERCHANT_post_order_refund_cancel (orh);
     120           0 : }
     121             : 
     122             : 
     123             : /**
     124             :  * Cancel a POST /orders/ID/refund request.
     125             :  *
     126             :  * @param orh the refund increasing operation to cancel
     127             :  */
     128             : void
     129           0 : TALER_MERCHANT_post_order_refund_cancel (
     130             :   struct TALER_MERCHANT_OrderRefundHandle *orh)
     131             : {
     132           0 :   if (NULL != orh->job)
     133             :   {
     134           0 :     GNUNET_CURL_job_cancel (orh->job);
     135           0 :     orh->job = NULL;
     136             :   }
     137           0 :   TALER_curl_easy_post_finished (&orh->post_ctx);
     138           0 :   GNUNET_free (orh->url);
     139           0 :   GNUNET_free (orh);
     140           0 : }
     141             : 
     142             : 
     143             : /**
     144             :  * Increase the refund granted for an order
     145             :  *
     146             :  * @param ctx the CURL context used to connect to the backend
     147             :  * @param backend_url backend's base URL, including final "/"
     148             :  * @param order_id id of the order whose refund is to be increased
     149             :  * @param refund amount to which increase the refund
     150             :  * @param reason human-readable reason justifying the refund
     151             :  * @param cb callback processing the response from /refund
     152             :  * @param cb_cls closure for cb
     153             :  */
     154             : struct TALER_MERCHANT_OrderRefundHandle *
     155           0 : TALER_MERCHANT_post_order_refund (struct GNUNET_CURL_Context *ctx,
     156             :                                   const char *backend_url,
     157             :                                   const char *order_id,
     158             :                                   const struct TALER_Amount *refund,
     159             :                                   const char *reason,
     160             :                                   TALER_MERCHANT_RefundCallback cb,
     161             :                                   void *cb_cls)
     162             : {
     163             :   struct TALER_MERCHANT_OrderRefundHandle *orh;
     164             :   json_t *req;
     165             :   CURL *eh;
     166             : 
     167           0 :   orh = GNUNET_new (struct TALER_MERCHANT_OrderRefundHandle);
     168           0 :   orh->ctx = ctx;
     169           0 :   orh->cb = cb;
     170           0 :   orh->cb_cls = cb_cls;
     171             :   {
     172             :     char *path;
     173             : 
     174           0 :     GNUNET_asprintf (&path,
     175             :                      "private/orders/%s/refund",
     176             :                      order_id);
     177           0 :     orh->url = TALER_url_join (backend_url,
     178             :                                path,
     179             :                                NULL);
     180           0 :     GNUNET_free (path);
     181             :   }
     182           0 :   if (NULL == orh->url)
     183             :   {
     184           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     185             :                 "Could not construct request URL.\n");
     186           0 :     GNUNET_free (orh);
     187           0 :     return NULL;
     188             :   }
     189           0 :   req = json_pack ("{s:o, s:s}",
     190             :                    "refund", TALER_JSON_from_amount (refund),
     191             :                    "reason", reason);
     192           0 :   GNUNET_assert (NULL != req);
     193           0 :   eh = curl_easy_init ();
     194           0 :   GNUNET_assert (NULL != eh);
     195           0 :   if (GNUNET_OK !=
     196           0 :       TALER_curl_easy_post (&orh->post_ctx,
     197             :                             eh,
     198             :                             req))
     199             :   {
     200           0 :     GNUNET_break (0);
     201           0 :     json_decref (req);
     202           0 :     GNUNET_free (orh->url);
     203           0 :     GNUNET_free (orh);
     204           0 :     return NULL;
     205             :   }
     206           0 :   json_decref (req);
     207           0 :   GNUNET_assert (CURLE_OK ==
     208             :                  curl_easy_setopt (eh,
     209             :                                    CURLOPT_URL,
     210             :                                    orh->url));
     211           0 :   orh->job = GNUNET_CURL_job_add2 (ctx,
     212             :                                    eh,
     213           0 :                                    orh->post_ctx.headers,
     214             :                                    &handle_refund_finished,
     215             :                                    orh);
     216           0 :   if (NULL == orh->job)
     217             :   {
     218           0 :     GNUNET_free (orh->url);
     219           0 :     GNUNET_free (orh);
     220           0 :     return NULL;
     221             :   }
     222           0 :   return orh;
     223             : }
     224             : 
     225             : 
     226             : /* end of merchant_api_post_order_refund.c */

Generated by: LCOV version 1.14