diff options
Diffstat (limited to 'src/backenddb')
-rw-r--r-- | src/backenddb/plugin_merchantdb_postgres.c | 83 |
1 files changed, 77 insertions, 6 deletions
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index 168fbb06..4f6b6a3e 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -70,14 +70,14 @@ postgres_initialize (void *cls, "CREATE %1$s TABLE IF NOT EXISTS payments (" "h_contract BYTEA NOT NULL," "h_wire BYTEA NOT NULL," - "transaction_id INT8 PRIMARY KEY," + "transaction_id INT8," /*WARNING: this column used to be primary key, but that wrong since multiple coins belong to the same id*/ "timestamp INT8 NOT NULL," "refund_deadline INT8 NOT NULL," "amount_without_fee_val INT8 NOT NULL," "amount_without_fee_frac INT4 NOT NULL," "amount_without_fee_curr VARCHAR(" TALER_CURRENCY_LEN_STR ") NOT NULL," "coin_pub BYTEA NOT NULL," - "mint_sig BYTEA NOT NULL);", + "mint_proof BYTEA NOT NULL);", tmp_str); ret = GNUNET_POSTGRES_exec (pg->conn, sql); @@ -96,7 +96,7 @@ postgres_initialize (void *cls, ",amount_without_fee_frac" ",amount_without_fee_curr" ",coin_pub" - ",mint_sig) VALUES " + ",mint_proof) VALUES " "($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", 10, NULL))) || (PGRES_COMMAND_OK != (status = PQresultStatus(res))) ) @@ -108,6 +108,22 @@ postgres_initialize (void *cls, } return GNUNET_SYSERR; } + if ( (NULL == (res = PQprepare (pg->conn, + "check_payment", + "SELECT * " + "FROM payments " + "WHERE transaction_id=$1", + 1, NULL))) || + (PGRES_COMMAND_OK != (status = PQresultStatus(res))) ) + { + if (NULL != res) + { + PQSQL_strerror (GNUNET_ERROR_TYPE_ERROR, "PQprepare", res); + PQclear (res); + } + return GNUNET_SYSERR; + } + PQclear (res); return GNUNET_OK; } @@ -124,7 +140,7 @@ postgres_initialize (void *cls, * @param refund refund deadline * @param amount_without_fee amount the mint will deposit * @param coin_pub public key of the coin - * @param merchant_pub our public key + * @param mint_proof proof from the mint that coin was accepted * @return #GNUNET_OK on success, #GNUNET_SYSERR upon error */ static int @@ -136,7 +152,7 @@ postgres_store_payment (void *cls, struct GNUNET_TIME_Absolute refund, const struct TALER_Amount *amount_without_fee, const struct TALER_CoinSpendPublicKeyP *coin_pub, - const struct TALER_MintSignatureP *mint_sig) + json_t *mint_proof) { struct PostgresClosure *pg = cls; PGresult *res; @@ -150,7 +166,7 @@ postgres_store_payment (void *cls, TALER_PQ_query_param_absolute_time (&refund), TALER_PQ_query_param_amount (amount_without_fee), TALER_PQ_query_param_auto_from_type (coin_pub), - TALER_PQ_query_param_auto_from_type (mint_sig), + TALER_PQ_query_param_json (mint_proof), TALER_PQ_query_param_end }; @@ -190,7 +206,61 @@ postgres_store_payment (void *cls, return GNUNET_OK; } +/** + * Check whether a payment has already been stored + * + * @param cls our plugin handle + * @param transaction_id the transaction id to search into + * the db + * + * @return GNUNET_OK if found, GNUNET_NO if not, GNUNET_SYSERR + * upon error + */ +static int +postgres_check_payment(void *cls, + uint64_t transaction_id) +{ + struct PostgresClosure *pg = cls; + PGresult *res; + ExecStatusType status; + struct TALER_PQ_QueryParam params[] = { + TALER_PQ_query_param_uint64 (&transaction_id), + TALER_PQ_query_param_end + }; + res = TALER_PQ_exec_prepared (pg->conn, + "check_payment", + params); + + status = PQresultStatus (res); + if (PGRES_TUPLES_OK != status) + { + const char *sqlstate; + + sqlstate = PQresultErrorField (res, PG_DIAG_SQLSTATE); + if (NULL == sqlstate) + { + /* very unexpected... */ + GNUNET_break (0); + PQclear (res); + return GNUNET_SYSERR; + } + + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not check if contract %llu is in DB: %s\n", + transaction_id, + sqlstate); + PQclear (res); + return GNUNET_SYSERR; + } + /* count rows */ + if (PQntuples (res) > 0) + return GNUNET_OK; + return GNUNET_NO; + + + +} /** * Initialize Postgres database subsystem. * @@ -220,6 +290,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->cls = pg; plugin->initialize = &postgres_initialize; plugin->store_payment = &postgres_store_payment; + plugin->check_payment = &postgres_check_payment; return plugin; } |