merchant

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

testing_api_cmd_forget_order.c (6171B)


      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
      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 /**
     21  * @file testing_api_cmd_forget_order.c
     22  * @brief command to forget fields of an order
     23  * @author Jonathan Buchanan
     24  */
     25 #include "platform.h"
     26 #include <taler/taler_exchange_service.h>
     27 #include <taler/taler_testing_lib.h>
     28 #include "taler_merchant_service.h"
     29 #include "taler_merchant_testing_lib.h"
     30 
     31 
     32 /**
     33  * State for a "order forget" CMD.
     34  */
     35 struct OrderForgetState
     36 {
     37   /**
     38    * The interpreter state.
     39    */
     40   struct TALER_TESTING_Interpreter *is;
     41 
     42   /**
     43    * URL of the merchant backend.
     44    */
     45   const char *merchant_url;
     46 
     47   /**
     48    * Expected status code.
     49    */
     50   unsigned int http_status;
     51 
     52   /**
     53    * PATCH /orders/$ORDER_ID/forget operation handle.
     54    */
     55   struct TALER_MERCHANT_OrderForgetHandle *ofh;
     56 
     57   /**
     58    * Reference to a order operation.
     59    */
     60   const char *order_reference;
     61 
     62   /**
     63    * Order id to forget for. If NULL, the @a order_reference
     64    * will offer this value.
     65    */
     66   const char *order_id;
     67 
     68   /**
     69    * The list of paths to forget in the contract terms.
     70    */
     71   const char **paths;
     72 
     73   /**
     74    * The length of @e paths.
     75    */
     76   unsigned int paths_length;
     77 };
     78 
     79 
     80 /**
     81  * Free the state of a "order forget" CMD, and possibly
     82  * cancel it if it did not complete.
     83  *
     84  * @param cls closure.
     85  * @param cmd command being freed.
     86  */
     87 static void
     88 order_forget_cleanup (void *cls,
     89                       const struct TALER_TESTING_Command *cmd)
     90 {
     91   struct OrderForgetState *ofs = cls;
     92 
     93   if (NULL != ofs->ofh)
     94   {
     95     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     96                 "Command '%s' did not complete\n",
     97                 cmd->label);
     98     TALER_MERCHANT_order_forget_cancel (ofs->ofh);
     99     ofs->ofh = NULL;
    100   }
    101   GNUNET_array_grow (ofs->paths,
    102                      ofs->paths_length,
    103                      0);
    104   GNUNET_free (ofs);
    105 }
    106 
    107 
    108 /**
    109  * Callback for "order forget" operation, to check the
    110  * response code is as expected.
    111  *
    112  * @param cls closure
    113  * @param hr HTTP response we got
    114  */
    115 static void
    116 order_forget_cb (void *cls,
    117                  const struct TALER_MERCHANT_HttpResponse *hr)
    118 {
    119   struct OrderForgetState *ofs = cls;
    120 
    121   ofs->ofh = NULL;
    122   if (ofs->http_status != hr->http_status)
    123   {
    124     TALER_TESTING_unexpected_status_with_body (ofs->is,
    125                                                hr->http_status,
    126                                                ofs->http_status,
    127                                                hr->reply);
    128     return;
    129   }
    130   TALER_TESTING_interpreter_next (ofs->is);
    131 }
    132 
    133 
    134 /**
    135  * Run the "order forget" CMD.
    136  *
    137  * @param cls closure.
    138  * @param cmd command currently being run.
    139  * @param is interpreter state.
    140  */
    141 static void
    142 order_forget_run (void *cls,
    143                   const struct TALER_TESTING_Command *cmd,
    144                   struct TALER_TESTING_Interpreter *is)
    145 {
    146   struct OrderForgetState *ofs = cls;
    147   const char *order_id;
    148 
    149   ofs->is = is;
    150   if (NULL != ofs->order_id)
    151   {
    152     order_id = ofs->order_id;
    153   }
    154   else
    155   {
    156     const struct TALER_TESTING_Command *order_cmd;
    157 
    158     order_cmd
    159       = TALER_TESTING_interpreter_lookup_command (is,
    160                                                   ofs->order_reference);
    161     if (NULL == order_cmd)
    162       TALER_TESTING_FAIL (is);
    163     if (GNUNET_OK !=
    164         TALER_TESTING_get_trait_order_id (order_cmd,
    165                                           &order_id))
    166       TALER_TESTING_FAIL (is);
    167   }
    168   ofs->ofh = TALER_MERCHANT_order_forget (
    169     TALER_TESTING_interpreter_get_context (is),
    170     ofs->merchant_url,
    171     order_id,
    172     ofs->paths_length,
    173     ofs->paths,
    174     &order_forget_cb,
    175     ofs);
    176   GNUNET_assert (NULL != ofs->ofh);
    177 }
    178 
    179 
    180 /**
    181  * Offer internal data to other commands.
    182  *
    183  * @param cls closure
    184  * @param[out] ret result (could be anything)
    185  * @param trait name of the trait
    186  * @param index index number of the object to extract.
    187  * @return #GNUNET_OK on success
    188  */
    189 static enum GNUNET_GenericReturnValue
    190 order_forget_traits (void *cls,
    191                      const void **ret,
    192                      const char *trait,
    193                      unsigned int index)
    194 {
    195   struct OrderForgetState *ofs = cls;
    196   struct TALER_TESTING_Trait traits[ofs->paths_length + 2];
    197 
    198   traits[0] = TALER_TESTING_make_trait_paths_length (&ofs->paths_length);
    199   for (unsigned int i = 0; i < ofs->paths_length; ++i)
    200     traits[i + 1] = TALER_TESTING_make_trait_paths (i,
    201                                                     ofs->paths[i]);
    202   traits[ofs->paths_length + 1] = TALER_TESTING_trait_end ();
    203 
    204   return TALER_TESTING_get_trait (traits,
    205                                   ret,
    206                                   trait,
    207                                   index);
    208 }
    209 
    210 
    211 struct TALER_TESTING_Command
    212 TALER_TESTING_cmd_merchant_forget_order (
    213   const char *label,
    214   const char *merchant_url,
    215   unsigned int http_status,
    216   const char *order_reference,
    217   const char *order_id,
    218   ...)
    219 {
    220   struct OrderForgetState *ofs;
    221 
    222   ofs = GNUNET_new (struct OrderForgetState);
    223   ofs->http_status = http_status;
    224   ofs->order_reference = order_reference;
    225   ofs->merchant_url = merchant_url;
    226   ofs->order_id = order_id;
    227   {
    228     const char *path;
    229     va_list ap;
    230 
    231     va_start (ap, order_id);
    232     while (NULL != (path = va_arg (ap, const char *)))
    233     {
    234       GNUNET_array_append (ofs->paths,
    235                            ofs->paths_length,
    236                            path);
    237     }
    238     va_end (ap);
    239   }
    240   {
    241     struct TALER_TESTING_Command cmd = {
    242       .cls = ofs,
    243       .label = label,
    244       .run = &order_forget_run,
    245       .cleanup = &order_forget_cleanup,
    246       .traits = &order_forget_traits
    247     };
    248 
    249     return cmd;
    250   }
    251 }