diff --git a/examples/PayPalRecurring/Constants.php b/examples/PayPalRecurring/Constants.php index d0fd6277..68f70111 100644 --- a/examples/PayPalRecurring/Constants.php +++ b/examples/PayPalRecurring/Constants.php @@ -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'); diff --git a/examples/PayPalRecurring/Controller.php b/examples/PayPalRecurring/Controller.php index 8fb8535d..990b2e60 100644 --- a/examples/PayPalRecurring/Controller.php +++ b/examples/PayPalRecurring/Controller.php @@ -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; diff --git a/examples/PayPalRecurring/ReturnController.php b/examples/PayPalRecurring/ReturnController.php new file mode 100644 index 00000000..13beb304 --- /dev/null +++ b/examples/PayPalRecurring/ReturnController.php @@ -0,0 +1,79 @@ + + * + * @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);