fakebank_twg_admin_add_incoming.c (5368B)
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/taler_fakebank_lib.h" 25 #include "taler/taler_bank_service.h" 26 #include "taler/taler_mhd_lib.h" 27 #include <gnunet/gnunet_mhd_compat.h> 28 #include <gnunet/gnunet_mhd_lib.h> 29 #include "fakebank.h" 30 #include "fakebank_common_make_admin_transfer.h" 31 #include "fakebank_twg_admin_add_incoming.h" 32 33 MHD_RESULT 34 TALER_FAKEBANK_twg_admin_add_incoming_ ( 35 struct TALER_FAKEBANK_Handle *h, 36 struct MHD_Connection *connection, 37 const char *account, 38 const char *upload_data, 39 size_t *upload_data_size, 40 void **con_cls) 41 { 42 struct ConnectionContext *cc = *con_cls; 43 enum GNUNET_MHD_PostResult pr; 44 json_t *json; 45 uint64_t row_id; 46 struct GNUNET_TIME_Timestamp timestamp; 47 48 if (NULL == cc) 49 { 50 cc = GNUNET_new (struct ConnectionContext); 51 cc->ctx_cleaner = &GNUNET_MHD_post_parser_cleanup; 52 *con_cls = cc; 53 } 54 pr = GNUNET_MHD_post_parser (REQUEST_BUFFER_MAX, 55 connection, 56 &cc->ctx, 57 upload_data, 58 upload_data_size, 59 &json); 60 switch (pr) 61 { 62 case GNUNET_MHD_PR_OUT_OF_MEMORY: 63 GNUNET_break (0); 64 return MHD_NO; 65 case GNUNET_MHD_PR_CONTINUE: 66 return MHD_YES; 67 case GNUNET_MHD_PR_REQUEST_TOO_LARGE: 68 GNUNET_break (0); 69 return MHD_NO; 70 case GNUNET_MHD_PR_JSON_INVALID: 71 GNUNET_break (0); 72 return MHD_NO; 73 case GNUNET_MHD_PR_SUCCESS: 74 break; 75 } 76 { 77 struct TALER_FullPayto debit_account; 78 struct TALER_Amount amount; 79 struct TALER_ReservePublicKeyP reserve_pub; 80 char *debit; 81 enum GNUNET_GenericReturnValue ret; 82 struct GNUNET_JSON_Specification spec[] = { 83 GNUNET_JSON_spec_fixed_auto ("reserve_pub", 84 &reserve_pub), 85 TALER_JSON_spec_full_payto_uri ("debit_account", 86 &debit_account), 87 TALER_JSON_spec_amount ("amount", 88 h->currency, 89 &amount), 90 GNUNET_JSON_spec_end () 91 }; 92 93 if (GNUNET_OK != 94 (ret = TALER_MHD_parse_json_data (connection, 95 json, 96 spec))) 97 { 98 GNUNET_break_op (0); 99 json_decref (json); 100 return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 101 } 102 if (0 != strcasecmp (amount.currency, 103 h->currency)) 104 { 105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 106 "Currency `%s' does not match our configuration\n", 107 amount.currency); 108 json_decref (json); 109 return TALER_MHD_reply_with_error ( 110 connection, 111 MHD_HTTP_CONFLICT, 112 TALER_EC_GENERIC_CURRENCY_MISMATCH, 113 NULL); 114 } 115 debit = TALER_xtalerbank_account_from_payto (debit_account); 116 if (NULL == debit) 117 { 118 GNUNET_break_op (0); 119 return TALER_MHD_reply_with_error ( 120 connection, 121 MHD_HTTP_BAD_REQUEST, 122 TALER_EC_GENERIC_PAYTO_URI_MALFORMED, 123 debit_account.full_payto); 124 } 125 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 126 "Receiving incoming wire transfer: %s->%s, subject: %s, amount: %s\n", 127 debit, 128 account, 129 TALER_B2S (&reserve_pub), 130 TALER_amount2s (&amount)); 131 ret = TALER_FAKEBANK_make_admin_transfer_ (h, 132 debit, 133 account, 134 &amount, 135 &reserve_pub, 136 &row_id, 137 ×tamp); 138 GNUNET_free (debit); 139 if (GNUNET_OK != ret) 140 { 141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 142 "Reserve public key not unique\n"); 143 json_decref (json); 144 return TALER_MHD_reply_with_error ( 145 connection, 146 MHD_HTTP_CONFLICT, 147 TALER_EC_BANK_DUPLICATE_RESERVE_PUB_SUBJECT, 148 NULL); 149 } 150 } 151 json_decref (json); 152 153 /* Finally build response object */ 154 return TALER_MHD_REPLY_JSON_PACK (connection, 155 MHD_HTTP_OK, 156 GNUNET_JSON_pack_uint64 ("row_id", 157 row_id), 158 GNUNET_JSON_pack_timestamp ("timestamp", 159 timestamp)); 160 }