summaryrefslogtreecommitdiff
path: root/src/backenddb/pg_insert_deposit.c
blob: f7f20d4753b13cf10a42258462d4a5496dd8785c (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
/*
   This file is part of TALER
   Copyright (C) 2022 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 backenddb/pg_insert_deposit.c
 * @brief Implementation of the insert_deposit function for Postgres
 * @author Iván Ávalos
 */
#include "platform.h"
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "pg_insert_deposit.h"
#include "pg_helper.h"

enum GNUNET_DB_QueryStatus
TMH_PG_insert_deposit (
  void *cls,
  const char *instance_id,
  struct GNUNET_TIME_Timestamp deposit_timestamp,
  const struct TALER_PrivateContractHashP *h_contract_terms,
  const struct TALER_CoinSpendPublicKeyP *coin_pub,
  const char *exchange_url,
  const struct TALER_Amount *amount_with_fee,
  const struct TALER_Amount *deposit_fee,
  const struct TALER_Amount *refund_fee,
  const struct TALER_Amount *wire_fee,
  const struct TALER_MerchantWireHashP *h_wire,
  const struct TALER_ExchangeSignatureP *exchange_sig,
  const struct TALER_ExchangePublicKeyP *exchange_pub)
{
  struct PostgresClosure *pg = cls;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
    GNUNET_PQ_query_param_timestamp (&deposit_timestamp), /* $3 */
    GNUNET_PQ_query_param_auto_from_type (coin_pub),
    GNUNET_PQ_query_param_string (exchange_url),
    TALER_PQ_query_param_amount (amount_with_fee), /* $6/$7 */
    TALER_PQ_query_param_amount (deposit_fee),  /* $8, $9 */
    TALER_PQ_query_param_amount (refund_fee), /* $10, $11 */
    TALER_PQ_query_param_amount (wire_fee),  /* $12, $13 */
    GNUNET_PQ_query_param_auto_from_type (h_wire), /* $14 */
    GNUNET_PQ_query_param_auto_from_type (exchange_sig), /* $15 */
    GNUNET_PQ_query_param_auto_from_type (exchange_pub), /* $16 */
    GNUNET_PQ_query_param_end
  };

  /* no preflight check here, run in transaction by caller! */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Storing deposit for instance `%s' h_contract_terms `%s', coin_pub: `%s', amount_with_fee: %s\n",
              instance_id,
              GNUNET_h2s (&h_contract_terms->hash),
              TALER_B2S (coin_pub),
              TALER_amount2s (amount_with_fee));
  check_connection (pg);
  PREPARE (pg,
           "insert_deposit",
           "WITH md AS"
           "  (SELECT account_serial, merchant_serial"
           "   FROM merchant_accounts"
           "   WHERE h_wire=$14"
           "    AND merchant_serial="
           "     (SELECT merchant_serial"
           "        FROM merchant_instances"
           "        WHERE merchant_id=$1))"
           ", ed AS"
           "  (SELECT signkey_serial"
           "   FROM merchant_exchange_signing_keys"
           "   WHERE exchange_pub=$16"
           "   ORDER BY start_date DESC"
           "   LIMIT 1)"
           "INSERT INTO merchant_deposits"
           "(order_serial"
           ",deposit_timestamp"
           ",coin_pub"
           ",exchange_url"
           ",amount_with_fee_val"
           ",amount_with_fee_frac"
           ",deposit_fee_val"
           ",deposit_fee_frac"
           ",refund_fee_val"
           ",refund_fee_frac"
           ",wire_fee_val"
           ",wire_fee_frac"
           ",exchange_sig"
           ",signkey_serial"
           ",account_serial)"
           " SELECT "
           "   order_serial"
           "  ,$3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $15"
           "  ,ed.signkey_serial"
           "  ,md.account_serial"
           "  FROM merchant_contract_terms"
           "   JOIN md USING (merchant_serial)"
           "   FULL OUTER JOIN ed ON TRUE"
           "  WHERE h_contract_terms=$2");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "insert_deposit",
                                             params);

}