diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-08-18 22:56:03 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-08-18 22:56:03 +0200 |
commit | 69bbbd9a4d9dd7bc2bd15cea190743f5c5276636 (patch) | |
tree | 2b5492b8afb1c7a3d9f109273d5ebe4ca5021e74 /src/frontend/generate_taler_contract.php | |
parent | 7e253f1d8b8d93348d8bd1b1144eaee446061dd0 (diff) | |
download | merchant-69bbbd9a4d9dd7bc2bd15cea190743f5c5276636.tar.gz merchant-69bbbd9a4d9dd7bc2bd15cea190743f5c5276636.tar.bz2 merchant-69bbbd9a4d9dd7bc2bd15cea190743f5c5276636.zip |
clean up logic, more comments, etc.
Diffstat (limited to 'src/frontend/generate_taler_contract.php')
-rw-r--r-- | src/frontend/generate_taler_contract.php | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/frontend/generate_taler_contract.php b/src/frontend/generate_taler_contract.php new file mode 100644 index 00000000..8dc77da8 --- /dev/null +++ b/src/frontend/generate_taler_contract.php @@ -0,0 +1,98 @@ +<?php +/* + This file is part of TALER + Copyright (C) 2014, 2015 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/> +*/ + +/* + This code generates a Taler contract in JSON format. Key steps are: + + 1. recover the PHP session with the contract information + 2. generate the JSON to forward to the backend + 3. forward the response with the contract from the backend to + to the wallet +*/ + + +// 1) recover the session information +session_start(); +if ( (! isset($_SESSION['receiver'])) || + (! isset($_SESSION['amount'])) +{ + http_response_code (404); + echo "Please select a contract before getting to this page..."; + exit (0); +} + +/* Obtain session state */ +$receiver = $_SESSION['receiver']; +$amount = intval ($_SESSION['amount']); + +/* Fill in variables for simple JSON contract */ +// fake product id +// --- FIXME: base on receiver for more realism! +$p_id = rand(0,1001); +// generate a front-end transaction id. +// In production context, we might want to +// record this value somewhere together +// with the rest of the contract data. +$transaction_id = rand(0, 1001); +// Human-readable description of this deal +$desc = "Donation to " . $receiver; +// Add the value +$value = $amount; +// We don't have a fraction. +$fraction = 0; +// This is our 'toy' currency +$currency = "KUDOS"; + +// pack the JSON for the contract +// --- FIXME: exact format needs review! +$json = json_encode (array ('desc' => $desc, + 'product' => $p_id, + 'cid' => $transaction_id, + 'price' => array ('value' => $value, + 'fraction' => $fraction, + 'currency' => $currency))); + +// Craft the HTTP request, note that the backend +// could be on an entirely different machine if +// desired. +$req = new http\Client\Request ("POST", + "/backend/contract", + 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) +{ + echo "Error while generating the contract, response code: " . $status_code; +} +else +{ + // send the contract back to the wallet without touching it + echo $resp->body->toString (); +} +?> |