merchant

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

testing_api_cmd_refund_external.c (5517B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 3, or
      8   (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file src/testing/testing_api_cmd_refund_external.c
     21  * @brief command to test POST /private/orders/$ORDER_ID/refund-external
     22  * @author Bohdan Potuzhnyi
     23  * @author Volodymyr Potuzhnyi
     24  */
     25 #include "platform.h"
     26 struct RefundExternalState;
     27 #define TALER_MERCHANT_POST_PRIVATE_ORDERS_REFUND_EXTERNAL_RESULT_CLOSURE \
     28   struct RefundExternalState
     29 #include <taler/taler_exchange_service.h>
     30 #include <taler/taler_testing_lib.h>
     31 #include "taler/taler_merchant_service.h"
     32 #include "taler/taler_merchant_testing_lib.h"
     33 #include <taler/merchant/post-private-orders-ORDER_ID-refund-external.h>
     34 
     35 
     36 /**
     37  * State of a "POST /private/orders/$ORDER_ID/refund-external" CMD.
     38  */
     39 struct RefundExternalState
     40 {
     41 
     42   /**
     43    * Handle for a "refund external" request.
     44    */
     45   struct TALER_MERCHANT_PostPrivateOrdersRefundExternalHandle *reh;
     46 
     47   /**
     48    * The interpreter state.
     49    */
     50   struct TALER_TESTING_Interpreter *is;
     51 
     52   /**
     53    * Base URL of the merchant serving the request.
     54    */
     55   const char *merchant_url;
     56 
     57   /**
     58    * ID of the order to record the refund for.
     59    */
     60   const char *order_id;
     61 
     62   /**
     63    * External refund entry to record.
     64    */
     65   json_t *body;
     66 
     67   /**
     68    * Identifier of the recorded refund, NULL if the request did not
     69    * (yet) succeed.
     70    */
     71   char *refund_id;
     72 
     73   /**
     74    * Expected HTTP response code.
     75    */
     76   unsigned int http_status;
     77 
     78 };
     79 
     80 
     81 /**
     82  * Callback for a POST /private/orders/$ID/refund-external operation.
     83  *
     84  * @param res closure for this function
     85  * @param rer response being processed
     86  */
     87 static void
     88 refund_external_cb (
     89   struct RefundExternalState *res,
     90   const struct TALER_MERCHANT_PostPrivateOrdersRefundExternalResponse *rer)
     91 {
     92   res->reh = NULL;
     93   if (res->http_status != rer->hr.http_status)
     94   {
     95     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     96                 "Unexpected response code %u (%d) to command %s\n",
     97                 rer->hr.http_status,
     98                 (int) rer->hr.ec,
     99                 TALER_TESTING_interpreter_get_current_label (res->is));
    100     TALER_TESTING_interpreter_fail (res->is);
    101     return;
    102   }
    103   switch (rer->hr.http_status)
    104   {
    105   case MHD_HTTP_OK:
    106     res->refund_id = GNUNET_strdup (rer->details.ok.refund_id);
    107     break;
    108   case MHD_HTTP_BAD_REQUEST:
    109     break;
    110   case MHD_HTTP_UNAUTHORIZED:
    111     break;
    112   case MHD_HTTP_NOT_FOUND:
    113     break;
    114   case MHD_HTTP_CONFLICT:
    115     break;
    116   default:
    117     GNUNET_break (0);
    118     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    119                 "Unhandled HTTP status %u for refund external.\n",
    120                 rer->hr.http_status);
    121   }
    122   TALER_TESTING_interpreter_next (res->is);
    123 }
    124 
    125 
    126 /**
    127  * Run the "refund external" CMD.
    128  *
    129  * @param cls closure.
    130  * @param cmd command being run now.
    131  * @param is interpreter state.
    132  */
    133 static void
    134 refund_external_run (void *cls,
    135                      const struct TALER_TESTING_Command *cmd,
    136                      struct TALER_TESTING_Interpreter *is)
    137 {
    138   struct RefundExternalState *res = cls;
    139 
    140   res->is = is;
    141   res->reh = TALER_MERCHANT_post_private_orders_refund_external_create (
    142     TALER_TESTING_interpreter_get_context (is),
    143     res->merchant_url,
    144     res->order_id,
    145     res->body);
    146   {
    147     enum TALER_ErrorCode ec;
    148 
    149     ec = TALER_MERCHANT_post_private_orders_refund_external_start (
    150       res->reh,
    151       &refund_external_cb,
    152       res);
    153     GNUNET_assert (TALER_EC_NONE == ec);
    154   }
    155 }
    156 
    157 
    158 /**
    159  * Free the state of a "refund external" CMD, and possibly
    160  * cancel a pending operation thereof.
    161  *
    162  * @param cls closure.
    163  * @param cmd command being run.
    164  */
    165 static void
    166 refund_external_cleanup (void *cls,
    167                          const struct TALER_TESTING_Command *cmd)
    168 {
    169   struct RefundExternalState *res = cls;
    170 
    171   if (NULL != res->reh)
    172   {
    173     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    174                 "POST /private/orders/$ORDER_ID/refund-external operation did not complete\n");
    175     TALER_MERCHANT_post_private_orders_refund_external_cancel (res->reh);
    176   }
    177   json_decref (res->body);
    178   GNUNET_free (res->refund_id);
    179   GNUNET_free (res);
    180 }
    181 
    182 
    183 struct TALER_TESTING_Command
    184 TALER_TESTING_cmd_merchant_refund_external (const char *label,
    185                                             const char *merchant_url,
    186                                             const char *order_id,
    187                                             const char *body,
    188                                             unsigned int http_status)
    189 {
    190   struct RefundExternalState *res;
    191 
    192   res = GNUNET_new (struct RefundExternalState);
    193   res->merchant_url = merchant_url;
    194   res->order_id = order_id;
    195   res->body = json_loads (body,
    196                           0,
    197                           NULL);
    198   GNUNET_assert (NULL != res->body);
    199   res->http_status = http_status;
    200   {
    201     struct TALER_TESTING_Command cmd = {
    202       .cls = res,
    203       .label = label,
    204       .run = &refund_external_run,
    205       .cleanup = &refund_external_cleanup
    206     };
    207 
    208     return cmd;
    209   }
    210 }