LCOV - code coverage report
Current view: top level - lib - merchant_api_wallet_get_order.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 156 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018, 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/merchant_api_wallet_get_order.c
      19             :  * @brief Implementation of the GET /orders/$ID request
      20             :  * @author Christian Grothoff
      21             :  * @author Marcello Stanisci
      22             :  * @author Florian Dold
      23             :  */
      24             : #include "platform.h"
      25             : #include <curl/curl.h>
      26             : #include <jansson.h>
      27             : #include <microhttpd.h> /* just for HTTP status codes */
      28             : #include <gnunet/gnunet_util_lib.h>
      29             : #include <gnunet/gnunet_curl_lib.h>
      30             : #include "taler_merchant_service.h"
      31             : #include <taler/taler_json_lib.h>
      32             : #include <taler/taler_signatures.h>
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A GET /orders/$ID handle
      37             :  */
      38             : struct TALER_MERCHANT_OrderWalletGetHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_MERCHANT_OrderWalletGetCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * Convenience function to call the callback in @a owgh with an error code of
      70             :  * @a ec and the exchange body being set to @a reply.
      71             :  *
      72             :  * @param owgh handle providing callback
      73             :  * @param ec error code to return to application
      74             :  * @param reply JSON reply we got from the exchange, can be NULL
      75             :  */
      76             : static void
      77           0 : cb_failure (struct TALER_MERCHANT_OrderWalletGetHandle *owgh,
      78             :             enum TALER_ErrorCode ec,
      79             :             const json_t *reply)
      80             : {
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82             :     .ec = TALER_EC_CHECK_PAYMENT_RESPONSE_MALFORMED,
      83             :     .reply = reply
      84             :   };
      85             : 
      86           0 :   owgh->cb (owgh->cb_cls,
      87             :             &hr,
      88             :             GNUNET_SYSERR,
      89             :             GNUNET_SYSERR,
      90             :             NULL,
      91             :             NULL,
      92             :             NULL,
      93             :             NULL,
      94             :             0,
      95             :             NULL);
      96           0 : }
      97             : 
      98             : 
      99             : /**
     100             :  * Function called when we're done processing the GET /check-payment request.
     101             :  *
     102             :  * @param cls the `struct TALER_MERCHANT_OrderWalletGetHandle`
     103             :  * @param response_code HTTP response code, 0 on error
     104             :  * @param json response body, should be NULL
     105             :  */
     106             : static void
     107           0 : handle_wallet_get_order_finished (void *cls,
     108             :                                   long response_code,
     109             :                                   const void *response)
     110             : {
     111           0 :   struct TALER_MERCHANT_OrderWalletGetHandle *owgh = cls;
     112           0 :   const json_t *json = response;
     113             : 
     114           0 :   owgh->job = NULL;
     115           0 :   switch (response_code)
     116             :   {
     117           0 :   case MHD_HTTP_OK:
     118             :     {
     119             :       struct TALER_Amount refund_amount;
     120             :       json_t *refunds;
     121             :       bool refunded;
     122             :       struct TALER_MerchantPublicKeyP merchant_pub;
     123             :       unsigned int refund_len;
     124             :       struct GNUNET_JSON_Specification spec[] = {
     125           0 :         GNUNET_JSON_spec_bool ("refunded",
     126             :                                &refunded),
     127           0 :         GNUNET_JSON_spec_fixed_auto ("merchant_pub",
     128             :                                      &merchant_pub),
     129           0 :         TALER_JSON_spec_amount ("refund_amount",
     130             :                                 &refund_amount),
     131           0 :         GNUNET_JSON_spec_json ("refunds",
     132             :                                &refunds),
     133           0 :         GNUNET_JSON_spec_end ()
     134             :       };
     135             : 
     136           0 :       if (GNUNET_OK !=
     137           0 :           GNUNET_JSON_parse (json,
     138             :                              spec,
     139             :                              NULL, NULL))
     140             :       {
     141           0 :         GNUNET_break_op (0);
     142           0 :         cb_failure (owgh,
     143             :                     TALER_EC_CHECK_PAYMENT_RESPONSE_MALFORMED,
     144             :                     json);
     145           0 :         TALER_MERCHANT_wallet_order_get_cancel (owgh);
     146           0 :         return;
     147             :       }
     148             : 
     149           0 :       if (! json_is_array (refunds))
     150             :       {
     151           0 :         GNUNET_break_op (0);
     152           0 :         cb_failure (owgh,
     153             :                     TALER_EC_CHECK_PAYMENT_RESPONSE_MALFORMED,
     154             :                     json);
     155           0 :         GNUNET_JSON_parse_free (spec);
     156           0 :         TALER_MERCHANT_wallet_order_get_cancel (owgh);
     157           0 :         return;
     158             :       }
     159             : 
     160           0 :       refund_len = json_array_size (refunds);
     161           0 :       {
     162           0 :         struct TALER_MERCHANT_RefundDetail rds[refund_len];
     163             : 
     164           0 :         memset (rds,
     165             :                 0,
     166             :                 sizeof (rds));
     167           0 :         for (unsigned int i = 0; i<refund_len; i++)
     168             :         {
     169           0 :           struct TALER_MERCHANT_RefundDetail *rd = &rds[i];
     170           0 :           const json_t *jrefund = json_array_get (refunds,
     171             :                                                   i);
     172             :           const char *refund_status_type;
     173             :           uint32_t exchange_status;
     174             :           int ret;
     175             :           struct GNUNET_JSON_Specification espec[] = {
     176           0 :             GNUNET_JSON_spec_uint32 ("exchange_status",
     177             :                                      &exchange_status),
     178           0 :             GNUNET_JSON_spec_end ()
     179             :           };
     180             : 
     181           0 :           if (GNUNET_OK !=
     182           0 :               GNUNET_JSON_parse (jrefund,
     183             :                                  espec,
     184             :                                  NULL, NULL))
     185             :           {
     186           0 :             GNUNET_break_op (0);
     187           0 :             cb_failure (owgh,
     188             :                         TALER_EC_CHECK_PAYMENT_RESPONSE_MALFORMED,
     189             :                         json);
     190           0 :             TALER_MERCHANT_wallet_order_get_cancel (owgh);
     191           0 :             return;
     192             :           }
     193             : 
     194           0 :           if (MHD_HTTP_OK == exchange_status)
     195             :           {
     196             :             struct GNUNET_JSON_Specification rspec[] = {
     197           0 :               GNUNET_JSON_spec_string ("type",
     198             :                                        &refund_status_type),
     199           0 :               GNUNET_JSON_spec_fixed_auto ("exchange_sig",
     200             :                                            &rd->exchange_sig),
     201           0 :               GNUNET_JSON_spec_fixed_auto ("exchange_pub",
     202             :                                            &rd->exchange_pub),
     203           0 :               GNUNET_JSON_spec_uint64 ("rtransaction_id",
     204             :                                        &rd->rtransaction_id),
     205           0 :               GNUNET_JSON_spec_fixed_auto ("coin_pub",
     206             :                                            &rd->coin_pub),
     207           0 :               TALER_JSON_spec_amount ("refund_amount",
     208             :                                       &rd->refund_amount),
     209           0 :               GNUNET_JSON_spec_end ()
     210             :             };
     211             : 
     212           0 :             ret = GNUNET_JSON_parse (jrefund,
     213             :                                      rspec,
     214             :                                      NULL, NULL);
     215           0 :             if (GNUNET_OK == ret)
     216             :             {
     217             :               /* check that type field is correct */
     218           0 :               if (0 != strcmp ("success", refund_status_type))
     219             :               {
     220           0 :                 GNUNET_break_op (0);
     221           0 :                 ret = GNUNET_SYSERR;
     222             :               }
     223             :             }
     224             :           }
     225             :           else
     226             :           {
     227             :             struct GNUNET_JSON_Specification rspec[] = {
     228           0 :               GNUNET_JSON_spec_string ("type",
     229             :                                        &refund_status_type),
     230           0 :               GNUNET_JSON_spec_fixed_auto ("coin_pub",
     231             :                                            &rd->coin_pub),
     232           0 :               GNUNET_JSON_spec_uint64 ("rtransaction_id",
     233             :                                        &rd->rtransaction_id),
     234           0 :               TALER_JSON_spec_amount ("refund_amount",
     235             :                                       &rd->refund_amount),
     236           0 :               GNUNET_JSON_spec_end ()
     237             :             };
     238             : 
     239           0 :             ret = GNUNET_JSON_parse (jrefund,
     240             :                                      rspec,
     241             :                                      NULL, NULL);
     242           0 :             if (GNUNET_OK == ret)
     243             :             {
     244             :               /* parse optional arguments */
     245             :               json_t *jec;
     246             : 
     247           0 :               jec = json_object_get (jrefund,
     248             :                                      "exchange_code");
     249           0 :               if (NULL != jec)
     250             :               {
     251           0 :                 if (! json_is_integer (jec))
     252             :                 {
     253           0 :                   GNUNET_break_op (0);
     254           0 :                   ret = GNUNET_SYSERR;
     255             :                 }
     256             :                 else
     257             :                 {
     258           0 :                   rd->hr.ec = (enum TALER_ErrorCode) json_integer_value (jec);
     259             :                 }
     260             :               }
     261           0 :               rd->hr.reply = json_object_get (jrefund,
     262             :                                               "exchange_reply");
     263             :               /* check that type field is correct */
     264           0 :               if (0 != strcmp ("failure", refund_status_type))
     265             :               {
     266           0 :                 GNUNET_break_op (0);
     267           0 :                 ret = GNUNET_SYSERR;
     268             :               }
     269             :             }
     270             :           }
     271           0 :           if (GNUNET_OK != ret)
     272             :           {
     273           0 :             GNUNET_break_op (0);
     274           0 :             cb_failure (owgh,
     275             :                         TALER_EC_CHECK_PAYMENT_RESPONSE_MALFORMED,
     276             :                         json);
     277           0 :             TALER_MERCHANT_wallet_order_get_cancel (owgh);
     278           0 :             return;
     279             :           }
     280           0 :           rd->hr.http_status = exchange_status;
     281             :         }
     282             : 
     283             :         {
     284           0 :           struct TALER_MERCHANT_HttpResponse hr = {
     285             :             .reply = json,
     286             :             .http_status = MHD_HTTP_OK
     287             :           };
     288             : 
     289           0 :           owgh->cb (owgh->cb_cls,
     290             :                     &hr,
     291             :                     GNUNET_YES,
     292             :                     refunded ? GNUNET_YES : GNUNET_NO,
     293           0 :                     refunded ? &refund_amount : NULL,
     294             :                     NULL, /* paid! */
     295             :                     NULL, /* paid! */
     296             :                     &merchant_pub,
     297             :                     refund_len,
     298             :                     rds);
     299             :         }
     300             :       }
     301           0 :       GNUNET_JSON_parse_free (spec);
     302           0 :       break;
     303             :     }
     304           0 :   case MHD_HTTP_PAYMENT_REQUIRED:
     305             :     {
     306             :       /* Status is: unpaid */
     307           0 :       const char *taler_pay_uri = json_string_value (json_object_get (json,
     308             :                                                                       "taler_pay_uri"));
     309           0 :       const char *already_paid = json_string_value (json_object_get (json,
     310             :                                                                      "already_paid_order_id"));
     311           0 :       if (NULL == taler_pay_uri)
     312             :       {
     313           0 :         GNUNET_break_op (0);
     314           0 :         cb_failure (owgh,
     315             :                     TALER_EC_CHECK_PAYMENT_RESPONSE_MALFORMED,
     316             :                     json);
     317             :       }
     318             :       else
     319             :       {
     320           0 :         struct TALER_MERCHANT_HttpResponse hr = {
     321             :           .reply = json,
     322             :           .http_status = MHD_HTTP_OK
     323             :         };
     324             : 
     325           0 :         owgh->cb (owgh->cb_cls,
     326             :                   &hr,
     327             :                   GNUNET_NO,
     328             :                   GNUNET_NO,
     329             :                   NULL,
     330             :                   taler_pay_uri,
     331             :                   already_paid,
     332             :                   NULL,
     333             :                   0,
     334             :                   NULL);
     335             :       }
     336           0 :       break;
     337             :     }
     338           0 :   default:
     339             :     {
     340             :       struct TALER_MERCHANT_HttpResponse hr;
     341             : 
     342           0 :       TALER_MERCHANT_parse_error_details_ (response,
     343             :                                            response_code,
     344             :                                            &hr);
     345           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     346             :                   "Checking order status failed with HTTP status code %u/%d\n",
     347             :                   (unsigned int) response_code,
     348             :                   (int) hr.ec);
     349           0 :       GNUNET_break_op (0);
     350           0 :       owgh->cb (owgh->cb_cls,
     351             :                 &hr,
     352             :                 GNUNET_SYSERR,
     353             :                 GNUNET_SYSERR,
     354             :                 NULL,
     355             :                 NULL,
     356             :                 NULL,
     357             :                 NULL,
     358             :                 0,
     359             :                 NULL);
     360           0 :       break;
     361             :     }
     362             :   }
     363           0 :   TALER_MERCHANT_wallet_order_get_cancel (owgh);
     364             : }
     365             : 
     366             : 
     367             : /**
     368             :  * Checks the status of a payment.  Issue a GET /orders/$ID request to the
     369             :  * backend.  The @a h_contract serves as identification of the wallet and is
     370             :  * used to authorize the request.
     371             :  *
     372             :  * @param ctx execution context
     373             :  * @param backend_url base URL of the merchant backend
     374             :  * @param order_id order id to identify the payment
     375             :  * @param h_contract hash of the contract to authenticate the wallet
     376             :  * @param timeout timeout to use in long polling (how long may the server wait to reply
     377             :  *        before generating an unpaid response). Note that this is just provided to
     378             :  *        the server, we as client will block until the response comes back or until
     379             :  *        #TALER_MERCHANT_order_get_cancel() is called.
     380             :  * @param session_id for which session should the payment status be checked
     381             :  * @param min_refund long poll for the service to approve a refund exceeding this value;
     382             :  *        use NULL to not wait for any refund (only for payment). Only makes sense
     383             :  *        with a non-zero @a timeout. Can be NULL.
     384             :  * @param await_refund_obtained long poll for the order's refunds to be
     385             :  *        picked up by the wallet.
     386             :  * @param cb callback which will work the response gotten from the backend
     387             :  * @param cb_cls closure to pass to @a cb
     388             :  * @return handle for this operation, NULL upon errors
     389             :  */
     390             : struct TALER_MERCHANT_OrderWalletGetHandle *
     391           0 : TALER_MERCHANT_wallet_order_get (struct GNUNET_CURL_Context *ctx,
     392             :                                  const char *backend_url,
     393             :                                  const char *order_id,
     394             :                                  const struct GNUNET_HashCode *h_contract,
     395             :                                  struct GNUNET_TIME_Relative timeout,
     396             :                                  const char *session_id,
     397             :                                  const struct TALER_Amount *min_refund,
     398             :                                  bool await_refund_obtained,
     399             :                                  TALER_MERCHANT_OrderWalletGetCallback cb,
     400             :                                  void *cb_cls)
     401             : {
     402             :   struct TALER_MERCHANT_OrderWalletGetHandle *owgh;
     403             :   unsigned long long tms;
     404             :   long tlong;
     405             : 
     406           0 :   GNUNET_assert (NULL != backend_url);
     407           0 :   GNUNET_assert (NULL != order_id);
     408           0 :   owgh = GNUNET_new (struct TALER_MERCHANT_OrderWalletGetHandle);
     409           0 :   owgh->ctx = ctx;
     410           0 :   owgh->cb = cb;
     411           0 :   owgh->cb_cls = cb_cls;
     412           0 :   tms = (unsigned long long) (timeout.rel_value_us
     413           0 :                               / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
     414             :   /* set curl timeout to *our* long poll timeout plus one minute
     415             :      (for network latency and processing delays) */
     416           0 :   tlong = (long) (GNUNET_TIME_relative_add (timeout,
     417           0 :                                             GNUNET_TIME_UNIT_MINUTES).
     418             :                   rel_value_us
     419           0 :                   / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
     420             :   {
     421             :     char timeout_ms[32];
     422             :     struct GNUNET_CRYPTO_HashAsciiEncoded h_contract_s;
     423             :     char *path;
     424             : 
     425           0 :     GNUNET_CRYPTO_hash_to_enc (h_contract,
     426             :                                &h_contract_s);
     427           0 :     GNUNET_snprintf (timeout_ms,
     428             :                      sizeof (timeout_ms),
     429             :                      "%llu",
     430             :                      tms);
     431           0 :     GNUNET_asprintf (&path,
     432             :                      "orders/%s",
     433             :                      order_id);
     434           0 :     owgh->url = TALER_url_join (backend_url,
     435             :                                 path,
     436             :                                 "h_contract",
     437             :                                 h_contract_s.encoding,
     438             :                                 "session_id",
     439             :                                 session_id,
     440             :                                 (0 != tms)
     441             :                                 ? "timeout_ms"
     442             :                                 : NULL,
     443             :                                 timeout_ms,
     444             :                                 (NULL != min_refund)
     445             :                                 ? "refund"
     446             :                                 : NULL,
     447             :                                 (NULL != min_refund)
     448           0 :                                 ? TALER_amount2s (min_refund)
     449             :                                 : NULL,
     450             :                                 await_refund_obtained
     451             :                                 ? "await_refund_obtained"
     452             :                                 : NULL,
     453             :                                 await_refund_obtained
     454             :                                 ? "yes"
     455             :                                 : NULL,
     456             :                                 NULL);
     457           0 :     GNUNET_free (path);
     458             :   }
     459           0 :   if (NULL == owgh->url)
     460             :   {
     461           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     462             :                 "Could not construct request URL.\n");
     463           0 :     GNUNET_free (owgh);
     464           0 :     return NULL;
     465             :   }
     466             : 
     467             :   {
     468             :     CURL *eh;
     469             : 
     470           0 :     eh = curl_easy_init ();
     471           0 :     if (NULL == eh)
     472             :     {
     473           0 :       GNUNET_break (0);
     474           0 :       GNUNET_free (owgh->url);
     475           0 :       GNUNET_free (owgh);
     476           0 :       return NULL;
     477             :     }
     478           0 :     if (CURLE_OK !=
     479           0 :         curl_easy_setopt (eh,
     480             :                           CURLOPT_URL,
     481             :                           owgh->url))
     482             :     {
     483           0 :       GNUNET_break (0);
     484           0 :       curl_easy_cleanup (eh);
     485           0 :       GNUNET_free (owgh->url);
     486           0 :       GNUNET_free (owgh);
     487           0 :       return NULL;
     488             :     }
     489           0 :     if (CURLE_OK !=
     490           0 :         curl_easy_setopt (eh,
     491             :                           CURLOPT_TIMEOUT_MS,
     492             :                           tlong))
     493             :     {
     494           0 :       GNUNET_break (0);
     495           0 :       curl_easy_cleanup (eh);
     496           0 :       GNUNET_free (owgh->url);
     497           0 :       GNUNET_free (owgh);
     498           0 :       return NULL;
     499             :     }
     500             : 
     501           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     502             :                 "Checking order status at %s\n",
     503             :                 owgh->url);
     504           0 :     if (NULL == (owgh->job =
     505           0 :                    GNUNET_CURL_job_add (ctx,
     506             :                                         eh,
     507             :                                         &handle_wallet_get_order_finished,
     508             :                                         owgh)))
     509             :     {
     510           0 :       GNUNET_break (0);
     511           0 :       GNUNET_free (owgh->url);
     512           0 :       GNUNET_free (owgh);
     513           0 :       return NULL;
     514             :     }
     515             :   }
     516           0 :   return owgh;
     517             : }
     518             : 
     519             : 
     520             : /**
     521             :  * Cancel a GET /orders/$ID request.
     522             :  *
     523             :  * @param owgh handle to the request to be canceled
     524             :  */
     525             : void
     526           0 : TALER_MERCHANT_wallet_order_get_cancel (
     527             :   struct TALER_MERCHANT_OrderWalletGetHandle *owgh)
     528             : {
     529           0 :   if (NULL != owgh->job)
     530             :   {
     531           0 :     GNUNET_CURL_job_cancel (owgh->job);
     532           0 :     owgh->job = NULL;
     533             :   }
     534           0 :   GNUNET_free (owgh->url);
     535           0 :   GNUNET_free (owgh);
     536           0 : }
     537             : 
     538             : 
     539             : /* end of merchant_api_wallet_get_order.c */

Generated by: LCOV version 1.14