merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

commit 1f56da974badbf2b0bb23c891ddf0935e126fe18
parent 7e2f81ed3130584a711b107bcdf52fcae297b1b8
Author: Florian Dold <florian.dold@gmail.com>
Date:   Sun, 24 Jan 2016 18:22:56 +0100

properly concatenate

Diffstat:
Msrc/frontend/fulfillment.php | 22+++++++---------------
Msrc/frontend/generate_taler_contract.php | 78++++++++++++++----------------------------------------------------------------
Msrc/frontend/pay.php | 37+++++--------------------------------
Asrc/frontend/util.php | 15+++++++++++++++
4 files changed, 41 insertions(+), 111 deletions(-)

diff --git a/src/frontend/fulfillment.php b/src/frontend/fulfillment.php @@ -40,9 +40,6 @@ */ -$cli_debug = false; -$backend_test = true; - function generate_msg ($link){ $msg = "<p>Thanks for donating to " . $_SESSION['receiver'] . ".</p>"; if (false != $link) @@ -50,22 +47,17 @@ function generate_msg ($link){ return $msg; } -if ($_GET['cli_debug'] == 'yes') - $cli_debug = true; +session_start(); -if ($_GET['backend_test'] == 'no') +if (!isset ($_SESSION['payment_ok'])) { - $cli_debug = true; - $backend_test = false; + echo "<p>Please come here after a successful payment!</p>"; } - -session_start(); - -if (! isset ($_SESSION['payment_ok'])) - echo "<p>Please land here after a successful payment!</p>"; -else{ +else +{ $news = false; - switch ($_SESSION['receiver']){ + switch ($_SESSION['receiver']) + { case "Taler": $news = "https://taler.net/news"; break; diff --git a/src/frontend/generate_taler_contract.php b/src/frontend/generate_taler_contract.php @@ -14,61 +14,17 @@ 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 <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 +include 'util.php'; - To test this feature from the command line, issue: - - - $ curl http://merchant_url/generate_taler_contract.php?cli_debug=yes - if the whole "journey" to the backend is begin tested - - $ curl http://merchant_url/generate_taler_contract.php?backend_test=no - if just the frontend job is being tested -*/ - -$cli_debug = false; -$backend_test = true; - -if (isset($_GET['cli_debug']) && $_GET['cli_debug'] == 'yes') - $cli_debug = true; - -if (isset($_GET['backend_test']) && $_GET['backend_test'] == 'no') -{ - $cli_debug = true; - $backend_test = false; -} - -// 1) recover the session information +// recover the session information session_start(); -if (!$cli_debug && (! isset($_SESSION['receiver']))) +if (!isset($_SESSION['receiver'])) { - http_response_code (404); - echo "Please select a contract before getting to this page..."; - echo "attempted : " . $_SESSION['receiver']; - exit (0); -} - -/* Obtain session state */ -if (!$cli_debug) -{ - $receiver = $_SESSION['receiver']; - $amount_value = intval ($_SESSION['amount_value']); - $amount_fraction = intval ($_SESSION['amount_fraction']); - $currency = $_SESSION['currency']; -} -else -{ - $receiver = "Test Receiver"; - $amount_value = 5; - $amount_fraction = 5; - $currency = "KUDOS"; - + http_response_code (400); + die(); } /* Fill in variables for simple JSON contract */ @@ -90,9 +46,6 @@ $teatax = array ('value' => 1, // Take a timestamp $now = new DateTime('now'); -$PAY_URL = "pay.php"; -$EXEC_URL = "execute.php"; - // pack the JSON for the contract // --- FIXME: exact format needs review! $contract = array ('amount' => array ('value' => $amount_value, @@ -142,17 +95,11 @@ $contract = array ('amount' => array ('value' => $amount_value, 'state' => 'Test State', 'region' => 'Test Region', 'province' => 'Test Province', - 'ZIP code' => 4908))); -$json = json_encode (array ('contract' => $contract, 'exec_url' => $EXEC_URL, 'pay_url' => $PAY_URL), JSON_PRETTY_PRINT); -if ($cli_debug && !$backend_test) -{ - echo $json . "\n"; - exit; -} + 'ZIP code' => 4908))); +$json = json_encode(array('contract' => $contract, JSON_PRETTY_PRINT); -$url = (new http\URL("http://".$_SERVER["HTTP_HOST"])) - ->mod(array ("path" => "backend/contract"), http\Url::JOIN_PATH); +$url = url_join("http://".$_SERVER["HTTP_HOST"], "backend/contract"); $req = new http\Client\Request("POST", $url, @@ -176,9 +123,12 @@ if ($status_code != 200) { echo "Error while generating the contract"; echo $resp->body->toString (); -} else -{ $got_json = json_decode ($resp->body->toString ()); +{ + $got_json = json_decode ($resp->body->toString ()); + $PAY_URL = "pay.php"; + $EXEC_URL = "execute.php"; + $got_json['pay_url'] = $_SESSION['H_contract'] = $got_json->H_contract; echo $resp->body->toString (); } diff --git a/src/frontend/pay.php b/src/frontend/pay.php @@ -25,21 +25,9 @@ */ -session_start(); - -$cli_debug = false; -$backend_test = true; +include 'util.php'; -if (isset($_GET['cli_debug']) && $_GET['cli_debug'] == 'yes') -{ - $cli_debug = true; -} - -if (isset($_GET['backend_test']) && $_GET['backend_test'] == 'no') -{ - $cli_debug = true; - $backend_test = false; -} +session_start(); if (!isset($_SESSION['H_contract'])) { @@ -52,8 +40,7 @@ if (isset($_SESSION['payment_ok']) && $_SESSION['payment_ok'] == true) { $_SESSION['payment_ok'] = true; http_response_code (301); - $url = (new http\URL($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) - ->mod(array ("path" => "fulfillment.php"), http\Url::JOIN_PATH); + $url = $url_rel("fulfillment.php"); header("Location: $url"); die(); } @@ -80,26 +67,13 @@ $new_deposit_permission_edate = array_merge($new_deposit_permission, $edate); could be on an entirely different machine if desired. */ -if ($cli_debug && !$backend_test) -{ - - /* DO NOTE the newline at the end of 'echo's argument */ - //echo json_encode ($new_deposit_permission_edate, JSON_PRETTY_PRINT) - echo json_encode($new_deposit_permission, JSON_PRETTY_PRINT) - . "\n"; - exit; -} - - // 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 = (new http\URL("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"])) -$url = (new http\URL("http://".$_SERVER["HTTP_HOST"])) - ->mod(array ("path" => "backend/pay"), http\Url::JOIN_PATH); +$url = url_join("http://".$_SERVER["HTTP_HOST"], "backend/pay"); $req = new http\Client\Request("POST", $url, @@ -132,8 +106,7 @@ else { $_SESSION['payment_ok'] = true; http_response_code (301); - $url = (new http\URL($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) - ->mod(array ("path" => "fulfillment.php"), http\Url::JOIN_PATH); + $url = url_rel("fulfillment.php"); header("Location: $url"); die(); } diff --git a/src/frontend/util.php b/src/frontend/util.php @@ -0,0 +1,15 @@ +<?php +function url_join($base, $path) { + $url = (new http\URL($base)) + ->mod(array ("path" => $path), http\Url::JOIN_PATH|http\URL::SANITIZE_PATH); + return $url->toString(); +} + +// Get a url with a path relative to the +// current script's path. +function url_rel($path) { + return url_join( + $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], + $path); +} +?>