Skip to content

Commit

Permalink
Instant redirection payment
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Apr 12, 2024
1 parent 02bd4ef commit 3b8f738
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 75 deletions.
1 change: 1 addition & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Config
public const TPAY_PAYMENT_BLIK = 'blik';
public const TPAY_PAYMENT_GPAY = 'gpay';
public const TPAY_PAYMENT_INSTALLMENTS = 'installments';
public const TPAY_PAYMENT_GENERIC = 'generic';

public const TPAY_GATEWAY_ALIOR_RATY = 109;
public const TPAY_GATEWAY_PEKAO_RATY = 169;
Expand Down
4 changes: 4 additions & 0 deletions src/Exception/TransactionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@

class TransactionException extends \Exception
{
public static function unableToCreateTransaction(array $data): self
{
return new self(sprintf('Unable to create transaction. Response: %s', json_encode($data)));
}
}
3 changes: 3 additions & 0 deletions src/Factory/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Tpay\Exception\PaymentException;
use Tpay\Handler\BasicPaymentHandler;
use Tpay\Handler\CreditCardPaymentHandler;
use Tpay\Handler\InstantPaymentHandler;
use Tpay\Handler\PaymentMethodHandler;

class PaymentFactory
Expand All @@ -40,6 +41,8 @@ public static function getPaymentMethod(string $type)
case Config::TPAY_PAYMENT_GPAY:
case Config::TPAY_PAYMENT_BLIK:
return new BasicPaymentHandler();
case Config::TPAY_PAYMENT_GENERIC:
return new InstantPaymenthandler();
case Config::TPAY_PAYMENT_CARDS:
return new CreditCardPaymentHandler();
default:
Expand Down
83 changes: 29 additions & 54 deletions src/Handler/BasicPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,12 @@
class BasicPaymentHandler implements PaymentMethodHandler
{
public const TYPE = 'transfer';
private $clientData;

/**
* @var \Tpay
*/
private $module;
/**
* @var \Order
*/
private $order;
/**
* @var \Customer
*/
private $customer;
/**
* @var \Context
*/
private $context;
/** @var array */
protected $clientData;

/** @var \Tpay */
protected $module;

public function getName(): string
{
Expand All @@ -53,18 +41,14 @@ public function getName(): string
* @throws \Exception
*/
public function createPayment(
\Tpay $module,
\Order $order,
\Tpay $module,
\Order $order,
\Customer $customer,
\Context $context,
array $clientData,
array $data
)
{
\Context $context,
array $clientData,
array $data
): void {
$this->module = $module;
$this->order = $order;
$this->customer = $customer;
$this->context = $context;
$this->clientData = $clientData;

$this->updatePayData($data);
Expand All @@ -80,43 +64,34 @@ public function createPayment(
}

/**
* Process of saving the transaction
*
* @param $transaction
* @param $orderId
* @param bool $redirect
*
* @param array|string $transaction
* @throws \Exception
*/
public function initTransactionProcess($transaction, $orderId, bool $redirect = true): void
public function initTransactionProcess($transaction, int $orderId, bool $redirect = true): void
{
if (isset($transaction['transactionId'])) {
/** @var TransactionService $transactionService */
$transactionService = $this->module->getService('tpay.service.transaction');
$transactionService->transactionProcess(
$transaction,
self::TYPE,
(int)$orderId,
$redirect
);
if (!isset($transaction['transactionId'])) {
return;
}

/** @var TransactionService $transactionService */
$transactionService = $this->module->getService('tpay.service.transaction');
$transactionService->transactionProcess(
$transaction,
self::TYPE,
$orderId,
$redirect
);
}

/**
* Create api transaction
*
* @throws TransactionException
*/
private function createTransaction()
protected function createTransaction(): array
{
$result = $this->module->api->Transactions->createTransaction(
$this->clientData
);
$result = $this->module->api()->transactions()->createTransaction($this->clientData);

if (!isset($result['transactionId'])) {
throw new TransactionException(
'Unable to create transaction. Response: ' . json_encode($result)
);
throw TransactionException::unableToCreateTransaction($result);
}

if (isset($this->clientData['pay']['channelId']) && $this->clientData['pay']['channelId']) {
Expand All @@ -126,7 +101,7 @@ private function createTransaction()
return $result;
}

private function updatePayData(array $data)
protected function updatePayData(array $data): void
{
if ($data['type'] == 'transfer' && !Helper::getMultistoreConfigurationValue('TPAY_TRANSFER_WIDGET')) {
unset($this->clientData['pay']);
Expand All @@ -135,7 +110,7 @@ private function updatePayData(array $data)
}
}

private function checkPayType(array $data)
protected function checkPayType(array $data): void
{
$gatewayId = $data['tpay_transfer_id'] ?? 0;
$channelId = $data['tpay_channel_id'] ?? 0;
Expand Down
4 changes: 1 addition & 3 deletions src/Handler/CreditCardPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ private function createTransaction()
);

if (!isset($result['transactionId'])) {
throw new TransactionException(
'Unable to create transaction. Response: ' . json_encode($result)
);
throw TransactionException::unableToCreateTransaction($result);
}

return $result;
Expand Down
49 changes: 49 additions & 0 deletions src/Handler/InstantPaymentHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tpay\Handler;

use Tpay\Exception\PaymentException;
use Tpay\Exception\TransactionException;

class InstantPaymentHandler extends BasicPaymentHandler
{
public function createPayment(
\Tpay $module,
\Order $order,
\Customer $customer,
\Context $context,
array $clientData,
array $data
): void {
$this->module = $module;
$this->clientData = $clientData;

$this->updatePayData($data);

$transaction = $this->createTransaction();

$this->initTransactionProcess($transaction, $this->module->currentOrder);


throw new PaymentException(
'Unable to create payment method. Response: ' . json_encode($transaction)
);
}

protected function createTransaction(): array
{
$result = $this->module->api()->transactions()->createTransactionWithInstantRedirection($this->clientData);

if (!isset($result['transactionId'])) {
throw TransactionException::unableToCreateTransaction($result);
}

if (isset($this->clientData['pay']['channelId']) && $this->clientData['pay']['channelId']) {
$result['transactionPaymentUrl'] = str_replace('gtitle', 'title', $result['transactionPaymentUrl']);
}

return $result;
}
}
27 changes: 10 additions & 17 deletions src/Handler/PaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class PaymentHandler
{
/**
* @var Tpay
* @var \Tpay
*/
private $module;
/**
Expand Down Expand Up @@ -61,7 +61,6 @@ public function __construct(
AddressCore $address,
array $data = []
) {

$this->module = $module;
$this->paymentMethodHandler = $paymentMethodHandler;
$this->order = $order;
Expand All @@ -73,11 +72,16 @@ public function __construct(

/**
* @throws \Exception
* @return array
*/
public function getCustomerDetails(): array
{
$customer = new CustomerData($this->address, $this->customer, $this->context, $this->context->cart, $this->order);
$customer = new CustomerData(
$this->address,
$this->customer,
$this->context,
$this->context->cart,
$this->order
);
$customer->createCallbacks($this->order, $this->paymentMethodHandler->getName());
$customer->createDescription($this->order);

Expand All @@ -87,20 +91,9 @@ public function getCustomerDetails(): array
/**
* @throws \Exception
*/
public function get()
{
$this->getPayment($this->paymentMethodHandler);
}

/**
* @param PaymentMethodHandler $method
*
* @throws \Exception
* @return mixed
*/
public function getPayment(PaymentMethodHandler $method)
public function get(): void
{
return $method->createPayment(
$this->paymentMethodHandler->createPayment(
$this->module,
$this->order,
$this->customer,
Expand Down
1 change: 1 addition & 0 deletions src/Service/PaymentOptions/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private function generateForm(string $moduleLink, $channelId): string
'tpay' => 'true',
'channelId' => $channelId,
'tpay_channel_id' => 0,
'type' => 'generic',
]);

return Context::getContext()->smarty->fetch('module:tpay/views/templates/hook/generic.tpl');
Expand Down
6 changes: 5 additions & 1 deletion src/Service/PaymentOptions/PaymentOptionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ protected function genericPayments(): array
}

return array_filter(array_map(function (string $generic) use ($channels) {
$channel = $channels[$generic];
$channel = $channels[$generic] ?? null;

if ($channel === null) {
return null;
}

if (!empty($channel['constraints']) && !$this->constraintValidator->validate($channel['constraints'])) {
return null;
Expand Down

0 comments on commit 3b8f738

Please sign in to comment.