From e5e2e394aaa673e43808b4ee8659079d57ced8e6 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Tue, 17 Oct 2023 14:25:11 -0600 Subject: Factor out 15 new functions (shit job) --- src/backenddb/Makefile.am | 15 + src/backenddb/pg_check_transfer_exists.c | 63 + src/backenddb/pg_check_transfer_exists.h | 43 + src/backenddb/pg_delete_transfer.c | 57 + src/backenddb/pg_delete_transfer.h | 43 + src/backenddb/pg_insert_refund_proof.c | 58 + src/backenddb/pg_insert_refund_proof.h | 43 + src/backenddb/pg_lookup_account.c | 62 + src/backenddb/pg_lookup_account.h | 43 + .../pg_lookup_deposits_by_contract_and_coin.c | 192 +++ .../pg_lookup_deposits_by_contract_and_coin.h | 48 + src/backenddb/pg_lookup_deposits_by_order.c | 161 +++ src/backenddb/pg_lookup_deposits_by_order.h | 43 + src/backenddb/pg_lookup_order_by_fulfillment.c | 65 + src/backenddb/pg_lookup_order_by_fulfillment.h | 47 + src/backenddb/pg_lookup_refund_proof.c | 63 + src/backenddb/pg_lookup_refund_proof.h | 43 + src/backenddb/pg_lookup_refunds_detailed.c | 185 +++ src/backenddb/pg_lookup_refunds_detailed.h | 45 + src/backenddb/pg_lookup_transfer.c | 125 ++ src/backenddb/pg_lookup_transfer.h | 57 + src/backenddb/pg_lookup_transfer_details.c | 155 ++ src/backenddb/pg_lookup_transfer_details.h | 45 + .../pg_lookup_transfer_details_by_order.c | 172 +++ .../pg_lookup_transfer_details_by_order.h | 44 + src/backenddb/pg_lookup_transfer_summary.c | 152 ++ src/backenddb/pg_lookup_transfer_summary.h | 45 + src/backenddb/pg_lookup_wire_fee.c | 83 ++ src/backenddb/pg_lookup_wire_fee.h | 52 + src/backenddb/pg_mark_order_wired.c | 48 + src/backenddb/pg_mark_order_wired.h | 39 + src/backenddb/plugin_merchantdb_postgres.c | 1475 +------------------- 32 files changed, 2383 insertions(+), 1428 deletions(-) create mode 100644 src/backenddb/pg_check_transfer_exists.c create mode 100644 src/backenddb/pg_check_transfer_exists.h create mode 100644 src/backenddb/pg_delete_transfer.c create mode 100644 src/backenddb/pg_delete_transfer.h create mode 100644 src/backenddb/pg_insert_refund_proof.c create mode 100644 src/backenddb/pg_insert_refund_proof.h create mode 100644 src/backenddb/pg_lookup_account.c create mode 100644 src/backenddb/pg_lookup_account.h create mode 100644 src/backenddb/pg_lookup_deposits_by_contract_and_coin.c create mode 100644 src/backenddb/pg_lookup_deposits_by_contract_and_coin.h create mode 100644 src/backenddb/pg_lookup_deposits_by_order.c create mode 100644 src/backenddb/pg_lookup_deposits_by_order.h create mode 100644 src/backenddb/pg_lookup_order_by_fulfillment.c create mode 100644 src/backenddb/pg_lookup_order_by_fulfillment.h create mode 100644 src/backenddb/pg_lookup_refund_proof.c create mode 100644 src/backenddb/pg_lookup_refund_proof.h create mode 100644 src/backenddb/pg_lookup_refunds_detailed.c create mode 100644 src/backenddb/pg_lookup_refunds_detailed.h create mode 100644 src/backenddb/pg_lookup_transfer.c create mode 100644 src/backenddb/pg_lookup_transfer.h create mode 100644 src/backenddb/pg_lookup_transfer_details.c create mode 100644 src/backenddb/pg_lookup_transfer_details.h create mode 100644 src/backenddb/pg_lookup_transfer_details_by_order.c create mode 100644 src/backenddb/pg_lookup_transfer_details_by_order.h create mode 100644 src/backenddb/pg_lookup_transfer_summary.c create mode 100644 src/backenddb/pg_lookup_transfer_summary.h create mode 100644 src/backenddb/pg_lookup_wire_fee.c create mode 100644 src/backenddb/pg_lookup_wire_fee.h create mode 100644 src/backenddb/pg_mark_order_wired.c create mode 100644 src/backenddb/pg_mark_order_wired.h (limited to 'src/backenddb') diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am index 22041a1d..ddf24df7 100644 --- a/src/backenddb/Makefile.am +++ b/src/backenddb/Makefile.am @@ -132,6 +132,21 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \ pg_lookup_order_status.h pg_lookup_order_status.c \ pg_lookup_order_status_by_serial.h pg_lookup_order_status_by_serial.c \ pg_lookup_payment_status.h pg_lookup_payment_status.c \ + pg_lookup_deposits_by_order.h pg_lookup_deposits_by_order.c \ + pg_lookup_transfer_details_by_order.h pg_lookup_transfer_details_by_order.c \ + pg_mark_order_wired.h pg_mark_order_wired.c \ + pg_lookup_refunds_detailed.h pg_lookup_refunds_detailed.c \ + pg_insert_refund_proof.h pg_insert_refund_proof.c \ + pg_lookup_refund_proof.h pg_lookup_refund_proof.c \ + pg_lookup_order_by_fulfillment.h pg_lookup_order_by_fulfillment.c \ + pg_delete_transfer.h pg_delete_transfer.c \ + pg_check_transfer_exists.h pg_check_transfer_exists.c \ + pg_lookup_account.h pg_lookup_account.c \ + pg_lookup_wire_fee.h pg_lookup_wire_fee.c \ + pg_lookup_deposits_by_contract_and_coin.h pg_lookup_deposits_by_contract_and_coin.c \ + pg_lookup_transfer.h pg_lookup_transfer.c \ + pg_lookup_transfer_summary.h pg_lookup_transfer_summary.c \ + pg_lookup_transfer_details.h pg_lookup_transfer_details.c \ plugin_merchantdb_postgres.c \ pg_helper.h pg_helper.c libtaler_plugin_merchantdb_postgres_la_LIBADD = \ diff --git a/src/backenddb/pg_check_transfer_exists.c b/src/backenddb/pg_check_transfer_exists.c new file mode 100644 index 00000000..2ab15ef5 --- /dev/null +++ b/src/backenddb/pg_check_transfer_exists.c @@ -0,0 +1,63 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_check_transfer_exists.c + * @brief Implementation of the check_transfer_exists function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_check_transfer_exists.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_check_transfer_exists (void *cls, + const char *instance_id, + uint64_t transfer_serial_id) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_uint64 (&transfer_serial_id), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + PREPARE (pg, + "check_transfer_exists", + "SELECT" + " 1" + " FROM merchant_transfers" + " JOIN merchant_accounts" + " USING (account_serial)" + " WHERE" + " credit_serial=$2" + " AND" + " merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1)"); + + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "check_transfer_exists", + params, + rs); +} diff --git a/src/backenddb/pg_check_transfer_exists.h b/src/backenddb/pg_check_transfer_exists.h new file mode 100644 index 00000000..0e75e6cb --- /dev/null +++ b/src/backenddb/pg_check_transfer_exists.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_check_transfer_exists.h + * @brief implementation of the check_transfer_exists function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_CHECK_TRANSFER_EXISTS_H +#define PG_CHECK_TRANSFER_EXISTS_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Check if information about a transfer exists with the + * backend. Returns no data, only the query status. + * + * @param cls closure + * @param instance_id instance to delete transfer of + * @param transfer_serial_id transfer to delete + * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT + * if the transfer record exists + */ +enum GNUNET_DB_QueryStatus +TMH_PG_check_transfer_exists (void *cls, + const char *instance_id, + uint64_t transfer_serial_id); + +#endif diff --git a/src/backenddb/pg_delete_transfer.c b/src/backenddb/pg_delete_transfer.c new file mode 100644 index 00000000..f23d97fa --- /dev/null +++ b/src/backenddb/pg_delete_transfer.c @@ -0,0 +1,57 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_delete_transfer.c + * @brief Implementation of the delete_transfer function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_delete_transfer.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_delete_transfer (void *cls, + const char *instance_id, + uint64_t transfer_serial_id) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_uint64 (&transfer_serial_id), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + PREPARE (pg, + "delete_transfer", + "DELETE FROM merchant_transfers" + " WHERE" + " credit_serial=$2" + " AND account_serial IN " + " (SELECT account_serial " + " FROM merchant_accounts" + " WHERE merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1))"); + + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "delete_transfer", + params); +} diff --git a/src/backenddb/pg_delete_transfer.h b/src/backenddb/pg_delete_transfer.h new file mode 100644 index 00000000..2be7125b --- /dev/null +++ b/src/backenddb/pg_delete_transfer.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_delete_transfer.h + * @brief implementation of the delete_transfer function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_DELETE_TRANSFER_H +#define PG_DELETE_TRANSFER_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Delete information about a transfer. Note that transfers + * confirmed by the exchange cannot be deleted anymore. + * + * @param cls closure + * @param instance_id instance to delete transfer of + * @param transfer_serial_id transfer to delete + * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS + * if deletion is prohibited OR transfer is unknown + */ +enum GNUNET_DB_QueryStatus +TMH_PG_delete_transfer (void *cls, + const char *instance_id, + uint64_t transfer_serial_id); + +#endif diff --git a/src/backenddb/pg_insert_refund_proof.c b/src/backenddb/pg_insert_refund_proof.c new file mode 100644 index 00000000..05d0d9cd --- /dev/null +++ b/src/backenddb/pg_insert_refund_proof.c @@ -0,0 +1,58 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_insert_refund_proof.c + * @brief Implementation of the insert_refund_proof function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_insert_refund_proof.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_insert_refund_proof (void *cls, + uint64_t refund_serial, + 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_uint64 (&refund_serial), + GNUNET_PQ_query_param_auto_from_type (exchange_sig), + GNUNET_PQ_query_param_auto_from_type (exchange_pub), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + PREPARE (pg, + "insert_refund_proof", + "INSERT INTO merchant_refund_proofs" + "(refund_serial" + ",exchange_sig" + ",signkey_serial)" + "SELECT $1, $2, signkey_serial" + " FROM merchant_exchange_signing_keys" + " WHERE exchange_pub=$3" + " ORDER BY start_date DESC" + " LIMIT 1"); + + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "insert_refund_proof", + params); +} diff --git a/src/backenddb/pg_insert_refund_proof.h b/src/backenddb/pg_insert_refund_proof.h new file mode 100644 index 00000000..906de077 --- /dev/null +++ b/src/backenddb/pg_insert_refund_proof.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_insert_refund_proof.h + * @brief implementation of the insert_refund_proof function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_INSERT_REFUND_PROOF_H +#define PG_INSERT_REFUND_PROOF_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Insert refund proof data from the exchange into the database. + * + * @param cls closure + * @param refund_serial serial number of the refund + * @param exchange_sig signature from exchange that coin was refunded + * @param exchange_pub signing key that was used for @a exchange_sig + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_insert_refund_proof (void *cls, + uint64_t refund_serial, + const struct TALER_ExchangeSignatureP *exchange_sig, + const struct TALER_ExchangePublicKeyP *exchange_pub); + +#endif diff --git a/src/backenddb/pg_lookup_account.c b/src/backenddb/pg_lookup_account.c new file mode 100644 index 00000000..89058815 --- /dev/null +++ b/src/backenddb/pg_lookup_account.c @@ -0,0 +1,62 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_account.c + * @brief Implementation of the lookup_account function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_account.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_account (void *cls, + const char *instance_id, + const char *payto_uri, + uint64_t *account_serial) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_string (payto_uri), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_uint64 ("account_serial", + account_serial), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + PREPARE (pg, + "lookup_account", + "SELECT" + " account_serial" + " FROM merchant_accounts" + " WHERE payto_uri=$2" + " AND merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1)"); + + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_account", + params, + rs); +} diff --git a/src/backenddb/pg_lookup_account.h b/src/backenddb/pg_lookup_account.h new file mode 100644 index 00000000..63b0aa73 --- /dev/null +++ b/src/backenddb/pg_lookup_account.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_account.h + * @brief implementation of the lookup_account function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_ACCOUNT_H +#define PG_LOOKUP_ACCOUNT_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Lookup account serial by payto URI. + * + * @param cls closure + * @param instance_id instance to lookup the account from + * @param payto_uri what is the merchant's bank account to lookup + * @param[out] account_serial serial number of the account + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_account (void *cls, + const char *instance_id, + const char *payto_uri, + uint64_t *account_serial); + +#endif diff --git a/src/backenddb/pg_lookup_deposits_by_contract_and_coin.c b/src/backenddb/pg_lookup_deposits_by_contract_and_coin.c new file mode 100644 index 00000000..9bf46d0c --- /dev/null +++ b/src/backenddb/pg_lookup_deposits_by_contract_and_coin.c @@ -0,0 +1,192 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_deposits_by_contract_and_coin.c + * @brief Implementation of the lookup_deposits_by_contract_and_coin function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_deposits_by_contract_and_coin.h" +#include "pg_helper.h" + +/** + * Closure for #lookup_deposits_by_contract_and_coin_cb(). + */ +struct LookupDepositsByCnCContext +{ + /** + * Function to call for each deposit. + */ + TALER_MERCHANTDB_CoinDepositCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Transaction result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct LookupDepositsByCnCContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +lookup_deposits_by_contract_and_coin_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupDepositsByCnCContext *ldcc = cls; + + for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + ldcc->cb (ldcc->cb_cls, + exchange_url, + &amount_with_fee, + &deposit_fee, + &refund_fee, + &wire_fee, + &h_wire, + deposit_timestamp, + refund_deadline, + &exchange_sig, + &exchange_pub); + GNUNET_PQ_cleanup_result (rs); + } + ldcc->qs = num_results; +} + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_deposits_by_contract_and_coin (void *cls, + const char *instance_id, + const struct TALER_PrivateContractHashP *h_contract_terms, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + TALER_MERCHANTDB_CoinDepositCallback cb, + void *cb_cls) +{ + 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_auto_from_type (coin_pub), + GNUNET_PQ_query_param_end + }; + struct LookupDepositsByCnCContext ldcc = { + .cb = cb, + .cb_cls = cb_cls, + .pg = pg + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + PREPARE (pg, + "lookup_deposits_by_contract_and_coin", + "SELECT" + " mcon.exchange_url" + ",dep.amount_with_fee" + ",dep.deposit_fee" + ",dep.refund_fee" + ",mcon.wire_fee" + ",acc.h_wire" + ",mcon.deposit_timestamp" + ",mct.refund_deadline" + ",mcon.exchange_sig" + ",msig.exchange_pub" + " FROM merchant_contract_terms mct" + " JOIN merchant_deposit_confirmations mcon" + " USING (order_serial)" + " JOIN merchant_deposits dep" + " USING (deposit_confirmation_serial)" + " JOIN merchant_exchange_signing_keys msig" + " USING (signkey_serial)" + " JOIN merchant_accounts acc" + " USING (account_serial)" + " WHERE h_contract_terms=$2" + " AND dep.coin_pub=$3" + " AND mct.merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1)"); + + qs = GNUNET_PQ_eval_prepared_multi_select ( + pg->conn, + "lookup_deposits_by_contract_and_coin", + params, + &lookup_deposits_by_contract_and_coin_cb, + &ldcc); + if (0 >= qs) + return qs; + return ldcc.qs; +} diff --git a/src/backenddb/pg_lookup_deposits_by_contract_and_coin.h b/src/backenddb/pg_lookup_deposits_by_contract_and_coin.h new file mode 100644 index 00000000..250e3dfc --- /dev/null +++ b/src/backenddb/pg_lookup_deposits_by_contract_and_coin.h @@ -0,0 +1,48 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_deposits_by_contract_and_coin.h + * @brief implementation of the lookup_deposits_by_contract_and_coin function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_DEPOSITS_BY_CONTRACT_AND_COIN_H +#define PG_LOOKUP_DEPOSITS_BY_CONTRACT_AND_COIN_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Lookup information about coin payments by @a h_contract_terms and + * @a coin_pub. + * + * @param cls closure + * @param instance_id instance to lookup payments for + * @param h_contract_terms proposal data's hashcode + * @param coin_pub public key to use for the search + * @param cb function to call with payment data + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_deposits_by_contract_and_coin (void *cls, + const char *instance_id, + const struct TALER_PrivateContractHashP *h_contract_terms, + const struct TALER_CoinSpendPublicKeyP *coin_pub, + TALER_MERCHANTDB_CoinDepositCallback cb, + void *cb_cls); + +#endif diff --git a/src/backenddb/pg_lookup_deposits_by_order.c b/src/backenddb/pg_lookup_deposits_by_order.c new file mode 100644 index 00000000..fdaf1dfc --- /dev/null +++ b/src/backenddb/pg_lookup_deposits_by_order.c @@ -0,0 +1,161 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_deposits_by_order.c + * @brief Implementation of the lookup_deposits_by_order function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_deposits_by_order.h" +#include "pg_helper.h" + +/** + * Closure for lookup_deposits_by_order_cb(). + */ +struct LookupDepositsByOrderContext +{ + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Function to call with all results. + */ + TALER_MERCHANTDB_DepositedCoinsCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Set to the query result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct LookupDepositsByOrderContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +lookup_deposits_by_order_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupDepositsByOrderContext *ldoc = cls; + + for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + ldoc->cb (ldoc->cb_cls, + deposit_serial, + exchange_url, + &h_wire, + &amount_with_fee, + &deposit_fee, + &coin_pub); + GNUNET_PQ_cleanup_result (rs); /* technically useless here */ + } + ldoc->qs = num_results; +} + + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_deposits_by_order (void *cls, + uint64_t order_serial, + TALER_MERCHANTDB_DepositedCoinsCallback cb, + void *cb_cls) +{ + struct PostgresClosure *pg = cls; + struct LookupDepositsByOrderContext ldoc = { + .pg = pg, + .cb = cb, + .cb_cls = cb_cls + }; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (&order_serial), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + PREPARE (pg, + "lookup_deposits_by_order", + "SELECT" + " dep.deposit_serial" + ",mcon.exchange_url" + ",acc.h_wire" + ",dep.amount_with_fee" + ",dep.deposit_fee" + ",dep.coin_pub" + " FROM merchant_deposits dep" + " JOIN merchant_deposit_confirmations mcon" + " USING(deposit_confirmation_serial)" + " JOIN merchant_accounts acc" + " USING (account_serial)" + " WHERE mcon.order_serial=$1"); + + qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, + "lookup_deposits_by_order", + params, + &lookup_deposits_by_order_cb, + &ldoc); + + if (qs < 0) + return qs; + return ldoc.qs; +} diff --git a/src/backenddb/pg_lookup_deposits_by_order.h b/src/backenddb/pg_lookup_deposits_by_order.h new file mode 100644 index 00000000..ecf2d367 --- /dev/null +++ b/src/backenddb/pg_lookup_deposits_by_order.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_deposits_by_order.h + * @brief implementation of the lookup_deposits_by_order function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_DEPOSITS_BY_ORDER_H +#define PG_LOOKUP_DEPOSITS_BY_ORDER_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Retrieve details about coins that were deposited for an order. + * + * @param cls closure + * @param order_serial identifies the order + * @param cb function to call for each deposited coin + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_deposits_by_order (void *cls, + uint64_t order_serial, + TALER_MERCHANTDB_DepositedCoinsCallback cb, + void *cb_cls); + +#endif diff --git a/src/backenddb/pg_lookup_order_by_fulfillment.c b/src/backenddb/pg_lookup_order_by_fulfillment.c new file mode 100644 index 00000000..0e9eba4a --- /dev/null +++ b/src/backenddb/pg_lookup_order_by_fulfillment.c @@ -0,0 +1,65 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_order_by_fulfillment.c + * @brief Implementation of the lookup_order_by_fulfillment function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_order_by_fulfillment.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_order_by_fulfillment (void *cls, + const char *instance_id, + const char *fulfillment_url, + const char *session_id, + char **order_id) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_string (fulfillment_url), + GNUNET_PQ_query_param_string (session_id), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_string ("order_id", + order_id), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + PREPARE (pg, + "lookup_order_by_fulfillment", + "SELECT" + " order_id" + " FROM merchant_contract_terms" + " WHERE fulfillment_url=$2" + " AND session_id=$3" + " AND merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1)"); + + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_order_by_fulfillment", + params, + rs); +} diff --git a/src/backenddb/pg_lookup_order_by_fulfillment.h b/src/backenddb/pg_lookup_order_by_fulfillment.h new file mode 100644 index 00000000..883ff4b9 --- /dev/null +++ b/src/backenddb/pg_lookup_order_by_fulfillment.h @@ -0,0 +1,47 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_order_by_fulfillment.h + * @brief implementation of the lookup_order_by_fulfillment function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_ORDER_BY_FULFILLMENT_H +#define PG_LOOKUP_ORDER_BY_FULFILLMENT_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Retrieve the order ID that was used to pay for a resource within a session. + * + * @param cls closure + * @param instance_id identifying the instance + * @param fulfillment_url URL that canonically identifies the resource + * being paid for + * @param session_id session id + * @param[out] order_id where to store the order ID that was used when + * paying for the resource URL + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_order_by_fulfillment (void *cls, + const char *instance_id, + const char *fulfillment_url, + const char *session_id, + char **order_id); + +#endif diff --git a/src/backenddb/pg_lookup_refund_proof.c b/src/backenddb/pg_lookup_refund_proof.c new file mode 100644 index 00000000..caa3827f --- /dev/null +++ b/src/backenddb/pg_lookup_refund_proof.c @@ -0,0 +1,63 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_refund_proof.c + * @brief Implementation of the lookup_refund_proof function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_refund_proof.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_refund_proof (void *cls, + uint64_t refund_serial, + struct TALER_ExchangeSignatureP *exchange_sig, + struct TALER_ExchangePublicKeyP *exchange_pub) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (&refund_serial), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_auto_from_type ("exchange_sig", + exchange_sig), + GNUNET_PQ_result_spec_auto_from_type ("exchange_pub", + exchange_pub), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + PREPARE (pg, + "lookup_refund_proof", + "SELECT" + " merchant_exchange_signing_keys.exchange_pub" + ",exchange_sig" + " FROM merchant_refund_proofs" + " JOIN merchant_exchange_signing_keys" + " USING (signkey_serial)" + " WHERE" + " refund_serial=$1"); + + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_refund_proof", + params, + rs); +} diff --git a/src/backenddb/pg_lookup_refund_proof.h b/src/backenddb/pg_lookup_refund_proof.h new file mode 100644 index 00000000..56d8ad97 --- /dev/null +++ b/src/backenddb/pg_lookup_refund_proof.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_refund_proof.h + * @brief implementation of the lookup_refund_proof function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_REFUND_PROOF_H +#define PG_LOOKUP_REFUND_PROOF_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Lookup refund proof data. + * + * @param cls closure + * @param refund_serial serial number of the refund + * @param[out] exchange_sig set to signature from exchange + * @param[out] exchange_pub signing key that was used for @a exchange_sig + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_refund_proof (void *cls, + uint64_t refund_serial, + struct TALER_ExchangeSignatureP *exchange_sig, + struct TALER_ExchangePublicKeyP *exchange_pub); + +#endif diff --git a/src/backenddb/pg_lookup_refunds_detailed.c b/src/backenddb/pg_lookup_refunds_detailed.c new file mode 100644 index 00000000..1b01f4d6 --- /dev/null +++ b/src/backenddb/pg_lookup_refunds_detailed.c @@ -0,0 +1,185 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_refunds_detailed.c + * @brief Implementation of the lookup_refunds_detailed function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_refunds_detailed.h" +#include "pg_helper.h" + +/** + * Closure for #lookup_refunds_detailed_cb(). + */ +struct LookupRefundsDetailedContext +{ + /** + * Function to call for each refund. + */ + TALER_MERCHANTDB_RefundDetailCallback rc; + + /** + * Closure for @e rc. + */ + void *rc_cls; + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Transaction result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct GetRefundsContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +lookup_refunds_detailed_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupRefundsDetailedContext *lrdc = cls; + + for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + lrdc->rc (lrdc->rc_cls, + refund_serial, + timestamp, + &coin_pub, + exchange_url, + rtransaction_id, + reason, + &refund_amount, + 0 != pending8); + GNUNET_PQ_cleanup_result (rs); + } + lrdc->qs = num_results; +} + + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_refunds_detailed (void *cls, + const char *instance_id, + const struct TALER_PrivateContractHashP *h_contract_terms, + TALER_MERCHANTDB_RefundDetailCallback rc, + void *rc_cls) +{ + 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_end + }; + struct LookupRefundsDetailedContext lrdc = { + .rc = rc, + .rc_cls = rc_cls, + .pg = pg + }; + enum GNUNET_DB_QueryStatus qs; + + /* no preflight check here, run in transaction by caller! */ + TALER_LOG_DEBUG ("Looking for refund %s + %s\n", + GNUNET_h2s (&h_contract_terms->hash), + instance_id); + + check_connection (pg); + PREPARE (pg, + "lookup_refunds_detailed", + "SELECT" + " ref.refund_serial" + ",ref.refund_timestamp" + ",dep.coin_pub" + ",mcon.exchange_url" + ",ref.rtransaction_id" + ",ref.reason" + ",ref.refund_amount" + ",merchant_refund_proofs.exchange_sig IS NULL AS pending" + " FROM merchant_deposit_confirmations mcon" + " JOIN merchant_deposits dep" + " USING (deposit_confirmation_serial)" + " JOIN merchant_refunds ref" + " USING (order_serial, coin_pub)" + " LEFT JOIN merchant_refund_proofs" + " USING (refund_serial)" + " WHERE mcon.order_serial=" + " (SELECT order_serial" + " FROM merchant_contract_terms" + " WHERE h_contract_terms=$2" + " AND merchant_serial=" + " (SELECT merchant_serial" + " FROM merchant_instances" + " WHERE merchant_id=$1))"); + + qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, + "lookup_refunds_detailed", + params, + &lookup_refunds_detailed_cb, + &lrdc); + if (0 >= qs) + return qs; + return lrdc.qs; +} diff --git a/src/backenddb/pg_lookup_refunds_detailed.h b/src/backenddb/pg_lookup_refunds_detailed.h new file mode 100644 index 00000000..c2531446 --- /dev/null +++ b/src/backenddb/pg_lookup_refunds_detailed.h @@ -0,0 +1,45 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_refunds_detailed.h + * @brief implementation of the lookup_refunds_detailed function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_REFUNDS_DETAILED_H +#define PG_LOOKUP_REFUNDS_DETAILED_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Obtain detailed refund data associated with a contract. + * + * @param cls closure, typically a connection to the db + * @param instance_id instance to lookup refunds for + * @param h_contract_terms hash code of the contract + * @param rc function to call for each coin on which there is a refund + * @param rc_cls closure for @a rc + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_refunds_detailed (void *cls, + const char *instance_id, + const struct TALER_PrivateContractHashP *h_contract_terms, + TALER_MERCHANTDB_RefundDetailCallback rc, + void *rc_cls); + +#endif diff --git a/src/backenddb/pg_lookup_transfer.c b/src/backenddb/pg_lookup_transfer.c new file mode 100644 index 00000000..03c2ccce --- /dev/null +++ b/src/backenddb/pg_lookup_transfer.c @@ -0,0 +1,125 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer.c + * @brief Implementation of the lookup_transfer function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_transfer.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer (void *cls, + const char *instance_id, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + struct TALER_Amount *total_amount, + struct TALER_Amount *wire_fee, + struct TALER_Amount *exchange_amount, + struct GNUNET_TIME_Timestamp *execution_time, + bool *have_exchange_sig, + bool *verified) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (exchange_url), + GNUNET_PQ_query_param_auto_from_type (wtid), + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_end + }; + uint8_t verified8; + /** Amount we got actually credited, _excludes_ the wire fee */ + bool no_sig; + struct TALER_Amount credit_amount; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_amount_with_currency ("credit_amount", + &credit_amount), + GNUNET_PQ_result_spec_allow_null ( + TALER_PQ_result_spec_amount_with_currency ("wire_fee", + wire_fee), + &no_sig), + GNUNET_PQ_result_spec_allow_null ( + TALER_PQ_result_spec_amount_with_currency ("exchange_amount", + exchange_amount), + NULL), + GNUNET_PQ_result_spec_allow_null ( + GNUNET_PQ_result_spec_timestamp ("execution_time", + execution_time), + NULL), + GNUNET_PQ_result_spec_auto_from_type ("verified", + &verified8), + GNUNET_PQ_result_spec_end + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + *execution_time = GNUNET_TIME_UNIT_ZERO_TS; + + PREPARE (pg, + "lookup_transfer", + "SELECT" + " mt.credit_amount AS credit_amount" + ",mts.credit_amount AS exchange_amount" + ",wire_fee" + ",execution_time" + ",verified" + " FROM merchant_transfers mt" + " JOIN merchant_accounts USING (account_serial)" + " JOIN merchant_instances USING (merchant_serial)" + " LEFT JOIN merchant_transfer_signatures mts USING (credit_serial)" + " WHERE wtid=$2" + " AND exchange_url=$1" + " AND merchant_id=$3;"); + + qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_transfer", + params, + rs); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Lookup transfer returned %d\n", + qs); + if (qs > 0) + { + *have_exchange_sig = ! no_sig; + *verified = (0 != verified8); + if (GNUNET_OK != + TALER_amount_cmp_currency (&credit_amount, + wire_fee)) + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + if ( (! no_sig) && + (0 > + TALER_amount_add (total_amount, + &credit_amount, + wire_fee)) ) + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + } + else + { + *verified = false; + *have_exchange_sig = false; + } + return qs; +} diff --git a/src/backenddb/pg_lookup_transfer.h b/src/backenddb/pg_lookup_transfer.h new file mode 100644 index 00000000..4dd9c3d7 --- /dev/null +++ b/src/backenddb/pg_lookup_transfer.h @@ -0,0 +1,57 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer.h + * @brief implementation of the lookup_transfer function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_TRANSFER_H +#define PG_LOOKUP_TRANSFER_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Lookup transfer status. + * + * @param cls closure + * @param instance_id at which instance should we resolve the transfer + * @param exchange_url the exchange that made the transfer + * @param wtid wire transfer subject + * @param[out] total_amount amount that was debited from our + * aggregate balance at the exchange (in total, sum of + * the wire transfer amount and the @a wire_fee) + * @param[out] wire_fee the wire fee the exchange charged (only set if @a have_exchange_sig is true) + * @param[out] exchange_amount the amount the exchange claims was transferred (only set if @a have_exchange_sig is true) + * @param[out] execution_time when the transfer was executed by the exchange (only set if @a have_exchange_sig is true) + * @param[out] have_exchange_sig do we have a response from the exchange about this transfer + * @param[out] verified did we confirm the transfer was OK + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer (void *cls, + const char *instance_id, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + struct TALER_Amount *total_amount, + struct TALER_Amount *wire_fee, + struct TALER_Amount *exchange_amount, + struct GNUNET_TIME_Timestamp *execution_time, + bool *have_exchange_sig, + bool *verified); + +#endif diff --git a/src/backenddb/pg_lookup_transfer_details.c b/src/backenddb/pg_lookup_transfer_details.c new file mode 100644 index 00000000..cd497ece --- /dev/null +++ b/src/backenddb/pg_lookup_transfer_details.c @@ -0,0 +1,155 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer_details.c + * @brief Implementation of the lookup_transfer_details function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_transfer_details.h" +#include "pg_helper.h" + +/** + * Closure for #lookup_transfer_details_cb(). + */ +struct LookupTransferDetailsContext +{ + /** + * Function to call for each order that was aggregated. + */ + TALER_MERCHANTDB_TransferDetailsCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Transaction result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct LookupTransferDetailsContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +lookup_transfer_details_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupTransferDetailsContext *ltdc = cls; + + for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + ltdc->cb (ltdc->cb_cls, + (unsigned int) current_offset, + &ttd); + GNUNET_PQ_cleanup_result (rs); + } + ltdc->qs = num_results; +} + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer_details (void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + TALER_MERCHANTDB_TransferDetailsCallback cb, + void *cb_cls) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (exchange_url), + GNUNET_PQ_query_param_auto_from_type (wtid), + GNUNET_PQ_query_param_end + }; + struct LookupTransferDetailsContext ltdc = { + .cb = cb, + .cb_cls = cb_cls, + .pg = pg + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + PREPARE (pg, + "lookup_transfer_details", + "SELECT" + " mterm.h_contract_terms" + ",mtcoin.offset_in_exchange_list" + ",dep.coin_pub" + ",mtcoin.exchange_deposit_value" + ",mtcoin.exchange_deposit_fee" + " FROM merchant_transfer_to_coin mtcoin" + " JOIN merchant_deposits dep" + " USING (deposit_serial)" + " JOIN merchant_deposit_confirmations mcon" + " USING (deposit_confirmation_serial)" + " JOIN merchant_contract_terms mterm" + " USING (order_serial)" + " JOIN merchant_transfers mtr" + " USING (credit_serial)" + " WHERE mtr.wtid=$2" + " AND mtr.exchange_url=$1"); + + qs = GNUNET_PQ_eval_prepared_multi_select ( + pg->conn, + "lookup_transfer_details", + params, + &lookup_transfer_details_cb, + <dc); + if (0 >= qs) + return qs; + return ltdc.qs; +} diff --git a/src/backenddb/pg_lookup_transfer_details.h b/src/backenddb/pg_lookup_transfer_details.h new file mode 100644 index 00000000..3f99fc67 --- /dev/null +++ b/src/backenddb/pg_lookup_transfer_details.h @@ -0,0 +1,45 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer_details.h + * @brief implementation of the lookup_transfer_details function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_TRANSFER_DETAILS_H +#define PG_LOOKUP_TRANSFER_DETAILS_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Lookup transfer details. + * + * @param cls closure + * @param exchange_url the exchange that made the transfer + * @param wtid wire transfer subject + * @param cb function to call with detailed transfer data + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer_details (void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + TALER_MERCHANTDB_TransferDetailsCallback cb, + void *cb_cls); + +#endif diff --git a/src/backenddb/pg_lookup_transfer_details_by_order.c b/src/backenddb/pg_lookup_transfer_details_by_order.c new file mode 100644 index 00000000..6ad07f4a --- /dev/null +++ b/src/backenddb/pg_lookup_transfer_details_by_order.c @@ -0,0 +1,172 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer_details_by_order.c + * @brief Implementation of the lookup_transfer_details_by_order function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_transfer_details_by_order.h" +#include "pg_helper.h" + +/** + * Closure for lookup_transfer_details_by_order_cb(). + */ +struct LookupTransferDetailsByOrderContext +{ + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Function to call with all results. + */ + TALER_MERCHANTDB_OrderTransferDetailsCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Set to the query result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct LookupTransferDetailsByOrderContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +lookup_transfer_details_by_order_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupTransferDetailsByOrderContext *ltdo = cls; + + for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + ltdo->cb (ltdo->cb_cls, + &wtid, + exchange_url, + execution_time, + &deposit_value, + &deposit_fee, + (0 != transfer_confirmed)); + GNUNET_PQ_cleanup_result (rs); /* technically useless here */ + } + ltdo->qs = num_results; +} + + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer_details_by_order (void *cls, + uint64_t order_serial, + TALER_MERCHANTDB_OrderTransferDetailsCallback cb, + void *cb_cls) +{ + struct PostgresClosure *pg = cls; + struct LookupTransferDetailsByOrderContext ltdo = { + .pg = pg, + .cb = cb, + .cb_cls = cb_cls + }; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (&order_serial), + GNUNET_PQ_query_param_end + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + PREPARE (pg, + "lookup_transfer_details_by_order", + "SELECT" + " md.deposit_serial" + ",mcon.exchange_url" + ",mt.wtid" + ",mtc.exchange_deposit_value" + ",mtc.exchange_deposit_fee" + ",mcon.deposit_timestamp" + ",mt.confirmed AS transfer_confirmed" + " FROM merchant_transfer_to_coin mtc" + " JOIN merchant_deposits md" + " USING (deposit_serial)" + " JOIN merchant_deposit_confirmations mcon" + " USING (deposit_confirmation_serial)" + " JOIN merchant_transfers mt" + " USING (credit_serial)" + " JOIN merchant_accounts acc" + " ON (acc.account_serial = mt.account_serial)" + /* Check that all this is for the same instance */ + " JOIN merchant_contract_terms contracts" + " USING (merchant_serial, order_serial)" + " WHERE mcon.order_serial=$1"); + + qs = GNUNET_PQ_eval_prepared_multi_select ( + pg->conn, + "lookup_transfer_details_by_order", + params, + &lookup_transfer_details_by_order_cb, + <do); + if (qs < 0) + return qs; + return ltdo.qs; +} diff --git a/src/backenddb/pg_lookup_transfer_details_by_order.h b/src/backenddb/pg_lookup_transfer_details_by_order.h new file mode 100644 index 00000000..3194888b --- /dev/null +++ b/src/backenddb/pg_lookup_transfer_details_by_order.h @@ -0,0 +1,44 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer_details_by_order.h + * @brief implementation of the lookup_transfer_details_by_order function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_TRANSFER_DETAILS_BY_ORDER_H +#define PG_LOOKUP_TRANSFER_DETAILS_BY_ORDER_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Retrieve wire transfer details for all deposits associated with + * a given @a order_serial. + * + * @param cls closure + * @param order_serial identifies the order + * @param cb function called with the wire transfer details + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer_details_by_order (void *cls, + uint64_t order_serial, + TALER_MERCHANTDB_OrderTransferDetailsCallback cb, + void *cb_cls); + +#endif diff --git a/src/backenddb/pg_lookup_transfer_summary.c b/src/backenddb/pg_lookup_transfer_summary.c new file mode 100644 index 00000000..b282d3bf --- /dev/null +++ b/src/backenddb/pg_lookup_transfer_summary.c @@ -0,0 +1,152 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer_summary.c + * @brief Implementation of the lookup_transfer_summary function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_transfer_summary.h" +#include "pg_helper.h" + +/** + * Closure for #lookup_transfer_summary_cb(). + */ +struct LookupTransferSummaryContext +{ + /** + * Function to call for each order that was aggregated. + */ + TALER_MERCHANTDB_TransferSummaryCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + + /** + * Plugin context. + */ + struct PostgresClosure *pg; + + /** + * Transaction result. + */ + enum GNUNET_DB_QueryStatus qs; +}; + + +/** + * Function to be called with the results of a SELECT statement + * that has returned @a num_results results. + * + * @param cls of type `struct LookupTransferSummaryContext *` + * @param result the postgres result + * @param num_results the number of results in @a result + */ +static void +lookup_transfer_summary_cb (void *cls, + PGresult *result, + unsigned int num_results) +{ + struct LookupTransferSummaryContext *ltdc = cls; + + for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; + return; + } + ltdc->cb (ltdc->cb_cls, + order_id, + &deposit_value, + &deposit_fee); + GNUNET_PQ_cleanup_result (rs); + } + ltdc->qs = num_results; +} + + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer_summary (void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + TALER_MERCHANTDB_TransferSummaryCallback cb, + void *cb_cls) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (exchange_url), + GNUNET_PQ_query_param_auto_from_type (wtid), + GNUNET_PQ_query_param_end + }; + struct LookupTransferSummaryContext ltdc = { + .cb = cb, + .cb_cls = cb_cls, + .pg = pg + }; + enum GNUNET_DB_QueryStatus qs; + + check_connection (pg); + PREPARE (pg, + "lookup_transfer_summary", + "SELECT" + " mct.order_id" + ",mtc.exchange_deposit_value" + ",mtc.exchange_deposit_fee" + " FROM merchant_transfers mtr" + " JOIN merchant_transfer_to_coin mtc" + " USING (credit_serial)" + " JOIN merchant_deposits dep" + " USING (deposit_serial)" + " JOIN merchant_deposit_confirmations mcon" + " USING (deposit_confirmation_serial)" + " JOIN merchant_contract_terms mct" + " USING (order_serial)" + " WHERE mtr.wtid=$2" + " AND mtr.exchange_url=$1"); + + qs = GNUNET_PQ_eval_prepared_multi_select ( + pg->conn, + "lookup_transfer_summary", + params, + &lookup_transfer_summary_cb, + <dc); + if (0 >= qs) + return qs; + return ltdc.qs; +} diff --git a/src/backenddb/pg_lookup_transfer_summary.h b/src/backenddb/pg_lookup_transfer_summary.h new file mode 100644 index 00000000..affcbcdc --- /dev/null +++ b/src/backenddb/pg_lookup_transfer_summary.h @@ -0,0 +1,45 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_transfer_summary.h + * @brief implementation of the lookup_transfer_summary function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_TRANSFER_SUMMARY_H +#define PG_LOOKUP_TRANSFER_SUMMARY_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Lookup transfer summary. + * + * @param cls closure + * @param exchange_url the exchange that made the transfer + * @param wtid wire transfer subject + * @param cb function to call with detailed transfer data + * @param cb_cls closure for @a cb + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_transfer_summary (void *cls, + const char *exchange_url, + const struct TALER_WireTransferIdentifierRawP *wtid, + TALER_MERCHANTDB_TransferSummaryCallback cb, + void *cb_cls); + +#endif diff --git a/src/backenddb/pg_lookup_wire_fee.c b/src/backenddb/pg_lookup_wire_fee.c new file mode 100644 index 00000000..9ef680a5 --- /dev/null +++ b/src/backenddb/pg_lookup_wire_fee.c @@ -0,0 +1,83 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_wire_fee.c + * @brief Implementation of the lookup_wire_fee function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_lookup_wire_fee.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_wire_fee (void *cls, + const struct TALER_MasterPublicKeyP *master_pub, + const char *wire_method, + struct GNUNET_TIME_Timestamp contract_date, + struct TALER_WireFeeSet *fees, + struct GNUNET_TIME_Timestamp *start_date, + struct GNUNET_TIME_Timestamp *end_date, + struct TALER_MasterSignatureP *master_sig) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_HashCode h_wire_method; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (master_pub), + GNUNET_PQ_query_param_auto_from_type (&h_wire_method), + GNUNET_PQ_query_param_timestamp (&contract_date), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_amount_with_currency ("wire_fee", + &fees->wire), + TALER_PQ_result_spec_amount_with_currency ("closing_fee", + &fees->closing), + GNUNET_PQ_result_spec_timestamp ("start_date", + start_date), + GNUNET_PQ_result_spec_timestamp ("end_date", + end_date), + GNUNET_PQ_result_spec_auto_from_type ("master_sig", + master_sig), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + GNUNET_CRYPTO_hash (wire_method, + strlen (wire_method) + 1, + &h_wire_method); + + PREPARE (pg, + "lookup_wire_fee", + "SELECT" + " wire_fee" + ",closing_fee" + ",start_date" + ",end_date" + ",master_sig" + " FROM merchant_exchange_wire_fees" + " WHERE master_pub=$1" + " AND h_wire_method=$2" + " AND start_date <= $3" + " AND end_date > $3"); + + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "lookup_wire_fee", + params, + rs); +} diff --git a/src/backenddb/pg_lookup_wire_fee.h b/src/backenddb/pg_lookup_wire_fee.h new file mode 100644 index 00000000..12a88050 --- /dev/null +++ b/src/backenddb/pg_lookup_wire_fee.h @@ -0,0 +1,52 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_lookup_wire_fee.h + * @brief implementation of the lookup_wire_fee function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_LOOKUP_WIRE_FEE_H +#define PG_LOOKUP_WIRE_FEE_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Obtain information about wire fees charged by an exchange, + * including signature (so we have proof). + * + * @param cls closure + * @param master_pub public key of the exchange + * @param wire_method the wire method + * @param contract_date date of the contract to use for the lookup + * @param[out] fees wire fees charged + * @param[out] start_date start of fee being used + * @param[out] end_date end of fee being used + * @param[out] master_sig signature of exchange over fee structure + * @return transaction status code + */ +enum GNUNET_DB_QueryStatus +TMH_PG_lookup_wire_fee (void *cls, + const struct TALER_MasterPublicKeyP *master_pub, + const char *wire_method, + struct GNUNET_TIME_Timestamp contract_date, + struct TALER_WireFeeSet *fees, + struct GNUNET_TIME_Timestamp *start_date, + struct GNUNET_TIME_Timestamp *end_date, + struct TALER_MasterSignatureP *master_sig); + +#endif diff --git a/src/backenddb/pg_mark_order_wired.c b/src/backenddb/pg_mark_order_wired.c new file mode 100644 index 00000000..fde1ecc7 --- /dev/null +++ b/src/backenddb/pg_mark_order_wired.c @@ -0,0 +1,48 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_mark_order_wired.c + * @brief Implementation of the mark_order_wired function for Postgres + * @author Iván Ávalos + */ +#include "platform.h" +#include +#include +#include +#include "pg_mark_order_wired.h" +#include "pg_helper.h" + +enum GNUNET_DB_QueryStatus +TMH_PG_mark_order_wired (void *cls, + uint64_t order_serial) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_uint64 (&order_serial), + GNUNET_PQ_query_param_end + }; + + check_connection (pg); + PREPARE (pg, + "mark_order_wired", + "UPDATE merchant_contract_terms SET" + " wired=TRUE" + " WHERE order_serial=$1"); + + return GNUNET_PQ_eval_prepared_non_select (pg->conn, + "mark_order_wired", + params); +} diff --git a/src/backenddb/pg_mark_order_wired.h b/src/backenddb/pg_mark_order_wired.h new file mode 100644 index 00000000..dd7cc97f --- /dev/null +++ b/src/backenddb/pg_mark_order_wired.h @@ -0,0 +1,39 @@ +/* + This file is part of TALER + Copyright (C) 2023 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 + */ +/** + * @file backenddb/pg_mark_order_wired.h + * @brief implementation of the mark_order_wired function for Postgres + * @author Iván Ávalos + */ +#ifndef PG_MARK_ORDER_WIRED_H +#define PG_MARK_ORDER_WIRED_H + +#include +#include +#include "taler_merchantdb_plugin.h" + +/** + * Set 'wired' status for an order to 'true'. + * + * @param cls closure + * @param order_serial serial number of the order + * @return transaction status + */ +enum GNUNET_DB_QueryStatus +TMH_PG_mark_order_wired (void *cls, + uint64_t order_serial); + +#endif diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index c2c99cd1..214902dd 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -97,6 +97,21 @@ #include "pg_lookup_order_status.h" #include "pg_lookup_order_status_by_serial.h" #include "pg_lookup_payment_status.h" +#include "pg_lookup_deposits_by_order.h" +#include "pg_lookup_transfer_details_by_order.h" +#include "pg_mark_order_wired.h" +#include "pg_lookup_refunds_detailed.h" +#include "pg_insert_refund_proof.h" +#include "pg_lookup_refund_proof.h" +#include "pg_lookup_order_by_fulfillment.h" +#include "pg_delete_transfer.h" +#include "pg_check_transfer_exists.h" +#include "pg_lookup_account.h" +#include "pg_lookup_wire_fee.h" +#include "pg_lookup_deposits_by_contract_and_coin.h" +#include "pg_lookup_transfer.h" +#include "pg_lookup_transfer_summary.h" +#include "pg_lookup_transfer_details.h" #include "pg_set_transfer_status_to_confirmed.h" #include "pg_insert_exchange_keys.h" #include "pg_select_exchange_keys.h" @@ -260,1186 +275,6 @@ check_connection (struct PostgresClosure *pg) } -/** - * Closure for lookup_deposits_by_order_cb(). - */ -struct LookupDepositsByOrderContext -{ - - /** - * Plugin context. - */ - struct PostgresClosure *pg; - - /** - * Function to call with all results. - */ - TALER_MERCHANTDB_DepositedCoinsCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Set to the query result. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct LookupDepositsByOrderContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_deposits_by_order_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupDepositsByOrderContext *ldoc = cls; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - ldoc->cb (ldoc->cb_cls, - deposit_serial, - exchange_url, - &h_wire, - &amount_with_fee, - &deposit_fee, - &coin_pub); - GNUNET_PQ_cleanup_result (rs); /* technically useless here */ - } - ldoc->qs = num_results; -} - - -/** - * Retrieve details about coins that were deposited for an order. - * - * @param cls closure - * @param order_serial identifies the order - * @param cb function to call for each deposited coin - * @param cb_cls closure for @a cb - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_deposits_by_order (void *cls, - uint64_t order_serial, - TALER_MERCHANTDB_DepositedCoinsCallback cb, - void *cb_cls) -{ - struct PostgresClosure *pg = cls; - struct LookupDepositsByOrderContext ldoc = { - .pg = pg, - .cb = cb, - .cb_cls = cb_cls - }; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&order_serial), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "lookup_deposits_by_order", - params, - &lookup_deposits_by_order_cb, - &ldoc); - if (qs < 0) - return qs; - return ldoc.qs; -} - - -/** - * Closure for lookup_deposits_by_order_cb(). - */ -struct LookupTransferDetailsByOrderContext -{ - - /** - * Plugin context. - */ - struct PostgresClosure *pg; - - /** - * Function to call with all results. - */ - TALER_MERCHANTDB_OrderTransferDetailsCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Set to the query result. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct LookupTransferDetailsByOrderContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_transfer_details_by_order_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupTransferDetailsByOrderContext *ltdo = cls; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - ltdo->cb (ltdo->cb_cls, - &wtid, - exchange_url, - execution_time, - &deposit_value, - &deposit_fee, - (0 != transfer_confirmed)); - GNUNET_PQ_cleanup_result (rs); /* technically useless here */ - } - ltdo->qs = num_results; -} - - -/** - * Retrieve wire transfer details for all deposits associated with - * a given @a order_serial. - * - * @param cls closure - * @param order_serial identifies the order - * @param cb function called with the wire transfer details - * @param cb_cls closure for @a cb - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_transfer_details_by_order ( - void *cls, - uint64_t order_serial, - TALER_MERCHANTDB_OrderTransferDetailsCallback cb, - void *cb_cls) -{ - struct PostgresClosure *pg = cls; - struct LookupTransferDetailsByOrderContext ltdo = { - .pg = pg, - .cb = cb, - .cb_cls = cb_cls - }; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&order_serial), - GNUNET_PQ_query_param_end - }; - enum GNUNET_DB_QueryStatus qs; - - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - "lookup_transfer_details_by_order", - params, - &lookup_transfer_details_by_order_cb, - <do); - if (qs < 0) - return qs; - return ltdo.qs; -} - - -/** - * Set 'wired' status for an order to 'true'. - * - * @param cls closure - * @param order_serial serial number of the order - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_mark_order_wired (void *cls, - uint64_t order_serial) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&order_serial), - GNUNET_PQ_query_param_end - }; - - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "mark_order_wired", - params); -} - - -/** - * Closure for #lookup_refunds_detailed_cb(). - */ -struct LookupRefundsDetailedContext -{ - /** - * Function to call for each refund. - */ - TALER_MERCHANTDB_RefundDetailCallback rc; - - /** - * Closure for @e rc. - */ - void *rc_cls; - - /** - * Plugin context. - */ - struct PostgresClosure *pg; - - /** - * Transaction result. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct GetRefundsContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_refunds_detailed_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupRefundsDetailedContext *lrdc = cls; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - lrdc->rc (lrdc->rc_cls, - refund_serial, - timestamp, - &coin_pub, - exchange_url, - rtransaction_id, - reason, - &refund_amount, - 0 != pending8); - GNUNET_PQ_cleanup_result (rs); - } - lrdc->qs = num_results; -} - - -/** - * Obtain detailed refund data associated with a contract. - * - * @param cls closure, typically a connection to the db - * @param instance_id instance to lookup refunds for - * @param h_contract_terms hash code of the contract - * @param rc function to call for each coin on which there is a refund - * @param rc_cls closure for @a rc - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_refunds_detailed ( - void *cls, - const char *instance_id, - const struct TALER_PrivateContractHashP *h_contract_terms, - TALER_MERCHANTDB_RefundDetailCallback rc, - void *rc_cls) -{ - 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_end - }; - struct LookupRefundsDetailedContext lrdc = { - .rc = rc, - .rc_cls = rc_cls, - .pg = pg - }; - enum GNUNET_DB_QueryStatus qs; - - /* no preflight check here, run in transaction by caller! */ - TALER_LOG_DEBUG ("Looking for refund %s + %s\n", - GNUNET_h2s (&h_contract_terms->hash), - instance_id); - check_connection (pg); - qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, - "lookup_refunds_detailed", - params, - &lookup_refunds_detailed_cb, - &lrdc); - if (0 >= qs) - return qs; - return lrdc.qs; -} - - -/** - * Insert refund proof data from the exchange into the database. - * - * @param cls closure - * @param refund_serial serial number of the refund - * @param exchange_sig signature from exchange that coin was refunded - * @param exchange_pub signing key that was used for @a exchange_sig - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_insert_refund_proof ( - void *cls, - uint64_t refund_serial, - 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_uint64 (&refund_serial), - GNUNET_PQ_query_param_auto_from_type (exchange_sig), - GNUNET_PQ_query_param_auto_from_type (exchange_pub), - GNUNET_PQ_query_param_end - }; - - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "insert_refund_proof", - params); -} - - -/** - * Lookup refund proof data. - * - * @param cls closure - * @param refund_serial serial number of the refund - * @param[out] exchange_sig set to signature from exchange - * @param[out] exchange_pub signing key that was used for @a exchange_sig - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_refund_proof (void *cls, - uint64_t refund_serial, - struct TALER_ExchangeSignatureP *exchange_sig, - struct TALER_ExchangePublicKeyP *exchange_pub) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_uint64 (&refund_serial), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_auto_from_type ("exchange_sig", - exchange_sig), - GNUNET_PQ_result_spec_auto_from_type ("exchange_pub", - exchange_pub), - GNUNET_PQ_result_spec_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_refund_proof", - params, - rs); -} - - -/** - * Retrieve the order ID that was used to pay for a resource within a session. - * - * @param cls closure - * @param instance_id identifying the instance - * @param fulfillment_url URL that canonically identifies the resource - * being paid for - * @param session_id session id - * @param[out] order_id where to store the order ID that was used when - * paying for the resource URL - * @return transaction status - */ -enum GNUNET_DB_QueryStatus -postgres_lookup_order_by_fulfillment (void *cls, - const char *instance_id, - const char *fulfillment_url, - const char *session_id, - char **order_id) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_string (fulfillment_url), - GNUNET_PQ_query_param_string (session_id), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_string ("order_id", - order_id), - GNUNET_PQ_result_spec_end - }; - - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_order_by_fulfillment", - params, - rs); -} - - -/** - * Delete information about a transfer. Note that transfers - * confirmed by the exchange cannot be deleted anymore. - * - * @param cls closure - * @param instance_id instance to delete transfer of - * @param transfer_serial_id transfer to delete - * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS - * if deletion is prohibited OR transfer is unknown - */ -static enum GNUNET_DB_QueryStatus -postgres_delete_transfer (void *cls, - const char *instance_id, - uint64_t transfer_serial_id) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_uint64 (&transfer_serial_id), - GNUNET_PQ_query_param_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_non_select (pg->conn, - "delete_transfer", - params); -} - - -/** - * Check if information about a transfer exists with the - * backend. Returns no data, only the query status. - * - * @param cls closure - * @param instance_id instance to delete transfer of - * @param transfer_serial_id transfer to delete - * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT - * if the transfer record exists - */ -static enum GNUNET_DB_QueryStatus -postgres_check_transfer_exists (void *cls, - const char *instance_id, - uint64_t transfer_serial_id) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_uint64 (&transfer_serial_id), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "check_transfer_exists", - params, - rs); -} - - -/** - * Lookup account serial by payto URI. - * - * @param cls closure - * @param instance_id instance to lookup the account from - * @param payto_uri what is the merchant's bank account to lookup - * @param[out] account_serial serial number of the account - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_account (void *cls, - const char *instance_id, - const char *payto_uri, - uint64_t *account_serial) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_string (payto_uri), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_uint64 ("account_serial", - account_serial), - GNUNET_PQ_result_spec_end - }; - - check_connection (pg); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_account", - params, - rs); -} - - -/** - * Obtain information about wire fees charged by an exchange, - * including signature (so we have proof). - * - * @param cls closure - * @param master_pub public key of the exchange - * @param wire_method the wire method - * @param contract_date date of the contract to use for the lookup - * @param[out] fees wire fees charged - * @param[out] start_date start of fee being used - * @param[out] end_date end of fee being used - * @param[out] master_sig signature of exchange over fee structure - * @return transaction status code - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_wire_fee (void *cls, - const struct TALER_MasterPublicKeyP *master_pub, - const char *wire_method, - struct GNUNET_TIME_Timestamp contract_date, - struct TALER_WireFeeSet *fees, - struct GNUNET_TIME_Timestamp *start_date, - struct GNUNET_TIME_Timestamp *end_date, - struct TALER_MasterSignatureP *master_sig) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_HashCode h_wire_method; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_auto_from_type (master_pub), - GNUNET_PQ_query_param_auto_from_type (&h_wire_method), - GNUNET_PQ_query_param_timestamp (&contract_date), - GNUNET_PQ_query_param_end - }; - struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_result_spec_amount_with_currency ("wire_fee", - &fees->wire), - TALER_PQ_result_spec_amount_with_currency ("closing_fee", - &fees->closing), - GNUNET_PQ_result_spec_timestamp ("start_date", - start_date), - GNUNET_PQ_result_spec_timestamp ("end_date", - end_date), - GNUNET_PQ_result_spec_auto_from_type ("master_sig", - master_sig), - GNUNET_PQ_result_spec_end - }; - - check_connection (pg); - GNUNET_CRYPTO_hash (wire_method, - strlen (wire_method) + 1, - &h_wire_method); - return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_wire_fee", - params, - rs); -} - - -/** - * Closure for #lookup_deposits_by_contract_and_coin_cb(). - */ -struct LookupDepositsByCnCContext -{ - /** - * Function to call for each deposit. - */ - TALER_MERCHANTDB_CoinDepositCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Plugin context. - */ - struct PostgresClosure *pg; - - /** - * Transaction result. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct LookupDepositsByCnCContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_deposits_by_contract_and_coin_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupDepositsByCnCContext *ldcc = cls; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - ldcc->cb (ldcc->cb_cls, - exchange_url, - &amount_with_fee, - &deposit_fee, - &refund_fee, - &wire_fee, - &h_wire, - deposit_timestamp, - refund_deadline, - &exchange_sig, - &exchange_pub); - GNUNET_PQ_cleanup_result (rs); - } - ldcc->qs = num_results; -} - - -/** - * Lookup information about coin payments by @a h_contract_terms and - * @a coin_pub. - * - * @param cls closure - * @param instance_id instance to lookup payments for - * @param h_contract_terms proposal data's hashcode - * @param coin_pub public key to use for the search - * @param cb function to call with payment data - * @param cb_cls closure for @a cb - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_deposits_by_contract_and_coin ( - void *cls, - const char *instance_id, - const struct TALER_PrivateContractHashP *h_contract_terms, - const struct TALER_CoinSpendPublicKeyP *coin_pub, - TALER_MERCHANTDB_CoinDepositCallback cb, - void *cb_cls) -{ - 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_auto_from_type (coin_pub), - GNUNET_PQ_query_param_end - }; - struct LookupDepositsByCnCContext ldcc = { - .cb = cb, - .cb_cls = cb_cls, - .pg = pg - }; - enum GNUNET_DB_QueryStatus qs; - - check_connection (pg); - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - "lookup_deposits_by_contract_and_coin", - params, - &lookup_deposits_by_contract_and_coin_cb, - &ldcc); - if (0 >= qs) - return qs; - return ldcc.qs; -} - - -/** - * Lookup transfer status. - * - * @param cls closure - * @param instance_id at which instance should we resolve the transfer - * @param exchange_url the exchange that made the transfer - * @param wtid wire transfer subject - * @param[out] total_amount amount that was debited from our - * aggregate balance at the exchange (in total, sum of - * the wire transfer amount and the @a wire_fee) - * @param[out] wire_fee the wire fee the exchange charged (only set if @a have_exchange_sig is true) - * @param[out] exchange_amount the amount the exchange claims was transferred (only set if @a have_exchange_sig is true) - * @param[out] execution_time when the transfer was executed by the exchange (only set if @a have_exchange_sig is true) - * @param[out] have_exchange_sig do we have a response from the exchange about this transfer - * @param[out] verified did we confirm the transfer was OK - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_transfer ( - void *cls, - const char *instance_id, - const char *exchange_url, - const struct TALER_WireTransferIdentifierRawP *wtid, - struct TALER_Amount *total_amount, - struct TALER_Amount *wire_fee, - struct TALER_Amount *exchange_amount, - struct GNUNET_TIME_Timestamp *execution_time, - bool *have_exchange_sig, - bool *verified) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (exchange_url), - GNUNET_PQ_query_param_auto_from_type (wtid), - GNUNET_PQ_query_param_string (instance_id), - GNUNET_PQ_query_param_end - }; - uint8_t verified8; - /** Amount we got actually credited, _excludes_ the wire fee */ - bool no_sig; - struct TALER_Amount credit_amount; - struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_result_spec_amount_with_currency ("credit_amount", - &credit_amount), - GNUNET_PQ_result_spec_allow_null ( - TALER_PQ_result_spec_amount_with_currency ("wire_fee", - wire_fee), - &no_sig), - GNUNET_PQ_result_spec_allow_null ( - TALER_PQ_result_spec_amount_with_currency ("exchange_amount", - exchange_amount), - NULL), - GNUNET_PQ_result_spec_allow_null ( - GNUNET_PQ_result_spec_timestamp ("execution_time", - execution_time), - NULL), - GNUNET_PQ_result_spec_auto_from_type ("verified", - &verified8), - GNUNET_PQ_result_spec_end - }; - enum GNUNET_DB_QueryStatus qs; - - check_connection (pg); - *execution_time = GNUNET_TIME_UNIT_ZERO_TS; - qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, - "lookup_transfer", - params, - rs); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Lookup transfer returned %d\n", - qs); - if (qs > 0) - { - *have_exchange_sig = ! no_sig; - *verified = (0 != verified8); - if (GNUNET_OK != - TALER_amount_cmp_currency (&credit_amount, - wire_fee)) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } - if ( (! no_sig) && - (0 > - TALER_amount_add (total_amount, - &credit_amount, - wire_fee)) ) - { - GNUNET_break (0); - return GNUNET_DB_STATUS_HARD_ERROR; - } - } - else - { - *verified = false; - *have_exchange_sig = false; - } - return qs; -} - - -/** - * Closure for #lookup_transfer_summary_cb(). - */ -struct LookupTransferSummaryContext -{ - /** - * Function to call for each order that was aggregated. - */ - TALER_MERCHANTDB_TransferSummaryCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Plugin context. - */ - struct PostgresClosure *pg; - - /** - * Transaction result. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct LookupTransferSummaryContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_transfer_summary_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupTransferSummaryContext *ltdc = cls; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - ltdc->cb (ltdc->cb_cls, - order_id, - &deposit_value, - &deposit_fee); - GNUNET_PQ_cleanup_result (rs); - } - ltdc->qs = num_results; -} - - -/** - * Lookup transfer summary. - * - * @param cls closure - * @param exchange_url the exchange that made the transfer - * @param wtid wire transfer subject - * @param cb function to call with detailed transfer data - * @param cb_cls closure for @a cb - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_transfer_summary ( - void *cls, - const char *exchange_url, - const struct TALER_WireTransferIdentifierRawP *wtid, - TALER_MERCHANTDB_TransferSummaryCallback cb, - void *cb_cls) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (exchange_url), - GNUNET_PQ_query_param_auto_from_type (wtid), - GNUNET_PQ_query_param_end - }; - struct LookupTransferSummaryContext ltdc = { - .cb = cb, - .cb_cls = cb_cls, - .pg = pg - }; - enum GNUNET_DB_QueryStatus qs; - - check_connection (pg); - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - "lookup_transfer_summary", - params, - &lookup_transfer_summary_cb, - <dc); - if (0 >= qs) - return qs; - return ltdc.qs; -} - - -/** - * Closure for #lookup_transfer_details_cb(). - */ -struct LookupTransferDetailsContext -{ - /** - * Function to call for each order that was aggregated. - */ - TALER_MERCHANTDB_TransferDetailsCallback cb; - - /** - * Closure for @e cb. - */ - void *cb_cls; - - /** - * Plugin context. - */ - struct PostgresClosure *pg; - - /** - * Transaction result. - */ - enum GNUNET_DB_QueryStatus qs; -}; - - -/** - * Function to be called with the results of a SELECT statement - * that has returned @a num_results results. - * - * @param cls of type `struct LookupTransferDetailsContext *` - * @param result the postgres result - * @param num_results the number of results in @a result - */ -static void -lookup_transfer_details_cb (void *cls, - PGresult *result, - unsigned int num_results) -{ - struct LookupTransferDetailsContext *ltdc = cls; - - for (unsigned int i = 0; iqs = GNUNET_DB_STATUS_HARD_ERROR; - return; - } - ltdc->cb (ltdc->cb_cls, - (unsigned int) current_offset, - &ttd); - GNUNET_PQ_cleanup_result (rs); - } - ltdc->qs = num_results; -} - - -/** - * Lookup transfer details. - * - * @param cls closure - * @param exchange_url the exchange that made the transfer - * @param wtid wire transfer subject - * @param cb function to call with detailed transfer data - * @param cb_cls closure for @a cb - * @return transaction status - */ -static enum GNUNET_DB_QueryStatus -postgres_lookup_transfer_details ( - void *cls, - const char *exchange_url, - const struct TALER_WireTransferIdentifierRawP *wtid, - TALER_MERCHANTDB_TransferDetailsCallback cb, - void *cb_cls) -{ - struct PostgresClosure *pg = cls; - struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_string (exchange_url), - GNUNET_PQ_query_param_auto_from_type (wtid), - GNUNET_PQ_query_param_end - }; - struct LookupTransferDetailsContext ltdc = { - .cb = cb, - .cb_cls = cb_cls, - .pg = pg - }; - enum GNUNET_DB_QueryStatus qs; - - check_connection (pg); - qs = GNUNET_PQ_eval_prepared_multi_select ( - pg->conn, - "lookup_transfer_details", - params, - &lookup_transfer_details_cb, - <dc); - if (0 >= qs) - return qs; - return ltdc.qs; -} - - /** * Closure for #lookup_pending_reserves_cb. */ @@ -3065,235 +1900,6 @@ postgres_connect (void *cls) struct GNUNET_PQ_PreparedStatement ps[] = { GNUNET_PQ_make_prepare ("end_transaction", "COMMIT"), - /* for postgres_lookup_deposits_by_order() */ - GNUNET_PQ_make_prepare ("lookup_deposits_by_order", - "SELECT" - " dep.deposit_serial" - ",mcon.exchange_url" - ",acc.h_wire" - ",dep.amount_with_fee" - ",dep.deposit_fee" - ",dep.coin_pub" - " FROM merchant_deposits dep" - " JOIN merchant_deposit_confirmations mcon" - " USING(deposit_confirmation_serial)" - " JOIN merchant_accounts acc" - " USING (account_serial)" - " WHERE mcon.order_serial=$1"), - /* for postgres_lookup_transfer_details_by_order() */ - GNUNET_PQ_make_prepare ("lookup_transfer_details_by_order", - "SELECT" - " md.deposit_serial" - ",mcon.exchange_url" - ",mt.wtid" - ",mtc.exchange_deposit_value" - ",mtc.exchange_deposit_fee" - ",mcon.deposit_timestamp" - ",mt.confirmed AS transfer_confirmed" - " FROM merchant_transfer_to_coin mtc" - " JOIN merchant_deposits md" - " USING (deposit_serial)" - " JOIN merchant_deposit_confirmations mcon" - " USING (deposit_confirmation_serial)" - " JOIN merchant_transfers mt" - " USING (credit_serial)" - " JOIN merchant_accounts acc" - " ON (acc.account_serial = mt.account_serial)" - /* Check that all this is for the same instance */ - " JOIN merchant_contract_terms contracts" - " USING (merchant_serial, order_serial)" - " WHERE mcon.order_serial=$1"), - /* for postgres_mark_order_wired() */ - GNUNET_PQ_make_prepare ("mark_order_wired", - "UPDATE merchant_contract_terms SET" - " wired=TRUE" - " WHERE order_serial=$1"), - /* for postgres_lookup_refunds_detailed() */ - GNUNET_PQ_make_prepare ("lookup_refunds_detailed", - "SELECT" - " ref.refund_serial" - ",ref.refund_timestamp" - ",dep.coin_pub" - ",mcon.exchange_url" - ",ref.rtransaction_id" - ",ref.reason" - ",ref.refund_amount" - ",merchant_refund_proofs.exchange_sig IS NULL AS pending" - " FROM merchant_deposit_confirmations mcon" - " JOIN merchant_deposits dep" - " USING (deposit_confirmation_serial)" - " JOIN merchant_refunds ref" - " USING (order_serial, coin_pub)" - " LEFT JOIN merchant_refund_proofs" - " USING (refund_serial)" - " WHERE mcon.order_serial=" - " (SELECT order_serial" - " FROM merchant_contract_terms" - " WHERE h_contract_terms=$2" - " AND merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1))"), - /* for postgres_insert_refund_proof() */ - GNUNET_PQ_make_prepare ("insert_refund_proof", - "INSERT INTO merchant_refund_proofs" - "(refund_serial" - ",exchange_sig" - ",signkey_serial)" - "SELECT $1, $2, signkey_serial" - " FROM merchant_exchange_signing_keys" - " WHERE exchange_pub=$3" - " ORDER BY start_date DESC" - " LIMIT 1"), - /* for postgres_lookup_refund_proof() */ - GNUNET_PQ_make_prepare ("lookup_refund_proof", - "SELECT" - " merchant_exchange_signing_keys.exchange_pub" - ",exchange_sig" - " FROM merchant_refund_proofs" - " JOIN merchant_exchange_signing_keys" - " USING (signkey_serial)" - " WHERE" - " refund_serial=$1"), - /* for postgres_lookup_order_by_fulfillment() */ - GNUNET_PQ_make_prepare ("lookup_order_by_fulfillment", - "SELECT" - " order_id" - " FROM merchant_contract_terms" - " WHERE fulfillment_url=$2" - " AND session_id=$3" - " AND merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)"), - /* for postgres_delete_transfer() */ - GNUNET_PQ_make_prepare ("delete_transfer", - "DELETE FROM merchant_transfers" - " WHERE" - " credit_serial=$2" - " AND account_serial IN " - " (SELECT account_serial " - " FROM merchant_accounts" - " WHERE merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1))"), - /* for postgres_check_transfer_exists() */ - GNUNET_PQ_make_prepare ("check_transfer_exists", - "SELECT" - " 1" - " FROM merchant_transfers" - " JOIN merchant_accounts" - " USING (account_serial)" - " WHERE" - " credit_serial=$2" - " AND" - " merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)"), - /* for postgres_lookup_account() */ - GNUNET_PQ_make_prepare ("lookup_account", - "SELECT" - " account_serial" - " FROM merchant_accounts" - " WHERE payto_uri=$2" - " AND merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)"), - /* for postgres_lookup_wire_fee() */ - GNUNET_PQ_make_prepare ("lookup_wire_fee", - "SELECT" - " wire_fee" - ",closing_fee" - ",start_date" - ",end_date" - ",master_sig" - " FROM merchant_exchange_wire_fees" - " WHERE master_pub=$1" - " AND h_wire_method=$2" - " AND start_date <= $3" - " AND end_date > $3"), - /* for postgres_lookup_deposits_by_contract_and_coin() */ - GNUNET_PQ_make_prepare ("lookup_deposits_by_contract_and_coin", - "SELECT" - " mcon.exchange_url" - ",dep.amount_with_fee" - ",dep.deposit_fee" - ",dep.refund_fee" - ",mcon.wire_fee" - ",acc.h_wire" - ",mcon.deposit_timestamp" - ",mct.refund_deadline" - ",mcon.exchange_sig" - ",msig.exchange_pub" - " FROM merchant_contract_terms mct" - " JOIN merchant_deposit_confirmations mcon" - " USING (order_serial)" - " JOIN merchant_deposits dep" - " USING (deposit_confirmation_serial)" - " JOIN merchant_exchange_signing_keys msig" - " USING (signkey_serial)" - " JOIN merchant_accounts acc" - " USING (account_serial)" - " WHERE h_contract_terms=$2" - " AND dep.coin_pub=$3" - " AND mct.merchant_serial=" - " (SELECT merchant_serial" - " FROM merchant_instances" - " WHERE merchant_id=$1)"), - /* for postgres_lookup_transfer() */ - GNUNET_PQ_make_prepare ("lookup_transfer", - "SELECT" - " mt.credit_amount AS credit_amount" - ",mts.credit_amount AS exchange_amount" - ",wire_fee" - ",execution_time" - ",verified" - " FROM merchant_transfers mt" - " JOIN merchant_accounts USING (account_serial)" - " JOIN merchant_instances USING (merchant_serial)" - " LEFT JOIN merchant_transfer_signatures mts USING (credit_serial)" - " WHERE wtid=$2" - " AND exchange_url=$1" - " AND merchant_id=$3;"), - /* for postgres_lookup_transfer_summary() */ - GNUNET_PQ_make_prepare ("lookup_transfer_summary", - "SELECT" - " mct.order_id" - ",mtc.exchange_deposit_value" - ",mtc.exchange_deposit_fee" - " FROM merchant_transfers mtr" - " JOIN merchant_transfer_to_coin mtc" - " USING (credit_serial)" - " JOIN merchant_deposits dep" - " USING (deposit_serial)" - " JOIN merchant_deposit_confirmations mcon" - " USING (deposit_confirmation_serial)" - " JOIN merchant_contract_terms mct" - " USING (order_serial)" - " WHERE mtr.wtid=$2" - " AND mtr.exchange_url=$1"), - /* for postgres_lookup_transfer_details() */ - GNUNET_PQ_make_prepare ("lookup_transfer_details", - "SELECT" - " mterm.h_contract_terms" - ",mtcoin.offset_in_exchange_list" - ",dep.coin_pub" - ",mtcoin.exchange_deposit_value" - ",mtcoin.exchange_deposit_fee" - " FROM merchant_transfer_to_coin mtcoin" - " JOIN merchant_deposits dep" - " USING (deposit_serial)" - " JOIN merchant_deposit_confirmations mcon" - " USING (deposit_confirmation_serial)" - " JOIN merchant_contract_terms mterm" - " USING (order_serial)" - " JOIN merchant_transfers mtr" - " USING (credit_serial)" - " WHERE mtr.wtid=$2" - " AND mtr.exchange_url=$1"), /* For postgres_insert_reserve() */ GNUNET_PQ_make_prepare ("insert_reserve_key", "INSERT INTO merchant_reward_reserve_keys" @@ -3838,27 +2444,40 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) = &TMH_PG_lookup_order_status_by_serial; plugin->lookup_payment_status = &TMH_PG_lookup_payment_status; - plugin->lookup_deposits_by_order = &postgres_lookup_deposits_by_order; - plugin->lookup_transfer_details_by_order = - &postgres_lookup_transfer_details_by_order; - plugin->mark_order_wired = &postgres_mark_order_wired; + plugin->lookup_deposits_by_order + = &TMH_PG_lookup_deposits_by_order; + plugin->lookup_transfer_details_by_order + = &TMH_PG_lookup_transfer_details_by_order; + plugin->mark_order_wired + = &TMH_PG_mark_order_wired; plugin->increase_refund = &TMH_PG_increase_refund; - plugin->lookup_refunds_detailed = &postgres_lookup_refunds_detailed; - plugin->insert_refund_proof = &postgres_insert_refund_proof; - plugin->lookup_refund_proof = &postgres_lookup_refund_proof; - plugin->lookup_order_by_fulfillment = &postgres_lookup_order_by_fulfillment; - plugin->delete_transfer = &postgres_delete_transfer; - plugin->check_transfer_exists = &postgres_check_transfer_exists; - plugin->lookup_account = &postgres_lookup_account; - plugin->lookup_wire_fee = &postgres_lookup_wire_fee; - plugin->lookup_deposits_by_contract_and_coin = - &postgres_lookup_deposits_by_contract_and_coin; - plugin->lookup_transfer = &postgres_lookup_transfer; - plugin->set_transfer_status_to_confirmed = - &TMH_PG_set_transfer_status_to_confirmed; - plugin->lookup_transfer_summary = &postgres_lookup_transfer_summary; - plugin->lookup_transfer_details = &postgres_lookup_transfer_details; + plugin->lookup_refunds_detailed + = &TMH_PG_lookup_refunds_detailed; + plugin->insert_refund_proof + = &TMH_PG_insert_refund_proof; + plugin->lookup_refund_proof + = &TMH_PG_lookup_refund_proof; + plugin->lookup_order_by_fulfillment + = &TMH_PG_lookup_order_by_fulfillment; + plugin->delete_transfer + = &TMH_PG_delete_transfer; + plugin->check_transfer_exists + = &TMH_PG_check_transfer_exists; + plugin->lookup_account + = &TMH_PG_lookup_account; + plugin->lookup_wire_fee + = &TMH_PG_lookup_wire_fee; + plugin->lookup_deposits_by_contract_and_coin + = &TMH_PG_lookup_deposits_by_contract_and_coin; + plugin->lookup_transfer + = &TMH_PG_lookup_transfer; + plugin->set_transfer_status_to_confirmed + = &TMH_PG_set_transfer_status_to_confirmed; + plugin->lookup_transfer_summary + = &TMH_PG_lookup_transfer_summary; + plugin->lookup_transfer_details + = &TMH_PG_lookup_transfer_details; plugin->lookup_instances = &TMH_PG_lookup_instances; plugin->lookup_instance -- cgit v1.2.3