summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_contract.c
blob: 01186beff4be4654fb6375d2c5d4d2e0c4aef14d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
  This file is part of TALER
  (C) 2024 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file taler-merchant-httpd_contract.c
 * @brief shared logic for contract terms handling
 * @author Christian Blättler
 */
#include "platform.h"
#include <jansson.h>
#include "taler-merchant-httpd_contract.h"

enum GNUNET_GenericReturnValue
TMH_serialize_contract_v0 (const struct TALER_MerchantContract *contract,
                           const struct TMH_MerchantInstance *instance,
                           const struct TMH_WireMethod *wm,
                           json_t *exchanges,
                           json_t *products,
                           struct TALER_Amount *max_fee,
                           json_t **out)
{
  json_t *merchant;

  {
    merchant = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("name",
                               instance->settings.name),
      GNUNET_JSON_pack_allow_null (
        GNUNET_JSON_pack_string ("website",
                                 instance->settings.website)),
      GNUNET_JSON_pack_allow_null (
        GNUNET_JSON_pack_string ("email",
                                 instance->settings.email)),
      GNUNET_JSON_pack_allow_null (
        GNUNET_JSON_pack_string ("logo",
                                 instance->settings.logo)));
    GNUNET_assert (NULL != merchant);
    {
      json_t *loca;

      /* Handle merchant address */
      loca = instance->settings.address;
      if (NULL != loca)
      {
        loca = json_deep_copy (loca);
        GNUNET_assert (NULL != loca);
        GNUNET_assert (0 ==
                       json_object_set_new (merchant,
                                            "address",
                                            loca));
      }
    }
    {
      json_t *juri;

      /* Handle merchant jurisdiction */
      juri = instance->settings.jurisdiction;
      if (NULL != juri)
      {
        juri = json_deep_copy (juri);
        GNUNET_assert (NULL != juri);
        GNUNET_assert (0 ==
                       json_object_set_new (merchant,
                                            "jurisdiction",
                                            juri));
      }
    }
  }

  *out = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("summary",
                             contract->v0.summary),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("summary_i18n",
                                      contract->v0.summary_i18n)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("public_reorder_url",
                               contract->public_reorder_url)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("fulfillment_message",
                               contract->v0.fulfillment_message)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("fulfillment_message_i18n",
                                      contract->v0.fulfillment_message_i18n)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("fulfillment_url",
                               contract->v0.fulfillment_url)),
    GNUNET_JSON_pack_array_incref ("products",
                                   products),
    GNUNET_JSON_pack_data_auto ("h_wire",
                                &wm->h_wire),
    GNUNET_JSON_pack_string ("wire_method",
                             wm->wire_method),
    GNUNET_JSON_pack_string ("order_id",
                             contract->order_id),
    GNUNET_JSON_pack_timestamp ("timestamp",
                                contract->timestamp),
    GNUNET_JSON_pack_timestamp ("pay_deadline",
                                contract->pay_deadline),
    GNUNET_JSON_pack_timestamp ("wire_transfer_deadline",
                                contract->wire_deadline),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_timestamp ("delivery_date",
                                  contract->delivery_date)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("delivery_location",
                                      contract->delivery_location)),
    GNUNET_JSON_pack_string ("merchant_base_url",
                             contract->merchant_base_url),
    GNUNET_JSON_pack_object_incref ("merchant",
                                    merchant),
    GNUNET_JSON_pack_data_auto ("merchant_pub",
                                &instance->merchant_pub),
    GNUNET_JSON_pack_array_incref ("exchanges",
                                   exchanges),
    TALER_JSON_pack_amount ("max_fee",
                            max_fee),
    TALER_JSON_pack_amount ("amount",
                            &contract->v0.brutto),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("extra",
                                      (json_t *) contract->extra))
    );

  /* Pack does not work here, because it doesn't set zero-values for timestamps */
  GNUNET_assert (0 ==
                 json_object_set_new (*out,
                                      "refund_deadline",
                                      GNUNET_JSON_from_timestamp (
                                        contract->refund_deadline)));
  GNUNET_log (
    GNUNET_ERROR_TYPE_INFO,
    "Refund deadline for contact is %llu\n",
    (unsigned long long) contract->refund_deadline.abs_time.abs_value_us);
  GNUNET_log (
    GNUNET_ERROR_TYPE_INFO,
    "Wallet timestamp for contact is %llu\n",
    (unsigned long long) contract->timestamp.abs_time.abs_value_us);

  /* Pack does not work here, because it sets zero-values for relative times */
  /* auto_refund should only be set if it is not 0 */
  if (! GNUNET_TIME_relative_is_zero (contract->auto_refund))
  {
    GNUNET_assert (0 ==
                   json_object_set_new (*out,
                                        "auto_refund",
                                        GNUNET_JSON_from_time_rel (
                                          contract->auto_refund)));
  }

  return GNUNET_OK;
};