merchant

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

commit ef280ada7398b602ccee733bc4ca0073fc4105cd
parent 059988ea264a795e5e6ead7b5943dca6f7d1f40a
Author: Iván Ávalos <avalos@disroot.org>
Date:   Wed,  5 Mar 2025 18:40:32 +0100

store choice_index for order v1 payments

Diffstat:
Msrc/backend/taler-merchant-httpd_post-orders-ID-paid.c | 10+++++++++-
Msrc/backend/taler-merchant-httpd_post-orders-ID-pay.c | 9+++++----
Msrc/backend/taler-merchant-httpd_private-get-orders-ID.c | 27+++++++++++----------------
Msrc/backenddb/pg_mark_contract_paid.c | 7++++++-
Msrc/backenddb/pg_mark_contract_paid.h | 4+++-
Msrc/backenddb/test_merchantdb.c | 3++-
Msrc/include/taler_merchantdb_plugin.h | 3++-
7 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_post-orders-ID-paid.c b/src/backend/taler-merchant-httpd_post-orders-ID-paid.c @@ -91,6 +91,8 @@ TMH_post_orders_ID_paid (const struct TMH_RequestHandler *rh, const char *order_id = hc->infix; struct TALER_MerchantSignatureP merchant_sig; const char *session_id; + int16_t choice_index; + bool choice_index_null; struct TALER_PrivateContractHashP hct; json_t *contract_terms; const char *fulfillment_url; @@ -104,10 +106,15 @@ TMH_post_orders_ID_paid (const struct TMH_RequestHandler *rh, &hct), GNUNET_JSON_spec_string ("session_id", &session_id), + GNUNET_JSON_spec_mark_optional ( + GNUNET_JSON_spec_int16 ("choice_index", + &choice_index), + NULL), GNUNET_JSON_spec_end () }; enum GNUNET_GenericReturnValue res; + choice_index = -1; res = TALER_MHD_parse_json_data (connection, hc->request_body, spec); @@ -202,7 +209,8 @@ TMH_post_orders_ID_paid (const struct TMH_RequestHandler *rh, qs = TMH_db->mark_contract_paid (TMH_db->cls, hc->instance->settings.id, &hct, - session_id); + session_id, + choice_index); /* If the order was paid already, we get qs == 0. */ if (0 > qs) { diff --git a/src/backend/taler-merchant-httpd_post-orders-ID-pay.c b/src/backend/taler-merchant-httpd_post-orders-ID-pay.c @@ -486,7 +486,7 @@ struct PayContext /** * Index of selected choice in the @e contract_terms choices array. */ - int64_t choice_index; + int16_t choice_index; /** * Number of token envelopes passed to this request. @@ -2349,7 +2349,8 @@ phase_execute_pay_transaction (struct PayContext *pc) qs = TMH_db->mark_contract_paid (TMH_db->cls, instance_id, &pc->check_contract.h_contract_terms, - pc->parse_pay.session_id); + pc->parse_pay.session_id, + pc->parse_wallet_data.choice_index); if (qs < 0) { TMH_db->rollback (TMH_db->cls); @@ -3260,7 +3261,7 @@ phase_check_contract (struct PayContext *pc) { GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Order `%s' has choices array with %u elements but " - "request has 'choice_index' field with value %ld\n", + "request has 'choice_index' field with value %d\n", pc->order_id, pc->check_contract.contract_terms->details.v1.choices_len, pc->parse_wallet_data.choice_index); @@ -3355,7 +3356,7 @@ phase_parse_wallet_data (struct PayContext *pc) const json_t *tokens_evs; struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_mark_optional ( - GNUNET_JSON_spec_int64 ("choice_index", + GNUNET_JSON_spec_int16 ("choice_index", &pc->parse_wallet_data.choice_index), NULL), GNUNET_JSON_spec_mark_optional ( diff --git a/src/backend/taler-merchant-httpd_private-get-orders-ID.c b/src/backend/taler-merchant-httpd_private-get-orders-ID.c @@ -745,6 +745,13 @@ phase_parse_contract (struct GetOrderRequestContext *gorc) gorc->contract_amount = gorc->contract_terms->details.v1.choices[gorc->choice_index].amount; } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "choice index %i for order %s is invalid or not yet available", + gorc->choice_index, + gorc->contract_terms->order_id); + } break; default: { @@ -1426,7 +1433,6 @@ phase_reply_result (struct GetOrderRequestContext *gorc) struct TMH_HandlerContext *hc = gorc->hc; MHD_RESULT ret; char *order_status_url; - json_t *choice_index; GNUNET_assert (gorc->paid); GNUNET_assert (! gorc->order_only); @@ -1457,17 +1463,6 @@ phase_reply_result (struct GetOrderRequestContext *gorc) TALER_amount_is_zero (&gorc->contract_amount)); gorc->last_payment = gorc->contract_terms->timestamp; } - if (-1 != gorc->choice_index) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Choice index is %d\n", - gorc->choice_index); - choice_index = json_integer ((json_int_t) gorc->choice_index); - } - else - { - choice_index = NULL; - } ret = TALER_MHD_REPLY_JSON_PACK ( gorc->sc.con, MHD_HTTP_OK, @@ -1507,10 +1502,10 @@ phase_reply_result (struct GetOrderRequestContext *gorc) gorc->refund_details), GNUNET_JSON_pack_string ("order_status_url", order_status_url), - GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_object_steal ( - "choice_index", - choice_index))); + (gorc->choice_index >= 0) + ? GNUNET_JSON_pack_int64 ("choice_index", + gorc->choice_index) + : GNUNET_JSON_pack_end_ ()); GNUNET_free (order_status_url); gorc->wire_details = NULL; gorc->refund_details = NULL; diff --git a/src/backenddb/pg_mark_contract_paid.c b/src/backenddb/pg_mark_contract_paid.c @@ -30,13 +30,17 @@ TMH_PG_mark_contract_paid ( void *cls, const char *instance_id, const struct TALER_PrivateContractHashP *h_contract_terms, - const char *session_id) + const char *session_id, + int16_t choice_index) { 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_string (session_id), + (choice_index >= 0) + ? GNUNET_PQ_query_param_int16 (&choice_index) + : GNUNET_PQ_query_param_null (), GNUNET_PQ_query_param_end }; struct GNUNET_PQ_QueryParam uparams[] = { @@ -60,6 +64,7 @@ TMH_PG_mark_contract_paid ( "UPDATE merchant_contract_terms SET" " paid=TRUE" ",session_id=$3" + ",choice_index=$4" " WHERE h_contract_terms=$2" " AND merchant_serial=" " (SELECT merchant_serial" diff --git a/src/backenddb/pg_mark_contract_paid.h b/src/backenddb/pg_mark_contract_paid.h @@ -34,6 +34,7 @@ * @param instance_id instance to mark contract as paid for * @param h_contract_terms hash of the contract that is now paid * @param session_id the session that paid the contract + * @param choice_index the contract v1 choice index to select, -1 for v0 * @return transaction status */ enum GNUNET_DB_QueryStatus @@ -41,6 +42,7 @@ TMH_PG_mark_contract_paid (void *cls, const char *instance_id, const struct TALER_PrivateContractHashP *h_contract_terms, - const char *session_id); + const char *session_id, + int16_t choice_index); #endif diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c @@ -1898,7 +1898,8 @@ test_mark_contract_paid (const struct InstanceData *instance, plugin->mark_contract_paid (plugin->cls, instance->instance.id, &h_contract_terms, - "test_orders_session"), + "test_orders_session", + -1), "Mark contract paid failed\n"); return 0; } diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h @@ -2481,7 +2481,8 @@ struct TALER_MERCHANTDB_Plugin void *cls, const char *instance_id, const struct TALER_PrivateContractHashP *h_contract_terms, - const char *session_id); + const char *session_id, + int16_t choice_index); /**