From 500223390d354d9a431350c815b198c32adeac66 Mon Sep 17 00:00:00 2001 From: Christiaan Goossens Date: Sun, 19 Feb 2017 11:57:38 +0100 Subject: [PATCH 1/3] Added the first API handler for external payments (to use with the Omnipay plugin) --- src/Application/API/Handler.php | 130 ++++++++++++++++++++++++++++++++ src/Application/Main.php | 10 ++- 2 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/Application/API/Handler.php diff --git a/src/Application/API/Handler.php b/src/Application/API/Handler.php new file mode 100644 index 0000000..ec18634 --- /dev/null +++ b/src/Application/API/Handler.php @@ -0,0 +1,130 @@ +group('/api', function () { + /** + * TransactionRequest API Endpoint + * + * Requires the following POST arguments: + * - clientId + * - transactionId + * - amount + * - description + * - returnUrl + * - sha1 + */ + $this->post('/transactionRequest', function ($request, $response, $args) { + $parsedBody = $request->getParsedBody(); + + if (isset($parsedBody['clientId']) && isset($parsedBody['transactionId']) && isset($parsedBody['amount']) && isset($parsedBody['description']) && isset($parsedBody['returnUrl']) && isset($parsedBody['sha1'])) { + // Correct request + + $clientSecret = Handler::getClientSecret($parsedBody['clientId']); + $sha = sha1($parsedBody['transactionId'] . $parsedBody['amount'] . $clientSecret); + + if ($sha === $parsedBody['sha1']) { + $responseJSON = array( + "success" => true, + "redirect" => "https://example.com" + ); + } else { + $response = $response->withStatus(403); + $responseJSON = array( + "success" => false, + "error" => "Incorrect sha1 verification hash." + ); + } + } else { + $response = $response->withStatus(400); + $responseJSON = array( + "success" => false, + "error" => "Missing one of the following attributes: [clientId, transactionId, amount, description, returnUrl, sha1]" + ); + } + + $body = $response->getBody(); + $body->write(json_encode($responseJSON)); + return $response->withBody($body); + }); + + /** + * StatusRequest API Endpoint + * + * Requires the following POST arguments: + * - clientId + * - transactionId + * - sha1 + */ + $this->post('/statusRequest', function ($request, $response, $args) { + $parsedBody = $request->getParsedBody(); + + if (isset($parsedBody['clientId']) && isset($parsedBody['transactionId']) && isset($parsedBody['sha1'])) { + // Correct request + + $clientSecret = Handler::getClientSecret($parsedBody['clientId']); + $sha = sha1($parsedBody['transactionId'] . $clientSecret); + + if ($sha === $parsedBody['sha1']) { + $responseJSON = array( + "success" => true, + "transaction" => array( + "status" => "Success", + "transactionId" => "notyetfromdb", + "someotherrandomkey" => "changethis" + ) + ); + } else { + $response = $response->withStatus(403); + $responseJSON = array( + "success" => false, + "error" => "Incorrect sha1 verification hash." + ); + } + } else { + $response = $response->withStatus(400); + $responseJSON = array( + "success" => false, + "error" => "Missing one of the following attributes: [clientId, transactionId, sha1]" + ); + } + + $body = $response->getBody(); + $body->write(json_encode($responseJSON)); + return $response->withBody($body); + }); + })->add(function ($request, $response, $next) { + /** + * Add the correct JSON headers to the responses + */ + $response = $response->withHeader('Content-type', 'application/json'); + return $next($request, $response); + }); + } + + public static function getClientSecret($clientId) + { + return '42'; + } +} diff --git a/src/Application/Main.php b/src/Application/Main.php index 33129b8..4a7bdf1 100644 --- a/src/Application/Main.php +++ b/src/Application/Main.php @@ -16,7 +16,8 @@ namespace InfD4p\Application; use \Slim\App; -class Main { +class Main +{ /** * Constructor function @@ -29,7 +30,12 @@ class Main { * Hier wordt onze applicatie gestart. De functie hieronder wordt aangeroepen bij het starten van de app. Hier kun je dus routes toevoegen. * */ - public function __construct(App $app) { + public function __construct(App $app) + { + // Load the API handler for the Omnipay plugin + new API\Handler($app); + + // Add the default view routes $app->get('/[{name}]', function ($request, $response, $args) { // Render index view return $this->renderer->render($response, 'index.phtml', $args); From efa5baedb607a8c48d85add928b017e0e6781182 Mon Sep 17 00:00:00 2001 From: Christiaan Goossens Date: Wed, 22 Feb 2017 13:23:08 +0100 Subject: [PATCH 2/3] Secrets changed. --- src/Application/API/Handler.php | 2 +- src/Application/Main.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Application/API/Handler.php b/src/Application/API/Handler.php index ec18634..d015a6a 100644 --- a/src/Application/API/Handler.php +++ b/src/Application/API/Handler.php @@ -125,6 +125,6 @@ class Handler public static function getClientSecret($clientId) { - return '42'; + return '3'; } } diff --git a/src/Application/Main.php b/src/Application/Main.php index 4a7bdf1..7208bbf 100644 --- a/src/Application/Main.php +++ b/src/Application/Main.php @@ -18,7 +18,6 @@ use \Slim\App; class Main { - /** * Constructor function * @param App $app App Dependency Injection From 36caeccb74ff5eab60ef7f5f6166d6bdf1a88a1d Mon Sep 17 00:00:00 2001 From: Christiaan Goossens Date: Thu, 23 Feb 2017 10:31:05 +0100 Subject: [PATCH 3/3] Namespace change + assets --- composer.json | 2 +- public/assets/css/demo.css | 18 ++++++++++++++++++ src/Application/API/Handler.php | 2 +- src/Application/Main.php | 5 +---- src/bootstrap.php | 5 ++++- templates/index.phtml | 21 +-------------------- 6 files changed, 26 insertions(+), 27 deletions(-) create mode 100644 public/assets/css/demo.css diff --git a/composer.json b/composer.json index 5055331..8b3bc56 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,6 @@ "start": "php -S 0.0.0.0:8080 -t public public/index.php" }, "autoload": { - "psr-4": {"InfD4p\\": "src/"} + "psr-4": {"Inforbank\\": "src/"} } } diff --git a/public/assets/css/demo.css b/public/assets/css/demo.css new file mode 100644 index 0000000..322b3bf --- /dev/null +++ b/public/assets/css/demo.css @@ -0,0 +1,18 @@ +body { + margin: 50px 0 0 0; + padding: 0; + width: 100%; + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + text-align: center; + color: #aaa; + font-size: 18px; +} + +h1 { + color: #719e40; + letter-spacing: -3px; + font-family: 'Lato', sans-serif; + font-size: 100px; + font-weight: 200; + margin-bottom: 0; +} diff --git a/src/Application/API/Handler.php b/src/Application/API/Handler.php index d015a6a..3690c71 100644 --- a/src/Application/API/Handler.php +++ b/src/Application/API/Handler.php @@ -12,7 +12,7 @@ * Jelmer Hinssen */ -namespace InfD4p\Application\API; +namespace Inforbank\Application\API; use \Slim\App; diff --git a/src/Application/Main.php b/src/Application/Main.php index 7208bbf..c48a78b 100644 --- a/src/Application/Main.php +++ b/src/Application/Main.php @@ -12,7 +12,7 @@ * Jelmer Hinssen */ -namespace InfD4p\Application; +namespace Inforbank\Application; use \Slim\App; @@ -31,9 +31,6 @@ class Main */ public function __construct(App $app) { - // Load the API handler for the Omnipay plugin - new API\Handler($app); - // Add the default view routes $app->get('/[{name}]', function ($request, $response, $args) { // Render index view diff --git a/src/bootstrap.php b/src/bootstrap.php index 892723b..57627f4 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -72,7 +72,10 @@ $container['renderer'] = function ($c) { * */ -new InfD4p\Application\Main($app); +new Inforbank\Application\Main($app); + +// Added API handler +new Inforbank\Application\API\Handler($app); /** * Bij het maken van dit object is in de class de __construct functie aangeroepen met de parameter $app. De functies die daar in staan zullen nu worden uitgevoerd. diff --git a/templates/index.phtml b/templates/index.phtml index d732287..a09481c 100644 --- a/templates/index.phtml +++ b/templates/index.phtml @@ -4,26 +4,7 @@ Slim 3 - +

Slim