lookup_order_by_fulfillment.c (3024B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/backenddb/lookup_order_by_fulfillment.c 18 * @brief Implementation of the lookup_order_by_fulfillment function for Postgres 19 * @author Iván Ávalos 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_order_by_fulfillment.h" 24 #include "helper.h" 25 26 27 enum GNUNET_DB_QueryStatus 28 TALER_MERCHANTDB_lookup_order_by_fulfillment ( 29 struct TALER_MERCHANTDB_PostgresContext *pg, 30 const char *instance_id, 31 const char *fulfillment_url, 32 const char *session_id, 33 bool allow_refunded_for_repurchase, 34 char **order_id) 35 { 36 struct GNUNET_PQ_QueryParam params[] = { 37 GNUNET_PQ_query_param_string (fulfillment_url), 38 GNUNET_PQ_query_param_string (session_id), 39 GNUNET_PQ_query_param_bool (allow_refunded_for_repurchase), 40 GNUNET_PQ_query_param_end 41 }; 42 struct GNUNET_PQ_ResultSpec rs[] = { 43 GNUNET_PQ_result_spec_string ("order_id", 44 order_id), 45 GNUNET_PQ_result_spec_end 46 }; 47 48 GNUNET_assert (NULL != pg->current_merchant_id); 49 GNUNET_assert (0 == strcmp (instance_id, 50 pg->current_merchant_id)); 51 check_connection (pg); 52 TMH_PQ_prepare_anon (pg, 53 "SELECT" 54 " mct.order_id" 55 " FROM merchant_contract_terms mct" 56 " LEFT JOIN merchant_refunds mref" 57 " USING (order_serial)" 58 " WHERE fulfillment_url=$1" 59 " AND session_id=$2" 60 " AND ((CAST($3 AS BOOL)) OR" 61 " mref.refund_serial IS NULL)" 62 /* Theoretically, multiple paid orders 63 for the same fulfillment URL could 64 exist for this session_id -- if a 65 wallet was broken and did multiple 66 payments without repurchase detection. 67 So we need to limit to 1 when returning! */ 68 " ORDER BY order_id DESC" 69 " LIMIT 1;"); 70 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 71 "", 72 params, 73 rs); 74 }