Skip to content

Commit

Permalink
Merge pull request #140 from unzerdev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Ryouzanpaku authored Apr 5, 2023
2 parents 7a40a94 + 515602b commit 7842ffa
Show file tree
Hide file tree
Showing 21 changed files with 871 additions and 28 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres
to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.1.0](https://github.com/unzerdev/php-sdk/compare/3.0.0..3.1.0)

### Added

* Add payment types "Post Finance Card" and "Post Finance eFinance".
* Add setter/getter to PayPage for `recurrenceType` and `exemptionType`.

### Changed

* Make setter and getter for `PayPage::AdditionalAttributes` public to allow adding information manually.

## [3.0.0](https://github.com/unzerdev/php-sdk/compare/1.2.3.0..3.0.0)

### Added
Expand Down
28 changes: 28 additions & 0 deletions examples/PostFinanceCard/Constants.php
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');
83 changes: 83 additions & 0 deletions examples/PostFinanceCard/Controller.php
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);
87 changes: 87 additions & 0 deletions examples/PostFinanceCard/index.php
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>
28 changes: 28 additions & 0 deletions examples/PostFinanceEfinance/Constants.php
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');
83 changes: 83 additions & 0 deletions examples/PostFinanceEfinance/Controller.php
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);
Loading

0 comments on commit 7842ffa

Please sign in to comment.