LCOV - code coverage report
Current view: top level - lib - merchant_api_wallet_post_order_refund.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 141 0.0 %
Date: 2021-04-12 06:04:58 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          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_wallet_post_order_refund.c
      19             :  * @brief Implementation of the (public) POST /orders/ID/refund request
      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             : #include <taler/taler_curl_lib.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a (public) POST /orders/ID/refund operation.
      36             :  */
      37             : struct TALER_MERCHANT_WalletOrderRefundHandle
      38             : {
      39             :   /**
      40             :    * Complete URL where the backend offers /refund
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Minor context that holds body and headers.
      46             :    */
      47             :   struct TALER_CURL_PostContext post_ctx;
      48             : 
      49             :   /**
      50             :    * The CURL context to connect to the backend
      51             :    */
      52             :   struct GNUNET_CURL_Context *ctx;
      53             : 
      54             :   /**
      55             :    * The callback to pass the backend response to
      56             :    */
      57             :   TALER_MERCHANT_WalletRefundCallback cb;
      58             : 
      59             :   /**
      60             :    * Clasure to pass to the callback
      61             :    */
      62             :   void *cb_cls;
      63             : 
      64             :   /**
      65             :    * Handle for the request
      66             :    */
      67             :   struct GNUNET_CURL_Job *job;
      68             : };
      69             : 
      70             : 
      71             : /**
      72             :  * Convenience function to call the callback in @a owgh with an error code of
      73             :  * @a ec and the exchange body being set to @a reply.
      74             :  *
      75             :  * @param orh handle providing callback
      76             :  * @param ec error code to return to application
      77             :  * @param reply JSON reply we got from the exchange, can be NULL
      78             :  */
      79             : static void
      80           0 : cb_failure (struct TALER_MERCHANT_WalletOrderRefundHandle *orh,
      81             :             enum TALER_ErrorCode ec,
      82             :             const json_t *reply)
      83             : {
      84           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      85             :     .ec = ec,
      86             :     .reply = reply
      87             :   };
      88             : 
      89           0 :   orh->cb (orh->cb_cls,
      90             :            &hr,
      91             :            NULL,
      92             :            NULL,
      93             :            NULL,
      94             :            0);
      95           0 : }
      96             : 
      97             : 
      98             : /**
      99             :  * Callback to process (public) POST /orders/ID/refund response
     100             :  *
     101             :  * @param cls the `struct TALER_MERCHANT_OrderRefundHandle`
     102             :  * @param response_code HTTP response code, 0 on error
     103             :  * @param response response body, NULL if not JSON
     104             :  */
     105             : static void
     106           0 : handle_refund_finished (void *cls,
     107             :                         long response_code,
     108             :                         const void *response)
     109             : {
     110           0 :   struct TALER_MERCHANT_WalletOrderRefundHandle *orh = cls;
     111           0 :   const json_t *json = response;
     112           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     113           0 :     .http_status = (unsigned int) response_code,
     114             :     .reply = json
     115             :   };
     116             : 
     117           0 :   orh->job = NULL;
     118             : 
     119           0 :   switch (response_code)
     120             :   {
     121           0 :   case 0:
     122           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     123           0 :     orh->cb (orh->cb_cls,
     124             :              &hr,
     125             :              NULL,
     126             :              NULL,
     127             :              NULL,
     128             :              0);
     129           0 :     break;
     130           0 :   case MHD_HTTP_OK:
     131             :     {
     132             :       struct TALER_Amount refund_amount;
     133             :       json_t *refunds;
     134             :       struct TALER_MerchantPublicKeyP merchant_pub;
     135             :       unsigned int refund_len;
     136             :       struct GNUNET_JSON_Specification spec[] = {
     137           0 :         TALER_JSON_spec_amount ("refund_amount",
     138             :                                 &refund_amount),
     139           0 :         GNUNET_JSON_spec_json ("refunds",
     140             :                                &refunds),
     141           0 :         GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     142             :                                      &merchant_pub),
     143           0 :         GNUNET_JSON_spec_end ()
     144             :       };
     145             : 
     146           0 :       if (GNUNET_OK !=
     147           0 :           GNUNET_JSON_parse (json,
     148             :                              spec,
     149             :                              NULL, NULL))
     150             :       {
     151           0 :         GNUNET_break_op (0);
     152           0 :         cb_failure (orh,
     153             :                     TALER_EC_GENERIC_REPLY_MALFORMED,
     154             :                     json);
     155           0 :         TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
     156           0 :         return;
     157             :       }
     158             : 
     159           0 :       if (! json_is_array (refunds))
     160             :       {
     161           0 :         GNUNET_break_op (0);
     162           0 :         cb_failure (orh,
     163             :                     TALER_EC_GENERIC_REPLY_MALFORMED,
     164             :                     json);
     165           0 :         GNUNET_JSON_parse_free (spec);
     166           0 :         TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
     167           0 :         return;
     168             :       }
     169             : 
     170           0 :       refund_len = json_array_size (refunds);
     171           0 :       {
     172           0 :         struct TALER_MERCHANT_RefundDetail rds[refund_len];
     173             : 
     174           0 :         memset (rds,
     175             :                 0,
     176             :                 sizeof (rds));
     177           0 :         for (unsigned int i = 0; i<refund_len; i++)
     178             :         {
     179           0 :           struct TALER_MERCHANT_RefundDetail *rd = &rds[i];
     180           0 :           const json_t *jrefund = json_array_get (refunds,
     181             :                                                   i);
     182             :           const char *refund_status_type;
     183             :           uint32_t exchange_status;
     184             :           int ret;
     185             :           struct GNUNET_JSON_Specification espec[] = {
     186           0 :             GNUNET_JSON_spec_uint32 ("exchange_status",
     187             :                                      &exchange_status),
     188           0 :             GNUNET_JSON_spec_end ()
     189             :           };
     190             : 
     191           0 :           if (GNUNET_OK !=
     192           0 :               GNUNET_JSON_parse (jrefund,
     193             :                                  espec,
     194             :                                  NULL, NULL))
     195             :           {
     196           0 :             GNUNET_break_op (0);
     197           0 :             cb_failure (orh,
     198             :                         TALER_EC_GENERIC_REPLY_MALFORMED,
     199             :                         json);
     200           0 :             TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
     201           0 :             return;
     202             :           }
     203             : 
     204           0 :           if (MHD_HTTP_OK == exchange_status)
     205             :           {
     206             :             struct GNUNET_JSON_Specification rspec[] = {
     207           0 :               GNUNET_JSON_spec_string ("type",
     208             :                                        &refund_status_type),
     209           0 :               GNUNET_JSON_spec_fixed_auto ("exchange_sig",
     210             :                                            &rd->exchange_sig),
     211           0 :               GNUNET_JSON_spec_fixed_auto ("exchange_pub",
     212             :                                            &rd->exchange_pub),
     213           0 :               GNUNET_JSON_spec_uint64 ("rtransaction_id",
     214             :                                        &rd->rtransaction_id),
     215           0 :               GNUNET_JSON_spec_fixed_auto ("coin_pub",
     216             :                                            &rd->coin_pub),
     217           0 :               TALER_JSON_spec_amount ("refund_amount",
     218             :                                       &rd->refund_amount),
     219           0 :               GNUNET_JSON_spec_end ()
     220             :             };
     221             : 
     222           0 :             ret = GNUNET_JSON_parse (jrefund,
     223             :                                      rspec,
     224             :                                      NULL, NULL);
     225           0 :             if (GNUNET_OK == ret)
     226             :             {
     227             :               /* check that type field is correct */
     228           0 :               if (0 != strcmp ("success", refund_status_type))
     229             :               {
     230           0 :                 GNUNET_break_op (0);
     231           0 :                 ret = GNUNET_SYSERR;
     232             :               }
     233             :             }
     234             :           }
     235             :           else
     236             :           {
     237             :             struct GNUNET_JSON_Specification rspec[] = {
     238           0 :               GNUNET_JSON_spec_string ("type",
     239             :                                        &refund_status_type),
     240           0 :               GNUNET_JSON_spec_fixed_auto ("coin_pub",
     241             :                                            &rd->coin_pub),
     242           0 :               GNUNET_JSON_spec_uint64 ("rtransaction_id",
     243             :                                        &rd->rtransaction_id),
     244           0 :               TALER_JSON_spec_amount ("refund_amount",
     245             :                                       &rd->refund_amount),
     246           0 :               GNUNET_JSON_spec_end ()
     247             :             };
     248             : 
     249           0 :             ret = GNUNET_JSON_parse (jrefund,
     250             :                                      rspec,
     251             :                                      NULL, NULL);
     252           0 :             if (GNUNET_OK == ret)
     253             :             {
     254             :               /* parse optional arguments */
     255             :               json_t *jec;
     256             : 
     257           0 :               jec = json_object_get (jrefund,
     258             :                                      "exchange_code");
     259           0 :               if (NULL != jec)
     260             :               {
     261           0 :                 if (! json_is_integer (jec))
     262             :                 {
     263           0 :                   GNUNET_break_op (0);
     264           0 :                   ret = GNUNET_SYSERR;
     265             :                 }
     266             :                 else
     267             :                 {
     268           0 :                   rd->hr.ec = (enum TALER_ErrorCode) json_integer_value (jec);
     269             :                 }
     270             :               }
     271           0 :               rd->hr.reply = json_object_get (jrefund,
     272             :                                               "exchange_reply");
     273             :               /* check that type field is correct */
     274           0 :               if (0 != strcmp ("failure", refund_status_type))
     275             :               {
     276           0 :                 GNUNET_break_op (0);
     277           0 :                 ret = GNUNET_SYSERR;
     278             :               }
     279             :             }
     280             :           }
     281           0 :           if (GNUNET_OK != ret)
     282             :           {
     283           0 :             GNUNET_break_op (0);
     284           0 :             cb_failure (orh,
     285             :                         TALER_EC_GENERIC_REPLY_MALFORMED,
     286             :                         json);
     287           0 :             TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
     288           0 :             return;
     289             :           }
     290           0 :           rd->hr.http_status = exchange_status;
     291             :         }
     292             : 
     293             :         {
     294           0 :           struct TALER_MERCHANT_HttpResponse hr = {
     295             :             .reply = json,
     296             :             .http_status = MHD_HTTP_OK
     297             :           };
     298             : 
     299           0 :           orh->cb (orh->cb_cls,
     300             :                    &hr,
     301             :                    &refund_amount,
     302             :                    &merchant_pub,
     303             :                    rds,
     304             :                    refund_len);
     305             :         }
     306             :       }
     307           0 :       GNUNET_JSON_parse_free (spec);
     308             :     }
     309           0 :     break;
     310           0 :   case MHD_HTTP_NO_CONTENT:
     311           0 :     orh->cb (orh->cb_cls,
     312             :              &hr,
     313             :              NULL,
     314             :              NULL,
     315             :              NULL,
     316             :              0);
     317           0 :     break;
     318           0 :   case MHD_HTTP_CONFLICT:
     319             :   case MHD_HTTP_NOT_FOUND:
     320           0 :     hr.ec = TALER_JSON_get_error_code (json);
     321           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     322           0 :     orh->cb (orh->cb_cls,
     323             :              &hr,
     324             :              NULL,
     325             :              NULL,
     326             :              NULL,
     327             :              0);
     328           0 :     break;
     329           0 :   default:
     330           0 :     GNUNET_break_op (0); /* unexpected status code */
     331           0 :     TALER_MERCHANT_parse_error_details_ (json,
     332             :                                          response_code,
     333             :                                          &hr);
     334           0 :     orh->cb (orh->cb_cls,
     335             :              &hr,
     336             :              NULL,
     337             :              NULL,
     338             :              NULL,
     339             :              0);
     340           0 :     break;
     341             :   }
     342           0 :   TALER_MERCHANT_wallet_post_order_refund_cancel (orh);
     343             : }
     344             : 
     345             : 
     346             : struct TALER_MERCHANT_WalletOrderRefundHandle *
     347           0 : TALER_MERCHANT_wallet_post_order_refund (
     348             :   struct GNUNET_CURL_Context *ctx,
     349             :   const char *backend_url,
     350             :   const char *order_id,
     351             :   const struct GNUNET_HashCode *h_contract_terms,
     352             :   TALER_MERCHANT_WalletRefundCallback cb,
     353             :   void *cb_cls)
     354             : {
     355             :   struct TALER_MERCHANT_WalletOrderRefundHandle *orh;
     356             :   json_t *req;
     357             :   CURL *eh;
     358             : 
     359           0 :   orh = GNUNET_new (struct TALER_MERCHANT_WalletOrderRefundHandle);
     360           0 :   orh->ctx = ctx;
     361           0 :   orh->cb = cb;
     362           0 :   orh->cb_cls = cb_cls;
     363             :   {
     364             :     char *path;
     365             : 
     366           0 :     GNUNET_asprintf (&path,
     367             :                      "orders/%s/refund",
     368             :                      order_id);
     369           0 :     orh->url = TALER_url_join (backend_url,
     370             :                                path,
     371             :                                NULL);
     372           0 :     GNUNET_free (path);
     373             :   }
     374           0 :   if (NULL == orh->url)
     375             :   {
     376           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     377             :                 "Could not construct request URL.\n");
     378           0 :     GNUNET_free (orh);
     379           0 :     return NULL;
     380             :   }
     381           0 :   req = json_pack ("{s:o}",
     382             :                    "h_contract",
     383             :                    GNUNET_JSON_from_data_auto (h_contract_terms));
     384           0 :   GNUNET_assert (NULL != req);
     385           0 :   eh = curl_easy_init ();
     386           0 :   GNUNET_assert (NULL != eh);
     387           0 :   if (GNUNET_OK !=
     388           0 :       TALER_curl_easy_post (&orh->post_ctx,
     389             :                             eh,
     390             :                             req))
     391             :   {
     392           0 :     GNUNET_break (0);
     393           0 :     json_decref (req);
     394           0 :     GNUNET_free (orh->url);
     395           0 :     GNUNET_free (orh);
     396           0 :     return NULL;
     397             :   }
     398           0 :   json_decref (req);
     399           0 :   GNUNET_assert (CURLE_OK ==
     400             :                  curl_easy_setopt (eh,
     401             :                                    CURLOPT_URL,
     402             :                                    orh->url));
     403           0 :   orh->job = GNUNET_CURL_job_add2 (ctx,
     404             :                                    eh,
     405           0 :                                    orh->post_ctx.headers,
     406             :                                    &handle_refund_finished,
     407             :                                    orh);
     408           0 :   if (NULL == orh->job)
     409             :   {
     410           0 :     GNUNET_free (orh->url);
     411           0 :     GNUNET_free (orh);
     412           0 :     return NULL;
     413             :   }
     414           0 :   return orh;
     415             : }
     416             : 
     417             : 
     418             : void
     419           0 : TALER_MERCHANT_wallet_post_order_refund_cancel (
     420             :   struct TALER_MERCHANT_WalletOrderRefundHandle *orh)
     421             : {
     422           0 :   if (NULL != orh->job)
     423             :   {
     424           0 :     GNUNET_CURL_job_cancel (orh->job);
     425           0 :     orh->job = NULL;
     426             :   }
     427           0 :   TALER_curl_easy_post_finished (&orh->post_ctx);
     428           0 :   GNUNET_free (orh->url);
     429           0 :   GNUNET_free (orh);
     430           0 : }
     431             : 
     432             : 
     433             : /* end of merchant_api_wallet_post_order_refund.c */

Generated by: LCOV version 1.14