Line data Source code
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 backenddb/pg_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 "pg_lookup_order_by_fulfillment.h" 26 : #include "pg_helper.h" 27 : 28 : 29 : enum GNUNET_DB_QueryStatus 30 18 : TMH_PG_lookup_order_by_fulfillment ( 31 : void *cls, 32 : const char *instance_id, 33 : const char *fulfillment_url, 34 : const char *session_id, 35 : bool allow_refunded_for_repurchase, 36 : char **order_id) 37 : { 38 18 : struct PostgresClosure *pg = cls; 39 18 : struct GNUNET_PQ_QueryParam params[] = { 40 18 : GNUNET_PQ_query_param_string (instance_id), 41 18 : GNUNET_PQ_query_param_string (fulfillment_url), 42 18 : GNUNET_PQ_query_param_string (session_id), 43 18 : GNUNET_PQ_query_param_bool (allow_refunded_for_repurchase), 44 : GNUNET_PQ_query_param_end 45 : }; 46 18 : struct GNUNET_PQ_ResultSpec rs[] = { 47 18 : GNUNET_PQ_result_spec_string ("order_id", 48 : order_id), 49 : GNUNET_PQ_result_spec_end 50 : }; 51 : 52 18 : check_connection (pg); 53 18 : PREPARE (pg, 54 : "lookup_order_by_fulfillment", 55 : "SELECT" 56 : " mct.order_id" 57 : " FROM merchant_contract_terms mct" 58 : " LEFT JOIN merchant_refunds mref" 59 : " USING (order_serial)" 60 : " WHERE fulfillment_url=$2" 61 : " AND session_id=$3" 62 : " AND merchant_serial=" 63 : " (SELECT merchant_serial" 64 : " FROM merchant_instances" 65 : " WHERE merchant_id=$1)" 66 : " AND ((CAST($4 AS BOOL)) OR" 67 : " mref.refund_serial IS NULL)" 68 : /* Theoretically, multiple paid orders 69 : for the same fulfillment URL could 70 : exist for this session_id -- if a 71 : wallet was broken and did multiple 72 : payments without repurchase detection. 73 : So we need to limit to 1 when returning! */ 74 : " ORDER BY order_id DESC" 75 : " LIMIT 1;"); 76 18 : return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 77 : "lookup_order_by_fulfillment", 78 : params, 79 : rs); 80 : }