merchant

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

mark_contract_paid.c (5066B)


      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/mark_contract_paid.c
     18  * @brief Implementation of the mark_contract_paid 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/mark_contract_paid.h"
     26 #include "helper.h"
     27 
     28 enum GNUNET_DB_QueryStatus
     29 TALER_MERCHANTDB_mark_contract_paid (struct TALER_MERCHANTDB_PostgresContext *pg,
     30                                      const char *instance_id,
     31                                      const struct TALER_PrivateContractHashP *h_contract_terms,
     32                                      const char *session_id,
     33                                      int16_t choice_index)
     34 {
     35   struct GNUNET_PQ_QueryParam params[] = {
     36     GNUNET_PQ_query_param_string (instance_id),
     37     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
     38     GNUNET_PQ_query_param_string (session_id),
     39     (choice_index >= 0)
     40       ? GNUNET_PQ_query_param_int16 (&choice_index)
     41       : GNUNET_PQ_query_param_null (),
     42     GNUNET_PQ_query_param_end
     43   };
     44   struct GNUNET_PQ_QueryParam uparams[] = {
     45     GNUNET_PQ_query_param_string (instance_id),
     46     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
     47     GNUNET_PQ_query_param_end
     48   };
     49   enum GNUNET_DB_QueryStatus qs;
     50 
     51   /* Session ID must always be given by the caller. */
     52   GNUNET_assert (NULL != session_id);
     53 
     54   /* no preflight check here, run in transaction by caller! */
     55   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     56               "Marking h_contract_terms '%s' of %s as paid for session `%s'\n",
     57               GNUNET_h2s (&h_contract_terms->hash),
     58               instance_id,
     59               session_id);
     60   PREPARE (pg,
     61            "mark_contract_paid",
     62            "UPDATE merchant_contract_terms SET"
     63            " paid=TRUE"
     64            ",session_id=$3"
     65            ",choice_index=$4"
     66            " WHERE h_contract_terms=$2"
     67            "   AND merchant_serial="
     68            "     (SELECT merchant_serial"
     69            "        FROM merchant_instances"
     70            "       WHERE merchant_id=$1)");
     71   qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     72                                            "mark_contract_paid",
     73                                            params);
     74   if (qs <= 0)
     75     return qs;
     76   PREPARE (pg,
     77            "mark_inventory_sold",
     78            "UPDATE merchant_inventory SET"
     79            " total_sold = total_sold"
     80            "   + order_locks.total_locked"
     81            "   + ((merchant_inventory.total_sold_frac::BIGINT"
     82            "       + order_locks.total_locked_frac::BIGINT) / 1000000)"
     83            " ,total_sold_frac ="
     84            "    ((merchant_inventory.total_sold_frac::BIGINT"
     85            "      + order_locks.total_locked_frac::BIGINT) % 1000000)::INT4"
     86            " FROM (SELECT total_locked,total_locked_frac,product_serial"
     87            "       FROM merchant_order_locks"
     88            "       WHERE order_serial="
     89            "       (SELECT order_serial"
     90            "          FROM merchant_contract_terms"
     91            "         WHERE h_contract_terms=$2"
     92            "           AND merchant_serial="
     93            "           (SELECT merchant_serial"
     94            "              FROM merchant_instances"
     95            "             WHERE merchant_id=$1))"
     96            "       ) AS order_locks"
     97            " WHERE merchant_inventory.product_serial"
     98            "             =order_locks.product_serial");
     99   qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
    100                                            "mark_inventory_sold",
    101                                            uparams);
    102   if (qs < 0)
    103     return qs; /* 0: no inventory management, that's OK! */
    104   /* ON DELETE CASCADE deletes from merchant_order_locks */
    105   PREPARE (pg,
    106            "delete_completed_order",
    107            "WITH md AS"
    108            "  (SELECT merchant_serial"
    109            "     FROM merchant_instances"
    110            "    WHERE merchant_id=$1) "
    111            "DELETE"
    112            " FROM merchant_orders"
    113            " WHERE order_serial="
    114            "       (SELECT order_serial"
    115            "          FROM merchant_contract_terms"
    116            "          JOIN md USING (merchant_serial)"
    117            "         WHERE h_contract_terms=$2)");
    118   return GNUNET_PQ_eval_prepared_non_select (pg->conn,
    119                                              "delete_completed_order",
    120                                              uparams);
    121 }