Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-1576/preauth #201

Merged
merged 8 commits into from
Oct 30, 2024
3 changes: 2 additions & 1 deletion src/Constants/IdStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class IdStrings
public const AUTHORIZE = 'aut';
public const CANCEL = 'cnl';
public const CHARGE = 'chg';
public const CHARGEBACK = 'cbk';
public const PAYOUT = 'out';
public const PREAUTHORIZE = 'preaut';
public const SHIPMENT = 'shp';
public const CHARGEBACK = 'cbk';

// Payment Types
public const ALIPAY = 'ali';
Expand Down
1 change: 1 addition & 0 deletions src/Constants/TransactionTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class TransactionTypes
{
public const AUTHORIZATION = 'authorize';
public const PREAUTHORIZATION = 'preauthorize';
public const CHARGE = 'charge';
public const REVERSAL = 'cancel-authorize';
public const REFUND = 'cancel-charge';
Expand Down
16 changes: 16 additions & 0 deletions src/Constants/WebhookEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class WebhookEvents
public const AUTHORIZE_RESUMED = 'authorize.resumed';
public const AUTHORIZE_SUCCEEDED = 'authorize.succeeded';

// preauthorize events
public const PREAUTHORIZE = 'preauthorize';
public const PREAUTHORIZE_CANCELED = 'preauthorize.canceled';
public const PREAUTHORIZE_EXPIRED = 'preauthorize.expired';
public const PREAUTHORIZE_FAILED = 'preauthorize.failed';
public const PREAUTHORIZE_PENDING = 'preauthorize.pending';
public const PREAUTHORIZE_RESUMED = 'preauthorize.resumed';
public const PREAUTHORIZE_SUCCEEDED = 'preauthorize.succeeded';

// charge events
public const CHARGE = 'charge';
public const CHARGE_CANCELED = 'charge.canceled';
Expand Down Expand Up @@ -69,6 +78,13 @@ class WebhookEvents
self::AUTHORIZE_PENDING,
self::AUTHORIZE_RESUMED,
self::AUTHORIZE_SUCCEEDED,
self::PREAUTHORIZE,
self::PREAUTHORIZE_CANCELED,
self::PREAUTHORIZE_EXPIRED,
self::PREAUTHORIZE_FAILED,
self::PREAUTHORIZE_PENDING,
self::PREAUTHORIZE_RESUMED,
self::PREAUTHORIZE_SUCCEEDED,
self::CHARGE,
self::CHARGE_CANCELED,
self::CHARGE_EXPIRED,
Expand Down
30 changes: 27 additions & 3 deletions src/Resources/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace UnzerSDK\Resources;

use RuntimeException;
use stdClass;
use UnzerSDK\Adapter\HttpAdapterInterface;
use UnzerSDK\Constants\CancelReasonCodes;
use UnzerSDK\Constants\IdStrings;
Expand All @@ -16,15 +18,13 @@
use UnzerSDK\Resources\TransactionTypes\Charge;
use UnzerSDK\Resources\TransactionTypes\Chargeback;
use UnzerSDK\Resources\TransactionTypes\Payout;
use UnzerSDK\Resources\TransactionTypes\PreAuthorization;
use UnzerSDK\Resources\TransactionTypes\Shipment;
use UnzerSDK\Services\IdService;
use UnzerSDK\Traits\HasInvoiceId;
use UnzerSDK\Traits\HasOrderId;
use UnzerSDK\Traits\HasPaymentState;
use UnzerSDK\Traits\HasTraceId;
use RuntimeException;
use stdClass;

use function is_string;

/**
Expand Down Expand Up @@ -903,6 +903,9 @@ private function updateResponseTransactions(array $transactions = []): void
case TransactionTypes::AUTHORIZATION:
$this->updateAuthorizationTransaction($transaction);
break;
case TransactionTypes::PREAUTHORIZATION:
$this->updatePreAuthorizationTransaction($transaction);
break;
case TransactionTypes::CHARGE:
$this->updateChargeTransaction($transaction);
break;
Expand Down Expand Up @@ -994,6 +997,27 @@ private function updateAuthorizationTransaction(stdClass $transaction): void
$authorization->handleResponse($transaction);
}

/**
* This updates the local Authorization object referenced by this Payment with the given Authorization transaction
* from the Payment response.
*
* @param stdClass $transaction The transaction from the Payment response containing the Authorization data.
*
* @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.
*/
private function updatePreAuthorizationTransaction(stdClass $transaction): void
{
$transactionId = IdService::getResourceIdFromUrl($transaction->url, IdStrings::PREAUTHORIZE);
$authorization = $this->getAuthorization(true);
if (!$authorization instanceof Authorization) {
$authorization = (new PreAuthorization())->setPayment($this)->setId($transactionId);
$this->setAuthorization($authorization);
}

$authorization->handleResponse($transaction);
}

/**
* This updates the local Charge object referenced by this Payment with the given Charge transaction from the
* Payment response.
Expand Down
22 changes: 22 additions & 0 deletions src/Resources/TransactionTypes/PreAuthorization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace UnzerSDK\Resources\TransactionTypes;

use UnzerSDK\Adapter\HttpAdapterInterface;

/**
* This represents the authorization transaction.
Ryouzanpaku marked this conversation as resolved.
Show resolved Hide resolved
*
* @link https://docs.unzer.com/
*
*/
class PreAuthorization extends Authorization
{
/**
* {@inheritDoc}
*/
protected function getResourcePath(string $httpMethod = HttpAdapterInterface::REQUEST_GET): string
{
return 'preauthorize';
}
}
42 changes: 37 additions & 5 deletions src/Services/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
namespace UnzerSDK\Services;

use DateTime;
use RuntimeException;
use UnzerSDK\Constants\TransactionTypes;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\EmbeddedResources\Paylater\InstallmentPlansQuery;
use UnzerSDK\Resources\PaylaterInstallmentPlans;
use UnzerSDK\Resources\PaymentTypes\PaylaterInstallment;
use UnzerSDK\Unzer;
use UnzerSDK\Interfaces\PaymentServiceInterface;
use UnzerSDK\Resources\AbstractUnzerResource;
use UnzerSDK\Resources\Basket;
use UnzerSDK\Resources\Customer;
use UnzerSDK\Resources\EmbeddedResources\Paylater\InstallmentPlansQuery;
use UnzerSDK\Resources\InstalmentPlans;
use UnzerSDK\Resources\Metadata;
use UnzerSDK\Resources\PaylaterInstallmentPlans;
use UnzerSDK\Resources\Payment;
use UnzerSDK\Resources\PaymentTypes\BasePaymentType;
use UnzerSDK\Resources\PaymentTypes\InstallmentSecured;
use UnzerSDK\Resources\PaymentTypes\PaylaterInstallment;
use UnzerSDK\Resources\PaymentTypes\Paypage;
use UnzerSDK\Resources\TransactionTypes\Authorization;
use UnzerSDK\Resources\TransactionTypes\Charge;
use UnzerSDK\Resources\TransactionTypes\Payout;
use UnzerSDK\Resources\TransactionTypes\Shipment;
use RuntimeException;
use UnzerSDK\Unzer;

/**
* This service provides for functionalities concerning payment transactions.
Expand Down Expand Up @@ -104,6 +104,38 @@ public function updateAuthorization($payment, Authorization $authorization): Aut
return $authorization;
}

public function performPreAuthorization(
Authorization $authorization,
$paymentType,
$customer = null,
Metadata $metadata = null,
Basket $basket = null
): Authorization
{
$payment = $this->createPayment($paymentType);
Ryouzanpaku marked this conversation as resolved.
Show resolved Hide resolved
$paymentType = $payment->getPaymentType();
$authorization->setSpecialParams($paymentType !== null ? $paymentType->getTransactionParams() : []);

$payment->setAuthorization($authorization)->setCustomer($customer)->setMetadata($metadata)->setBasket($basket);

$this->getResourceService()->createResource($authorization);
return $authorization;
}

/**
* {@inheritDoc}
*
* @param Authorization $payment
*/
public function updatePreAuthorization($payment, Authorization $authorization): Authorization
{
$authorization->setId(null);
$paymentResource = $this->getResourceService()->getPaymentResource($payment);
$authorization->setPayment($paymentResource);
$this->getResourceService()->patchResource($authorization);
return $authorization;
}

/**
* {@inheritDoc}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Services/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public function fetchResourceByUrl($url)
case $resourceType === IdStrings::AUTHORIZE:
$resource = $unzer->fetchAuthorization(IdService::getResourceIdFromUrl($url, IdStrings::PAYMENT));
break;
case $resourceType === IdStrings::PREAUTHORIZE:
$resource = $unzer->fetchAuthorization(IdService::getResourceIdFromUrl($url, IdStrings::PAYMENT));
break;
case $resourceType === IdStrings::CHARGE:
$resource = $unzer->fetchChargeById(
IdService::getResourceIdFromUrl($url, IdStrings::PAYMENT),
Expand Down
19 changes: 19 additions & 0 deletions src/Unzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,25 @@ public function updateAuthorization($payment, Authorization $authorization): Aut
return $this->paymentService->updateAuthorization($payment, $authorization);
}

/**
* {@inheritDoc}
*/
public function performPreAuthorization(
Authorization $authorization,
$paymentType,
$customer = null,
Metadata $metadata = null,
Basket $basket = null
): Authorization
{
return $this->paymentService->performAuthorization($authorization, $paymentType, $customer, $metadata, $basket);
}

public function updatePreAuthorization($payment, Authorization $authorization): Authorization
{
return $this->paymentService->updateAuthorization($payment, $authorization);
}

/**
* {@inheritDoc}
*/
Expand Down
Loading
Loading