summaryrefslogtreecommitdiff
path: root/src/lib/merchant_api_merchant_get_order.c
blob: 53811ec1a627cbffcbb1187acc8782361d3609a3 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
  This file is part of TALER
  Copyright (C) 2018, 2019, 2020 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 2.1, 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file lib/merchant_api_merchant_get_order.c
 * @brief Implementation of the GET /private/orders/$ORDER request
 * @author Christian Grothoff
 * @author Marcello Stanisci
 * @author Florian Dold
 */
#include "platform.h"
#include <curl/curl.h>
#include <jansson.h>
#include <microhttpd.h> /* just for HTTP status codes */
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_curl_lib.h>
#include "taler_merchant_service.h"
#include <taler/taler_json_lib.h>
#include <taler/taler_signatures.h>


/**
 * @brief A GET /private/orders/$ORDER handle
 */
struct TALER_MERCHANT_OrderMerchantGetHandle
{

  /**
   * The url for this request.
   */
  char *url;

  /**
   * Handle for the request.
   */
  struct GNUNET_CURL_Job *job;

  /**
   * Function to call with the result.
   */
  TALER_MERCHANT_OrderMerchantGetCallback cb;

  /**
   * Closure for @a cb.
   */
  void *cb_cls;

  /**
   * Reference to the execution context.
   */
  struct GNUNET_CURL_Context *ctx;
};


/**
 * Function called when we're done processing the GET /private/orders/$ORDER
 * request and we got an HTTP status of OK and the order was unpaid. Parse
 * the response and call the callback.
 *
 * @param omgh handle for the request
 * @param[in,out] hr HTTP response we got
 */
static void
handle_unpaid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
               struct TALER_MERCHANT_HttpResponse *hr)
{
  const char *taler_pay_uri
    = json_string_value (json_object_get (hr->reply,
                                          "taler_pay_uri"));
  const char *already_paid_order_id
    = json_string_value (json_object_get (hr->reply,
                                          "already_paid_order_id"));
  if (NULL == taler_pay_uri)
  {
    GNUNET_break_op (0);
    hr->http_status = 0;
    hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
    omgh->cb (omgh->cb_cls,
              hr,
              NULL);
    return;
  }
  {
    struct TALER_MERCHANT_OrderStatusResponse osr = {
      .paid = false,
      .details.unpaid.taler_pay_uri = taler_pay_uri,
      .details.unpaid.already_paid_order_id = already_paid_order_id
    };

    omgh->cb (omgh->cb_cls,
              hr,
              &osr);
  }
}


/**
 * Function called when we're done processing the GET /private/orders/$ORDER
 * request and we got an HTTP status of OK and the order was paid. Parse
 * the response and call the callback.
 *
 * @param omgh handle for the request
 * @param[in,out] hr HTTP response we got
 */
static void
handle_paid (struct TALER_MERCHANT_OrderMerchantGetHandle *omgh,
             struct TALER_MERCHANT_HttpResponse *hr)
{
  uint32_t ec32;
  uint32_t hc32;
  json_t *wire_details;
  json_t *wire_reports;
  json_t *refund_details;
  struct TALER_MERCHANT_OrderStatusResponse osr = {
    .paid = true
  };
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_bool ("refunded",
                           &osr.details.paid.refunded),
    GNUNET_JSON_spec_bool ("wired",
                           &osr.details.paid.wired),
    TALER_JSON_spec_amount ("deposit_total",
                            &osr.details.paid.deposit_total),
    GNUNET_JSON_spec_uint32 ("exchange_ec",
                             &ec32),
    GNUNET_JSON_spec_uint32 ("exchange_hc",
                             &hc32),
    TALER_JSON_spec_amount ("refund_amount",
                            &osr.details.paid.refund_amount),
    GNUNET_JSON_spec_json ("contract_terms",
                           (json_t **) &osr.details.paid.contract_terms),
    GNUNET_JSON_spec_json ("wire_details",
                           &wire_details),
    GNUNET_JSON_spec_json ("wire_reports",
                           &wire_reports),
    GNUNET_JSON_spec_json ("refund_details",
                           &refund_details),
    GNUNET_JSON_spec_end ()
  };

  if (GNUNET_OK !=
      GNUNET_JSON_parse (hr->reply,
                         spec,
                         NULL, NULL))
  {
    GNUNET_break_op (0);
    hr->http_status = 0;
    hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
    omgh->cb (omgh->cb_cls,
              hr,
              NULL);
    return;
  }
  if (! (json_is_array (wire_details) &&
         json_is_array (wire_reports) &&
         json_is_array (refund_details) &&
         json_is_object (osr.details.paid.contract_terms)) )
  {
    GNUNET_break_op (0);
    hr->http_status = 0;
    hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
    omgh->cb (omgh->cb_cls,
              hr,
              NULL);
    GNUNET_JSON_parse_free (spec);
    return;
  }
  osr.details.paid.exchange_ec = (enum TALER_ErrorCode) ec32;
  osr.details.paid.exchange_hc = (unsigned int) hc32;
  {
    unsigned int wts_len = json_array_size (wire_details);
    unsigned int wrs_len = json_array_size (wire_reports);
    unsigned int ref_len = json_array_size (refund_details);
    struct TALER_MERCHANT_WireTransfer wts[wts_len];
    struct TALER_MERCHANT_WireReport wrs[wrs_len];
    struct TALER_MERCHANT_RefundOrderDetail ref[ref_len];

    for (unsigned int i = 0; i<wts_len; i++)
    {
      struct TALER_MERCHANT_WireTransfer *wt = &wts[i];
      const json_t *w = json_array_get (wire_details,
                                        i);
      struct GNUNET_JSON_Specification ispec[] = {
        GNUNET_JSON_spec_string ("exchange_url",
                                 &wt->exchange_url),
        GNUNET_JSON_spec_fixed_auto ("wtid",
                                     &wt->wtid),
        TALER_JSON_spec_absolute_time ("execution_time",
                                       &wt->execution_time),
        TALER_JSON_spec_amount ("amount",
                                &wt->total_amount),
        GNUNET_JSON_spec_bool ("confirmed",
                               &wt->confirmed),
        GNUNET_JSON_spec_end ()
      };

      if (GNUNET_OK !=
          GNUNET_JSON_parse (w,
                             ispec,
                             NULL, NULL))
      {
        GNUNET_break_op (0);
        hr->http_status = 0;
        hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
        omgh->cb (omgh->cb_cls,
                  hr,
                  NULL);
        GNUNET_JSON_parse_free (spec);
        return;
      }
    }

    for (unsigned int i = 0; i<wrs_len; i++)
    {
      struct TALER_MERCHANT_WireReport *wr = &wrs[i];
      const json_t *w = json_array_get (wire_reports, i);
      uint32_t c32;
      uint32_t eec32;
      uint32_t ehs32;
      struct GNUNET_JSON_Specification ispec[] = {
        GNUNET_JSON_spec_uint32 ("code",
                                 &c32),
        GNUNET_JSON_spec_uint32 ("exchange_ec",
                                 &eec32),
        GNUNET_JSON_spec_uint32 ("exchange_hc",
                                 &ehs32),
        GNUNET_JSON_spec_string ("hint",
                                 &wr->hint),
        GNUNET_JSON_spec_fixed_auto ("coin_pub",
                                     &wr->coin_pub),
        GNUNET_JSON_spec_end ()
      };

      if (GNUNET_OK !=
          GNUNET_JSON_parse (w,
                             ispec,
                             NULL, NULL))
      {
        GNUNET_break_op (0);
        hr->http_status = 0;
        hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
        omgh->cb (omgh->cb_cls,
                  hr,
                  NULL);
        GNUNET_JSON_parse_free (spec);
        return;
      }
      wr->code = (enum TALER_ErrorCode) c32;
      wr->hr.ec = (enum TALER_ErrorCode) eec32;
      wr->hr.http_status = (unsigned int) ehs32;
    }

    for (unsigned int i = 0; i<ref_len; i++)
    {
      struct TALER_MERCHANT_RefundOrderDetail *ro = &ref[i];
      const json_t *w = json_array_get (refund_details,
                                        i);
      struct GNUNET_JSON_Specification ispec[] = {
        TALER_JSON_spec_amount ("amount",
                                &ro->refund_amount),
        GNUNET_JSON_spec_string ("reason",
                                 &ro->reason),
        TALER_JSON_spec_absolute_time ("timestamp",
                                       &ro->refund_time),
        GNUNET_JSON_spec_end ()
      };

      if (GNUNET_OK !=
          GNUNET_JSON_parse (w,
                             ispec,
                             NULL, NULL))
      {
        GNUNET_break_op (0);
        hr->http_status = 0;
        hr->ec = TALER_EC_MERCHANT_ORDER_GET_REPLY_MALFORMED;
        omgh->cb (omgh->cb_cls,
                  hr,
                  NULL);
        GNUNET_JSON_parse_free (spec);
        return;
      }
    }

    osr.details.paid.wts = wts;
    osr.details.paid.wts_len = wts_len;
    osr.details.paid.wrs = wrs;
    osr.details.paid.wrs_len = wrs_len;
    osr.details.paid.refunds = ref;
    osr.details.paid.refunds_len = ref_len;
    omgh->cb (omgh->cb_cls,
              hr,
              &osr);
  }
  GNUNET_JSON_parse_free (spec);
}


/**
 * Function called when we're done processing the GET /private/orders/$ORDER
 * request.
 *
 * @param cls the `struct TALER_MERCHANT_OrderMerchantGetHandle`
 * @param response_code HTTP response code, 0 on error
 * @param json response body, should be NULL
 */
static void
handle_merchant_order_get_finished (void *cls,
                                    long response_code,
                                    const void *response)
{
  struct TALER_MERCHANT_OrderMerchantGetHandle *omgh = cls;
  const json_t *json = response;
  struct TALER_MERCHANT_HttpResponse hr = {
    .http_status = (unsigned int) response_code,
    .reply = json
  };

  omgh->job = NULL;
  switch (response_code)
  {
  case MHD_HTTP_NOT_FOUND:
    hr.ec = TALER_JSON_get_error_code (json);
    hr.hint = TALER_JSON_get_error_hint (json);
    omgh->cb (omgh->cb_cls,
              &hr,
              NULL);
    TALER_MERCHANT_merchant_order_get_cancel (omgh);
    return;
  case MHD_HTTP_OK:
    /* see below */
    break;
  default:
    hr.ec = TALER_JSON_get_error_code (json);
    hr.hint = TALER_JSON_get_error_hint (json);
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Polling payment failed with HTTP status code %u/%d\n",
                (unsigned int) response_code,
                (int) hr.ec);
    GNUNET_break_op (0);
    omgh->cb (omgh->cb_cls,
              &hr,
              NULL);
    TALER_MERCHANT_merchant_order_get_cancel (omgh);
    return;
  }

  /* HTTP OK */
  if (! json_boolean_value (json_object_get (json, "paid")))
    handle_unpaid (omgh,
                   &hr);
  else
    handle_paid (omgh,
                 &hr);
}


/**
 * Checks the status of a payment.  Issue a GET /private/orders/$ID request to
 * the backend.
 *
 * @param ctx execution context
 * @param backend_url base URL of the merchant backend
 * @param order_id order id to identify the payment
 * @param session_id sesion id for the payment (or NULL if the check is not
 *                   bound to a session)
 * @param transfer if true, obtain the wire transfer status from the exhcange.
 *               Otherwise, the wire transfer status MAY be returned if it is available.
 * @param timeout timeout to use in long polling (how long may the server wait to reply
 *        before generating an unpaid response). Note that this is just provided to
 *        the server, we as client will block until the response comes back or until
 *        #TALER_MERCHANT_order_get_cancel() is called.
 * @param cb callback which will work the response gotten from the backend
 * @param cb_cls closure to pass to @a cb
 * @return handle for this operation, NULL upon errors
 */
struct TALER_MERCHANT_OrderMerchantGetHandle *
TALER_MERCHANT_merchant_order_get (struct GNUNET_CURL_Context *ctx,
                                   const char *backend_url,
                                   const char *order_id,
                                   const char *session_id,
                                   bool transfer,
                                   struct GNUNET_TIME_Relative timeout,
                                   TALER_MERCHANT_OrderMerchantGetCallback cb,
                                   void *cb_cls)
{
  struct TALER_MERCHANT_OrderMerchantGetHandle *omgh;
  unsigned long long tms;
  long tlong;

  GNUNET_assert (NULL != backend_url);
  GNUNET_assert (NULL != order_id);
  tms = (unsigned long long) (timeout.rel_value_us
                              / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
  /* set curl timeout to *our* long poll timeout plus one minute
     (for network latency and processing delays) */
  tlong = (long) (GNUNET_TIME_relative_add (timeout,
                                            GNUNET_TIME_UNIT_MINUTES).
                  rel_value_us
                  / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
  omgh = GNUNET_new (struct TALER_MERCHANT_OrderMerchantGetHandle);
  omgh->ctx = ctx;
  omgh->cb = cb;
  omgh->cb_cls = cb_cls;
  {
    char *path;
    char timeout_ms[32];

    GNUNET_snprintf (timeout_ms,
                     sizeof (timeout_ms),
                     "%llu",
                     tms);
    GNUNET_asprintf (&path,
                     "private/orders/%s",
                     order_id);
    omgh->url = TALER_url_join (backend_url,
                                path,
                                "session_id", session_id,
                                "transfer", transfer ? "YES" : "NO",
                                (0 != tms) ? "timeout_ms" : NULL,
                                timeout_ms,
                                NULL);
  }
  if (NULL == omgh->url)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Could not construct request URL.\n");
    GNUNET_free (omgh);
    return NULL;
  }

  {
    CURL *eh;

    eh = curl_easy_init ();
    if (NULL == eh)
    {
      GNUNET_break (0);
      GNUNET_free (omgh->url);
      GNUNET_free (omgh);
      return NULL;
    }
    if (CURLE_OK != curl_easy_setopt (eh,
                                      CURLOPT_URL,
                                      omgh->url))
    {
      GNUNET_break (0);
      curl_easy_cleanup (eh);
      GNUNET_free (omgh->url);
      GNUNET_free (omgh);
      return NULL;
    }
    if (CURLE_OK != curl_easy_setopt (eh,
                                      CURLOPT_TIMEOUT_MS,
                                      tlong))
    {
      GNUNET_break (0);
      curl_easy_cleanup (eh);
      GNUNET_free (omgh->url);
      GNUNET_free (omgh);
      return NULL;
    }

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Getting order status from %s\n",
                omgh->url);
    if (NULL == (omgh->job =
                   GNUNET_CURL_job_add (ctx,
                                        eh,
                                        GNUNET_YES,
                                        &handle_merchant_order_get_finished,
                                        omgh)))
    {
      GNUNET_break (0);
      GNUNET_free (omgh->url);
      GNUNET_free (omgh);
      return NULL;
    }
  }
  return omgh;
}


/**
 * Cancel a GET /private/orders/$ORDER request.
 *
 * @param omgh handle to the request to be canceled
 */
void
TALER_MERCHANT_merchant_order_get_cancel (
  struct TALER_MERCHANT_OrderMerchantGetHandle *omgh)
{
  if (NULL != omgh->job)
  {
    GNUNET_CURL_job_cancel (omgh->job);
    omgh->job = NULL;
  }
  GNUNET_free (omgh->url);
  GNUNET_free (omgh);
}


/* end of merchant_api_merchant_get_order.c */