aboutsummaryrefslogtreecommitdiff
path: root/src/bank-lib/fakebank_twg_get_transfers_id.c
blob: 5d2667a83daa43f5a3ebad77d14a4b10c5cbb91d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
  This file is part of TALER
  (C) 2024 Taler Systems SA

  TALER is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 3,
  or (at your option) any later version.

  TALER is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public
  License along with TALER; see the file COPYING.  If not,
  see <http://www.gnu.org/licenses/>
*/
/**
 * @file bank-lib/fakebank_twg_get_transfers_id.c
 * @brief routines to return outgoing transfer details
 * @author Christian Grothoff <christian@grothoff.org>
 */
#include "platform.h"
#include <pthread.h>
#include "taler_fakebank_lib.h"
#include "taler_bank_service.h"
#include "taler_mhd_lib.h"
#include <gnunet/gnunet_mhd_compat.h>
#include "fakebank.h"
#include "fakebank_common_lookup.h"
#include "fakebank_common_lp.h"
#include "fakebank_common_parser.h"
#include "fakebank_twg_get_transfers.h"


MHD_RESULT
TALER_FAKEBANK_twg_get_transfers_id_ (
  struct TALER_FAKEBANK_Handle *h,
  struct MHD_Connection *connection,
  const char *account,
  const char *id,
  void **con_cls)
{
  struct Account *acc;
  unsigned long long row_id;
  json_t *trans;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Handling /transfers/%s connection %p\n",
              id,
              connection);
  {
    char dummy;

    if (1 !=
        sscanf (id,
                "%llu%c",
                &row_id,
                &dummy))
    {
      return TALER_MHD_reply_with_error (
        connection,
        MHD_HTTP_BAD_REQUEST,
        TALER_EC_GENERIC_PARAMETER_MALFORMED,
        id);
    }
  }
  GNUNET_assert (0 ==
                 pthread_mutex_lock (&h->big_lock));
  acc = TALER_FAKEBANK_lookup_account_ (h,
                                        account,
                                        NULL);
  if (NULL == acc)
  {
    GNUNET_assert (0 ==
                   pthread_mutex_unlock (&h->big_lock));
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_NOT_FOUND,
                                       TALER_EC_BANK_UNKNOWN_ACCOUNT,
                                       account);
  }
  {
    struct Transaction *t = h->transactions[row_id % h->ram_limit];
    char *credit_payto;

    if (t->debit_account != acc)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Invalid ID specified, transaction %llu not with account %s!\n",
                  (unsigned long long) row_id,
                  account);
      GNUNET_assert (0 ==
                     pthread_mutex_unlock (&h->big_lock));
      return MHD_NO;
    }
    GNUNET_asprintf (&credit_payto,
                     "payto://x-taler-bank/localhost/%s?receiver-name=%s",
                     t->credit_account->account_name,
                     t->credit_account->receiver_name);
    trans = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_data_auto (
        "wtid",
        &t->subject.debit.wtid),
      GNUNET_JSON_pack_string (
        "exchange_base_url",
        t->subject.debit.exchange_base_url),
      GNUNET_JSON_pack_timestamp (
        "timestamp",
        t->date),
      TALER_JSON_pack_amount (
        "amount",
        &t->amount),
      GNUNET_JSON_pack_string (
        "credit_account",
        credit_payto),
      GNUNET_JSON_pack_string (
        "status",
        "success"));
    GNUNET_assert (NULL != trans);
    GNUNET_free (credit_payto);
  }
  GNUNET_assert (0 ==
                 pthread_mutex_unlock (&h->big_lock));
  return TALER_MHD_reply_json (
    connection,
    trans,
    MHD_HTTP_OK);
}