exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

fakebank_twg_admin_add_incoming.c (5396B)


      1 /*
      2   This file is part of TALER
      3   (C) 2016-2024 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or
      6   modify it under the terms of the GNU General Public License
      7   as published by the Free Software Foundation; either version 3,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful,
     11   but 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,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file bank-lib/fakebank_twg_admin_add_incoming.c
     21  * @brief library that fakes being a Taler bank for testcases
     22  * @author Christian Grothoff <christian@grothoff.org>
     23  */
     24 #include "taler/platform.h"
     25 #include "taler/taler_fakebank_lib.h"
     26 #include "taler/taler_bank_service.h"
     27 #include "taler/taler_mhd_lib.h"
     28 #include <gnunet/gnunet_mhd_compat.h>
     29 #include <gnunet/gnunet_mhd_lib.h>
     30 #include "fakebank.h"
     31 #include "fakebank_common_make_admin_transfer.h"
     32 #include "fakebank_twg_admin_add_incoming.h"
     33 
     34 MHD_RESULT
     35 TALER_FAKEBANK_twg_admin_add_incoming_ (
     36   struct TALER_FAKEBANK_Handle *h,
     37   struct MHD_Connection *connection,
     38   const char *account,
     39   const char *upload_data,
     40   size_t *upload_data_size,
     41   void **con_cls)
     42 {
     43   struct ConnectionContext *cc = *con_cls;
     44   enum GNUNET_MHD_PostResult pr;
     45   json_t *json;
     46   uint64_t row_id;
     47   struct GNUNET_TIME_Timestamp timestamp;
     48 
     49   if (NULL == cc)
     50   {
     51     cc = GNUNET_new (struct ConnectionContext);
     52     cc->ctx_cleaner = &GNUNET_MHD_post_parser_cleanup;
     53     *con_cls = cc;
     54   }
     55   pr = GNUNET_MHD_post_parser (REQUEST_BUFFER_MAX,
     56                                connection,
     57                                &cc->ctx,
     58                                upload_data,
     59                                upload_data_size,
     60                                &json);
     61   switch (pr)
     62   {
     63   case GNUNET_MHD_PR_OUT_OF_MEMORY:
     64     GNUNET_break (0);
     65     return MHD_NO;
     66   case GNUNET_MHD_PR_CONTINUE:
     67     return MHD_YES;
     68   case GNUNET_MHD_PR_REQUEST_TOO_LARGE:
     69     GNUNET_break (0);
     70     return MHD_NO;
     71   case GNUNET_MHD_PR_JSON_INVALID:
     72     GNUNET_break (0);
     73     return MHD_NO;
     74   case GNUNET_MHD_PR_SUCCESS:
     75     break;
     76   }
     77   {
     78     struct TALER_FullPayto debit_account;
     79     struct TALER_Amount amount;
     80     struct TALER_ReservePublicKeyP reserve_pub;
     81     char *debit;
     82     enum GNUNET_GenericReturnValue ret;
     83     struct GNUNET_JSON_Specification spec[] = {
     84       GNUNET_JSON_spec_fixed_auto ("reserve_pub",
     85                                    &reserve_pub),
     86       TALER_JSON_spec_full_payto_uri ("debit_account",
     87                                       &debit_account),
     88       TALER_JSON_spec_amount ("amount",
     89                               h->currency,
     90                               &amount),
     91       GNUNET_JSON_spec_end ()
     92     };
     93 
     94     if (GNUNET_OK !=
     95         (ret = TALER_MHD_parse_json_data (connection,
     96                                           json,
     97                                           spec)))
     98     {
     99       GNUNET_break_op (0);
    100       json_decref (json);
    101       return (GNUNET_NO == ret) ? MHD_YES : MHD_NO;
    102     }
    103     if (0 != strcasecmp (amount.currency,
    104                          h->currency))
    105     {
    106       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    107                   "Currency `%s' does not match our configuration\n",
    108                   amount.currency);
    109       json_decref (json);
    110       return TALER_MHD_reply_with_error (
    111         connection,
    112         MHD_HTTP_CONFLICT,
    113         TALER_EC_GENERIC_CURRENCY_MISMATCH,
    114         NULL);
    115     }
    116     debit = TALER_xtalerbank_account_from_payto (debit_account);
    117     if (NULL == debit)
    118     {
    119       GNUNET_break_op (0);
    120       return TALER_MHD_reply_with_error (
    121         connection,
    122         MHD_HTTP_BAD_REQUEST,
    123         TALER_EC_GENERIC_PAYTO_URI_MALFORMED,
    124         debit_account.full_payto);
    125     }
    126     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    127                 "Receiving incoming wire transfer: %s->%s, subject: %s, amount: %s\n",
    128                 debit,
    129                 account,
    130                 TALER_B2S (&reserve_pub),
    131                 TALER_amount2s (&amount));
    132     ret = TALER_FAKEBANK_make_admin_transfer_ (h,
    133                                                debit,
    134                                                account,
    135                                                &amount,
    136                                                &reserve_pub,
    137                                                &row_id,
    138                                                &timestamp);
    139     GNUNET_free (debit);
    140     if (GNUNET_OK != ret)
    141     {
    142       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    143                   "Reserve public key not unique\n");
    144       json_decref (json);
    145       return TALER_MHD_reply_with_error (
    146         connection,
    147         MHD_HTTP_CONFLICT,
    148         TALER_EC_BANK_DUPLICATE_RESERVE_PUB_SUBJECT,
    149         NULL);
    150     }
    151   }
    152   json_decref (json);
    153 
    154   /* Finally build response object */
    155   return TALER_MHD_REPLY_JSON_PACK (connection,
    156                                     MHD_HTTP_OK,
    157                                     GNUNET_JSON_pack_uint64 ("row_id",
    158                                                              row_id),
    159                                     GNUNET_JSON_pack_timestamp ("timestamp",
    160                                                                 timestamp));
    161 }