taler-merchant-httpd_post-private-transfers.c (5006B)
1 /* 2 This file is part of TALER 3 (C) 2014-2023, 2025, 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file taler-merchant-httpd_post-private-transfers.c 18 * @brief implement API for registering wire transfers 19 * @author Marcello Stanisci 20 * @author Christian Grothoff 21 */ 22 #include "taler/platform.h" 23 #include <jansson.h> 24 #include <taler/taler_signatures.h> 25 #include <taler/taler_json_lib.h> 26 #include <taler/taler_dbevents.h> 27 #include "taler-merchant-httpd_get-exchanges.h" 28 #include "taler-merchant-httpd_helper.h" 29 #include "taler-merchant-httpd_post-private-transfers.h" 30 31 32 /** 33 * How often do we retry the simple INSERT database transaction? 34 */ 35 #define MAX_RETRIES 5 36 37 38 MHD_RESULT 39 TMH_private_post_transfers (const struct TMH_RequestHandler *rh, 40 struct MHD_Connection *connection, 41 struct TMH_HandlerContext *hc) 42 { 43 struct TALER_FullPayto payto_uri; 44 const char *exchange_url; 45 struct TALER_WireTransferIdentifierRawP wtid; 46 struct TALER_Amount amount; 47 struct GNUNET_JSON_Specification spec[] = { 48 TALER_JSON_spec_amount_any ("credit_amount", 49 &amount), 50 GNUNET_JSON_spec_fixed_auto ("wtid", 51 &wtid), 52 TALER_JSON_spec_full_payto_uri ("payto_uri", 53 &payto_uri), 54 TALER_JSON_spec_web_url ("exchange_url", 55 &exchange_url), 56 GNUNET_JSON_spec_end () 57 }; 58 enum GNUNET_GenericReturnValue res; 59 enum GNUNET_DB_QueryStatus qs; 60 bool no_instance; 61 bool no_account; 62 bool conflict; 63 64 res = TALER_MHD_parse_json_data (connection, 65 hc->request_body, 66 spec); 67 if (GNUNET_OK != res) 68 return (GNUNET_NO == res) 69 ? MHD_YES 70 : MHD_NO; 71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 72 "New inbound wire transfer over %s to %s from %s\n", 73 TALER_amount2s (&amount), 74 payto_uri.full_payto, 75 exchange_url); 76 77 /* Check if transfer data is in database, if not, add it. */ 78 qs = TMH_db->insert_transfer (TMH_db->cls, 79 hc->instance->settings.id, 80 exchange_url, 81 &wtid, 82 &amount, 83 payto_uri, 84 0 /* no bank serial known! */, 85 &no_instance, 86 &no_account, 87 &conflict); 88 switch (qs) 89 { 90 case GNUNET_DB_STATUS_HARD_ERROR: 91 GNUNET_break (0); 92 return TALER_MHD_reply_with_error (connection, 93 MHD_HTTP_INTERNAL_SERVER_ERROR, 94 TALER_EC_GENERIC_DB_STORE_FAILED, 95 "insert_transfer"); 96 case GNUNET_DB_STATUS_SOFT_ERROR: 97 GNUNET_break (0); 98 return TALER_MHD_reply_with_error (connection, 99 MHD_HTTP_INTERNAL_SERVER_ERROR, 100 TALER_EC_GENERIC_DB_STORE_FAILED, 101 "insert_transfer"); 102 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 103 GNUNET_assert (0); /* should be impossible */ 104 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 105 break; 106 } 107 if (no_instance) 108 { 109 /* should be only possible if instance was concurrently deleted, 110 that's so theoretical we rather log as error... */ 111 GNUNET_break (0); 112 return TALER_MHD_reply_with_error ( 113 connection, 114 MHD_HTTP_NOT_FOUND, 115 TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN, 116 hc->instance->settings.id); 117 } 118 if (no_account) 119 { 120 GNUNET_break_op (0); 121 return TALER_MHD_reply_with_error ( 122 connection, 123 MHD_HTTP_CONFLICT, 124 TALER_EC_MERCHANT_GENERIC_ACCOUNT_UNKNOWN, 125 payto_uri.full_payto); 126 } 127 if (conflict) 128 { 129 GNUNET_break_op (0); 130 return TALER_MHD_reply_with_error ( 131 connection, 132 MHD_HTTP_CONFLICT, 133 TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_SUBMISSION, 134 NULL); 135 } 136 return TALER_MHD_reply_static (connection, 137 MHD_HTTP_NO_CONTENT, 138 NULL, 139 NULL, 140 0); 141 } 142 143 144 /* end of taler-merchant-httpd_post-private-transfers.c */