Skip to content

Commit 7842ffa

Browse files
authored
Merge pull request #140 from unzerdev/develop
Develop
2 parents 7a40a94 + 515602b commit 7842ffa

21 files changed

+871
-28
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres
66
to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [3.1.0](https://github.com/unzerdev/php-sdk/compare/3.0.0..3.1.0)
9+
10+
### Added
11+
12+
* Add payment types "Post Finance Card" and "Post Finance eFinance".
13+
* Add setter/getter to PayPage for `recurrenceType` and `exemptionType`.
14+
15+
### Changed
16+
17+
* Make setter and getter for `PayPage::AdditionalAttributes` public to allow adding information manually.
18+
819
## [3.0.0](https://github.com/unzerdev/php-sdk/compare/1.2.3.0..3.0.0)
920

1021
### Added
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file defines the constants needed for the PostFinanceCard example.
5+
*
6+
* Copyright (C) 2020 - today Unzer E-Com GmbH
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* @link https://docs.unzer.com/
21+
*
22+
* @package UnzerSDK\examples
23+
*/
24+
25+
require_once __DIR__ . '/../Constants.php';
26+
27+
define('EXAMPLE_URL', EXAMPLE_BASE_FOLDER . 'PostFinanceCard');
28+
define('CONTROLLER_URL', EXAMPLE_URL . '/Controller.php');
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* This is the controller for the PostFinanceCard example.
4+
* It is called when the pay button on the index page is clicked.
5+
*
6+
* Copyright (C) 2020 - today Unzer E-Com GmbH
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* @link https://docs.unzer.com/
21+
*
22+
* @package UnzerSDK\examples
23+
*/
24+
25+
/** Require the constants of this example */
26+
require_once __DIR__ . '/Constants.php';
27+
28+
/** @noinspection PhpIncludeInspection */
29+
/** Require the composer autoloader file */
30+
require_once __DIR__ . '/../../../../autoload.php';
31+
32+
use UnzerSDK\examples\ExampleDebugHandler;
33+
use UnzerSDK\Exceptions\UnzerApiException;
34+
use UnzerSDK\Unzer;
35+
36+
session_start();
37+
session_unset();
38+
39+
$clientMessage = 'Something went wrong. Please try again later.';
40+
$merchantMessage = 'Something went wrong. Please try again later.';
41+
42+
function redirect($url, $merchantMessage = '', $clientMessage = '')
43+
{
44+
$_SESSION['merchantMessage'] = $merchantMessage;
45+
$_SESSION['clientMessage'] = $clientMessage;
46+
header('Location: ' . $url);
47+
die();
48+
}
49+
50+
// You will need the id of the payment type created in the frontend (index.php)
51+
if (!isset($_POST['resourceId'])) {
52+
redirect(FAILURE_URL, 'Resource id is missing!', $clientMessage);
53+
}
54+
$paymentTypeId = $_POST['resourceId'];
55+
56+
// Catch API errors, write the message to your log and show the ClientMessage to the client.
57+
try {
58+
// Create an Unzer object using your private key and register a debug handler if you want to.
59+
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY);
60+
$unzer->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler());
61+
62+
// Create a charge transaction to get the redirectUrl.
63+
$transaction = new \UnzerSDK\Resources\TransactionTypes\Charge(12.32, 'CHF', RETURN_CONTROLLER_URL);
64+
$unzer->performCharge($transaction, $paymentTypeId);
65+
66+
// You'll need to remember the paymentId for later in the ReturnController
67+
$_SESSION['PaymentId'] = $transaction->getPaymentId();
68+
$_SESSION['ShortId'] = $transaction->getShortId();
69+
70+
// Redirect to the PostFinance page
71+
if (!$transaction->isError() && $transaction->getRedirectUrl() !== null) {
72+
redirect($transaction->getRedirectUrl());
73+
}
74+
75+
// Check the result message of the charge to find out what went wrong.
76+
$merchantMessage = $transaction->getMessage()->getCustomer();
77+
} catch (UnzerApiException $e) {
78+
$merchantMessage = $e->getMerchantMessage();
79+
$clientMessage = $e->getClientMessage();
80+
} catch (RuntimeException $e) {
81+
$merchantMessage = $e->getMessage();
82+
}
83+
redirect(FAILURE_URL, $merchantMessage, $clientMessage);

examples/PostFinanceCard/index.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* This file provides an example implementation of the PostFinanceCard payment type.
4+
*
5+
* Copyright (C) 2020 - today Unzer E-Com GmbH
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
* @link https://docs.unzer.com/
20+
*
21+
* @package UnzerSDK\examples
22+
*/
23+
24+
/** Require the constants of this example */
25+
require_once __DIR__ . '/Constants.php';
26+
27+
/** @noinspection PhpIncludeInspection */
28+
/** Require the composer autoloader file */
29+
require_once __DIR__ . '/../../../../autoload.php';
30+
?>
31+
32+
<!DOCTYPE html>
33+
<html lang="en">
34+
<head>
35+
<meta charset="UTF-8">
36+
<title>Unzer UI Examples</title>
37+
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
38+
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
39+
crossorigin="anonymous"></script>
40+
41+
<link rel="stylesheet" href="https://static.unzer.com/v1/unzer.css" />
42+
<script type="text/javascript" src="https://static.unzer.com/v1/unzer.js"></script>
43+
</head>
44+
45+
<body style="margin: 70px 70px 0;">
46+
47+
<p><a href="https://docs.unzer.com/reference/test-data" target="_blank">Click here to open our test data in new tab.</a></p>
48+
49+
<form id="payment-form" class="unzerUI form" novalidate>
50+
<div class="field" id="error-holder" style="color: #9f3a38"> </div>
51+
<div class="field">
52+
<button class="unzerUI primary button fluid" id="submit-button" type="submit">Pay</button>
53+
</div>
54+
</form>
55+
56+
<script>
57+
// Create an Unzer instance with your public key
58+
let unzerInstance = new unzer('<?php echo UNZER_PAPI_PUBLIC_KEY; ?>');
59+
60+
// Create an PostFinanceCard instance
61+
let postFinanceCard = unzerInstance.PostFinanceCard();
62+
63+
// Handle payment form submission
64+
let form = document.getElementById('payment-form');
65+
form.addEventListener('submit', function(event) {
66+
event.preventDefault();
67+
// Creating a postFinanceCard resource
68+
postFinanceCard.createResource()
69+
.then(function(result) {
70+
let hiddenInput = document.createElement('input');
71+
hiddenInput.setAttribute('type', 'hidden');
72+
hiddenInput.setAttribute('name', 'resourceId');
73+
hiddenInput.setAttribute('value', result.id);
74+
form.appendChild(hiddenInput);
75+
form.setAttribute('method', 'POST');
76+
form.setAttribute('action', '<?php echo CONTROLLER_URL; ?>');
77+
78+
// Submitting the form
79+
form.submit();
80+
})
81+
.catch(function(error) {
82+
$('#error-holder').html(error.message)
83+
})
84+
});
85+
</script>
86+
</body>
87+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file defines the constants needed for the PostFinanceEfinance example.
5+
*
6+
* Copyright (C) 2020 - today Unzer E-Com GmbH
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* @link https://docs.unzer.com/
21+
*
22+
* @package UnzerSDK\examples
23+
*/
24+
25+
require_once __DIR__ . '/../Constants.php';
26+
27+
define('EXAMPLE_URL', EXAMPLE_BASE_FOLDER . 'PostFinanceEfinance');
28+
define('CONTROLLER_URL', EXAMPLE_URL . '/Controller.php');
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* This is the controller for the PostFinanceEfinance example.
4+
* It is called when the pay button on the index page is clicked.
5+
*
6+
* Copyright (C) 2020 - today Unzer E-Com GmbH
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* @link https://docs.unzer.com/
21+
*
22+
* @package UnzerSDK\examples
23+
*/
24+
25+
/** Require the constants of this example */
26+
require_once __DIR__ . '/Constants.php';
27+
28+
/** @noinspection PhpIncludeInspection */
29+
/** Require the composer autoloader file */
30+
require_once __DIR__ . '/../../../../autoload.php';
31+
32+
use UnzerSDK\examples\ExampleDebugHandler;
33+
use UnzerSDK\Exceptions\UnzerApiException;
34+
use UnzerSDK\Unzer;
35+
36+
session_start();
37+
session_unset();
38+
39+
$clientMessage = 'Something went wrong. Please try again later.';
40+
$merchantMessage = 'Something went wrong. Please try again later.';
41+
42+
function redirect($url, $merchantMessage = '', $clientMessage = '')
43+
{
44+
$_SESSION['merchantMessage'] = $merchantMessage;
45+
$_SESSION['clientMessage'] = $clientMessage;
46+
header('Location: ' . $url);
47+
die();
48+
}
49+
50+
// You will need the id of the payment type created in the frontend (index.php)
51+
if (!isset($_POST['resourceId'])) {
52+
redirect(FAILURE_URL, 'Resource id is missing!', $clientMessage);
53+
}
54+
$paymentTypeId = $_POST['resourceId'];
55+
56+
// Catch API errors, write the message to your log and show the ClientMessage to the client.
57+
try {
58+
// Create an Unzer object using your private key and register a debug handler if you want to.
59+
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY);
60+
$unzer->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler());
61+
62+
// Create a charge transaction to get the redirectUrl.
63+
$transaction = new \UnzerSDK\Resources\TransactionTypes\Charge(12.32, 'CHF', RETURN_CONTROLLER_URL);
64+
$unzer->performCharge($transaction, $paymentTypeId);
65+
66+
// You'll need to remember the paymentId for later in the ReturnController
67+
$_SESSION['PaymentId'] = $transaction->getPaymentId();
68+
$_SESSION['ShortId'] = $transaction->getShortId();
69+
70+
// Redirect to the PostFinance page
71+
if (!$transaction->isError() && $transaction->getRedirectUrl() !== null) {
72+
redirect($transaction->getRedirectUrl());
73+
}
74+
75+
// Check the result message of the charge to find out what went wrong.
76+
$merchantMessage = $transaction->getMessage()->getCustomer();
77+
} catch (UnzerApiException $e) {
78+
$merchantMessage = $e->getMerchantMessage();
79+
$clientMessage = $e->getClientMessage();
80+
} catch (RuntimeException $e) {
81+
$merchantMessage = $e->getMessage();
82+
}
83+
redirect(FAILURE_URL, $merchantMessage, $clientMessage);

0 commit comments

Comments
 (0)