Skip to content

Commit

Permalink
Merge pull request #54 from unzerdev/develop
Browse files Browse the repository at this point in the history
1.1.4.0 release
  • Loading branch information
Ryouzanpaku authored Aug 31, 2021
2 parents d094b9e + 763d767 commit e18bf5e
Show file tree
Hide file tree
Showing 31 changed files with 804 additions and 66 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file.

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

## [1.1.4.0](https://github.com/unzerdev/php-sdk/compare/1.1.3.0..1.1.4.0)
### Added
* Enable recurrence type to be set for `charge`, `authorize` and `activateRecurringPayment` methods.

### Changed
* Enable recurring examples (card paypal)to trigger subsequent transaction from success page.
* Enable card recurring example to use recurrence type.
* Several minor improvements.

## [1.1.3.0](https://github.com/unzerdev/php-sdk/compare/1.1.2.0..1.1.3.0)
### Added
* Enable PHP 8.0 compatibility.
Expand Down
7 changes: 6 additions & 1 deletion examples/CardRecurring/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/** @noinspection PhpIncludeInspection */
require_once __DIR__ . '/../../../../autoload.php';

use UnzerSDK\Constants\RecurrenceTypes;
use UnzerSDK\examples\ExampleDebugHandler;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Unzer;
Expand All @@ -55,17 +56,21 @@ function redirect($url, $merchantMessage = '', $clientMessage = '')
}
$paymentTypeId = $_POST['resourceId'];

// Just for this example: Use selected recurrence type. Scheduled will be used as default.
$recurrenceTyp = $_POST['recurrence_type'] ?? RecurrenceTypes::SCHEDULED;

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

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

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

// Redirect to the 3ds page or to success depending on the state of the transaction
$redirect = !empty($recurring->getRedirectUrl());
Expand Down
95 changes: 95 additions & 0 deletions examples/CardRecurring/RecurringPaymentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* Controller for subsequent transactions.
*
* 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 David Owusu <development@unzer.com>
*
* @package UnzerSDK
*
*/

/** Require the constants of this example */
require_once __DIR__ . '/Constants.php';

/** Require the composer autoloader file */
/** @noinspection PhpIncludeInspection */
require_once __DIR__ . '/../../../../autoload.php';

use UnzerSDK\examples\ExampleDebugHandler;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\CustomerFactory;
use UnzerSDK\Unzer;

session_start();

$clientMessage = 'Something went wrong. Please try again later.';
$merchantMessage = 'Something went wrong. Please try again later.';
$debugHandler = new ExampleDebugHandler();

function redirect($url, $merchantMessage = '', $clientMessage = '')
{
$_SESSION['merchantMessage'] = $merchantMessage;
$_SESSION['clientMessage'] = $clientMessage;
header('Location: ' . $url);
die();
}

// You will need the id of the payment type created in the frontend (index.php)
if (!isset($_POST['payment_type_id'])) {
redirect(FAILURE_URL, 'Resource id is missing!', $clientMessage);
}
$paymentTypeId = $_POST['payment_type_id'];

// Reuse the recurrence type of the recurring transaction, if set.
$recurrenceTyp = $_SESSION['recurrenceType'] ?? null;

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

$customer = CustomerFactory::createCustomer('Max', 'Mustermann');
$customer->setEmail('test@test.com');

$transaction = $unzer->charge(12.99, 'EUR', $paymentTypeId, RETURN_CONTROLLER_URL, $customer, null, null, null, true, null, null, $recurrenceTyp);

// You'll need to remember the paymentId for later in the ReturnController (in case of 3ds)
$_SESSION['PaymentTypeId'] = $paymentTypeId;
$_SESSION['ShortId'] = $transaction->getShortId();

// Redirect to the failure page or to success depending on the state of the transaction
$redirect = !empty($transaction->getRedirectUrl());
if (!$redirect && $transaction->isSuccess()) {
redirect(SUCCESS_URL);
} elseif ($redirect && $transaction->isPending()) {
redirect(FAILURE_URL, 'Transaction initiated by merchant should not redirect to 3ds Page. The customer needs to
do the 3ds authentication first for that payment type.');
}

// Check the result message of the transaction to find out what went wrong.
$merchantMessage = $transaction->getMessage()->getCustomer();
} catch (UnzerApiException $e) {
$merchantMessage = $e->getMerchantMessage();
$clientMessage = $e->getClientMessage();
} catch (RuntimeException $e) {
$merchantMessage = $e->getMessage();
}
redirect(FAILURE_URL, $merchantMessage, $clientMessage);
18 changes: 18 additions & 0 deletions examples/CardRecurring/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@
<p><a href="https://docs.unzer.com/reference/test-data" target="_blank">Click here to open our test data in new tab.</a></p>

<form id="payment-form" class="unzerUI form" novalidate>
<!-- This is just for the example - Start -->
<div class="fields inline">
<label for="transaction_type">Chose the recurrence type you want to use:</label>
<div class="field">
<div class="unzerUI radio checkbox">
<input type="radio" name="recurrence_type" value="scheduled" checked="checked">
<label>Scheduled</label>
</div>
</div>
<div class="field">
<div class="unzerUI radio checkbox">
<input type="radio" name="recurrence_type" value="unscheduled">
<label>Unscheduled</label>
</div>
</div>
</div>
<!-- This is just for the example - End -->

<div class="field">
<div id="card-element-id-number" class="unzerInput">
<!-- Card number UI Element will be inserted here. -->
Expand Down
1 change: 1 addition & 0 deletions examples/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
define('PENDING_URL', EXAMPLE_BASE_FOLDER . 'Pending.php');
define('FAILURE_URL', EXAMPLE_BASE_FOLDER . 'Failure.php');
define('RETURN_CONTROLLER_URL', EXAMPLE_BASE_FOLDER . 'ReturnController.php');
define('RECURRING_PAYMENT_CONTROLLER_URL', EXAMPLE_BASE_FOLDER . 'CardRecurring/RecurringPaymentController.php');
38 changes: 29 additions & 9 deletions examples/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,47 @@
* @package UnzerSDK\examples
*/

require_once __DIR__ . '/Constants.php';

session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unzer UI Examples</title>

<link rel="stylesheet" href="https://static.unzer.com/v1/unzer.css" />
</head>
<body>
<h1 id="result">Success</h1>
<p>
The order has been successfully placed.

<?php
if (isset($_SESSION['additionalPaymentInformation'])) {
echo $_SESSION['additionalPaymentInformation'];
}
if (isset($_SESSION['additionalPaymentInformation'])) {
echo $_SESSION['additionalPaymentInformation'];
}

if (isset($_SESSION['ShortId']) && !empty($_SESSION['ShortId'])) {
echo '<p>Please look for ShortId ' . $_SESSION['ShortId'] . ' in Unzer Insights to see the transaction.</p>';
}
if (isset($_SESSION['PaymentId']) && !empty($_SESSION['PaymentId'])) {
echo '<p>The PaymentId of your transaction is \'' . $_SESSION['PaymentId'] . '\'.</p>';
}
if (isset($_SESSION['ShortId']) && !empty($_SESSION['ShortId'])) {
echo '<p>Please look for ShortId ' . $_SESSION['ShortId'] . ' in Unzer Insights to see the transaction.</p>';
}
if (isset($_SESSION['PaymentId']) && !empty($_SESSION['PaymentId'])) {
echo '<p>The PaymentId of your transaction is \'' . $_SESSION['PaymentId'] . '\'.</p>';
}
if (isset($_SESSION['PaymentTypeId']) && !empty($_SESSION['PaymentTypeId'])) {
echo '<p>The TypeId for the recurring payment is \'' . $_SESSION['PaymentTypeId'] . '\'. You can use it
now for subsequent transactions.</p>
<form id="payment-form" class="unzerUI form" action="' . RECURRING_PAYMENT_CONTROLLER_URL . '" method="post">
<input type="hidden" name="payment_type_id" value="' . $_SESSION['PaymentTypeId'] . ' ">
<div class="fields inline">
<div class="field">
<button class="unzerUI primary button fluid" id="submit-button" type="submit">Charge payment type again.</button>
</div>
</div>
</form>';
}
?>
</p>
<p><a href=".">start again</a></p>
Expand Down
38 changes: 38 additions & 0 deletions src/Constants/RecurrenceTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file contains the different recurrence types.
*
* 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 David Owusu <development@unzer.com>
*
* @package UnzerSDK\Constants
*/
namespace UnzerSDK\Constants;

class RecurrenceTypes
{
/** @var string Recurring with a defined interval and a defined amount.*/
public const SCHEDULED = 'scheduled';

/** @var string Recurring with a undefined interval and/or an undefined amount.*/
public const UNSCHEDULED = 'unscheduled';

/** @var string If the payment type should be used again for future transactions.*/
public const ONE_CLICK = 'oneclick';
}
37 changes: 21 additions & 16 deletions src/Interfaces/PaymentServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ interface PaymentServiceInterface
/**
* Performs an Authorization transaction and returns the resulting Authorization resource.
*
* @param float $amount The amount to authorize.
* @param string $currency The currency of the amount.
* @param string|BasePaymentType $paymentType The PaymentType object or the id of the PaymentType to use.
* @param string $returnUrl The URL used to return to the shop if the process requires leaving it.
* @param Customer|string|null $customer The Customer object or the id of the customer resource to reference.
* @param string|null $orderId A custom order id which can be set by the merchant.
* @param Metadata|null $metadata The Metadata object containing custom information for the payment.
* @param Basket|null $basket The Basket object corresponding to the payment.
* The Basket object will be created automatically if it does not exist
* yet (i.e. has no id).
* @param bool|null $card3ds Enables 3ds channel for credit cards if available. This parameter is
* optional and will be ignored if not applicable.
* @param string|null $invoiceId The external id of the invoice.
* @param string|null $referenceText A reference text for the payment.
* @param float $amount The amount to authorize.
* @param string $currency The currency of the amount.
* @param string|BasePaymentType $paymentType The PaymentType object or the id of the PaymentType to use.
* @param string $returnUrl The URL used to return to the shop if the process requires leaving it.
* @param Customer|string|null $customer The Customer object or the id of the customer resource to reference.
* @param string|null $orderId A custom order id which can be set by the merchant.
* @param Metadata|null $metadata The Metadata object containing custom information for the payment.
* @param Basket|null $basket The Basket object corresponding to the payment.
* The Basket object will be created automatically if it does not exist
* yet (i.e. has no id).
* @param bool|null $card3ds Enables 3ds channel for credit cards if available. This parameter is
* optional and will be ignored if not applicable.
* @param string|null $invoiceId The external id of the invoice.
* @param string|null $referenceText A reference text for the payment.
* @param string|null $recurrenceType Recurrence type used for recurring payment.
*
* @return Authorization The resulting object of the Authorization resource.
*
Expand All @@ -76,7 +77,8 @@ public function authorize(
$basket = null,
$card3ds = null,
$invoiceId = null,
$referenceText = null
$referenceText = null,
$recurrenceType = null
): Authorization;

/**
Expand All @@ -96,6 +98,8 @@ public function authorize(
* optional and will be ignored if not applicable.
* @param string|null $invoiceId The external id of the invoice.
* @param string|null $paymentReference A reference text for the payment.
* @param string|null $recurrenceType Recurrence type used for recurring payment.
* See \UnzerSDK\Constants\RecurrenceTypes to find all supported types.
*
* @return Charge The resulting object of the Charge resource.
*
Expand All @@ -113,7 +117,8 @@ public function charge(
$basket = null,
$card3ds = null,
$invoiceId = null,
$paymentReference = null
$paymentReference = null,
$recurrenceType = null
): Charge;

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Interfaces/ResourceServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ public function fetchPayout($payment): Payout;
/**
* Activate recurring payment for the given payment type (if possible).
*
* @param string|BasePaymentType $paymentType The payment to activate recurring payment for.
* @param string $returnUrl The URL to which the customer gets redirected in case of a 3ds
* transaction
* @param string|BasePaymentType $paymentType The payment to activate recurring payment for.
* @param string $returnUrl The URL to which the customer gets redirected in case of a 3ds
* transaction
* @param string $recurrenceType Recurrence type used for recurring payment.
*
* @return Recurring The recurring object.
*
* @throws UnzerApiException An UnzerApiException is thrown if there is an error returned on API-request.
* @throws RuntimeException A RuntimeException is thrown when there is an error while using the SDK.
*/
public function activateRecurringPayment($paymentType, $returnUrl): Recurring;
public function activateRecurringPayment($paymentType, $returnUrl, string $recurrenceType = null): Recurring;

/**
* Fetch and return payment by given payment id or payment object.
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
*/
namespace UnzerSDK\Resources;

use UnzerSDK\Traits\HasAdditionalTransactionData;
use UnzerSDK\Traits\HasCustomerMessage;
use UnzerSDK\Traits\HasDate;
use UnzerSDK\Traits\HasRecurrenceType;
use UnzerSDK\Traits\HasStates;
use UnzerSDK\Traits\HasUniqueAndShortId;

Expand All @@ -35,6 +37,8 @@ class Recurring extends AbstractUnzerResource
use HasUniqueAndShortId;
use HasCustomerMessage;
use HasDate;
use HasAdditionalTransactionData;
use HasRecurrenceType;

/** @var string $returnUrl */
protected $returnUrl;
Expand Down
Loading

0 comments on commit e18bf5e

Please sign in to comment.