-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from unzerdev/develop
Develop
- Loading branch information
Showing
21 changed files
with
871 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* This file defines the constants needed for the PostFinanceCard example. | ||
* | ||
* Copyright (C) 2020 - today Unzer E-Com GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @link https://docs.unzer.com/ | ||
* | ||
* @package UnzerSDK\examples | ||
*/ | ||
|
||
require_once __DIR__ . '/../Constants.php'; | ||
|
||
define('EXAMPLE_URL', EXAMPLE_BASE_FOLDER . 'PostFinanceCard'); | ||
define('CONTROLLER_URL', EXAMPLE_URL . '/Controller.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* This is the controller for the PostFinanceCard example. | ||
* It is called when the pay button on the index page is clicked. | ||
* | ||
* Copyright (C) 2020 - today Unzer E-Com GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @link https://docs.unzer.com/ | ||
* | ||
* @package UnzerSDK\examples | ||
*/ | ||
|
||
/** Require the constants of this example */ | ||
require_once __DIR__ . '/Constants.php'; | ||
|
||
/** @noinspection PhpIncludeInspection */ | ||
/** Require the composer autoloader file */ | ||
require_once __DIR__ . '/../../../../autoload.php'; | ||
|
||
use UnzerSDK\examples\ExampleDebugHandler; | ||
use UnzerSDK\Exceptions\UnzerApiException; | ||
use UnzerSDK\Unzer; | ||
|
||
session_start(); | ||
session_unset(); | ||
|
||
$clientMessage = 'Something went wrong. Please try again later.'; | ||
$merchantMessage = 'Something went wrong. Please try again later.'; | ||
|
||
function redirect($url, $merchantMessage = '', $clientMessage = '') | ||
{ | ||
$_SESSION['merchantMessage'] = $merchantMessage; | ||
$_SESSION['clientMessage'] = $clientMessage; | ||
header('Location: ' . $url); | ||
die(); | ||
} | ||
|
||
// You will need the id of the payment type created in the frontend (index.php) | ||
if (!isset($_POST['resourceId'])) { | ||
redirect(FAILURE_URL, 'Resource id is missing!', $clientMessage); | ||
} | ||
$paymentTypeId = $_POST['resourceId']; | ||
|
||
// Catch API errors, write the message to your log and show the ClientMessage to the client. | ||
try { | ||
// Create an Unzer object using your private key and register a debug handler if you want to. | ||
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY); | ||
$unzer->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler()); | ||
|
||
// Create a charge transaction to get the redirectUrl. | ||
$transaction = new \UnzerSDK\Resources\TransactionTypes\Charge(12.32, 'CHF', RETURN_CONTROLLER_URL); | ||
$unzer->performCharge($transaction, $paymentTypeId); | ||
|
||
// You'll need to remember the paymentId for later in the ReturnController | ||
$_SESSION['PaymentId'] = $transaction->getPaymentId(); | ||
$_SESSION['ShortId'] = $transaction->getShortId(); | ||
|
||
// Redirect to the PostFinance page | ||
if (!$transaction->isError() && $transaction->getRedirectUrl() !== null) { | ||
redirect($transaction->getRedirectUrl()); | ||
} | ||
|
||
// Check the result message of the charge to find out what went wrong. | ||
$merchantMessage = $transaction->getMessage()->getCustomer(); | ||
} catch (UnzerApiException $e) { | ||
$merchantMessage = $e->getMerchantMessage(); | ||
$clientMessage = $e->getClientMessage(); | ||
} catch (RuntimeException $e) { | ||
$merchantMessage = $e->getMessage(); | ||
} | ||
redirect(FAILURE_URL, $merchantMessage, $clientMessage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* This file provides an example implementation of the PostFinanceCard payment type. | ||
* | ||
* Copyright (C) 2020 - today Unzer E-Com GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @link https://docs.unzer.com/ | ||
* | ||
* @package UnzerSDK\examples | ||
*/ | ||
|
||
/** Require the constants of this example */ | ||
require_once __DIR__ . '/Constants.php'; | ||
|
||
/** @noinspection PhpIncludeInspection */ | ||
/** Require the composer autoloader file */ | ||
require_once __DIR__ . '/../../../../autoload.php'; | ||
?> | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Unzer UI Examples</title> | ||
<script src="https://code.jquery.com/jquery-3.6.0.min.js" | ||
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" | ||
crossorigin="anonymous"></script> | ||
|
||
<link rel="stylesheet" href="https://static.unzer.com/v1/unzer.css" /> | ||
<script type="text/javascript" src="https://static.unzer.com/v1/unzer.js"></script> | ||
</head> | ||
|
||
<body style="margin: 70px 70px 0;"> | ||
|
||
<p><a href="https://docs.unzer.com/reference/test-data" target="_blank">Click here to open our test data in new tab.</a></p> | ||
|
||
<form id="payment-form" class="unzerUI form" novalidate> | ||
<div class="field" id="error-holder" style="color: #9f3a38"> </div> | ||
<div class="field"> | ||
<button class="unzerUI primary button fluid" id="submit-button" type="submit">Pay</button> | ||
</div> | ||
</form> | ||
|
||
<script> | ||
// Create an Unzer instance with your public key | ||
let unzerInstance = new unzer('<?php echo UNZER_PAPI_PUBLIC_KEY; ?>'); | ||
|
||
// Create an PostFinanceCard instance | ||
let postFinanceCard = unzerInstance.PostFinanceCard(); | ||
|
||
// Handle payment form submission | ||
let form = document.getElementById('payment-form'); | ||
form.addEventListener('submit', function(event) { | ||
event.preventDefault(); | ||
// Creating a postFinanceCard resource | ||
postFinanceCard.createResource() | ||
.then(function(result) { | ||
let hiddenInput = document.createElement('input'); | ||
hiddenInput.setAttribute('type', 'hidden'); | ||
hiddenInput.setAttribute('name', 'resourceId'); | ||
hiddenInput.setAttribute('value', result.id); | ||
form.appendChild(hiddenInput); | ||
form.setAttribute('method', 'POST'); | ||
form.setAttribute('action', '<?php echo CONTROLLER_URL; ?>'); | ||
|
||
// Submitting the form | ||
form.submit(); | ||
}) | ||
.catch(function(error) { | ||
$('#error-holder').html(error.message) | ||
}) | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* This file defines the constants needed for the PostFinanceEfinance example. | ||
* | ||
* Copyright (C) 2020 - today Unzer E-Com GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @link https://docs.unzer.com/ | ||
* | ||
* @package UnzerSDK\examples | ||
*/ | ||
|
||
require_once __DIR__ . '/../Constants.php'; | ||
|
||
define('EXAMPLE_URL', EXAMPLE_BASE_FOLDER . 'PostFinanceEfinance'); | ||
define('CONTROLLER_URL', EXAMPLE_URL . '/Controller.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* This is the controller for the PostFinanceEfinance example. | ||
* It is called when the pay button on the index page is clicked. | ||
* | ||
* Copyright (C) 2020 - today Unzer E-Com GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @link https://docs.unzer.com/ | ||
* | ||
* @package UnzerSDK\examples | ||
*/ | ||
|
||
/** Require the constants of this example */ | ||
require_once __DIR__ . '/Constants.php'; | ||
|
||
/** @noinspection PhpIncludeInspection */ | ||
/** Require the composer autoloader file */ | ||
require_once __DIR__ . '/../../../../autoload.php'; | ||
|
||
use UnzerSDK\examples\ExampleDebugHandler; | ||
use UnzerSDK\Exceptions\UnzerApiException; | ||
use UnzerSDK\Unzer; | ||
|
||
session_start(); | ||
session_unset(); | ||
|
||
$clientMessage = 'Something went wrong. Please try again later.'; | ||
$merchantMessage = 'Something went wrong. Please try again later.'; | ||
|
||
function redirect($url, $merchantMessage = '', $clientMessage = '') | ||
{ | ||
$_SESSION['merchantMessage'] = $merchantMessage; | ||
$_SESSION['clientMessage'] = $clientMessage; | ||
header('Location: ' . $url); | ||
die(); | ||
} | ||
|
||
// You will need the id of the payment type created in the frontend (index.php) | ||
if (!isset($_POST['resourceId'])) { | ||
redirect(FAILURE_URL, 'Resource id is missing!', $clientMessage); | ||
} | ||
$paymentTypeId = $_POST['resourceId']; | ||
|
||
// Catch API errors, write the message to your log and show the ClientMessage to the client. | ||
try { | ||
// Create an Unzer object using your private key and register a debug handler if you want to. | ||
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY); | ||
$unzer->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler()); | ||
|
||
// Create a charge transaction to get the redirectUrl. | ||
$transaction = new \UnzerSDK\Resources\TransactionTypes\Charge(12.32, 'CHF', RETURN_CONTROLLER_URL); | ||
$unzer->performCharge($transaction, $paymentTypeId); | ||
|
||
// You'll need to remember the paymentId for later in the ReturnController | ||
$_SESSION['PaymentId'] = $transaction->getPaymentId(); | ||
$_SESSION['ShortId'] = $transaction->getShortId(); | ||
|
||
// Redirect to the PostFinance page | ||
if (!$transaction->isError() && $transaction->getRedirectUrl() !== null) { | ||
redirect($transaction->getRedirectUrl()); | ||
} | ||
|
||
// Check the result message of the charge to find out what went wrong. | ||
$merchantMessage = $transaction->getMessage()->getCustomer(); | ||
} catch (UnzerApiException $e) { | ||
$merchantMessage = $e->getMerchantMessage(); | ||
$clientMessage = $e->getClientMessage(); | ||
} catch (RuntimeException $e) { | ||
$merchantMessage = $e->getMessage(); | ||
} | ||
redirect(FAILURE_URL, $merchantMessage, $clientMessage); |
Oops, something went wrong.