commit 2efb84a0a4565e8b9a4336eef31a94091b923880
parent 4b97cefcac97e4dc398d49126fc53be8828b8bcb
Author: Marcello Stanisci <marcello.stanisci@inria.fr>
Date: Wed, 16 Nov 2016 00:24:05 +0100
finer splitting of sources
Diffstat:
8 files changed, 122 insertions(+), 102 deletions(-)
diff --git a/php/backend.php b/php/backend.php
@@ -0,0 +1,28 @@
+<?php
+
+ include 'copying.php';
+
+/**
+ * 'body' is an object, representing the JSON to POST. NOTE: we do NOT
+ * rely on a more structured way of doing HTTP, like the one offered by
+ * pecl_http, as its installation was NOT always straightforward.
+ */
+ function post_to_backend($backend_uri, $body){
+ $json = json_encode($body);
+ $c = curl_init(url_join ($GLOBALS['BACKEND'], $backend_uri));
+ $options = array(CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_CUSTOMREQUEST => "POST",
+ CURLOPT_POSTFIELDS => $json,
+ // Disabling SSL for the sole purpose of speed
+ // up the tutorial, as the normal user is likely
+ // to NOT have certs for taler.net
+ CURLOPT_SSL_VERIFYHOST => false,
+ CURLOPT_SSL_VERIFYPEER => false,
+ CURLOPT_HTTPHEADER =>
+ array('Content-Type: application/json'));
+ curl_setopt_array($c, $options);
+ $r = curl_exec($c);
+ return array("status_code" => curl_getinfo($c, CURLINFO_HTTP_CODE),
+ "body" => $r);
+ }
+?>
diff --git a/php/config.php b/php/config.php
@@ -0,0 +1,5 @@
+<?php
+
+ $BACKEND = "https://backend.test.taler.net/";
+
+?>
diff --git a/php/contract.php b/php/contract.php
@@ -0,0 +1,68 @@
+<?php
+
+ include 'copying.php';
+
+ function make_contract($transaction_id, $now){
+ $contract = array ('amount' =>
+ array ('value' => 1,
+ 'fraction' => 0,
+ 'currency' => "KUDOS"),
+ 'max_fee' =>
+ array ('value' => 0,
+ 'fraction' => 50000,
+ 'currency' => "KUDOS"),
+ 'transaction_id' => $transaction_id,
+ 'products' => array (
+ array ('description' => "Donation to charity program",
+ 'quantity' => 1,
+ 'price' =>
+ array ('value' => 1,
+ 'fraction' => 0,
+ 'currency' => "KUDOS"),
+ 'product_id' => 0,
+ 'taxes' => array(),
+ 'delivery_date' => "/Date(" . $now->getTimestamp() . ")/",
+ 'delivery_location' => 'LNAME1')),
+ 'summary' => "Personal donation to charity program",
+ 'timestamp' => "/Date(" . $now->getTimestamp() . ")/",
+ 'fulfillment_url' => url_rel("/fulfillment.php?"
+ . "transaction_id=$transaction_id×tamp="
+ . $now->getTimestamp()),
+ 'repurchase_correlation_id' => '',
+ 'expiry' =>
+ "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/",
+ 'refund_deadline' =>
+ "/Date(" . $now->add(new DateInterval('P3M'))->getTimestamp() . ")/",
+ 'merchant' =>
+ array ('address' => 'LNAME2',
+ 'name' => "Charity donation shop",
+ 'jurisdiction' => 'LNAME3'),
+ 'locations' =>
+ array ('LNAME1' =>
+ array ('country' => 'Test Country 1',
+ 'city' => 'Test City 1',
+ 'state' => 'Test State 1',
+ 'region' => 'Test Region 1',
+ 'province' => 'Test Province 1',
+ 'ZIP code' => 49081,
+ 'street' => 'test street 1',
+ 'street number' => 201),
+ 'LNAME2' =>
+ array ('country' => 'Test Country 2',
+ 'city' => 'Test City 2',
+ 'state' => 'Test State 2',
+ 'region' => 'Test Region 2',
+ 'province' => 'Test Province 2',
+ 'ZIP code' => 49082,
+ 'street' => 'test street 2',
+ 'street number' => 202),
+ 'LNAME3' =>
+ array ('country' => 'Test Country 3',
+ 'city' => 'Test City 3',
+ 'state' => 'Test State 3',
+ 'region' => 'Test Region 3',
+ 'province' => 'Test Province 3',
+ 'ZIP code' => 49083)));
+ return array ('contract' => $contract);
+ }
+?>
diff --git a/php/error.php b/php/error.php
@@ -0,0 +1,12 @@
+<?php
+
+ include 'copying.php';
+
+ function build_error($response, $hint){
+ return json_encode(array(
+ 'error' => "internal error",
+ 'hint' => $hint,
+ 'detail' => $response["body"]),
+ JSON_PRETTY_PRINT);
+ }
+?>
diff --git a/php/fulfillment.php b/php/fulfillment.php
@@ -1,6 +1,10 @@
<?php
+
include 'copying.php';
+ include 'contract.php';
+ include 'backend.php';
include 'helpers.php';
+ include 'error.php';
session_start();
@@ -20,7 +24,6 @@
$rec_proposal = make_contract(intval($_GET['transaction_id']), $now);
$response = post_to_backend("/contract", $rec_proposal);
- file_put_contents("/tmp/php.out", $response["body"], FILE_APPEND);
http_response_code($response["code"]);
if (200 != $response["status_code"]) {
echo build_error($response, "Failed to reconstruct the contract");
diff --git a/php/generate-contract.php b/php/generate-contract.php
@@ -1,7 +1,9 @@
<?php
include 'copying.php';
- include 'helpers.php';
+ include 'contract.php';
+ include 'backend.php';
+ include 'error.php';
// this variable is the JSON of a contract proposal,
// see https://api.taler.net/api-merchant.html#post--contract
@@ -10,7 +12,6 @@
$proposal = make_contract($transaction_id, new DateTime('now'));
// Here the frontend POSTs the proposal to the backend
$response = post_to_backend("/contract", $proposal);
- file_put_contents("/tmp/php.out", $response["body"], FILE_APPEND);
// We always return verbatim what the backend returned
http_response_code($response["code"]);
if (200 != $response["status_code"]) {
diff --git a/php/helpers.php b/php/helpers.php
@@ -18,107 +18,8 @@
// $path must have a leading '/'.
function url_rel($path){
- file_put_contents("/tmp/php.out", "Dumping _SERVER[]..", FILE_APPEND);
- file_put_contents("/tmp/php.out", print_r($_SERVER, true), FILE_APPEND);
// Make sure 'REQUEST_SCHEME' is http/https, as in some setups it may
// be "HTTP/1.1".
return $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$path;
}
-
- function make_contract($transaction_id, $now){
- $contract = array ('amount' =>
- array ('value' => 1,
- 'fraction' => 0,
- 'currency' => "KUDOS"),
- 'max_fee' =>
- array ('value' => 0,
- 'fraction' => 50000,
- 'currency' => "KUDOS"),
- 'transaction_id' => $transaction_id,
- 'products' => array (
- array ('description' => "Donation to charity program",
- 'quantity' => 1,
- 'price' =>
- array ('value' => 1,
- 'fraction' => 0,
- 'currency' => "KUDOS"),
- 'product_id' => 0,
- 'taxes' => array(),
- 'delivery_date' => "/Date(" . $now->getTimestamp() . ")/",
- 'delivery_location' => 'LNAME1')),
- 'summary' => "Personal donation to charity program",
- 'timestamp' => "/Date(" . $now->getTimestamp() . ")/",
- 'fulfillment_url' => url_rel("/fulfillment.php?"
- . "transaction_id=$transaction_id×tamp="
- . $now->getTimestamp()),
- 'repurchase_correlation_id' => '',
- 'expiry' =>
- "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/",
- 'refund_deadline' =>
- "/Date(" . $now->add(new DateInterval('P3M'))->getTimestamp() . ")/",
- 'merchant' =>
- array ('address' => 'LNAME2',
- 'name' => "Charity donation shop",
- 'jurisdiction' => 'LNAME3'),
- 'locations' =>
- array ('LNAME1' =>
- array ('country' => 'Test Country 1',
- 'city' => 'Test City 1',
- 'state' => 'Test State 1',
- 'region' => 'Test Region 1',
- 'province' => 'Test Province 1',
- 'ZIP code' => 49081,
- 'street' => 'test street 1',
- 'street number' => 201),
- 'LNAME2' =>
- array ('country' => 'Test Country 2',
- 'city' => 'Test City 2',
- 'state' => 'Test State 2',
- 'region' => 'Test Region 2',
- 'province' => 'Test Province 2',
- 'ZIP code' => 49082,
- 'street' => 'test street 2',
- 'street number' => 202),
- 'LNAME3' =>
- array ('country' => 'Test Country 3',
- 'city' => 'Test City 3',
- 'state' => 'Test State 3',
- 'region' => 'Test Region 3',
- 'province' => 'Test Province 3',
- 'ZIP code' => 49083)));
- return array ('contract' => $contract);
- }
-
- function build_error($response, $hint){
- return json_encode(array(
- 'error' => "internal error",
- 'hint' => $hint,
- 'detail' => $response["body"]),
- JSON_PRETTY_PRINT);
- }
-
-
- /**
- * 'body' is an object, representing the JSON to POST. NOTE: we do NOT
- * rely on a more structured way of doing HTTP, like the one offered by
- * pecl_http, as its installation was NOT always straightforward.
- */
- function post_to_backend($backend_uri, $body){
- $json = json_encode($body);
- $c = curl_init(url_join ("https://backend.demo.taler.net/", $backend_uri));
- $options = array(CURLOPT_RETURNTRANSFER => true,
- CURLOPT_CUSTOMREQUEST => "POST",
- CURLOPT_POSTFIELDS => $json,
- // Disabling SSL for the sole purpose of speed
- // up the tutorial, as the normal user is likely
- // to NOT have certs for taler.net
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_HTTPHEADER =>
- array('Content-Type: application/json'));
- curl_setopt_array($c, $options);
- $r = curl_exec($c);
- return array("status_code" => curl_getinfo($c, CURLINFO_HTTP_CODE),
- "body" => $r);
- }
?>
diff --git a/php/pay.php b/php/pay.php
@@ -1,5 +1,7 @@
<?php
include 'copying.php';
+ include 'backend.php';
+ include 'error.php';
session_start();
if(!isset($_SESSION['paid'])){