From 417a952bc6451364d776e6973bae72664d3e7e6b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sun, 15 Oct 2017 22:56:49 +0200 Subject: add refund permissions to /pay --- src/backend/taler-merchant-httpd_pay.c | 17 +++++++- src/backend/taler-merchant-httpd_refund.c | 67 ++++++++++++++++++++++--------- src/backend/taler-merchant-httpd_refund.h | 17 ++++++++ 3 files changed, 80 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/backend/taler-merchant-httpd_pay.c b/src/backend/taler-merchant-httpd_pay.c index 3c3d43d0..7769ef2f 100644 --- a/src/backend/taler-merchant-httpd_pay.c +++ b/src/backend/taler-merchant-httpd_pay.c @@ -31,6 +31,7 @@ #include "taler-merchant-httpd_responses.h" #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" +#include "taler-merchant-httpd_refund.h" /** @@ -405,6 +406,16 @@ abort_deposit (struct PayContext *pc) struct MHD_Response * sign_success_response (struct PayContext *pc) { + json_t *refunds; + enum TALER_ErrorCode ec; + const char *errmsg; + + refunds = TM_get_refund_json (pc->mi, &pc->h_contract_terms, &ec, &errmsg); + + if (NULL == refunds) { + return TMH_RESPONSE_make_internal_error (ec, errmsg); + } + struct GNUNET_CRYPTO_EddsaSignature sig; struct PaymentResponsePS mr; @@ -416,14 +427,16 @@ sign_success_response (struct PayContext *pc) &mr.purpose, &sig); - return TMH_RESPONSE_make_json_pack ("{s:O, s:o, s:o}", + return TMH_RESPONSE_make_json_pack ("{s:O, s:o, s:o, s:o}", "contract_terms", pc->contract_terms, "sig", GNUNET_JSON_from_data_auto (&sig), "h_contract_terms", GNUNET_JSON_from_data (&pc->h_contract_terms, - sizeof (struct GNUNET_HashCode))); + sizeof (struct GNUNET_HashCode)), + "refund_permissions", + refunds); } diff --git a/src/backend/taler-merchant-httpd_refund.c b/src/backend/taler-merchant-httpd_refund.c index ad4ff326..9c5df273 100644 --- a/src/backend/taler-merchant-httpd_refund.c +++ b/src/backend/taler-merchant-httpd_refund.c @@ -44,12 +44,12 @@ struct ProcessRefundData * Hashed version of contract terms; needed by the callback * to pack the response. */ - struct GNUNET_HashCode *h_contract_terms; + const struct GNUNET_HashCode *h_contract_terms; /** * Both public and private key are needed by the callback */ - struct MerchantInstance *merchant; + const struct MerchantInstance *merchant; /** * Return code: #TALER_EC_NONE if successful. @@ -388,7 +388,6 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, json_t *contract_terms; struct MerchantInstance *mi; enum GNUNET_DB_QueryStatus qs; - struct ProcessRefundData prd; instance = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, @@ -464,15 +463,51 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, TALER_EC_INTERNAL_LOGIC_ERROR, "Could not hash contract terms"); } + + json_t *response; + enum TALER_ErrorCode ec; + const char *errmsg; + + response = TM_get_refund_json (mi, &h_contract_terms, &ec, &errmsg); + + if (NULL == response) { + return TMH_RESPONSE_reply_internal_error (connection, ec, errmsg); + } + + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:o}", + "refund_permissions", response); +} + + +/** + * Get the JSON representation of a refund. + * + * @param merchant_pub the merchant's public key + * @param mi merchant instance + * @param ret_ec where to store error code + * @param ret_errmsg where to store error message + * @return NULL on error, JSON array with refunds on success + */ +json_t * +TM_get_refund_json (const struct MerchantInstance *mi, + const struct GNUNET_HashCode *h_contract_terms, + enum TALER_ErrorCode *ret_ec, + const char **ret_errmsg) +{ + enum GNUNET_DB_QueryStatus qs; + struct ProcessRefundData prd; + prd.response = json_array (); - prd.h_contract_terms = &h_contract_terms; + prd.h_contract_terms = h_contract_terms; prd.merchant = mi; prd.ec = TALER_EC_NONE; for (unsigned int i=0;iget_refunds_from_contract_terms_hash (db->cls, &mi->pubkey, - &h_contract_terms, + h_contract_terms, &process_refunds_cb, &prd); if (GNUNET_DB_STATUS_SOFT_ERROR != qs) @@ -481,27 +516,21 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, if (0 > qs) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Database hard error on order_id lookup: %s\n", - order_id); + "Database hard error on refunds_from_contract_terms_hash lookup: %s\n", + GNUNET_h2s (h_contract_terms)); json_decref (prd.response); - return TMH_RESPONSE_reply_internal_error (connection, - TALER_EC_REFUND_LOOKUP_DB_ERROR, - "database hard error: looking for " - "h_contract_terms in merchant_refunds table"); + *ret_ec = TALER_EC_REFUND_LOOKUP_DB_ERROR; + *ret_errmsg = ("database hard error: looking for " + "h_contract_terms in merchant_refunds table"); } if (TALER_EC_NONE != prd.ec) { json_decref (prd.response); /* NOTE: error already logged by the callback */ - return TMH_RESPONSE_reply_internal_error (connection, - prd.ec, - "Could not generate a response"); + *ret_ec = prd.ec; + *ret_errmsg = "Could not generate a response"; } - - return TMH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:o}", - "refund_permissions", prd.response); + return prd.response; } diff --git a/src/backend/taler-merchant-httpd_refund.h b/src/backend/taler-merchant-httpd_refund.h index 64da3f9b..32083273 100644 --- a/src/backend/taler-merchant-httpd_refund.h +++ b/src/backend/taler-merchant-httpd_refund.h @@ -25,6 +25,7 @@ #include #include "taler-merchant-httpd.h" + /** * Handle request for increasing the refund associated with * a contract. @@ -59,4 +60,20 @@ MH_handler_refund_lookup (struct TMH_RequestHandler *rh, void **connection_cls, const char *upload_data, size_t *upload_data_size); + +/** + * Get the JSON representation of a refund. + * + * @param merchant_pub the merchant's public key + * @param mi merchant instance + * @param ret_ec where to store error code + * @param ret_errmsg where to store error message + * @return NULL on error, JSON array with refunds on success + */ +json_t * +TM_get_refund_json (const struct MerchantInstance *mi, + const struct GNUNET_HashCode *h_contract_terms, + enum TALER_ErrorCode *ret_ec, + const char **ret_errmsg); + #endif -- cgit v1.2.3