From 6fe7b256a692a24b6e119d0c1532dee96c9c9c4a Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 25 Jan 2016 13:45:09 +0100 Subject: New fulfillment URL mechanism. --- src/frontend/fulfillment.php | 1 - src/frontend/generate_taler_contract.php | 148 +++++++++++++++++-------------- src/frontend/pay.php | 47 ++++------ 3 files changed, 97 insertions(+), 99 deletions(-) diff --git a/src/frontend/fulfillment.php b/src/frontend/fulfillment.php index 6d49971b..936329de 100644 --- a/src/frontend/fulfillment.php +++ b/src/frontend/fulfillment.php @@ -37,7 +37,6 @@ You should have received a copy of the GNU Lesser General Public License along with TALER; see the file COPYING. If not, If not, see - */ function generate_msg ($link){ diff --git a/src/frontend/generate_taler_contract.php b/src/frontend/generate_taler_contract.php index e44a4d9b..1fdfdc34 100644 --- a/src/frontend/generate_taler_contract.php +++ b/src/frontend/generate_taler_contract.php @@ -1,7 +1,7 @@ - */ - include 'util.php'; session_start(); if (!isset($_SESSION['receiver'])) { - http_response_code (400); + http_response_code(400); die(); } $receiver = $_SESSION['receiver']; $receiver = $_SESSION['receiver']; -$amount_value = intval ($_SESSION['amount_value']); -$amount_fraction = intval ($_SESSION['amount_fraction']); +$amount_value = intval($_SESSION['amount_value']); +$amount_fraction = intval($_SESSION['amount_fraction']); $currency = $_SESSION['currency']; /* 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; + // The tax for this deal -$teatax = array ('value' => 1, - 'fraction' => 0, - 'currency' => $currency); +$teatax = array('value' => 1, + 'fraction' => 0, + 'currency' => $currency); // Take a timestamp $now = new DateTime('now'); // pack the JSON for the contract -// --- FIXME: exact format needs review! -$contract = array ('amount' => array ('value' => $amount_value, - 'fraction' => $amount_fraction, - 'currency' => $currency), - 'max_fee' => array ('value' => 3, - 'fraction' => 01010, - 'currency' => $currency), - 'transaction_id' => $transaction_id, - 'products' => array ( - array ('description' => $desc, - 'quantity' => 1, - 'price' => array ('value' => $amount_value, - 'fraction' => $amount_fraction, - 'currency' => $currency), - 'product_id' => $p_id, - 'taxes' => array (array ('teatax' => $teatax)), - 'delivery_date' => "Some Date Format", - 'delivery_location' => 'LNAME1')), - 'timestamp' => "/Date(" . $now->getTimestamp() . ")/", - 'expiry' => "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/", - 'refund_deadline' => "/Date(" . $now->add(new DateInterval('P3M'))->getTimestamp() . ")/", - 'merchant' => array ('address' => 'LNAME2', - 'name' => 'test merchant', - 'jurisdiction' => 'LNAME3'), - - 'locations' => array ('LNAME1' => array ('country' => 'Test Country', - 'city' => 'Test City', - 'state' => 'Test State', - 'region' => 'Test Region', - 'province' => 'Test Province', - 'ZIP code' => 4908, - 'street' => 'test street', - 'street number' => 20), - 'LNAME2' => array ('country' => 'Test Country', - 'city' => 'Test City', - 'state' => 'Test State', - 'region' => 'Test Region', - 'province' => 'Test Province', - 'ZIP code' => 4908, - 'street' => 'test street', - 'street number' => 20), - 'LNAME3' => array ('country' => 'Test Country', - 'city' => 'Test City', - 'state' => 'Test State', - 'region' => 'Test Region', - 'province' => 'Test Province', - 'ZIP code' => 4908))); - -$json = json_encode(array('contract' => $contract, JSON_PRETTY_PRINT)); +$contract = array( + 'amount' => array( + 'value' => $amount_value, + 'fraction' => $amount_fraction, + 'currency' => $currency), + 'max_fee' => array('value' => 3, + 'fraction' => 01010, + 'currency' => $currency), + 'transaction_id' => $transaction_id, + 'products' => array( + array( + 'description' => $desc, + 'quantity' => 1, + 'price' => array ('value' => $amount_value, + 'fraction' => $amount_fraction, + 'currency' => $currency), + 'product_id' => $p_id, + 'taxes' => array(array('teatax' => $teatax)), + 'delivery_date' => "Some Date Format", + 'delivery_location' => 'LNAME1')), + 'timestamp' => "/Date(" . $now->getTimestamp() . ")/", + 'expiry' => "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/", + 'refund_deadline' => "/Date(" . $now->add(new DateInterval('P3M'))->getTimestamp() . ")/", + 'merchant' => array( + 'address' => 'LNAME2', + 'name' => 'test merchant', + 'jurisdiction' => 'LNAME3'), + 'locations' => array( + 'LNAME1' => array( + 'country' => 'Test Country', + 'city' => 'Test City', + 'state' => 'Test State', + 'region' => 'Test Region', + 'province' => 'Test Province', + 'ZIP code' => 4908, + 'street' => 'test street', + 'street number' => 20), + 'LNAME2' => array( + 'country' => 'Test Country', + 'city' => 'Test City', + 'state' => 'Test State', + 'region' => 'Test Region', + 'province' => 'Test Province', + 'ZIP code' => 4908, + 'street' => 'test street', + 'street number' => 20), + 'LNAME3' => array( + 'country' => 'Test Country', + 'city' => 'Test City', + 'state' => 'Test State', + 'region' => 'Test Region', + 'province' => 'Test Province', + 'ZIP code' => 4908))); + +$json = json_encode( array( + 'contract' => $contract, + 'fulfillment_url' => (url_rel("fulfillment.php") . '?uuid=${H_contract}') +), JSON_PRETTY_PRINT); $url = url_join("http://".$_SERVER["HTTP_HOST"], "backend/contract"); @@ -109,30 +118,31 @@ $req = new http\Client\Request("POST", $url, array ("Content-Type" => "application/json")); -$req->getBody()->append ($json); +$req->getBody()->append($json); // Execute the HTTP request $client = new http\Client; -$client->enqueue($req)->send (); +$client->enqueue($req)->send(); // Fetch the response -$resp = $client->getResponse (); -$status_code = $resp->getResponseCode (); +$resp = $client->getResponse(); +$status_code = $resp->getResponseCode(); // Our response code is the same we got from the backend: -http_response_code ($status_code); +http_response_code($status_code); // Now generate our body if ($status_code != 200) { - echo "Error while generating the contract"; - echo $resp->body->toString (); + echo json_encode(array( + 'error' => "internal error", + 'hint' => "backend indicated error", + 'detail' => $resp->body->toString() + ), JSON_PRETTY_PRINT); } else { - $got_json = json_decode ($resp->body->toString (), true); - $got_json['pay_url'] = url_rel("pay.php"); - $got_json['exec_url'] = url_rel("execute.php") . "?H_contract=" . $got_json["H_contract"]; + $got_json = json_decode($resp->body->toString(), true); $_SESSION['H_contract'] = $got_json["H_contract"]; echo json_encode ($got_json, JSON_PRETTY_PRINT); } diff --git a/src/frontend/pay.php b/src/frontend/pay.php index 9fbf5f87..e8674d6e 100644 --- a/src/frontend/pay.php +++ b/src/frontend/pay.php @@ -16,15 +16,6 @@ */ -/* - 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 - - */ - include 'util.php'; function respond_success() { @@ -42,7 +33,7 @@ if (!isset($_SESSION['H_contract'])) $json = json_encode( array("error" => "No session active")); echo $json; - http_response_code (401); + http_response_code(401); die(); } @@ -55,17 +46,20 @@ if (isset($_SESSION['payment_ok']) && $_SESSION['payment_ok'] == true) $post_body = file_get_contents('php://input'); $now = new DateTime('now'); -$edate = array ('edate' => - "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/"); +$edate = array ( + 'edate' => + "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/"); $deposit_permission = json_decode ($post_body, true); -$to_add = array('max_fee' => array('value' => 3, - 'fraction' => 8, - 'currency' => $_SESSION['currency']), - 'amount' => array('value' => $_SESSION['amount_value'], - 'fraction' => $_SESSION['amount_fraction'], - 'currency' => $_SESSION['currency'])); +$to_add = array( + 'max_fee' => array( + 'value' => 3, + 'fraction' => 8, + 'currency' => $_SESSION['currency']), + 'amount' => array('value' => $_SESSION['amount_value'], + 'fraction' => $_SESSION['amount_fraction'], + 'currency' => $_SESSION['currency'])); $new_deposit_permission = array_merge($deposit_permission, $to_add); $new_deposit_permission_edate = array_merge($new_deposit_permission, $edate); @@ -75,25 +69,20 @@ $new_deposit_permission_edate = array_merge($new_deposit_permission, $edate); desired. */ // Backend is relative to the shop site. -/** - * WARNING: the "shop site" is '"http://".$_SERVER["HTTP_HOST"]' - * So do not attach $_SERVER["REQUEST_URI"] before proxying requests - * to the backend - */ -$url = url_join("http://".$_SERVER["HTTP_HOST"], "backend/pay"); +$url = url_rel("backend/pay"); $req = new http\Client\Request("POST", $url, - array ("Content-Type" => "application/json")); + array("Content-Type" => "application/json")); $req->getBody()->append (json_encode ($new_deposit_permission)); // Execute the HTTP request $client = new http\Client; -$client->enqueue($req)->send (); +$client->enqueue($req)->send(); // Fetch the response -$resp = $client->getResponse (); -$status_code = $resp->getResponseCode (); +$resp = $client->getResponse(); +$status_code = $resp->getResponseCode(); // Our response code is the same we got from the backend: http_response_code ($status_code); @@ -105,7 +94,7 @@ if ($status_code != 200) array( "error" => "backend error", "status" => $status_code, - "detail" => $resp->body->toString ())); + "detail" => $resp->body->toString())); echo $json; } else -- cgit v1.2.3 From 7878dac503af039f2fcbc17585187af0554da3e7 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 25 Jan 2016 14:36:40 +0100 Subject: add fulfillment URL in the right place --- src/frontend/generate_taler_contract.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/frontend/generate_taler_contract.php b/src/frontend/generate_taler_contract.php index 1fdfdc34..1e3c1fb1 100644 --- a/src/frontend/generate_taler_contract.php +++ b/src/frontend/generate_taler_contract.php @@ -107,9 +107,8 @@ $contract = array( 'province' => 'Test Province', 'ZIP code' => 4908))); -$json = json_encode( array( - 'contract' => $contract, - 'fulfillment_url' => (url_rel("fulfillment.php") . '?uuid=${H_contract}') +$json = json_encode(array( + 'contract' => $contract ), JSON_PRETTY_PRINT); $url = url_join("http://".$_SERVER["HTTP_HOST"], "backend/contract"); @@ -143,6 +142,7 @@ if ($status_code != 200) else { $got_json = json_decode($resp->body->toString(), true); + $got_json['fulfillment_url']= url_rel("fulfillment.php") . '?uuid=${H_contract}'; $_SESSION['H_contract'] = $got_json["H_contract"]; echo json_encode ($got_json, JSON_PRETTY_PRINT); } -- cgit v1.2.3 From a91b81e696dc6581947c1882b565224ed365e16d Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 25 Jan 2016 14:52:31 +0100 Subject: add fulfillment URL in the right place (next try) --- src/frontend/generate_taler_contract.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/generate_taler_contract.php b/src/frontend/generate_taler_contract.php index 1e3c1fb1..c405e818 100644 --- a/src/frontend/generate_taler_contract.php +++ b/src/frontend/generate_taler_contract.php @@ -54,6 +54,7 @@ $now = new DateTime('now'); // pack the JSON for the contract $contract = array( + 'fulfillment_url' => (url_rel("fulfillment.php") . '?uuid=${H_contract}'), 'amount' => array( 'value' => $amount_value, 'fraction' => $amount_fraction, @@ -142,7 +143,6 @@ if ($status_code != 200) else { $got_json = json_decode($resp->body->toString(), true); - $got_json['fulfillment_url']= url_rel("fulfillment.php") . '?uuid=${H_contract}'; $_SESSION['H_contract'] = $got_json["H_contract"]; echo json_encode ($got_json, JSON_PRETTY_PRINT); } -- cgit v1.2.3