merchant

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

taler-merchant-httpd_post-private-transfers.c (4730B)


      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 src/backend/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 "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 #include "merchant-database/insert_transfer.h"
     31 
     32 
     33 /**
     34  * How often do we retry the simple INSERT database transaction?
     35  */
     36 #define MAX_RETRIES 5
     37 
     38 
     39 enum MHD_Result
     40 TMH_private_post_transfers (const struct TMH_RequestHandler *rh,
     41                             struct MHD_Connection *connection,
     42                             struct TMH_HandlerContext *hc)
     43 {
     44   struct TALER_FullPayto payto_uri;
     45   const char *exchange_url;
     46   struct TALER_WireTransferIdentifierRawP wtid;
     47   struct TALER_Amount amount;
     48   struct GNUNET_JSON_Specification spec[] = {
     49     TALER_JSON_spec_amount_any ("credit_amount",
     50                                 &amount),
     51     GNUNET_JSON_spec_fixed_auto ("wtid",
     52                                  &wtid),
     53     TALER_JSON_spec_full_payto_uri ("payto_uri",
     54                                     &payto_uri),
     55     TALER_JSON_spec_web_url ("exchange_url",
     56                              &exchange_url),
     57     GNUNET_JSON_spec_end ()
     58   };
     59   enum GNUNET_GenericReturnValue res;
     60   enum GNUNET_DB_QueryStatus qs;
     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 = TALER_MERCHANTDB_insert_transfer (TMH_db,
     79                                          hc->instance->settings.id,
     80                                          exchange_url,
     81                                          &wtid,
     82                                          &amount,
     83                                          payto_uri,
     84                                          0 /* no bank serial known! */,
     85                                          &no_account,
     86                                          &conflict);
     87   switch (qs)
     88   {
     89   case GNUNET_DB_STATUS_HARD_ERROR:
     90     GNUNET_break (0);
     91     return TALER_MHD_reply_with_error (connection,
     92                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     93                                        TALER_EC_GENERIC_DB_STORE_FAILED,
     94                                        "insert_transfer");
     95   case GNUNET_DB_STATUS_SOFT_ERROR:
     96     GNUNET_break (0);
     97     return TALER_MHD_reply_with_error (connection,
     98                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     99                                        TALER_EC_GENERIC_DB_STORE_FAILED,
    100                                        "insert_transfer");
    101   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    102     GNUNET_assert (0);   /* should be impossible */
    103   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    104     break;
    105   }
    106   if (no_account)
    107   {
    108     GNUNET_break_op (0);
    109     return TALER_MHD_reply_with_error (
    110       connection,
    111       MHD_HTTP_CONFLICT,
    112       TALER_EC_MERCHANT_GENERIC_ACCOUNT_UNKNOWN,
    113       payto_uri.full_payto);
    114   }
    115   if (conflict)
    116   {
    117     GNUNET_break_op (0);
    118     return TALER_MHD_reply_with_error (
    119       connection,
    120       MHD_HTTP_CONFLICT,
    121       TALER_EC_MERCHANT_PRIVATE_POST_TRANSFERS_CONFLICTING_SUBMISSION,
    122       NULL);
    123   }
    124   return TALER_MHD_reply_static (connection,
    125                                  MHD_HTTP_NO_CONTENT,
    126                                  NULL,
    127                                  NULL,
    128                                  0);
    129 }
    130 
    131 
    132 /* end of taler-merchant-httpd_post-private-transfers.c */