summaryrefslogtreecommitdiff
path: root/src/backenddb
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2023-04-23 17:45:53 +0200
committerChristian Grothoff <christian@grothoff.org>2023-04-23 17:45:53 +0200
commit889595f986d922ffbcdcd746fdfad7f1a0e53595 (patch)
tree41571e0f04caa95c06610440d2b8c9c3698b08ed /src/backenddb
parent4b7d9f5e19e9f28ec1163f3b6e5c00f0805e6221 (diff)
downloadmerchant-889595f986d922ffbcdcd746fdfad7f1a0e53595.tar.gz
merchant-889595f986d922ffbcdcd746fdfad7f1a0e53595.tar.bz2
merchant-889595f986d922ffbcdcd746fdfad7f1a0e53595.zip
make taler-merchant-wirewatch multi-instance capable
Diffstat (limited to 'src/backenddb')
-rw-r--r--src/backenddb/Makefile.am3
-rw-r--r--src/backenddb/merchant-0005.sql19
-rw-r--r--src/backenddb/pg_insert_wirewatch_progress.c53
-rw-r--r--src/backenddb/pg_insert_wirewatch_progress.h44
-rw-r--r--src/backenddb/pg_lookup_instances.c31
-rw-r--r--src/backenddb/pg_select_wirewatch_accounts.c147
-rw-r--r--src/backenddb/pg_select_wirewatch_accounts.h (renamed from src/backenddb/pg_select_wirewatch_progress.h)18
-rw-r--r--src/backenddb/pg_select_wirewatch_progress.c56
-rw-r--r--src/backenddb/pg_update_wirewatch_progress.c16
-rw-r--r--src/backenddb/pg_update_wirewatch_progress.h8
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c13
11 files changed, 216 insertions, 192 deletions
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am
index 01947a81..189b7a32 100644
--- a/src/backenddb/Makefile.am
+++ b/src/backenddb/Makefile.am
@@ -52,9 +52,8 @@ libtalermerchantdb_la_LDFLAGS = \
-no-undefined
libtaler_plugin_merchantdb_postgres_la_SOURCES = \
- pg_insert_wirewatch_progress.h pg_insert_wirewatch_progress.c \
pg_update_wirewatch_progress.h pg_update_wirewatch_progress.c \
- pg_select_wirewatch_progress.h pg_select_wirewatch_progress.c \
+ pg_select_wirewatch_accounts.h pg_select_wirewatch_accounts.c \
pg_lookup_instances.h pg_lookup_instances.c \
pg_lookup_transfers.h pg_lookup_transfers.c \
plugin_merchantdb_postgres.c pg_helper.h
diff --git a/src/backenddb/merchant-0005.sql b/src/backenddb/merchant-0005.sql
index f558cbd2..1f3b9131 100644
--- a/src/backenddb/merchant-0005.sql
+++ b/src/backenddb/merchant-0005.sql
@@ -29,15 +29,16 @@ COMMENT ON COLUMN merchant_instances.user_type
IS 'what type of user is this (individual or business)';
-CREATE TABLE IF NOT EXISTS merchant_wirewatch
- (account_section VARCHAR PRIMARY KEY
- ,last_bank_serial INT8 NOT NULL
- );
-COMMENT ON TABLE merchant_wirewatch
- IS 'table used to keep track of progress made by the taler-merchant-wirewatch tool';
-COMMENT ON COLUMN merchant_wirewatch.account_section
- IS 'Name of the configuration section that specifies the bank account details and merchant instance being tracked here';
-COMMENT ON COLUMN merchant_wirewatch.last_bank_serial
+ALTER TABLE merchant_accounts
+ ADD COLUMN credit_facade_url VARCHAR,
+ ADD COLUMN credit_facade_credentials VARCHAR,
+ ADD COLUMN last_bank_serial INT8 NOT NULL DEFAULT (0);
+
+COMMENT ON COLUMN merchant_accounts.credit_facade_url
+ IS 'Base URL of a facade where the merchant can inquire about incoming bank transactions into this account';
+COMMENT ON COLUMN merchant_accounts.credit_facade_credentials
+ IS 'JSON with credentials needed to access the credit facade';
+COMMENT ON COLUMN merchant_accounts.last_bank_serial
IS 'Serial number of the bank of the last transaction we successfully imported';
-- Complete transaction
diff --git a/src/backenddb/pg_insert_wirewatch_progress.c b/src/backenddb/pg_insert_wirewatch_progress.c
deleted file mode 100644
index f4d1b0f8..00000000
--- a/src/backenddb/pg_insert_wirewatch_progress.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- This file is part of TALER
- Copyright (C) 2022 Taler Systems SA
-
- TALER is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
- */
-/**
- * @file backenddb/pg_insert_wirewatch_progress.c
- * @brief Implementation of the insert_wirewatch_progress function for Postgres
- * @author Christian Grothoff
- */
-#include "platform.h"
-#include <taler/taler_error_codes.h>
-#include <taler/taler_dbevents.h>
-#include <taler/taler_pq_lib.h>
-#include "pg_insert_wirewatch_progress.h"
-#include "pg_helper.h"
-
-
-enum GNUNET_DB_QueryStatus
-TMH_PG_insert_wirewatch_progress (
- void *cls,
- const char *section,
- uint64_t last_serial)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (section),
- GNUNET_PQ_query_param_uint64 (&last_serial),
- GNUNET_PQ_query_param_end
- };
-
- PREPARE (pg,
- "insert_wirewatch_progress",
- "INSERT INTO merchant_wirewatch"
- " (account_section"
- " ,last_bank_serial)"
- " VALUES"
- " ($1,$2);");
- check_connection (pg);
- return GNUNET_PQ_eval_prepared_non_select (pg->conn,
- "insert_wirewatch_progress",
- params);
-}
diff --git a/src/backenddb/pg_insert_wirewatch_progress.h b/src/backenddb/pg_insert_wirewatch_progress.h
deleted file mode 100644
index 4af11b9e..00000000
--- a/src/backenddb/pg_insert_wirewatch_progress.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- This file is part of TALER
- Copyright (C) 2022 Taler Systems SA
-
- TALER is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
- */
-/**
- * @file backenddb/pg_insert_wirewatch_progress.h
- * @brief implementation of the insert_wirewatch_progress function for Postgres
- * @author Christian Grothoff
- */
-#ifndef PG_INSERT_WIREWATCH_PROGRESS_H
-#define PG_INSERT_WIREWATCH_PROGRESS_H
-
-#include <taler/taler_util.h>
-#include <taler/taler_json_lib.h>
-#include "taler_merchantdb_plugin.h"
-
-
-/**
- * Insert information about progress made by taler-merchant-wirewatch.
- *
- * @param cls closure
- * @param section configuration section of the taler-merchant-wirewatch
- * @param last_serial last serial imported from the bank
- * @return transaction status
- */
-enum GNUNET_DB_QueryStatus
-TMH_PG_insert_wirewatch_progress (
- void *cls,
- const char *section,
- uint64_t last_serial);
-
-
-#endif
diff --git a/src/backenddb/pg_lookup_instances.c b/src/backenddb/pg_lookup_instances.c
index acf3ada3..d876ad69 100644
--- a/src/backenddb/pg_lookup_instances.c
+++ b/src/backenddb/pg_lookup_instances.c
@@ -1,6 +1,6 @@
/*
This file is part of TALER
- Copyright (C) 2022 Taler Systems SA
+ Copyright (C) 2022, 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
@@ -98,6 +98,8 @@ prepare (struct PostgresClosure *pg)
" h_wire"
",salt"
",payto_uri"
+ ",credit_facade_url"
+ ",credit_facade_credentials"
",active"
" FROM merchant_accounts"
" WHERE merchant_serial=$1");
@@ -165,7 +167,7 @@ call_with_accounts (struct LookupInstancesContext *lic,
* Function to be called with the results of a SELECT statement
* that has returned @a num_results results about accounts.
*
- * @param cls of type `struct FindInstancesContext *`
+ * @param cls of type `struct LookupInstancesContext *`
* @param result the postgres result
* @param num_results the number of results in @a result
*/
@@ -176,8 +178,16 @@ lookup_accounts_cb (void *cls,
{
struct LookupInstancesContext *lic = cls;
char *paytos[num_results];
+ char *facade_urls[num_results];
+ json_t *credentials[num_results];
struct TALER_MERCHANTDB_AccountDetails accounts[num_results];
+ memset (facade_urls,
+ 0,
+ sizeof (facade_urls));
+ memset (credentials,
+ 0,
+ sizeof (credentials));
/* Note: this memset is completely superfluous, but gcc-11 (and gcc-12) have
a bug creating a warning without it! See #7585 */
memset (accounts,
@@ -193,6 +203,14 @@ lookup_accounts_cb (void *cls,
&account->salt),
GNUNET_PQ_result_spec_string ("payto_uri",
&paytos[i]),
+ GNUNET_PQ_result_spec_allow_null (
+ GNUNET_PQ_result_spec_string ("credit_facade_url",
+ &facade_urls[i]),
+ NULL),
+ GNUNET_PQ_result_spec_allow_null (
+ TALER_PQ_result_spec_json ("credit_facade_credentials",
+ &credentials[i]),
+ NULL),
GNUNET_PQ_result_spec_bool ("active",
&account->active),
GNUNET_PQ_result_spec_end
@@ -206,7 +224,11 @@ lookup_accounts_cb (void *cls,
GNUNET_break (0);
lic->qs = GNUNET_DB_STATUS_HARD_ERROR;
for (unsigned int j = 0; j < i; j++)
+ {
GNUNET_free (paytos[j]);
+ GNUNET_free (facade_urls[j]);
+ json_decref (credentials[j]);
+ }
return;
}
account->payto_uri = paytos[i];
@@ -215,7 +237,11 @@ lookup_accounts_cb (void *cls,
num_results,
accounts);
for (unsigned int i = 0; i < num_results; i++)
+ {
GNUNET_free (paytos[i]);
+ GNUNET_free (facade_urls[i]);
+ json_decref (credentials[i]);
+ }
}
@@ -442,4 +468,3 @@ TMH_PG_lookup_instance (void *cls,
return lic.qs;
return qs;
}
-
diff --git a/src/backenddb/pg_select_wirewatch_accounts.c b/src/backenddb/pg_select_wirewatch_accounts.c
new file mode 100644
index 00000000..f3a7e789
--- /dev/null
+++ b/src/backenddb/pg_select_wirewatch_accounts.c
@@ -0,0 +1,147 @@
+/*
+ This file is part of TALER
+ Copyright (C) 2022, 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 <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_select_wirewatch_accounts.c
+ * @brief Implementation of the select_wirewatch_accounts function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <taler/taler_error_codes.h>
+#include <taler/taler_dbevents.h>
+#include <taler/taler_pq_lib.h>
+#include "pg_select_wirewatch_accounts.h"
+#include "pg_helper.h"
+
+
+/**
+ * Closure for #handle_results().
+ */
+struct Context
+{
+ /**
+ * Function to call with results.
+ */
+ TALER_MERCHANTDB_WirewatchWorkCallback cb;
+
+ /**
+ * Closure for @e cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Set to true if the parsing failed.
+ */
+ bool failure;
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results about accounts.
+ *
+ * @param cls of type `struct Context *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+handle_results (void *cls,
+ PGresult *result,
+ unsigned int num_results)
+{
+ struct Context *ctx = cls;
+
+ for (unsigned int i = 0; i < num_results; i++)
+ {
+ char *instance;
+ char *payto_uri;
+ char *facade_url;
+ json_t *credential;
+ uint64_t last_serial;
+ struct GNUNET_PQ_ResultSpec rs[] = {
+ GNUNET_PQ_result_spec_auto_from_type ("instance_id",
+ &instance),
+ GNUNET_PQ_result_spec_string ("payto_uri",
+ &payto_uri),
+ GNUNET_PQ_result_spec_string ("credit_facade_url",
+ &facade_url),
+ GNUNET_PQ_result_spec_allow_null (
+ TALER_PQ_result_spec_json ("credit_facade_credentials",
+ &credential),
+ NULL),
+ GNUNET_PQ_result_spec_uint64 ("last_bank_serial",
+ &last_serial),
+ GNUNET_PQ_result_spec_end
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_PQ_extract_result (result,
+ rs,
+ i))
+ {
+ GNUNET_break (0);
+ ctx->failure = true;
+ return;
+ }
+ ctx->cb (ctx->cb_cls,
+ instance,
+ payto_uri,
+ facade_url,
+ credential,
+ last_serial);
+ GNUNET_PQ_cleanup_result (rs);
+ }
+}
+
+
+enum GNUNET_DB_QueryStatus
+TMH_PG_select_wirewatch_accounts (
+ void *cls,
+ TALER_MERCHANTDB_WirewatchWorkCallback cb,
+ void *cb_cls)
+{
+ struct PostgresClosure *pg = cls;
+ struct Context ctx = {
+ .cb = cb,
+ .cb_cls = cb_cls
+ };
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_end
+ };
+ enum GNUNET_DB_QueryStatus qs;
+
+ PREPARE (pg,
+ "select_wirewatch_progress",
+ "SELECT"
+ " last_bank_serial"
+ ",instance_id"
+ ",payto_uri"
+ ",credit_facade_url"
+ ",credit_facade_credentials"
+ " FROM merchant_accounts"
+ " JOIN merchant_instances"
+ " USING (merchant_serial)"
+ " WHERE active"
+ " AND credit_facade_url IS NOT NULL");
+ check_connection (pg);
+ qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+ "select_wirewatch_progress",
+ params,
+ &handle_results,
+ &ctx);
+ if (ctx.failure)
+ return GNUNET_DB_STATUS_HARD_ERROR;
+ return qs;
+}
diff --git a/src/backenddb/pg_select_wirewatch_progress.h b/src/backenddb/pg_select_wirewatch_accounts.h
index e2cda0a8..cff263d3 100644
--- a/src/backenddb/pg_select_wirewatch_progress.h
+++ b/src/backenddb/pg_select_wirewatch_accounts.h
@@ -14,12 +14,12 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
- * @file backenddb/pg_select_wirewatch_progress.h
- * @brief implementation of the select_wirewatch_progress function for Postgres
+ * @file backenddb/pg_select_wirewatch_accounts.h
+ * @brief implementation of the select_wirewatch_accounts function for Postgres
* @author Christian Grothoff
*/
-#ifndef PG_SELECT_WIREWATCH_PROGRESS_H
-#define PG_SELECT_WIREWATCH_PROGRESS_H
+#ifndef PG_SELECT_WIREWATCH_ACCOUNTS_H
+#define PG_SELECT_WIREWATCH_ACCOUNTS_H
#include <taler/taler_util.h>
#include <taler/taler_json_lib.h>
@@ -30,15 +30,15 @@
* Select information about progress made by taler-merchant-wirewatch.
*
* @param cls closure
- * @param section configuration section of the taler-merchant-wirewatch
- * @param[out] last_serial set to last serial imported from the bank
+ * @param cb function to call with results
+ * @param cb_cls closure for @a cb
* @return transaction status
*/
enum GNUNET_DB_QueryStatus
-TMH_PG_select_wirewatch_progress (
+TMH_PG_select_wirewatch_accounts (
void *cls,
- const char *section,
- uint64_t *last_serial);
+ TALER_MERCHANTDB_WirewatchWorkCallback cb,
+ void *cb_cls);
#endif
diff --git a/src/backenddb/pg_select_wirewatch_progress.c b/src/backenddb/pg_select_wirewatch_progress.c
deleted file mode 100644
index c8049e14..00000000
--- a/src/backenddb/pg_select_wirewatch_progress.c
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- This file is part of TALER
- Copyright (C) 2022 Taler Systems SA
-
- TALER is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
- */
-/**
- * @file backenddb/pg_select_wirewatch_progress.c
- * @brief Implementation of the select_wirewatch_progress function for Postgres
- * @author Christian Grothoff
- */
-#include "platform.h"
-#include <taler/taler_error_codes.h>
-#include <taler/taler_dbevents.h>
-#include <taler/taler_pq_lib.h>
-#include "pg_select_wirewatch_progress.h"
-#include "pg_helper.h"
-
-
-enum GNUNET_DB_QueryStatus
-TMH_PG_select_wirewatch_progress (
- void *cls,
- const char *section,
- uint64_t *last_serial)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (section),
- GNUNET_PQ_query_param_end
- };
- struct GNUNET_PQ_ResultSpec rs[] = {
- GNUNET_PQ_result_spec_uint64 ("last_bank_serial",
- last_serial),
- GNUNET_PQ_result_spec_end
- };
-
- PREPARE (pg,
- "select_wirewatch_progress",
- "SELECT last_bank_serial"
- " FROM merchant_wirewatch"
- " WHERE account_section=$1");
- check_connection (pg);
- return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
- "select_wirewatch_progress",
- params,
- rs);
-}
diff --git a/src/backenddb/pg_update_wirewatch_progress.c b/src/backenddb/pg_update_wirewatch_progress.c
index d702c314..8ffdfe70 100644
--- a/src/backenddb/pg_update_wirewatch_progress.c
+++ b/src/backenddb/pg_update_wirewatch_progress.c
@@ -29,21 +29,27 @@
enum GNUNET_DB_QueryStatus
TMH_PG_update_wirewatch_progress (
void *cls,
- const char *section,
+ const char *instance,
+ const char *payto_uri,
uint64_t last_serial)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (section),
+ GNUNET_PQ_query_param_string (instance),
+ GNUNET_PQ_query_param_string (payto_uri),
GNUNET_PQ_query_param_uint64 (&last_serial),
GNUNET_PQ_query_param_end
};
PREPARE (pg,
"update_wirewatch_progress",
- "UPDATE merchant_wirewatch"
- " SET last_bank_serial=$2"
- " WHERE account_section=$1");
+ "UPDATE merchant_accounts"
+ " SET last_bank_serial=$3"
+ " WHERE payto_uri=$2"
+ " AND merchant_serial ="
+ " (SELECT merchant_serial"
+ " FROM merchant_instances"
+ " WHERE merchant_id=$1)");
check_connection (pg);
return GNUNET_PQ_eval_prepared_non_select (pg->conn,
"update_wirewatch_progress",
diff --git a/src/backenddb/pg_update_wirewatch_progress.h b/src/backenddb/pg_update_wirewatch_progress.h
index f50e8066..0e762adc 100644
--- a/src/backenddb/pg_update_wirewatch_progress.h
+++ b/src/backenddb/pg_update_wirewatch_progress.h
@@ -30,14 +30,16 @@
* Update information about progress made by taler-merchant-wirewatch.
*
* @param cls closure
- * @param section configuration section of the taler-merchant-wirewatch
- * @param last_serial last serial imported from the bank
+ * @param instance name of the instance to record progress for
+ * @param payto_uri bank account URI to record progress for
+ * @param last_serial latest serial of a transaction that was processed
* @return transaction status
*/
enum GNUNET_DB_QueryStatus
TMH_PG_update_wirewatch_progress (
void *cls,
- const char *section,
+ const char *instance,
+ const char *payto_uri,
uint64_t last_serial);
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index 4728801f..19bd022a 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -32,9 +32,8 @@
#include "pg_helper.h"
#include "pg_lookup_instances.h"
#include "pg_lookup_transfers.h"
-#include "pg_insert_wirewatch_progress.h"
#include "pg_update_wirewatch_progress.h"
-#include "pg_select_wirewatch_progress.h"
+#include "pg_select_wirewatch_accounts.h"
/**
@@ -7395,8 +7394,8 @@ postgres_connect (void *cls)
GNUNET_PQ_make_prepare ("inactivate_account",
"UPDATE merchant_accounts SET"
" active=FALSE"
- " WHERE h_wire=$2 AND"
- " merchant_serial="
+ " WHERE h_wire=$2"
+ " AND merchant_serial="
" (SELECT merchant_serial"
" FROM merchant_instances"
" WHERE merchant_id=$1)"),
@@ -9802,12 +9801,10 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
= &TMH_PG_lookup_instance;
plugin->lookup_transfers
= &TMH_PG_lookup_transfers;
- plugin->insert_wirewatch_progress
- = &TMH_PG_insert_wirewatch_progress;
plugin->update_wirewatch_progress
= &TMH_PG_update_wirewatch_progress;
- plugin->select_wirewatch_progress
- = &TMH_PG_select_wirewatch_progress;
+ plugin->select_wirewatch_accounts
+ = &TMH_PG_select_wirewatch_accounts;
plugin->store_wire_fee_by_exchange = &postgres_store_wire_fee_by_exchange;
plugin->insert_reserve = &postgres_insert_reserve;
plugin->activate_reserve = &postgres_activate_reserve;