lookup_order_by_fulfillment.c (3237B)
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_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "merchant-database/lookup_order_by_fulfillment.h" 26 #include "helper.h" 27 28 29 enum GNUNET_DB_QueryStatus 30 TALER_MERCHANTDB_lookup_order_by_fulfillment (struct TALER_MERCHANTDB_PostgresContext *pg, 31 const char *instance_id, 32 const char *fulfillment_url, 33 const char *session_id, 34 bool allow_refunded_for_repurchase, 35 char **order_id) 36 { 37 struct GNUNET_PQ_QueryParam params[] = { 38 GNUNET_PQ_query_param_string (instance_id), 39 GNUNET_PQ_query_param_string (fulfillment_url), 40 GNUNET_PQ_query_param_string (session_id), 41 GNUNET_PQ_query_param_bool (allow_refunded_for_repurchase), 42 GNUNET_PQ_query_param_end 43 }; 44 struct GNUNET_PQ_ResultSpec rs[] = { 45 GNUNET_PQ_result_spec_string ("order_id", 46 order_id), 47 GNUNET_PQ_result_spec_end 48 }; 49 50 check_connection (pg); 51 PREPARE (pg, 52 "lookup_order_by_fulfillment", 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=$2" 59 " AND session_id=$3" 60 " AND merchant_serial=" 61 " (SELECT merchant_serial" 62 " FROM merchant_instances" 63 " WHERE merchant_id=$1)" 64 " AND ((CAST($4 AS BOOL)) OR" 65 " mref.refund_serial IS NULL)" 66 /* Theoretically, multiple paid orders 67 for the same fulfillment URL could 68 exist for this session_id -- if a 69 wallet was broken and did multiple 70 payments without repurchase detection. 71 So we need to limit to 1 when returning! */ 72 " ORDER BY order_id DESC" 73 " LIMIT 1;"); 74 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 75 "lookup_order_by_fulfillment", 76 params, 77 rs); 78 }