Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/PayPalRecurring/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@

define('EXAMPLE_URL', EXAMPLE_BASE_FOLDER . 'PayPalRecurring');
define('CONTROLLER_URL', EXAMPLE_URL . '/Controller.php');
define('MY_RETURN_CONTROLLER_URL', EXAMPLE_URL . '/ReturnController.php');
2 changes: 1 addition & 1 deletion examples/PayPalRecurring/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function redirect($url, $merchantMessage = '', $clientMessage = '')
$unzer = new Unzer(UNZER_PAPI_PRIVATE_KEY);
$unzer->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler());

$recurring = $unzer->activateRecurringPayment($paymentTypeId, RETURN_CONTROLLER_URL);
$recurring = $unzer->activateRecurringPayment($paymentTypeId, MY_RETURN_CONTROLLER_URL);

// You'll need to remember the paymentId for later in the ReturnController (in case of 3ds)
$_SESSION['PaymentTypeId'] = $paymentTypeId;
Expand Down
79 changes: 79 additions & 0 deletions examples/PayPalRecurring/ReturnController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* This is the return controller for the PayPal recurring example.
* It is called when the client is redirected back to the shop from the external page.
*
* Copyright (C) 2021 - 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/
*
* @author Simon Gabriel <development@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;
use UnzerSDK\Resources\PaymentTypes\Card;

session_start();

$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();
}

// Retrieve the paymentId you remembered within the Controller
if (!isset($_SESSION['PaymentTypeId'])) {
redirect(FAILURE_URL, 'The payment type id is missing.', $clientMessage);
}
$paymentTypeId = $_SESSION['PaymentTypeId'];

// 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());

// Redirect to success if the payment has been successfully completed or is still in handled.
/** @var Card $paymentType */
$paymentType = $unzer->fetchPaymentType($paymentTypeId);

if ($paymentType->isRecurring()) {
redirect(SUCCESS_URL);
}

} catch (UnzerApiException $e) {
$merchantMessage = $e->getMerchantMessage();
$clientMessage = $e->getClientMessage();
} catch (RuntimeException $e) {
$merchantMessage = $e->getMessage();
}

redirect(FAILURE_URL, $merchantMessage, $clientMessage);