Skip to content

Commit e18bf5e

Browse files
authored
Merge pull request #54 from unzerdev/develop
1.1.4.0 release
2 parents d094b9e + 763d767 commit e18bf5e

31 files changed

+804
-66
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file.
33

44
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).
55

6+
## [1.1.4.0](https://github.com/unzerdev/php-sdk/compare/1.1.3.0..1.1.4.0)
7+
### Added
8+
* Enable recurrence type to be set for `charge`, `authorize` and `activateRecurringPayment` methods.
9+
10+
### Changed
11+
* Enable recurring examples (card paypal)to trigger subsequent transaction from success page.
12+
* Enable card recurring example to use recurrence type.
13+
* Several minor improvements.
14+
615
## [1.1.3.0](https://github.com/unzerdev/php-sdk/compare/1.1.2.0..1.1.3.0)
716
### Added
817
* Enable PHP 8.0 compatibility.

examples/CardRecurring/Controller.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/** @noinspection PhpIncludeInspection */
3232
require_once __DIR__ . '/../../../../autoload.php';
3333

34+
use UnzerSDK\Constants\RecurrenceTypes;
3435
use UnzerSDK\examples\ExampleDebugHandler;
3536
use UnzerSDK\Exceptions\UnzerApiException;
3637
use UnzerSDK\Unzer;
@@ -55,17 +56,21 @@ function redirect($url, $merchantMessage = '', $clientMessage = '')
5556
}
5657
$paymentTypeId = $_POST['resourceId'];
5758

59+
// Just for this example: Use selected recurrence type. Scheduled will be used as default.
60+
$recurrenceTyp = $_POST['recurrence_type'] ?? RecurrenceTypes::SCHEDULED;
61+
5862
// Catch API errors, write the message to your log and show the ClientMessage to the client.
5963
try {
6064
// Create an Unzer object using your private key and register a debug handler if you want to.
6165
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY);
6266
$unzer->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler());
6367

64-
$recurring = $unzer->activateRecurringPayment($paymentTypeId, MY_RETURN_CONTROLLER_URL);
68+
$recurring = $unzer->activateRecurringPayment($paymentTypeId, MY_RETURN_CONTROLLER_URL, $recurrenceTyp);
6569

6670
// You'll need to remember the paymentId for later in the ReturnController (in case of 3ds)
6771
$_SESSION['PaymentTypeId'] = $paymentTypeId;
6872
$_SESSION['ShortId'] = $recurring->getShortId();
73+
$_SESSION['recurrenceType'] = $recurring->getRecurrenceType();
6974

7075
// Redirect to the 3ds page or to success depending on the state of the transaction
7176
$redirect = !empty($recurring->getRedirectUrl());
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* Controller for subsequent transactions.
5+
*
6+
* Copyright (C) 2021 - 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+
* @author David Owusu <[email protected]>
23+
*
24+
* @package UnzerSDK
25+
*
26+
*/
27+
28+
/** Require the constants of this example */
29+
require_once __DIR__ . '/Constants.php';
30+
31+
/** Require the composer autoloader file */
32+
/** @noinspection PhpIncludeInspection */
33+
require_once __DIR__ . '/../../../../autoload.php';
34+
35+
use UnzerSDK\examples\ExampleDebugHandler;
36+
use UnzerSDK\Exceptions\UnzerApiException;
37+
use UnzerSDK\Resources\CustomerFactory;
38+
use UnzerSDK\Unzer;
39+
40+
session_start();
41+
42+
$clientMessage = 'Something went wrong. Please try again later.';
43+
$merchantMessage = 'Something went wrong. Please try again later.';
44+
$debugHandler = new ExampleDebugHandler();
45+
46+
function redirect($url, $merchantMessage = '', $clientMessage = '')
47+
{
48+
$_SESSION['merchantMessage'] = $merchantMessage;
49+
$_SESSION['clientMessage'] = $clientMessage;
50+
header('Location: ' . $url);
51+
die();
52+
}
53+
54+
// You will need the id of the payment type created in the frontend (index.php)
55+
if (!isset($_POST['payment_type_id'])) {
56+
redirect(FAILURE_URL, 'Resource id is missing!', $clientMessage);
57+
}
58+
$paymentTypeId = $_POST['payment_type_id'];
59+
60+
// Reuse the recurrence type of the recurring transaction, if set.
61+
$recurrenceTyp = $_SESSION['recurrenceType'] ?? null;
62+
63+
// Catch API errors, write the message to your log and show the ClientMessage to the client.
64+
try {
65+
// Create an Unzer object using your private key and register a debug handler if you want to.
66+
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY);
67+
$unzer->setDebugMode(true)->setDebugHandler($debugHandler);
68+
69+
$customer = CustomerFactory::createCustomer('Max', 'Mustermann');
70+
$customer->setEmail('[email protected]');
71+
72+
$transaction = $unzer->charge(12.99, 'EUR', $paymentTypeId, RETURN_CONTROLLER_URL, $customer, null, null, null, true, null, null, $recurrenceTyp);
73+
74+
// You'll need to remember the paymentId for later in the ReturnController (in case of 3ds)
75+
$_SESSION['PaymentTypeId'] = $paymentTypeId;
76+
$_SESSION['ShortId'] = $transaction->getShortId();
77+
78+
// Redirect to the failure page or to success depending on the state of the transaction
79+
$redirect = !empty($transaction->getRedirectUrl());
80+
if (!$redirect && $transaction->isSuccess()) {
81+
redirect(SUCCESS_URL);
82+
} elseif ($redirect && $transaction->isPending()) {
83+
redirect(FAILURE_URL, 'Transaction initiated by merchant should not redirect to 3ds Page. The customer needs to
84+
do the 3ds authentication first for that payment type.');
85+
}
86+
87+
// Check the result message of the transaction to find out what went wrong.
88+
$merchantMessage = $transaction->getMessage()->getCustomer();
89+
} catch (UnzerApiException $e) {
90+
$merchantMessage = $e->getMerchantMessage();
91+
$clientMessage = $e->getClientMessage();
92+
} catch (RuntimeException $e) {
93+
$merchantMessage = $e->getMessage();
94+
}
95+
redirect(FAILURE_URL, $merchantMessage, $clientMessage);

examples/CardRecurring/index.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@
6363
<p><a href="https://docs.unzer.com/reference/test-data" target="_blank">Click here to open our test data in new tab.</a></p>
6464

6565
<form id="payment-form" class="unzerUI form" novalidate>
66+
<!-- This is just for the example - Start -->
67+
<div class="fields inline">
68+
<label for="transaction_type">Chose the recurrence type you want to use:</label>
69+
<div class="field">
70+
<div class="unzerUI radio checkbox">
71+
<input type="radio" name="recurrence_type" value="scheduled" checked="checked">
72+
<label>Scheduled</label>
73+
</div>
74+
</div>
75+
<div class="field">
76+
<div class="unzerUI radio checkbox">
77+
<input type="radio" name="recurrence_type" value="unscheduled">
78+
<label>Unscheduled</label>
79+
</div>
80+
</div>
81+
</div>
82+
<!-- This is just for the example - End -->
83+
6684
<div class="field">
6785
<div id="card-element-id-number" class="unzerInput">
6886
<!-- Card number UI Element will be inserted here. -->

examples/Constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
define('PENDING_URL', EXAMPLE_BASE_FOLDER . 'Pending.php');
3434
define('FAILURE_URL', EXAMPLE_BASE_FOLDER . 'Failure.php');
3535
define('RETURN_CONTROLLER_URL', EXAMPLE_BASE_FOLDER . 'ReturnController.php');
36+
define('RECURRING_PAYMENT_CONTROLLER_URL', EXAMPLE_BASE_FOLDER . 'CardRecurring/RecurringPaymentController.php');

examples/Success.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,47 @@
2323
* @package UnzerSDK\examples
2424
*/
2525

26+
require_once __DIR__ . '/Constants.php';
27+
2628
session_start();
2729
?>
2830

2931
<!DOCTYPE html>
3032
<html lang="en">
33+
<head>
34+
<meta charset="UTF-8">
35+
<title>Unzer UI Examples</title>
36+
37+
<link rel="stylesheet" href="https://static.unzer.com/v1/unzer.css" />
38+
</head>
3139
<body>
3240
<h1 id="result">Success</h1>
3341
<p>
3442
The order has been successfully placed.
3543

3644
<?php
37-
if (isset($_SESSION['additionalPaymentInformation'])) {
38-
echo $_SESSION['additionalPaymentInformation'];
39-
}
45+
if (isset($_SESSION['additionalPaymentInformation'])) {
46+
echo $_SESSION['additionalPaymentInformation'];
47+
}
4048

41-
if (isset($_SESSION['ShortId']) && !empty($_SESSION['ShortId'])) {
42-
echo '<p>Please look for ShortId ' . $_SESSION['ShortId'] . ' in Unzer Insights to see the transaction.</p>';
43-
}
44-
if (isset($_SESSION['PaymentId']) && !empty($_SESSION['PaymentId'])) {
45-
echo '<p>The PaymentId of your transaction is \'' . $_SESSION['PaymentId'] . '\'.</p>';
46-
}
49+
if (isset($_SESSION['ShortId']) && !empty($_SESSION['ShortId'])) {
50+
echo '<p>Please look for ShortId ' . $_SESSION['ShortId'] . ' in Unzer Insights to see the transaction.</p>';
51+
}
52+
if (isset($_SESSION['PaymentId']) && !empty($_SESSION['PaymentId'])) {
53+
echo '<p>The PaymentId of your transaction is \'' . $_SESSION['PaymentId'] . '\'.</p>';
54+
}
55+
if (isset($_SESSION['PaymentTypeId']) && !empty($_SESSION['PaymentTypeId'])) {
56+
echo '<p>The TypeId for the recurring payment is \'' . $_SESSION['PaymentTypeId'] . '\'. You can use it
57+
now for subsequent transactions.</p>
58+
<form id="payment-form" class="unzerUI form" action="' . RECURRING_PAYMENT_CONTROLLER_URL . '" method="post">
59+
<input type="hidden" name="payment_type_id" value="' . $_SESSION['PaymentTypeId'] . ' ">
60+
<div class="fields inline">
61+
<div class="field">
62+
<button class="unzerUI primary button fluid" id="submit-button" type="submit">Charge payment type again.</button>
63+
</div>
64+
</div>
65+
</form>';
66+
}
4767
?>
4868
</p>
4969
<p><a href=".">start again</a></p>

src/Constants/RecurrenceTypes.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* This file contains the different recurrence types.
5+
*
6+
* Copyright (C) 2021 - 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+
* @author David Owusu <[email protected]>
23+
*
24+
* @package UnzerSDK\Constants
25+
*/
26+
namespace UnzerSDK\Constants;
27+
28+
class RecurrenceTypes
29+
{
30+
/** @var string Recurring with a defined interval and a defined amount.*/
31+
public const SCHEDULED = 'scheduled';
32+
33+
/** @var string Recurring with a undefined interval and/or an undefined amount.*/
34+
public const UNSCHEDULED = 'unscheduled';
35+
36+
/** @var string If the payment type should be used again for future transactions.*/
37+
public const ONE_CLICK = 'oneclick';
38+
}

src/Interfaces/PaymentServiceInterface.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ interface PaymentServiceInterface
4545
/**
4646
* Performs an Authorization transaction and returns the resulting Authorization resource.
4747
*
48-
* @param float $amount The amount to authorize.
49-
* @param string $currency The currency of the amount.
50-
* @param string|BasePaymentType $paymentType The PaymentType object or the id of the PaymentType to use.
51-
* @param string $returnUrl The URL used to return to the shop if the process requires leaving it.
52-
* @param Customer|string|null $customer The Customer object or the id of the customer resource to reference.
53-
* @param string|null $orderId A custom order id which can be set by the merchant.
54-
* @param Metadata|null $metadata The Metadata object containing custom information for the payment.
55-
* @param Basket|null $basket The Basket object corresponding to the payment.
56-
* The Basket object will be created automatically if it does not exist
57-
* yet (i.e. has no id).
58-
* @param bool|null $card3ds Enables 3ds channel for credit cards if available. This parameter is
59-
* optional and will be ignored if not applicable.
60-
* @param string|null $invoiceId The external id of the invoice.
61-
* @param string|null $referenceText A reference text for the payment.
48+
* @param float $amount The amount to authorize.
49+
* @param string $currency The currency of the amount.
50+
* @param string|BasePaymentType $paymentType The PaymentType object or the id of the PaymentType to use.
51+
* @param string $returnUrl The URL used to return to the shop if the process requires leaving it.
52+
* @param Customer|string|null $customer The Customer object or the id of the customer resource to reference.
53+
* @param string|null $orderId A custom order id which can be set by the merchant.
54+
* @param Metadata|null $metadata The Metadata object containing custom information for the payment.
55+
* @param Basket|null $basket The Basket object corresponding to the payment.
56+
* The Basket object will be created automatically if it does not exist
57+
* yet (i.e. has no id).
58+
* @param bool|null $card3ds Enables 3ds channel for credit cards if available. This parameter is
59+
* optional and will be ignored if not applicable.
60+
* @param string|null $invoiceId The external id of the invoice.
61+
* @param string|null $referenceText A reference text for the payment.
62+
* @param string|null $recurrenceType Recurrence type used for recurring payment.
6263
*
6364
* @return Authorization The resulting object of the Authorization resource.
6465
*
@@ -76,7 +77,8 @@ public function authorize(
7677
$basket = null,
7778
$card3ds = null,
7879
$invoiceId = null,
79-
$referenceText = null
80+
$referenceText = null,
81+
$recurrenceType = null
8082
): Authorization;
8183

8284
/**
@@ -96,6 +98,8 @@ public function authorize(
9698
* optional and will be ignored if not applicable.
9799
* @param string|null $invoiceId The external id of the invoice.
98100
* @param string|null $paymentReference A reference text for the payment.
101+
* @param string|null $recurrenceType Recurrence type used for recurring payment.
102+
* See \UnzerSDK\Constants\RecurrenceTypes to find all supported types.
99103
*
100104
* @return Charge The resulting object of the Charge resource.
101105
*
@@ -113,7 +117,8 @@ public function charge(
113117
$basket = null,
114118
$card3ds = null,
115119
$invoiceId = null,
116-
$paymentReference = null
120+
$paymentReference = null,
121+
$recurrenceType = null
117122
): Charge;
118123

119124
/**

src/Interfaces/ResourceServiceInterface.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ public function fetchPayout($payment): Payout;
6060
/**
6161
* Activate recurring payment for the given payment type (if possible).
6262
*
63-
* @param string|BasePaymentType $paymentType The payment to activate recurring payment for.
64-
* @param string $returnUrl The URL to which the customer gets redirected in case of a 3ds
65-
* transaction
63+
* @param string|BasePaymentType $paymentType The payment to activate recurring payment for.
64+
* @param string $returnUrl The URL to which the customer gets redirected in case of a 3ds
65+
* transaction
66+
* @param string $recurrenceType Recurrence type used for recurring payment.
6667
*
6768
* @return Recurring The recurring object.
6869
*
6970
* @throws UnzerApiException An UnzerApiException is thrown if there is an error returned on API-request.
7071
* @throws RuntimeException A RuntimeException is thrown when there is an error while using the SDK.
7172
*/
72-
public function activateRecurringPayment($paymentType, $returnUrl): Recurring;
73+
public function activateRecurringPayment($paymentType, $returnUrl, string $recurrenceType = null): Recurring;
7374

7475
/**
7576
* Fetch and return payment by given payment id or payment object.

src/Resources/Recurring.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
*/
2525
namespace UnzerSDK\Resources;
2626

27+
use UnzerSDK\Traits\HasAdditionalTransactionData;
2728
use UnzerSDK\Traits\HasCustomerMessage;
2829
use UnzerSDK\Traits\HasDate;
30+
use UnzerSDK\Traits\HasRecurrenceType;
2931
use UnzerSDK\Traits\HasStates;
3032
use UnzerSDK\Traits\HasUniqueAndShortId;
3133

@@ -35,6 +37,8 @@ class Recurring extends AbstractUnzerResource
3537
use HasUniqueAndShortId;
3638
use HasCustomerMessage;
3739
use HasDate;
40+
use HasAdditionalTransactionData;
41+
use HasRecurrenceType;
3842

3943
/** @var string $returnUrl */
4044
protected $returnUrl;

0 commit comments

Comments
 (0)