Skip to content

Commit

Permalink
GenericOnSite
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Jan 5, 2024
1 parent 22abed7 commit 47a5873
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 12 deletions.
27 changes: 24 additions & 3 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 Down Expand Up @@ -108,12 +124,17 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio
$this->handleBlikData($data, $additionalPaymentInformation['blik_code']);
} else {
$data['group'] = (int) $additionalPaymentInformation['group'];
$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
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
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());
}
}
133 changes: 133 additions & 0 deletions Model/GenericOnSiteConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace tpaycom\magento2basic\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Payment\Model\MethodInterface;
use Magento\Payment\Model\MethodList;
use Magento\Store\Model\ScopeInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;

#[\AllowDynamicProperties]
class GenericOnSiteConfigProvider implements ConfigProviderInterface
{
protected $paymentMethod;

public function __construct(
PaymentHelper $paymentHelper,
Repository $assetRepository,
MethodList $methods,
ScopeConfigInterface $scopeConfig,
TransactionApiFacade $transactionApiFacade
) {
$this->paymentHelper = $paymentHelper;
$this->assetRepository = $assetRepository;
$this->methodList = $methods;
$this->scopeConfig = $scopeConfig;
$this->transactionApiFacade = $transactionApiFacade;
}

/**
* @inheritDoc
*/
public function getConfig()
{
$tpay = $this->getPaymentMethodInstance();
$onsites = explode(',', $this->scopeConfig->getValue('payment/tpaycom_magento2basic/onsite_channels', ScopeInterface::SCOPE_STORE));

$config = [
'tpay' => [
'payment' => [
'redirectUrl' => $tpay->getPaymentRedirectUrl(),
'tpayLogoUrl' => $this->generateURL('tpaycom_magento2basic::images/logo_tpay.png'),
'merchantId' => $tpay->getMerchantId(),
'showPaymentChannels' => $this->showChannels(),
'getTerms' => $this->getTerms(),
'addCSS' => $this->createCSS('tpaycom_magento2basic::css/tpay.css'),
'blikStatus' => $this->getPaymentMethodInstance()->checkBlikLevel0Settings(),
'onlyOnlineChannels' => $this->getPaymentMethodInstance()->onlyOnlineChannels(),
'getBlikChannelID' => TransactionModel::BLIK_CHANNEL,
'isInstallmentsAmountValid' => $this->getPaymentMethodInstance()->getInstallmentsAmountValid(),
],
],
];

$channels = $this->transactionApiFacade->channels();

foreach ($channels as $channel) {
$config['generic'][$channel['id']] = [
'id' => $channel['id'],
'name' => $channel['fullName'],
'logoUrl' => $channel['image']['url'],
];
}


return $config;
}

/**
* @param string $name
*
* @return string
*/
public function generateURL($name)
{
return $this->assetRepository->createAsset($name)->getUrl();
}

/** @return null|string */
public function showChannels()
{
$script = 'tpaycom_magento2basic::js/render_channels.js';

return $this->createScript($script);
}

/**
* @param string $script
*
* @return string
*/
public function createScript($script)
{
return "
<script type=\"text/javascript\">
require(['jquery'], function ($) {
$.getScript('{$this->generateURL($script)}');
});
</script>";
}

/** @return null|string */
public function getTerms()
{
return $this->getPaymentMethodInstance()->getTermsURL();
}

/**
* @param string $css
*
* @return string
*/
public function createCSS($css)
{
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->generateURL($css)}\">";
}

/** @return MethodInterface|TpayInterface */
protected function getPaymentMethodInstance()
{
if (null === $this->paymentMethod) {
$this->paymentMethod = $this->paymentHelper->getMethodInstance(TpayInterface::CODE);
}

return $this->paymentMethod;
}
}
Loading

0 comments on commit 47a5873

Please sign in to comment.