merchant

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

pg_insert_contract_terms.c (4657B)


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