merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit 0a0fe09e9b144e96d4c6b7f1dab937f49de58f08
parent bd799135b4368144a87f4b596899f443f91bea69
Author: Christian Grothoff <christian@grothoff.org>
Date:   Mon, 22 Jan 2018 21:59:27 +0100

fix #5262

Diffstat:
MChangeLog | 15+++++++++++++++
Msrc/backend/taler-merchant-httpd_pay.c | 38+++++++++++++++++++++++++++++++++++---
Msrc/backenddb/plugin_merchantdb_postgres.c | 59++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Msrc/backenddb/test_merchantdb.c | 10++++++++++
Msrc/include/taler_merchantdb_plugin.h | 16+++++++++++++++-
Msrc/lib/test_merchant_api.c | 10++++++++++
6 files changed, 137 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,18 @@ +Mon Jan 22 21:54:42 CET 2018 + Address #5262. -CG + +Tue Jan 2 00:27:29 2018 + Implement #5158 (proper handling of aborted payments). -CG + +Wed Dec 27 11:21:43 2017 + Complete logic to allow /pay to span coins from multiple exchanges. -CG + +Wed Dec 13 21:50:59 2017 + Use new wire transfer logic in payments generator. -CG + +Thu Dec 7 07:42:40 2017 + Implemented new tipping feature (now with private keys in files). -CG + Wed Oct 18 15:33:23 CEST 2017 Releasing taler-merchant 0.4.0. -CG diff --git a/src/backend/taler-merchant-httpd_pay.c b/src/backend/taler-merchant-httpd_pay.c @@ -681,8 +681,6 @@ check_payment_sufficient (struct PayContext *pc) } } - - /* Now compare exchange wire fee compared to what we are willing to pay */ if (GNUNET_YES != @@ -1033,7 +1031,6 @@ process_pay_with_exchange (void *cls, return; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found transaction data for proposal `%s' of merchant `%s', initiating deposits\n", GNUNET_h2s (&pc->h_contract_terms), @@ -1715,8 +1712,43 @@ begin_transaction (struct PayContext *pc) if (PC_MODE_ABORT_REFUND == pc->mode) { + json_t *terms; + /* The wallet is going for a refund, (on aborted operation)! */ + /* check payment was indeed incomplete */ + qs = db->find_paid_contract_terms_from_hash (db->cls, + &terms, + &pc->h_contract_terms, + &pc->mi->pubkey); + if (0 > qs) + { + db->rollback (db->cls); + if (GNUNET_DB_STATUS_SOFT_ERROR == qs) + { + begin_transaction (pc); + return; + } + /* Always report on hard error as well to enable diagnostics */ + GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); + resume_pay_with_error (pc, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_PAY_DB_STORE_PAY_ERROR, + "Merchant database error"); + return; + } + if (0 < qs) + { + /* Payment had been complete! */ + json_decref (terms); + db->rollback (db->cls); + resume_pay_with_error (pc, + MHD_HTTP_FORBIDDEN, + TALER_EC_PAY_ABORT_REFUND_REFUSED_PAYMENT_COMPLETE, + "Payment complete, refusing to abort"); + return; + } + /* Store refund in DB */ qs = db->increase_refund_for_contract (db->cls, &pc->h_contract_terms, diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c @@ -378,6 +378,14 @@ postgres_initialize (void *cls) " WHERE h_contract_terms=$1" " AND merchant_pub=$2", 2), + GNUNET_PQ_make_prepare ("find_paid_contract_terms_from_hash", + "SELECT" + " contract_terms" + " FROM merchant_contract_terms" + " WHERE h_contract_terms=$1" + " AND merchant_pub=$2" + " AND paid=TRUE", + 2), GNUNET_PQ_make_prepare ("end_transaction", "COMMIT", 0), @@ -796,9 +804,9 @@ postgres_commit (void *cls) */ static enum GNUNET_DB_QueryStatus postgres_find_contract_terms_from_hash (void *cls, - json_t **contract_terms, - const struct GNUNET_HashCode *h_contract_terms, - const struct TALER_MerchantPublicKeyP *merchant_pub) + json_t **contract_terms, + const struct GNUNET_HashCode *h_contract_terms, + const struct TALER_MerchantPublicKeyP *merchant_pub) { struct PostgresClosure *pg = cls; struct GNUNET_PQ_QueryParam params[] = { @@ -821,6 +829,41 @@ postgres_find_contract_terms_from_hash (void *cls, /** + * Retrieve proposal data given its proposal data's hashcode + * + * @param cls closure + * @param contract_terms where to store the retrieved proposal data + * @param h_contract_terms proposal data's hashcode that will be used to + * perform the lookup + * @return transaction status + */ +static enum GNUNET_DB_QueryStatus +postgres_find_paid_contract_terms_from_hash (void *cls, + json_t **contract_terms, + const struct GNUNET_HashCode *h_contract_terms, + const struct TALER_MerchantPublicKeyP *merchant_pub) +{ + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (h_contract_terms), + GNUNET_PQ_query_param_auto_from_type (merchant_pub), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + TALER_PQ_result_spec_json ("contract_terms", + contract_terms), + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "find_paid_contract_terms_from_hash", + params, + rs); +} + + +/** * Retrieve proposal data given its order id. Ignores if the * proposal has been paid or not. * @@ -1232,10 +1275,11 @@ postgres_find_contract_terms_history (void *cls, rs); if (qs <= 0) return qs; - cb (cb_cls, - order_id, - 0, - contract_terms); + if (NULL != cb) + cb (cb_cls, + order_id, + 0, + contract_terms); GNUNET_PQ_cleanup_result (rs); return qs; } @@ -3363,6 +3407,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) plugin->find_contract_terms_by_date = &postgres_find_contract_terms_by_date; plugin->find_contract_terms_by_date_and_range = &postgres_find_contract_terms_by_date_and_range; plugin->find_contract_terms_from_hash = &postgres_find_contract_terms_from_hash; + plugin->find_paid_contract_terms_from_hash = &postgres_find_paid_contract_terms_from_hash; plugin->get_refunds_from_contract_terms_hash = &postgres_get_refunds_from_contract_terms_hash; plugin->lookup_wire_fee = &postgres_lookup_wire_fee; plugin->increase_refund_for_contract = &postgres_increase_refund_for_contract; diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c @@ -838,6 +838,11 @@ run (void *cls) timestamp, contract_terms)); + FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != + plugin->find_paid_contract_terms_from_hash (plugin->cls, + &out, + &h_contract_terms, + &merchant_pub)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != plugin->mark_proposal_paid (plugin->cls, &h_contract_terms, @@ -858,6 +863,11 @@ run (void *cls) NULL)); FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + plugin->find_paid_contract_terms_from_hash (plugin->cls, + &out, + &h_contract_terms, + &merchant_pub)); + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != plugin->find_contract_terms_from_hash (plugin->cls, &out, &h_contract_terms, diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h @@ -271,7 +271,6 @@ struct TALER_MERCHANTDB_Plugin const struct TALER_MerchantPublicKeyP *merchant_pub); - /** * Retrieve proposal data given its hashcode * @@ -288,6 +287,21 @@ struct TALER_MERCHANTDB_Plugin const struct TALER_MerchantPublicKeyP *merchant_pub); + /** + * Retrieve paid contract terms data given its hashcode. + * + * @param cls closure + * @param[out] contract_terms where to store the result + * @param h_contract_terms hashcode used to lookup. + * @param merchant_pub instance's public key. + * @return transaction status + */ + enum GNUNET_DB_QueryStatus + (*find_paid_contract_terms_from_hash) (void *cls, + json_t **contract_terms, + const struct GNUNET_HashCode *h_contract_terms, + const struct TALER_MerchantPublicKeyP *merchant_pub); + /** * Return proposals whose timestamp are older than `date`. diff --git a/src/lib/test_merchant_api.c b/src/lib/test_merchant_api.c @@ -2863,6 +2863,9 @@ pay_refund_cb (void *cls, if ( (MHD_HTTP_OK == http_status) && (TALER_EC_NONE == ec) ) { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received %u refunds\n", + num_refunds); cmd->details.pay_abort.num_refunds = num_refunds; cmd->details.pay_abort.res = GNUNET_new_array (num_refunds, @@ -4153,6 +4156,13 @@ run (void *cls) .details.check_payment.contract_ref = "create-proposal-1", .details.check_payment.expect_paid = GNUNET_YES }, + /* Test for #5262: abort after full payment */ + { .oc = OC_PAY_ABORT, + .label = "pay-abort-2", + .expected_response_code = MHD_HTTP_FORBIDDEN, + .details.pay_abort.pay_ref = "deposit-simple", + }, + /* Try to replay payment reusing coin */ { .oc = OC_PAY, .label = "replay-simple",