merchant

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

testing_api_cmd_post_orders_paid.c (6925B)


      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  * @file src/testing/testing_api_cmd_post_orders_paid.c
     21  * @brief command to test POST /orders/$ID/paid.
     22  * @author Jonathan Buchanan
     23  */
     24 #include "platform.h"
     25 struct PostOrdersPaidState;
     26 #define TALER_MERCHANT_POST_ORDERS_PAID_RESULT_CLOSURE struct PostOrdersPaidState
     27 #include <taler/taler_exchange_service.h>
     28 #include <taler/taler_testing_lib.h>
     29 #include "taler/taler_merchant_service.h"
     30 #include "taler/taler_merchant_testing_lib.h"
     31 #include <taler/merchant/post-orders-ORDER_ID-paid.h>
     32 
     33 
     34 /**
     35  * State of a "POST /orders/$ID/paid" CMD.
     36  */
     37 struct PostOrdersPaidState
     38 {
     39 
     40   /**
     41    * Handle for a "POST /paid" request.
     42    */
     43   struct TALER_MERCHANT_PostOrdersPaidHandle *oph;
     44 
     45   /**
     46    * The interpreter state.
     47    */
     48   struct TALER_TESTING_Interpreter *is;
     49 
     50   /**
     51    * Base URL of the merchant serving the request.
     52    */
     53   const char *merchant_url;
     54 
     55   /**
     56    * Reference to the "pay" command to verify.
     57    */
     58   const char *pay_reference;
     59 
     60   /**
     61    * The session to use for the request.
     62    */
     63   const char *session_id;
     64 
     65   /**
     66    * Expected HTTP response code.
     67    */
     68   unsigned int http_status;
     69 
     70 };
     71 
     72 
     73 /**
     74  * Response from the merchant after POST /paid.
     75  *
     76  * @param cls pointer to `struct PostOrdersPaidState`.
     77  * @param opr the response.
     78  */
     79 static void
     80 paid_cb (struct PostOrdersPaidState *ops,
     81          const struct TALER_MERCHANT_PostOrdersPaidResponse *opr)
     82 {
     83 
     84   ops->oph = NULL;
     85   if (ops->http_status != opr->hr.http_status)
     86   {
     87     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     88                 "Unexpected response code %u (%d) to command %s\n",
     89                 opr->hr.http_status,
     90                 (int) opr->hr.ec,
     91                 TALER_TESTING_interpreter_get_current_label (ops->is));
     92     TALER_TESTING_FAIL (ops->is);
     93   }
     94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     95               "Successful order-paid (HTTP status: %u)\n",
     96               ops->http_status);
     97   TALER_TESTING_interpreter_next (ops->is);
     98 }
     99 
    100 
    101 /**
    102  * Run a "paid" CMD.
    103  *
    104  * @param cls closure
    105  * @param cmd command being run.
    106  * @param is interpreter state
    107  */
    108 static void
    109 paid_run (void *cls,
    110           const struct TALER_TESTING_Command *cmd,
    111           struct TALER_TESTING_Interpreter *is)
    112 {
    113   struct PostOrdersPaidState *ops = cls;
    114   const struct TALER_TESTING_Command *pay_cmd;
    115   const char *proposal_reference;
    116   const struct TALER_TESTING_Command *proposal_cmd;
    117   const char *order_id;
    118   const struct TALER_PrivateContractHashP *h_contract_terms;
    119   const struct TALER_MerchantSignatureP *merchant_sig;
    120 
    121   ops->is = is;
    122   pay_cmd = TALER_TESTING_interpreter_lookup_command (is,
    123                                                       ops->pay_reference);
    124   if (NULL == pay_cmd)
    125     TALER_TESTING_FAIL (is);
    126   if (GNUNET_OK !=
    127       TALER_TESTING_get_trait_merchant_sig (pay_cmd,
    128                                             &merchant_sig))
    129     TALER_TESTING_FAIL (is);
    130   if (GNUNET_OK !=
    131       TALER_TESTING_get_trait_proposal_reference (pay_cmd,
    132                                                   &proposal_reference))
    133     TALER_TESTING_FAIL (is);
    134   proposal_cmd = TALER_TESTING_interpreter_lookup_command (is,
    135                                                            proposal_reference);
    136 
    137   if (NULL == proposal_cmd)
    138     TALER_TESTING_FAIL (is);
    139 
    140   {
    141     const json_t *contract_terms;
    142     const char *error_name;
    143     unsigned int error_line;
    144 
    145     if (GNUNET_OK !=
    146         TALER_TESTING_get_trait_contract_terms (proposal_cmd,
    147                                                 &contract_terms))
    148       TALER_TESTING_FAIL (is);
    149     {
    150       /* Get information that needs to be put verbatim in the
    151        * deposit permission */
    152       struct GNUNET_JSON_Specification spec[] = {
    153         GNUNET_JSON_spec_string ("order_id",
    154                                  &order_id),
    155         GNUNET_JSON_spec_end ()
    156       };
    157 
    158       if (GNUNET_OK !=
    159           GNUNET_JSON_parse (contract_terms,
    160                              spec,
    161                              &error_name,
    162                              &error_line))
    163       {
    164         char *js;
    165 
    166         js = json_dumps (contract_terms,
    167                          JSON_INDENT (1));
    168         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    169                     "Parser failed on %s:%u for input `%s'\n",
    170                     error_name,
    171                     error_line,
    172                     js);
    173         free (js);
    174         TALER_TESTING_FAIL (is);
    175       }
    176     }
    177   }
    178 
    179   if (GNUNET_OK !=
    180       TALER_TESTING_get_trait_h_contract_terms (proposal_cmd,
    181                                                 &h_contract_terms))
    182     TALER_TESTING_FAIL (is);
    183 
    184   ops->oph = TALER_MERCHANT_post_orders_paid_create (
    185     TALER_TESTING_interpreter_get_context (is),
    186     ops->merchant_url,
    187     order_id,
    188     ops->session_id,
    189     h_contract_terms,
    190     merchant_sig);
    191   {
    192     enum TALER_ErrorCode ec;
    193 
    194     ec = TALER_MERCHANT_post_orders_paid_start (
    195       ops->oph,
    196       &paid_cb,
    197       ops);
    198     GNUNET_assert (TALER_EC_NONE == ec);
    199   }
    200 }
    201 
    202 
    203 /**
    204  * Free a "paid" CMD, and cancel it if need be.
    205  *
    206  * @param cls closure.
    207  * @param cmd command currently being freed.
    208  */
    209 static void
    210 paid_cleanup (void *cls,
    211               const struct TALER_TESTING_Command *cmd)
    212 {
    213   struct PostOrdersPaidState *ops = cls;
    214 
    215   if (NULL != ops->oph)
    216   {
    217     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    218                 "Command `%s' did not complete.\n",
    219                 TALER_TESTING_interpreter_get_current_label (
    220                   ops->is));
    221     TALER_MERCHANT_post_orders_paid_cancel (ops->oph);
    222   }
    223   GNUNET_free (ops);
    224 }
    225 
    226 
    227 struct TALER_TESTING_Command
    228 TALER_TESTING_cmd_merchant_post_orders_paid (const char *label,
    229                                              const char *merchant_url,
    230                                              const char *pay_reference,
    231                                              const char *session_id,
    232                                              unsigned int http_status)
    233 {
    234   struct PostOrdersPaidState *ops;
    235 
    236   ops = GNUNET_new (struct PostOrdersPaidState);
    237   ops->http_status = http_status;
    238   ops->pay_reference = pay_reference;
    239   ops->merchant_url = merchant_url;
    240   ops->session_id = session_id;
    241   {
    242     struct TALER_TESTING_Command cmd = {
    243       .cls = ops,
    244       .label = label,
    245       .run = &paid_run,
    246       .cleanup = &paid_cleanup
    247     };
    248 
    249     return cmd;
    250   }
    251 }