insert_contract_terms.c (4827B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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/insert_contract_terms.c 18 * @brief Implementation of the insert_contract_terms 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/insert_contract_terms.h" 26 #include "helper.h" 27 28 enum GNUNET_DB_QueryStatus 29 TALER_MERCHANTDB_insert_contract_terms (struct TALER_MERCHANTDB_PostgresContext *pg, 30 const char *instance_id, 31 const char *order_id, 32 json_t *contract_terms, 33 uint64_t *order_serial) 34 { 35 struct GNUNET_TIME_Timestamp pay_deadline; 36 struct GNUNET_TIME_Timestamp refund_deadline; 37 const char *fulfillment_url; 38 struct TALER_PrivateContractHashP h_contract_terms; 39 40 if (GNUNET_OK != 41 TALER_JSON_contract_hash (contract_terms, 42 &h_contract_terms)) 43 { 44 GNUNET_break (0); 45 return GNUNET_DB_STATUS_HARD_ERROR; 46 } 47 48 { 49 struct GNUNET_JSON_Specification spec[] = { 50 GNUNET_JSON_spec_timestamp ("pay_deadline", 51 &pay_deadline), 52 GNUNET_JSON_spec_timestamp ("refund_deadline", 53 &refund_deadline), 54 GNUNET_JSON_spec_end () 55 }; 56 enum GNUNET_GenericReturnValue res; 57 const char *error_json_name; 58 unsigned int error_line; 59 60 res = GNUNET_JSON_parse (contract_terms, 61 spec, 62 &error_json_name, 63 &error_line); 64 if (GNUNET_OK != res) 65 { 66 GNUNET_break (0); 67 return GNUNET_DB_STATUS_HARD_ERROR; 68 } 69 } 70 71 fulfillment_url = 72 json_string_value (json_object_get (contract_terms, 73 "fulfillment_url")); 74 check_connection (pg); 75 { 76 struct GNUNET_PQ_QueryParam params[] = { 77 GNUNET_PQ_query_param_string (instance_id), 78 GNUNET_PQ_query_param_string (order_id), 79 TALER_PQ_query_param_json (contract_terms), 80 GNUNET_PQ_query_param_auto_from_type (&h_contract_terms), 81 GNUNET_PQ_query_param_timestamp (&pay_deadline), 82 GNUNET_PQ_query_param_timestamp (&refund_deadline), 83 (NULL == fulfillment_url) 84 ? GNUNET_PQ_query_param_null () 85 : GNUNET_PQ_query_param_string (fulfillment_url), 86 GNUNET_PQ_query_param_end 87 }; 88 struct GNUNET_PQ_ResultSpec rs[] = { 89 GNUNET_PQ_result_spec_uint64 ("order_serial", 90 order_serial), 91 GNUNET_PQ_result_spec_end 92 }; 93 PREPARE (pg, 94 "insert_contract_terms", 95 "INSERT INTO merchant_contract_terms" 96 "(order_serial" 97 ",merchant_serial" 98 ",order_id" 99 ",contract_terms" 100 ",h_contract_terms" 101 ",creation_time" 102 ",pay_deadline" 103 ",refund_deadline" 104 ",fulfillment_url" 105 ",claim_token" 106 ",pos_key" 107 ",pos_algorithm)" 108 "SELECT" 109 " mo.order_serial" 110 ",mo.merchant_serial" 111 ",mo.order_id" 112 ",$3::TEXT::JSONB" /* contract_terms */ 113 ",$4" /* h_contract_terms */ 114 ",mo.creation_time" 115 ",$5" /* pay_deadline */ 116 ",$6" /* refund_deadline */ 117 ",$7" /* fulfillment_url */ 118 ",mo.claim_token" 119 ",mo.pos_key" 120 ",mo.pos_algorithm" 121 " FROM merchant_orders mo" 122 " WHERE order_id=$2" 123 " AND merchant_serial=" 124 " (SELECT merchant_serial" 125 " FROM merchant_instances" 126 " WHERE merchant_id=$1)" 127 " RETURNING order_serial"); 128 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 129 "insert_contract_terms", 130 params, 131 rs); 132 } 133 }