Skip to content

Commit

Permalink
GenericOnSite (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly authored Jan 8, 2024
1 parent 22abed7 commit 692db32
Show file tree
Hide file tree
Showing 26 changed files with 832 additions and 199 deletions.
3 changes: 2 additions & 1 deletion Api/TpayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
interface TpayInterface
{
public const CODE = 'tpaycom_magento2basic';
public const CHANNEL = 'group';
public const GROUP = 'group';
public const CHANNEL = 'channel';
public const BLIK_CODE = 'blik_code';
public const TERMS_ACCEPT = 'accept_tos';
public const CARDDATA = 'card_data';
Expand Down
33 changes: 27 additions & 6 deletions Controller/tpay/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\CacheInterface;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;
Expand All @@ -25,23 +26,38 @@ class Create extends Action
/** @var TransactionApiFacade */
private $transaction;

public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession)
{
/** @var CacheInterface */
private $cache;

/**
* {@inheritdoc}
* @param TpayInterface $tpayModel
* @param TpayService $tpayService
*/
public function __construct(
Context $context,
TpayInterface $tpayModel,
TpayService $tpayService,
Session $checkoutSession,
CacheInterface $cache
) {
$this->tpay = $tpayModel;
$this->tpayService = $tpayService;
$this->checkoutSession = $checkoutSession;
$this->cache = $cache;
Util::$loggingEnabled = false;

parent::__construct($context);
}

/** {@inheritdoc} */
public function execute()
{
$orderId = $this->checkoutSession->getLastRealOrderId();
if ($orderId) {
$payment = $this->tpayService->getPayment($orderId);
$paymentData = $payment->getData();
$this->transaction = new TransactionApiFacade($this->tpay);
$this->transaction = new TransactionApiFacade($this->tpay, $this->cache);
$additionalPaymentInformation = $paymentData['additional_information'];

$transaction = $this->prepareTransaction($orderId, $additionalPaymentInformation);
Expand All @@ -63,7 +79,7 @@ public function execute()
$paymentData['additional_information']['transaction_url'] = $transactionUrl;
$payment->setData($paymentData)->save();

if (6 === strlen($additionalPaymentInformation['blik_code']) && $this->tpay->checkBlikLevel0Settings()) {
if (6 === strlen($additionalPaymentInformation['blik_code'] ?? '') && $this->tpay->checkBlikLevel0Settings()) {
if (true === $this->transaction->isOpenApiUse()) {
return $this->_redirect('magento2basic/tpay/success');
}
Expand Down Expand Up @@ -103,17 +119,22 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio
{
$data = $this->tpay->getTpayFormData($orderId);

if (6 === strlen($additionalPaymentInformation['blik_code'])) {
if (6 === strlen($additionalPaymentInformation['blik_code'] ?? '')) {
$data['group'] = TransactionOriginApi::BLIK_CHANNEL;
$this->handleBlikData($data, $additionalPaymentInformation['blik_code']);
} else {
$data['group'] = (int) $additionalPaymentInformation['group'];
$data['group'] = (int) ($additionalPaymentInformation['group'] ?? null);
$data['channel'] = (int) ($additionalPaymentInformation['channel'] ?? null);

if ($this->tpay->redirectToChannel()) {
$data['direct'] = 1;
}
}

if ($data['channel']) {
return $this->transaction->createWithInstantRedirection($data);
}

return $this->transaction->create($data);
}

Expand Down
6 changes: 5 additions & 1 deletion Controller/tpay/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public function execute()
{
$uid = $this->getRequest()->getParam('uid');
$orderId = $this->checkoutSession->getLastRealOrderId();

if (!$orderId || !$uid) {
return $this->_redirect('checkout/cart');
}

$payment = $this->tpayService->getPayment($orderId);
$paymentData = $payment->getData();
$additionalPaymentInfo = $paymentData['additional_information'];
Expand All @@ -41,7 +43,9 @@ public function execute()
return $this->_redirect('magento2basic/tpay/CardPayment');
}

if ((!array_key_exists(TpayInterface::CHANNEL, $additionalPaymentInfo) || (int) $additionalPaymentInfo[TpayInterface::CHANNEL] < 1) && (!array_key_exists(TpayInterface::BLIK_CODE, $additionalPaymentInfo) || 6 !== strlen($additionalPaymentInfo[TpayInterface::BLIK_CODE]))) {


if (empty(array_intersect(array_keys($additionalPaymentInfo), [TpayInterface::GROUP, TpayInterface::CHANNEL])) && (!array_key_exists(TpayInterface::BLIK_CODE, $additionalPaymentInfo) || 6 !== strlen($additionalPaymentInfo[TpayInterface::BLIK_CODE]))) {
return $this->_redirect('checkout/cart');
}
$this->tpayService->setOrderStatePendingPayment($orderId, true);
Expand Down
2 changes: 2 additions & 0 deletions Model/ApiFacade/CardTransaction/CardOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ private function processSavedCardPayment(string $orderId, int $cardId): string
return 'magento2basic/tpay/success';
}

$paymentResult = $result['payments'] ?? [];

if (isset($paymentResult['status']) && 'declined' === $paymentResult['status']) {
$this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, Elavon rejection code: '.$paymentResult['reason']);
} else {
Expand Down
38 changes: 27 additions & 11 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ class OpenApi extends TpayApi
public function create(array $data): array
{
$transactionData = $this->handleDataStructure($data);
$transaction = $this->Transactions->createTransaction($transactionData);
$transaction = $this->transactions()->createTransaction($transactionData);

return $this->updateRedirectUrl($transaction);
}

public function createWithInstantRedirect(array $data)
{
$transactionData = $this->handleDataStructure($data);
$transaction = $this->transactions()->createTransactionWithInstantRedirection($transactionData);

return $this->updateRedirectUrl($transaction);
}
Expand All @@ -20,6 +28,11 @@ public function makeRefund(InfoInterface $payment, string $amount): array
return $this->Transactions->createRefundByTransactionId(['amount' => number_format($amount, 2)], $payment->getAdditionalInformation('transaction_id'));
}

public function channels(): array
{
return $this->transactions()->getChannels();
}

private function handleDataStructure(array $data): array
{
$paymentData = [
Expand All @@ -35,17 +48,12 @@ private function handleDataStructure(array $data): array
'city' => $data['city'],
'country' => $data['country'],
],
'pay' => [
'groupId' => $data['group'],
],
'callbacks' => [
'payerUrls' => [
'success' => $data['return_url'],
'error' => $data['return_error_url'],
],
'notification' => [
'url' => $data['result_url'],
"callbacks" => [
"payerUrls" => [
"success" => $data['return_url'],
"error" => $data['return_error_url']
],
'notification' => ['url' => $data['result_url']],
],
];

Expand All @@ -55,6 +63,14 @@ private function handleDataStructure(array $data): array
];
}

if ($data['group']) {
$paymentData['pay'] = ['groupId' => $data['group']];
}

if ($data['channel']) {
$paymentData['pay'] = ['channelId' => $data['channel']];
}

return $paymentData;
}

Expand Down
4 changes: 4 additions & 0 deletions Model/ApiFacade/TpayConfig/ConfigFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ConfigFacade

/** @var bool */
private $useOpenApi;
/**
* @var TpayInterface
*/
private $tpay;

public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService)
{
Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/TpayConfig/ConfigOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getConfig(): array
];
$config = array_merge($config, $this->getCardConfig());

return $this->tpay->isAvailable() ? $config : [];
return $config;
}

public function generateURL(string $name): string
Expand Down
43 changes: 41 additions & 2 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
namespace tpaycom\magento2basic\Model\ApiFacade\Transaction;

use Exception;
use Magento\Framework\App\CacheInterface;
use Tpay\OpenApi\Utilities\TpayException;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\OpenApi;

class TransactionApiFacade
{
private const CHANNELS_CACHE_KEY = 'tpay_channels';
private const CACHE_LIFETIME = 86400;

/** @var TransactionOriginApi */
private $originApi;

Expand All @@ -17,10 +22,14 @@ class TransactionApiFacade
/** @var bool */
private $useOpenApi;

public function __construct(TpayInterface $tpay)
/** @var CacheInterface */
private $cache;

public function __construct(TpayInterface $tpay, CacheInterface $cache)
{
$this->originApi = new TransactionOriginApi($tpay->getApiPassword(), $tpay->getApiKey(), $tpay->getMerchantId(), $tpay->getSecurityCode(), !$tpay->useSandboxMode());
$this->createOpenApiInstance($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode());
$this->createOpenApiInstance($tpay->getClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode());
$this->cache = $cache;
}

public function isOpenApiUse()
Expand All @@ -33,11 +42,41 @@ public function create(array $config)
return $this->getCurrentApi()->create($config);
}

public function createWithInstantRedirection(array $config)
{
if (!$this->useOpenApi) {
throw new TpayException('OpenAPI not availabile - Failed to create transaction with instant redirection');
}

return $this->openApi->createWithInstantRedirect($config);
}

public function blik($blikTransactionId, $blikCode)
{
return $this->originApi->blik($blikTransactionId, $blikCode);
}

public function channels(): array
{
$channels = $this->cache->load(self::CHANNELS_CACHE_KEY);

if ($channels) {
return json_decode($channels, true);
}

if (false === $this->useOpenApi) {
return [];
}

$channels = array_filter($this->openApi->channels()['channels'], function (array $channel) {
return $channel['available'] === true && empty($channel['constraints']) === true;
});

$this->cache->save(json_encode($channels), self::CHANNELS_CACHE_KEY, []);

return $channels;
}

private function getCurrentApi()
{
return $this->useOpenApi ? $this->openApi : $this->originApi;
Expand Down
43 changes: 43 additions & 0 deletions Model/Config/Source/OnsiteChannels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace tpaycom\magento2basic\Model\Config\Source;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\Data\OptionSourceInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;

class OnsiteChannels implements OptionSourceInterface
{

/** @var TransactionApiFacade */
private $transactions;

public function __construct(TpayInterface $tpay, CacheInterface $cache)
{
$this->transactions = new TransactionApiFacade($tpay, $cache);
}

public function getLabelFromValue(int $value): ?string
{
foreach ($this->toOptionArray() as $option) {
if ($option['value'] === $value) {
return $option['label'];
}
}

return null;
}

/**
* @inheritDoc
*
* @return array{array{value: int, label: string}}
*/
public function toOptionArray(): array
{
return array_map(function (array $channel) {
return ['value' => (int) $channel['id'], 'label' => $channel['fullName']];
}, $this->transactions->channels());
}
}
Loading

0 comments on commit 692db32

Please sign in to comment.