Skip to content

Commit

Permalink
Card update
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jan 10, 2024
1 parent 951ea92 commit acb1763
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 17 deletions.
9 changes: 7 additions & 2 deletions Controller/tpay/CardPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Model\Context as ModelContext;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\CardTransaction\CardApiFacade;
Expand All @@ -28,12 +29,16 @@ class CardPayment extends Action
/** @var TpayTokensService */
private $tokensService;

public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession, ModelContext $modelContext, Registry $registry, ResourceConnection $resourceConnection)
/** @var StoreManagerInterface */
private $storeManager;

public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession, ModelContext $modelContext, Registry $registry, ResourceConnection $resourceConnection, StoreManagerInterface $storeManager)
{
$this->tpay = $tpayModel;
$this->tpayService = $tpayService;
$this->checkoutSession = $checkoutSession;
$this->tokensService = new TpayTokensService($modelContext, $registry, $resourceConnection);
$this->storeManager = $storeManager;
Util::$loggingEnabled = false;
parent::__construct($context);
}
Expand All @@ -44,7 +49,7 @@ public function execute()
$orderId = $this->checkoutSession->getLastRealOrderId();

if ($orderId) {
$cardTransaction = new CardApiFacade($this->tpay, $this->tokensService, $this->tpayService);
$cardTransaction = new CardApiFacade($this->tpay, $this->tokensService, $this->tpayService, $this->storeManager);
$redirectUrl = $cardTransaction->makeCardTransaction($orderId);

return $this->_redirect($redirectUrl);
Expand Down
14 changes: 13 additions & 1 deletion Model/ApiFacade/CardTransaction/CardApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace tpaycom\magento2basic\Model\ApiFacade\CardTransaction;

use Exception;
use Magento\Store\Model\StoreManagerInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Service\TpayService;
use tpaycom\magento2basic\Service\TpayTokensService;
Expand All @@ -15,11 +16,15 @@ class CardApiFacade
/** @var CardOpen */
private $cardOpen;

/** @var StoreManagerInterface */
private $storeManager;

/** @var bool */
private $useOpenCard;

public function __construct(TpayInterface $tpay, TpayTokensService $tokensService, TpayService $tpayService)
public function __construct(TpayInterface $tpay, TpayTokensService $tokensService, TpayService $tpayService, StoreManagerInterface $storeManager)
{
$this->storeManager = $storeManager;
$this->cardOrigin = new CardOrigin($tpay, $tokensService, $tpayService);
$this->createOpenApiInstance($tpay, $tokensService, $tpayService);
}
Expand All @@ -36,6 +41,13 @@ private function getCurrent()

private function createOpenApiInstance(TpayInterface $tpay, TpayTokensService $tokensService, TpayService $tpayService)
{
if ($this->storeManager->getStore()->getCurrentCurrencyCode() !== 'PLN') {
$this->cardOpen = null;
$this->useOpenCard = false;

return;
}

try {
$this->cardOpen = new CardOpen($tpay, $tokensService, $tpayService);
$this->useOpenCard = true;
Expand Down
14 changes: 11 additions & 3 deletions Model/ApiFacade/TpayConfig/ConfigFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Magento\Framework\View\Asset\Repository;
use Magento\Store\Model\StoreManagerInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Service\TpayTokensService;

Expand All @@ -18,10 +19,10 @@ class ConfigFacade
/** @var bool */
private $useOpenApi;

public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService)
public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager)
{
$this->originApi = new ConfigOrigin($tpay, $assetRepository, $tokensService);
$this->createOpenApiInstance($tpay, $assetRepository, $tokensService);
$this->createOpenApiInstance($tpay, $assetRepository, $tokensService, $storeManager);
}

public function getConfig(): array
Expand All @@ -34,8 +35,15 @@ private function getCurrentApi()
return $this->useOpenApi ? $this->openApi : $this->originApi;
}

private function createOpenApiInstance(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService)
private function createOpenApiInstance(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager)
{
if ($storeManager->getStore()->getCurrentCurrencyCode() !== 'PLN') {
$this->openApi = null;
$this->useOpenApi = false;

return;
}

try {
$this->openApi = new ConfigOpen($tpay, $assetRepository, $tokensService);
$this->useOpenApi = true;
Expand Down
3 changes: 3 additions & 0 deletions Model/GenericPaymentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function beforeImportData(Payment $compiled, array $data): array
$data['channel'] = explode('-', $data['method'])[1];
$data['method'] = TpayInterface::CODE;
}
if ($data['method'] == 'tpaycom_magento2basic_cards') {
$data['method'] = TpayInterface::CODE;
}

return [$data];
}
Expand Down
19 changes: 18 additions & 1 deletion Model/MethodListPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Payment\Model\MethodInterface;
use Magento\Payment\Model\MethodList;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\Config\Source\OnsiteChannels;

Expand All @@ -23,11 +24,15 @@ class MethodListPlugin
/** @var OnsiteChannels */
private $onsiteChannels;

public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels)
/** @var StoreManagerInterface */
private $storeManager;

public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels, StoreManagerInterface $storeManager)
{
$this->data = $data;
$this->scopeConfig = $scopeConfig;
$this->onsiteChannels = $onsiteChannels;
$this->storeManager = $storeManager;
}

public function afterGetAvailableMethods(MethodList $compiled, $result)
Expand All @@ -36,6 +41,7 @@ public function afterGetAvailableMethods(MethodList $compiled, $result)
$channels = $onsiteChannels ? explode(',', $onsiteChannels) : [];

$result[] = $this->getMethodInstance('tpay.com - Płatność kartą', 'tpaycom_magento2basic_cards');
$result = $this->filterResult($result);

foreach ($channels as $onsiteChannel) {
$result[] = $this->getMethodInstance(
Expand All @@ -55,4 +61,15 @@ public function getMethodInstance(string $title, string $code): MethodInterface

return $method;
}

private function filterResult(array $result): array
{
if ($this->storeManager->getStore()->getCurrentCurrencyCode() === 'PLN') {
return $result;
}

return array_filter($result, function ($method) {
return $method->getCode() !== 'tpaycom_magento2basic';
});
}
}
8 changes: 1 addition & 7 deletions Model/Tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function isAvailable(?CartInterface $quote = null)
return false;
}

if (!$this->getMerchantId() || ($quote && !$this->isAvailableForCurrency($quote->getCurrency()->getQuoteCurrencyCode()))) {
if (!$this->getMerchantId()) {
return false;
}

Expand Down Expand Up @@ -460,12 +460,6 @@ protected function getOrder(?string $orderId = null): \Magento\Sales\Api\Data\Or
return $this->orderRepository->getByIncrementId($orderId);
}

/** Availability for currency */
protected function isAvailableForCurrency(string $currencyCode): bool
{
return !(!in_array($currencyCode, $this->availableCurrencyCodes));
}

private function getMagentoVersion()
{
$objectManager = ObjectManager::getInstance();
Expand Down
4 changes: 3 additions & 1 deletion Model/TpayConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Store\Model\StoreManagerInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\TpayConfig\ConfigFacade;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;
Expand All @@ -29,12 +30,13 @@ class TpayConfigProvider implements ConfigProviderInterface
public function __construct(
PaymentHelper $paymentHelper,
Repository $assetRepository,
StoreManagerInterface $storeManager,
TpayTokensService $tokensService,
TransactionApiFacade $transactionApiFacade
) {
$this->paymentHelper = $paymentHelper;
$this->transactionApi = $transactionApiFacade;
$this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $assetRepository, $tokensService);
$this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $assetRepository, $tokensService, $storeManager);
}

public function getConfig(): array
Expand Down
3 changes: 1 addition & 2 deletions Service/TpayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,9 @@ private function registerCaptureNotificationTpay(OrderPaymentInterface $payment,
$order = $payment->getOrder();
$amount = (float) $amount;
$invoice = $this->getInvoiceForTransactionId($order, $payment->getTransactionId());
$orderCurrency = $order->getOrderCurrency()->getCode();
// register new capture

if (!$invoice && 'PLN' === $orderCurrency && $payment->isCaptureFinal($amount)) {
if (!$invoice && $payment->isCaptureFinal($amount)) {
$invoice = $order->prepareInvoice()->register();
$invoice->setOrder($order);
$order->addRelatedObject($invoice);
Expand Down

0 comments on commit acb1763

Please sign in to comment.