merchant

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

report_deliveries.c (5673B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2026 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 #include "platform.h"
     17 #include <taler/taler_pq_lib.h>
     18 #include "merchant-database/report_deliveries.h"
     19 #include "helper.h"
     20 
     21 
     22 enum GNUNET_DB_QueryStatus
     23 TALER_MERCHANTDB_queue_report_test (
     24   struct TALER_MERCHANTDB_PostgresContext *pg,
     25   uint64_t schedule_id,
     26   const char *description,
     27   const char *program,
     28   const char *source,
     29   const char *target,
     30   const char *mime,
     31   uint64_t *delivery_id)
     32 {
     33   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
     34   struct GNUNET_PQ_QueryParam params[] = {
     35     GNUNET_PQ_query_param_uint64 (&schedule_id),
     36     GNUNET_PQ_query_param_string (description),
     37     GNUNET_PQ_query_param_string (program),
     38     GNUNET_PQ_query_param_string (source),
     39     GNUNET_PQ_query_param_string (target),
     40     GNUNET_PQ_query_param_string (mime),
     41     GNUNET_PQ_query_param_absolute_time (&now),
     42     GNUNET_PQ_query_param_end
     43   };
     44   struct GNUNET_PQ_ResultSpec rs[] = {
     45     GNUNET_PQ_result_spec_uint64 ("id", delivery_id),
     46     GNUNET_PQ_result_spec_end
     47   };
     48 
     49   GNUNET_assert (NULL != pg->current_merchant_id);
     50   TMH_PQ_prepare_anon (pg,
     51                        "SELECT merchant.queue_report_delivery(NULLIF($1::bigint,0),NULL,"
     52                        " '[Test] ' || $2::text,$3,$4,$5,$6,$7) AS id");
     53   return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     54                                                    "",
     55                                                    params,
     56                                                    rs);
     57 }
     58 
     59 
     60 enum GNUNET_DB_QueryStatus
     61 TALER_MERCHANTDB_claim_delivery (
     62   struct TALER_MERCHANTDB_PostgresContext *pg,
     63   uint64_t job,
     64   struct GNUNET_TIME_Absolute due,
     65   struct GNUNET_TIME_Absolute next,
     66   uint64_t *delivery_id)
     67 {
     68   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
     69   uint32_t timeout = TALER_EC_GENERIC_TIMEOUT;
     70   struct GNUNET_PQ_QueryParam params[] = {
     71     GNUNET_PQ_query_param_uint64 (&job),
     72     GNUNET_PQ_query_param_absolute_time (&due),
     73     GNUNET_PQ_query_param_absolute_time (&next),
     74     GNUNET_PQ_query_param_absolute_time (&now),
     75     GNUNET_PQ_query_param_uint32 (&timeout),
     76     GNUNET_PQ_query_param_end
     77   };
     78   struct GNUNET_PQ_ResultSpec rs[] = {
     79     GNUNET_PQ_result_spec_uint64 ("id", delivery_id),
     80     GNUNET_PQ_result_spec_end
     81   };
     82 
     83   GNUNET_assert (NULL != pg->current_merchant_id);
     84   TMH_PQ_prepare_anon (pg,
     85                        "SELECT id FROM merchant.claim_report_delivery($1,$2,$3,$4,$5) id"
     86                        " WHERE id IS NOT NULL");
     87   return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     88                                                    "",
     89                                                    params,
     90                                                    rs);
     91 }
     92 
     93 
     94 enum GNUNET_DB_QueryStatus
     95 TALER_MERCHANTDB_finish_delivery (
     96   struct TALER_MERCHANTDB_PostgresContext *pg,
     97   uint64_t delivery_id,
     98   uint32_t ec,
     99   const char *detail)
    100 {
    101   struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    102   uint32_t ok;
    103   struct GNUNET_PQ_QueryParam params[] = {
    104     GNUNET_PQ_query_param_uint64 (&delivery_id),
    105     GNUNET_PQ_query_param_timestamp (&now),
    106     GNUNET_PQ_query_param_uint32 (&ec),
    107     NULL == detail ? GNUNET_PQ_query_param_null ()
    108     : GNUNET_PQ_query_param_string (detail),
    109     GNUNET_PQ_query_param_end
    110   };
    111   struct GNUNET_PQ_ResultSpec rs[] = {
    112     GNUNET_PQ_result_spec_uint32 ("ok", &ok),
    113     GNUNET_PQ_result_spec_end
    114   };
    115 
    116   GNUNET_assert (NULL != pg->current_merchant_id);
    117   TMH_PQ_prepare_anon (pg,
    118                        "SELECT 1 AS ok FROM merchant.finish_report_delivery($1,$2,$3,$4)");
    119   return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    120                                                    "",
    121                                                    params,
    122                                                    rs);
    123 }
    124 
    125 
    126 enum GNUNET_DB_QueryStatus
    127 TALER_MERCHANTDB_read_deliveries (
    128   struct TALER_MERCHANTDB_PostgresContext *pg,
    129   uint64_t id,
    130   uint64_t schedule_id,
    131   uint64_t offset,
    132   int64_t limit,
    133   json_t **deliveries)
    134 {
    135   struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    136   uint32_t timeout = TALER_EC_GENERIC_TIMEOUT;
    137   struct GNUNET_PQ_QueryParam params[] = {
    138     GNUNET_PQ_query_param_uint64 (&id),
    139     GNUNET_PQ_query_param_uint64 (&schedule_id),
    140     GNUNET_PQ_query_param_uint64 (&offset),
    141     GNUNET_PQ_query_param_int64 (&limit),
    142     GNUNET_PQ_query_param_timestamp (&now),
    143     GNUNET_PQ_query_param_uint32 (&timeout),
    144     GNUNET_PQ_query_param_end
    145   };
    146   struct GNUNET_PQ_ResultSpec rs[] = {
    147     TALER_PQ_result_spec_json ("result", deliveries),
    148     GNUNET_PQ_result_spec_end
    149   };
    150 
    151   GNUNET_assert (NULL != pg->current_merchant_id);
    152   TMH_PQ_prepare_anon (pg,
    153                        "SELECT merchant.read_report_deliveries($1,$2,$3,$4,$5,$6) AS result");
    154   return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    155                                                    "",
    156                                                    params,
    157                                                    rs);
    158 }