|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This is the controller for the PayU 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, 'PLN', 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 PayU 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