LCOV - code coverage report
Current view: top level - lib - merchant_api_check_payment.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 49 55 89.1 %
Date: 2018-09-21 06:18:36 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2018 GNUnet e.V. and INRIA
       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_check_payment.c
      19             :  * @brief Implementation of the /check-payment GET 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 check payment operation handle
      37             :  */
      38             : struct TALER_MERCHANT_CheckPaymentOperation
      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_CheckPaymentCallback 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             :  * Function called when we're done processing the GET /check-payment request.
      70             :  *
      71             :  * @param cls the `struct TALER_MERCHANT_CheckPaymentOperation`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param json response body, should be NULL
      74             :  */
      75             : static void
      76          11 : handle_check_payment_finished (void *cls,
      77             :                                long response_code,
      78             :                                const json_t *json)
      79             : {
      80          11 :   struct TALER_MERCHANT_CheckPaymentOperation *cpo = cls;
      81          11 :   struct TALER_Amount refund_amount = { 0 };
      82             :   int refunded;
      83             :   
      84          11 :   struct GNUNET_JSON_Specification spec[] = {
      85             :     GNUNET_JSON_spec_boolean ("refunded",
      86             :                               &refunded),
      87             :     TALER_JSON_spec_amount ("refund_amount",
      88             :                             &refund_amount),
      89             :     GNUNET_JSON_spec_end()
      90             :   };
      91             : 
      92          11 :   cpo->job = NULL;
      93             : 
      94          11 :   if (MHD_HTTP_OK != response_code)
      95             :   {
      96           1 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      97             :                 "Checking payment failed with HTTP status code %u\n",
      98             :                 (unsigned int) response_code);
      99           1 :     GNUNET_break_op (0);
     100           1 :     cpo->cb (cpo->cb_cls,
     101             :              response_code,
     102             :              json,
     103             :              GNUNET_SYSERR,
     104             :              GNUNET_SYSERR,
     105             :              &refund_amount,
     106             :              NULL);
     107           1 :     TALER_MERCHANT_check_payment_cancel (cpo);
     108           1 :     return;
     109             :   }
     110             : 
     111          10 :   if (! json_boolean_value (json_object_get (json, "paid")))
     112             :   {
     113           5 :     const char *payment_redirect_url = json_string_value (json_object_get (json, "payment_redirect_url"));
     114           5 :     if (NULL == payment_redirect_url)
     115             :     {
     116           1 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     117             :                   "no payment_redirect_url in unpaid check-payment response\n");
     118           1 :       GNUNET_break_op (0);
     119           1 :       cpo->cb (cpo->cb_cls,
     120             :                0,
     121             :                json,
     122             :                GNUNET_SYSERR,
     123             :                GNUNET_SYSERR,
     124             :                &refund_amount,
     125             :                NULL);
     126             :     } else {
     127           4 :       cpo->cb (cpo->cb_cls,
     128             :                response_code,
     129             :                json,
     130             :                GNUNET_NO,
     131             :                GNUNET_NO,
     132             :                &refund_amount,
     133             :                payment_redirect_url);
     134             :     }
     135           5 :     TALER_MERCHANT_check_payment_cancel (cpo);
     136           5 :     return;
     137             :   }
     138             : 
     139           5 :   if (GNUNET_OK !=
     140           5 :       GNUNET_JSON_parse (json,
     141             :                          spec,
     142             :                          NULL, NULL))
     143             :   {
     144           1 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     145             :                 "check payment failed to parse JSON\n");
     146           1 :     GNUNET_break_op (0);
     147           1 :     cpo->cb (cpo->cb_cls,
     148             :              0,
     149             :              json,
     150             :              GNUNET_SYSERR,
     151             :              GNUNET_SYSERR,
     152             :              &refund_amount,
     153             :              NULL);
     154           1 :     TALER_MERCHANT_check_payment_cancel (cpo);
     155           1 :     return;
     156             :   }
     157             : 
     158           4 :   cpo->cb (cpo->cb_cls,
     159             :            MHD_HTTP_OK,
     160             :            json,
     161             :            GNUNET_YES,
     162             :            refunded,
     163             :            &refund_amount,
     164             :            NULL);
     165           4 :   GNUNET_JSON_parse_free (spec);
     166           4 :   TALER_MERCHANT_check_payment_cancel (cpo);
     167             : }
     168             : 
     169             : 
     170             : /**
     171             :  * Issue a /check-payment request to the backend.  Checks the status
     172             :  * of a payment.
     173             :  *
     174             :  * @param ctx execution context
     175             :  * @param backend_url base URL of the merchant backend
     176             :  * @param instance instance used for the transaction
     177             :  * @param order_id order id to identify the payment
     178             :  * @parem resource_url resource URL to identify duplicate payments (can be NULL)
     179             :  * @parem session_id sesion id for the payment (or NULL if the payment is not bound to a session) 
     180             :  * @parem session_id sesion signature for the payment (or NULL if the payment
     181             :  *        is not bound to a session or the session is not signed yet) 
     182             :  * @param check_payment_cb callback which will work the response gotten from the backend
     183             :  * @param check_payment_cb_cls closure to pass to @a check_payment_cb
     184             :  * @return handle for this operation, NULL upon errors
     185             :  */
     186             : struct TALER_MERCHANT_CheckPaymentOperation *
     187          11 : TALER_MERCHANT_check_payment (struct GNUNET_CURL_Context *ctx,
     188             :                               const char *backend_url,
     189             :                               const char *instance,
     190             :                               const char *order_id,
     191             :                               const char *resource_url,
     192             :                               const char *session_id,
     193             :                               const char *session_sig,
     194             :                               TALER_MERCHANT_CheckPaymentCallback check_payment_cb,
     195             :                               void *check_payment_cb_cls)
     196             : {
     197             :   struct TALER_MERCHANT_CheckPaymentOperation *cpo;
     198             :   CURL *eh;
     199             : 
     200          11 :   GNUNET_assert (NULL != backend_url);
     201          11 :   GNUNET_assert (NULL != instance);
     202          11 :   GNUNET_assert (NULL != order_id);
     203             : 
     204          11 :   cpo = GNUNET_new (struct TALER_MERCHANT_CheckPaymentOperation);
     205          11 :   cpo->ctx = ctx;
     206          11 :   cpo->cb = check_payment_cb;
     207          11 :   cpo->cb_cls = check_payment_cb_cls;
     208          11 :   cpo->url = TALER_url_join (backend_url, "/check-payment",
     209             :                              "instance", instance,
     210             :                              "order_id", order_id,
     211             :                              "resource_url", resource_url,
     212             :                              "session_id", session_id,
     213             :                              "session_sig", session_sig,
     214             :                              NULL);
     215          11 :   eh = curl_easy_init ();
     216          11 :   if (CURLE_OK != curl_easy_setopt (eh,
     217             :                                     CURLOPT_URL,
     218             :                                     cpo->url))
     219             :   {
     220           0 :     GNUNET_break (0);
     221           0 :     return NULL;
     222             :   }
     223             : 
     224          11 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     225             :               "checking payment from %s\n",
     226             :               cpo->url);
     227             : 
     228          11 :   if (NULL == (cpo->job = GNUNET_CURL_job_add (ctx,
     229             :                                                eh,
     230             :                                                GNUNET_YES,
     231             :                                                &handle_check_payment_finished,
     232             :                                                cpo)))
     233             :   {
     234           0 :     GNUNET_break (0);
     235           0 :     return NULL;
     236             :   }
     237          11 :   return cpo;
     238             : }
     239             : 
     240             : 
     241             : /**
     242             :  * Cancel a GET /check-payment request.
     243             :  *
     244             :  * @param cph handle to the request to be canceled
     245             :  */
     246             : void
     247          11 : TALER_MERCHANT_check_payment_cancel (struct TALER_MERCHANT_CheckPaymentOperation *cph)
     248             : {
     249          11 :   if (NULL != cph->job)
     250             :   {
     251           0 :     GNUNET_CURL_job_cancel (cph->job);
     252           0 :     cph->job = NULL;
     253             :   }
     254          11 :   GNUNET_free (cph->url);
     255          11 :   GNUNET_free (cph);
     256          11 : }
     257             : 
     258             : /* end of merchant_api_check_payment.c */

Generated by: LCOV version 1.13