merchant

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

testing_api_cmd_post_orders_paid.c (6967B)


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