diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-10-29 22:59:58 +0100 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2015-10-29 22:59:58 +0100 |
commit | 1a345ed83345c54631540f9f509129d048bdc33f (patch) | |
tree | 26bfc72cc79d78a9a0fecf55c0f38c8bfaa697d5 | |
parent | aafc4a5ab30da39132f0dda8862762bfa3e5e19d (diff) | |
download | merchant-1a345ed83345c54631540f9f509129d048bdc33f.tar.gz merchant-1a345ed83345c54631540f9f509129d048bdc33f.tar.bz2 merchant-1a345ed83345c54631540f9f509129d048bdc33f.zip |
Adding HTTP capability to frontend /pay handler,
plus fixing compilation errors relative to /pay
handler (in backend)
-rw-r--r-- | src/backend/Makefile.am | 5 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd.c | 12 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_contract.c | 8 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_pay.c | 22 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_pay.h | 46 | ||||
-rw-r--r-- | src/frontend/deposit.php | 56 | ||||
-rw-r--r-- | src/frontend/generate_taler_contract.php | 2 |
7 files changed, 137 insertions, 14 deletions
diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index f0c543ea..d5b283b8 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -13,7 +13,10 @@ taler_merchant_httpd_SOURCES = \ taler-mint-httpd.h \ taler-mint-httpd_mhd.c taler-mint-httpd_mhd.h \ taler-merchant-httpd_contract.c \ - taler-merchant-httpd_contract.h + taler-merchant-httpd_contract.h \ + taler-merchant-httpd_pay.c \ + taler-merchant-httpd_pay.h + taler_merchant_httpd_LDADD = \ $(LIBGCRYPT_LIBS) \ diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index d6d81124..3806115a 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -35,6 +35,7 @@ #include "taler_merchant_lib.h" #include "taler-mint-httpd_mhd.h" #include "taler-merchant-httpd_contract.h" +#include "taler-merchant-httpd_pay.h" /** * Our hostname @@ -183,7 +184,7 @@ url_handler (void *cls, "Hello, Customer.\n", 0, &TMH_MHD_handler_static_response, MHD_HTTP_OK }, - { "/contract", MHD_HTTP_METHOD_GET, "application/json", + { "/contract", MHD_HTTP_METHOD_POST, "application/json", NULL, 0, &MH_handler_contract, MHD_HTTP_OK }, @@ -191,6 +192,15 @@ url_handler (void *cls, "Only POST is allowed", 0, &TMH_MHD_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED }, + { "/pay", MHD_HTTP_METHOD_POST, "application/json", + NULL, 0, + &MH_handler_pay, MHD_HTTP_OK }, + + { "/pay", NULL, "text/plain", + "Only POST is allowed", 0, + &TMH_MHD_handler_send_json_pack_error, MHD_HTTP_METHOD_NOT_ALLOWED }, + + {NULL, NULL, NULL, NULL, 0, 0 } }; diff --git a/src/backend/taler-merchant-httpd_contract.c b/src/backend/taler-merchant-httpd_contract.c index d28ad4ed..4d1584b3 100644 --- a/src/backend/taler-merchant-httpd_contract.c +++ b/src/backend/taler-merchant-httpd_contract.c @@ -77,10 +77,10 @@ MH_handler_contract (struct TMH_RequestHandler *rh, struct GNUNET_CRYPTO_EddsaSignature contract_sig; res = TMH_PARSE_post_json (connection, - connection_cls, - upload_data, - upload_data_size, - &root); + connection_cls, + upload_data, + upload_data_size, + &root); if (GNUNET_SYSERR == res) return MHD_NO; /* the POST's body has to be further fetched */ diff --git a/src/backend/taler-merchant-httpd_pay.c b/src/backend/taler-merchant-httpd_pay.c index b03da899..fb74cda8 100644 --- a/src/backend/taler-merchant-httpd_pay.c +++ b/src/backend/taler-merchant-httpd_pay.c @@ -45,9 +45,11 @@ extern long long salt; * Accomplish this payment. * @param rh context of the handler * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) + * @param[in,out] connection_cls the connection's closure + * (can be updated) * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data + * @param[in,out] upload_data_size number of bytes (left) in @a + * upload_data * @return MHD result code */ int @@ -57,6 +59,20 @@ MH_handler_pay (struct TMH_RequestHandler *rh, const char *upload_data, size_t *upload_data_size) { + json_t *root; - json_t *j_wire; + int res; + + res = TMH_PARSE_post_json (connection, + connection_cls, + upload_data, + upload_data_size, + &root); + if (GNUNET_SYSERR == res) + return MHD_NO; + /* the POST's body has to be further fetched */ + if ((GNUNET_NO == res) || (NULL == root)) + return MHD_YES; + + /* 1 Check if the total deposit fee is leq the limit */ } diff --git a/src/backend/taler-merchant-httpd_pay.h b/src/backend/taler-merchant-httpd_pay.h new file mode 100644 index 00000000..6a796e06 --- /dev/null +++ b/src/backend/taler-merchant-httpd_pay.h @@ -0,0 +1,46 @@ +/* + This file is part of TALER + (C) 2014 Christian Grothoff (and other contributing authors) + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU 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, If not, see <http://www.gnu.org/licenses/> +*/ + +/** + * @file merchant/backend/taler-merchant-httpd_contract.h + * @brief headers for /pay handler + * @author Marcello Stanisci + */ + +#ifndef TALER_MINT_HTTPD_PAY_H +#define TALER_MINT_HTTPD_PAY_H +#include <microhttpd.h> +#include "taler-mint-httpd.h" + +/** + * Manage a payment + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] connection_cls the connection's closure (can be updated) + * @param upload_data upload data + * @param[in,out] upload_data_size number of bytes (left) in @a upload_data + * + * @return MHD result code + */ +int +MH_handler_pay (struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + void **connection_cls, + const char *upload_data, + size_t *upload_data_size); + +#endif diff --git a/src/frontend/deposit.php b/src/frontend/deposit.php index 27cf9360..aabc456e 100644 --- a/src/frontend/deposit.php +++ b/src/frontend/deposit.php @@ -16,11 +16,59 @@ */ +/* + This serving module adds the 'max_fee' field to the object which + sends to the backend, and optionally the field 'edate' (indicating + to the mint the tollerated deadline to receive funds for this payment) + NOTE: 'max_fee' must be consistent with the same value indicated within + the contract; thus, a "real" merchant must implement such a mapping + +*/ + $post_body = file_get_contents('php://input'); -$post_obj = json_decode($post_body); -//debug and get test-ish data for the backend -file_put_contents('/tmp/deposit_permission', - json_encode($post_obj, JSON_PRETTY_PRINT)); +$deposit_permission = json_decode ($post_body); +$max_fee = array ('max_fee' => array ('value' => 3, + 'fraction' => 01010, + 'currency' => $currency)); + + +$new_deposit_permission = array_merge ($deposit_permission, $max_fee); +// Craft the HTTP request, note that the backend +// could be on an entirely different machine if +// desired. +file_put_contents('/tmp/deposit_permission_maxfee.sample', json_encode($new_deposit_permission, JSON_PRETTY_PRINT)); +return; + +$req = new http\Client\Request ("GET", + "http://" . $_SERVER["SERVER_NAME"] . "/backend/pay", + array ("Content-Type" => "application/json")); +$req->getBody()->append ($json); + +// Execute the HTTP request +$client = new http\Client; +$client->enqueue($req)->send (); + +// Fetch the response +$resp = $client->getResponse (); +$status_code = $resp->getResponseCode (); + +// Our response code is the same we got from the backend: +http_response_code ($status_code); + +// Now generate our body +if ($status_code != 200) +{ + /* error: just forwarding to the wallet what + gotten from the backend (which is forwarding 'as is' + the error gotten from the mint) */ + echo $resp->body->toString (); + +} +else +{ + echo "Payment succedeed!\n"; +} + ?> diff --git a/src/frontend/generate_taler_contract.php b/src/frontend/generate_taler_contract.php index 090213a2..2010fefd 100644 --- a/src/frontend/generate_taler_contract.php +++ b/src/frontend/generate_taler_contract.php @@ -152,7 +152,7 @@ if ($cli_debug && !$backend_test) // Craft the HTTP request, note that the backend // could be on an entirely different machine if // desired. -$req = new http\Client\Request ("GET", +$req = new http\Client\Request ("POST", "http://" . $_SERVER["SERVER_NAME"] . "/backend/contract", array ("Content-Type" => "application/json")); $req->getBody()->append ($json); |