merchant-frontend-examples

ZZZ: Inactive/Deprecated
Log | Files | Refs

commit f9cca6501824f02759fb3f22af03451dc949983a
parent 3da4a65a903ce07c13deac070355594acc148b02
Author: Marcello Stanisci <marcello.stanisci@inria.fr>
Date:   Tue, 15 Nov 2016 15:40:43 +0100

Getting rid of pecl_http in URL manipulations

Diffstat:
Mphp/donate-handler.php | 2+-
Mphp/generate-contract.php | 20++++++++++++--------
Mphp/helpers.php | 24++++++++++++------------
Mphp/index.php | 2+-
4 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/php/donate-handler.php b/php/donate-handler.php @@ -1,6 +1,6 @@ <?php http_response_code (402); // 402: Payment required - header ('X-Taler-Contract-Url: /php/generate-contract.php'); + header ('X-Taler-Contract-Url: /generate-contract.php'); echo "<html> <head> <title>Select payment method</title> diff --git a/php/generate-contract.php b/php/generate-contract.php @@ -40,6 +40,7 @@ 'delivery_date' => "/Date(" . $now->getTimestamp() . ")/", 'delivery_location' => 'LNAME1')), 'timestamp' => "/Date(" . $now->getTimestamp() . ")/", + 'summary' => "Personal donation to charity program", 'expiry' => "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/", 'refund_deadline' => @@ -78,11 +79,11 @@ 'region' => 'Test Region 3', 'province' => 'Test Province 3', 'ZIP code' => 49083))); - $json = json_encode (array ('contract' => $contract), JSON_PRETTY_PRINT); - return $json; + return array ('contract' => $contract); } + /* this variable is the JSON of a contract proposal, see https://api.taler.net/api-merchant.html#post--contract the second parameter is the transaction id */ @@ -90,13 +91,16 @@ $proposal = make_contract($transaction_id, new DateTime('now')); # Here the frontend POSTs the proposal to the backend $response = post_to_backend("/contract", $proposal); - if (200 != $response->getResponseCode()) { + 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"]) { echo json_encode(array( - ’error’ => "internal error", - ’hint’ => "failed to generate contract", - ’detail’ => $resp->body->toString() - ), JSON_PRETTY_PRINT); + 'error' => "internal error", + 'hint' => "failed to generate contract", + 'detail' => $response["body"]), + JSON_PRETTY_PRINT); return; } - echo $response->body; + echo $response["body"]; ?> diff --git a/php/helpers.php b/php/helpers.php @@ -18,20 +18,20 @@ include 'config.php'; - // Get a url with a path relative to the - // current script's path. - function url_rel($path, $strip=false) { - return url_join( - $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], - $path, - $strip); + function url_join($base, $path) { + // Please note that this trivial way of joining URLs is + // dur to avoiding using pecl_http and to NOT bloat too + // much tutorial code. + return $base . $path; } - function url_join($base, $path, $strip=false) { - $flags = $strip ? (http\Url::STRIP_PATH|http\URL::STRIP_QUERY) : 0; - return (new http\URL($base, null, $flags)) - ->mod(array ("path" => $path), http\Url::JOIN_PATH|http\URL::SANITIZE_PATH) - ->toString(); + // $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; } /** diff --git a/php/index.php b/php/index.php @@ -4,7 +4,7 @@ <title>Taler tutorial</title> </head> <body> - <form action='/php/donate-handler.php' method='GET'> + <form action='/donate-handler.php' method='GET'> <input type='submit' value='Donate!'></input> </form> </body>