Line data Source code
1 : /* 2 : This file is part of TALER 3 : (C) 2016-2023 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_tbi_get_withdrawal_operation.c 21 : * @brief Implementation of the GET /withdrawal-operation/ request of the Taler Bank Integration API 22 : * @author Christian Grothoff <christian@grothoff.org> 23 : */ 24 : #include "taler/platform.h" 25 : #include <pthread.h> 26 : #include "taler/taler_fakebank_lib.h" 27 : #include "taler/taler_bank_service.h" 28 : #include "taler/taler_mhd_lib.h" 29 : #include <gnunet/gnunet_mhd_compat.h> 30 : #include "fakebank.h" 31 : #include "fakebank_common_lookup.h" 32 : #include "fakebank_common_lp.h" 33 : #include "fakebank_tbi_get_withdrawal_operation.h" 34 : 35 : /** 36 : * Function called to clean up a withdraw context. 37 : * 38 : * @param cls a `struct WithdrawContext *` 39 : */ 40 : static void 41 0 : withdraw_cleanup (void *cls) 42 : { 43 0 : struct WithdrawContext *wc = cls; 44 : 45 0 : GNUNET_free (wc); 46 0 : } 47 : 48 : 49 : MHD_RESULT 50 0 : TALER_FAKEBANK_tbi_get_withdrawal_operation_ ( 51 : struct TALER_FAKEBANK_Handle *h, 52 : struct MHD_Connection *connection, 53 : const char *wopid, 54 : struct GNUNET_TIME_Relative lp, 55 : void **con_cls) 56 : { 57 0 : struct ConnectionContext *cc = *con_cls; 58 : struct WithdrawContext *wc; 59 : const char *status_string; 60 : 61 0 : GNUNET_assert (0 == 62 : pthread_mutex_lock (&h->big_lock)); 63 0 : if (NULL == cc) 64 : { 65 0 : cc = GNUNET_new (struct ConnectionContext); 66 0 : cc->ctx_cleaner = &withdraw_cleanup; 67 0 : *con_cls = cc; 68 0 : wc = GNUNET_new (struct WithdrawContext); 69 0 : cc->ctx = wc; 70 0 : wc->wo = TALER_FAKEBANK_lookup_withdrawal_operation_ (h, 71 : wopid); 72 0 : if (NULL == wc->wo) 73 : { 74 0 : GNUNET_assert (0 == 75 : pthread_mutex_unlock (&h->big_lock)); 76 0 : return TALER_MHD_reply_with_error (connection, 77 : MHD_HTTP_NOT_FOUND, 78 : TALER_EC_BANK_TRANSACTION_NOT_FOUND, 79 : wopid); 80 : } 81 0 : wc->timeout = GNUNET_TIME_relative_to_absolute (lp); 82 : } 83 : else 84 : { 85 0 : wc = cc->ctx; 86 : } 87 0 : if (GNUNET_TIME_absolute_is_past (wc->timeout) || 88 0 : h->in_shutdown || 89 0 : wc->wo->confirmation_done || 90 0 : wc->wo->aborted) 91 : { 92 : json_t *wt; 93 : 94 0 : wt = json_array (); 95 0 : GNUNET_assert (NULL != wt); 96 0 : GNUNET_assert (0 == 97 : json_array_append_new (wt, 98 : json_string ("x-taler-bank"))); 99 0 : GNUNET_assert (0 == 100 : pthread_mutex_unlock (&h->big_lock)); 101 0 : if (wc->wo->aborted) 102 0 : status_string = "aborted"; 103 0 : else if (wc->wo->confirmation_done) 104 0 : status_string = "confirmed"; 105 0 : else if (wc->wo->selection_done) 106 0 : status_string = "selected"; 107 : else 108 0 : status_string = "pending"; 109 0 : return TALER_MHD_REPLY_JSON_PACK ( 110 : connection, 111 : MHD_HTTP_OK, 112 : // FIXME: deprecated field, should be removed in the future. 113 : GNUNET_JSON_pack_bool ("aborted", 114 : wc->wo->aborted), 115 : // FIXME: deprecated field, should be removed in the future. 116 : GNUNET_JSON_pack_bool ("selection_done", 117 : wc->wo->selection_done), 118 : // FIXME: deprecated field, should be removed in the future. 119 : GNUNET_JSON_pack_bool ("transfer_done", 120 : wc->wo->confirmation_done), 121 : GNUNET_JSON_pack_string ("status", 122 : status_string), 123 : GNUNET_JSON_pack_string ("sender_wire", 124 : wc->wo->debit_account->payto_uri), 125 : GNUNET_JSON_pack_allow_null ( 126 : GNUNET_JSON_pack_string ("suggested_exchange", 127 : h->exchange_url)), 128 : GNUNET_JSON_pack_allow_null ( 129 : TALER_JSON_pack_amount ("amount", 130 : wc->wo->amount)), 131 : GNUNET_JSON_pack_array_steal ("wire_types", 132 : wt)); 133 : } 134 : 135 0 : TALER_FAKEBANK_start_lp_ (h, 136 : connection, 137 0 : wc->wo->debit_account, 138 : GNUNET_TIME_absolute_get_remaining (wc->timeout), 139 : LP_WITHDRAW, 140 0 : wc->wo); 141 0 : GNUNET_assert (0 == 142 : pthread_mutex_unlock (&h->big_lock)); 143 0 : return MHD_YES; 144 : }