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