Skip to content

Commit

Permalink
Fix base grand total (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed May 21, 2024
1 parent 4572bb0 commit 0217d19
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Api/TpayConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public function getMaxOrderTotal(): int;
public function getMagentoVersion(): string;

public function buildMagentoInfo(): string;

public function isPlnPayment(): bool;
}
9 changes: 2 additions & 7 deletions Model/ApiFacade/CardTransaction/CardApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tpay\Magento2\Model\ApiFacade\CardTransaction;

use Exception;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Service\TpayService;
Expand All @@ -29,19 +28,15 @@ class CardApiFacade
/** @var TpayService */
private $tpayService;

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

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

public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, TpayTokensService $tokensService, TpayService $tpayService, StoreManagerInterface $storeManager)
public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, TpayTokensService $tokensService, TpayService $tpayService)
{
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->tokensService = $tokensService;
$this->tpayService = $tpayService;
$this->storeManager = $storeManager;
}

public function makeCardTransaction(string $orderId, ?array $customerToken = null): string
Expand Down Expand Up @@ -95,7 +90,7 @@ private function createCardOriginApiInstance(TpayInterface $tpay, TpayConfigInte

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

Expand Down
13 changes: 4 additions & 9 deletions Model/ApiFacade/TpayConfig/CardConfigFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Magento\Framework\View\Asset\Repository;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Model\ApiFacade\CardTransaction\CardOrigin;
Expand All @@ -31,22 +30,18 @@ class CardConfigFacade
/** @var TpayTokensService */
private $tokensService;

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

/** @var TpayService */
private $tpayService;

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

public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager, TpayService $tpayService)
public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, TpayService $tpayService)
{
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->assetRepository = $assetRepository;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
$this->tpayService = $tpayService;
}

Expand All @@ -66,7 +61,7 @@ private function connectApi()
{
if (null == $this->openApi && null === $this->originApi) {
$this->createOriginApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->tpayService);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService);
}
}

Expand All @@ -86,9 +81,9 @@ private function createOriginApiInstance(TpayInterface $tpay, TpayConfigInterfac
}
}

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

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

use Exception;
use Magento\Framework\View\Asset\Repository;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionOriginApi;
Expand Down Expand Up @@ -34,22 +33,18 @@ class ConfigFacade
/** @var TpayTokensService */
private $tokensService;

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

/** @var TpayService */
private $tpayService;

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

public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager, TpayService $tpayService)
public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, TpayService $tpayService)
{
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->assetRepository = $assetRepository;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
$this->tpayService = $tpayService;
}

Expand All @@ -69,8 +64,8 @@ private function connectApi()
{
if (null == $this->openApi && null === $this->originConfig) {
$this->createOriginApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager);
$this->cardConfig = new CardConfigFacade($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager, $this->tpayService);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService);
$this->cardConfig = new CardConfigFacade($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->tpayService);
}
}

Expand All @@ -90,9 +85,9 @@ private function createOriginApiInstance(TpayInterface $tpay, TpayConfigInterfac
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions Model/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public function isClientCountryValid(bool $isAllowed, string $clientCountry, arr

private function validateMinimalTotal(float $minimal): bool
{
return $this->checkoutSession->getQuote()->getGrandTotal() >= $minimal;
return $this->checkoutSession->getQuote()->getBaseGrandTotal() >= $minimal;
}

private function validateMaximalTotal(float $maximal): bool
{
return $this->checkoutSession->getQuote()->getGrandTotal() <= $maximal;
return $this->checkoutSession->getQuote()->getBaseGrandTotal() <= $maximal;
}
}
17 changes: 3 additions & 14 deletions Model/MethodListPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Magento\Payment\Model\MethodInterface;
use Magento\Payment\Model\MethodList;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionApiFacade;
Expand All @@ -30,9 +29,6 @@ class MethodListPlugin
/** @var OnsiteChannels */
private $onsiteChannels;

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

/** @var TpayPayment */
private $tpay;

Expand All @@ -52,7 +48,6 @@ public function __construct(
Data $data,
ScopeConfigInterface $scopeConfig,
OnsiteChannels $onsiteChannels,
StoreManagerInterface $storeManager,
TpayPayment $tpay,
TpayConfigInterface $tpayConfig,
Session $checkoutSession,
Expand All @@ -63,7 +58,6 @@ public function __construct(
$this->data = $data;
$this->scopeConfig = $scopeConfig;
$this->onsiteChannels = $onsiteChannels;
$this->storeManager = $storeManager;
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->checkoutSession = $checkoutSession;
Expand All @@ -87,14 +81,14 @@ public function afterGetAvailableMethods(MethodList $compiled, $result)
return [];
}

if (!$this->tpay->isCartValid($this->checkoutSession->getQuote()->getGrandTotal())) {
if (!$this->tpay->isCartValid($this->checkoutSession->getQuote()->getBaseGrandTotal())) {
return $result;
}

$result = $this->addCardMethod($result);
$result = $this->filterResult($result);

if (!$this->transactions->isOpenApiUse() || !$this->isPlnPayment()) {
if (!$this->transactions->isOpenApiUse() || !$this->tpayConfig->isPlnPayment()) {
return $result;
}

Expand Down Expand Up @@ -139,18 +133,13 @@ private function filterResult(array $result): array
return $this->filterTransaction($result);
}

if ($this->isPlnPayment()) {
if ($this->tpayConfig->isPlnPayment()) {
return $result;
}

return $this->filterTransaction($result);
}

private function isPlnPayment(): bool
{
return 'PLN' === $this->storeManager->getStore()->getCurrentCurrencyCode();
}

private function filterTransaction(array $result): array
{
return array_filter($result, function ($method) {
Expand Down
4 changes: 1 addition & 3 deletions Model/TpayConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Model\ApiFacade\TpayConfig\ConfigFacade;
Expand All @@ -32,15 +31,14 @@ class TpayConfigProvider implements ConfigProviderInterface
public function __construct(
PaymentHelper $paymentHelper,
Repository $assetRepository,
StoreManagerInterface $storeManager,
TpayTokensService $tokensService,
TransactionApiFacade $transactionApiFacade,
TpayService $tpayService,
TpayConfigInterface $tpayConfig
) {
$this->paymentHelper = $paymentHelper;
$this->transactionApi = $transactionApiFacade;
$this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $tpayConfig, $assetRepository, $tokensService, $storeManager, $tpayService);
$this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $tpayConfig, $assetRepository, $tokensService, $tpayService);
}

public function getConfig(): array
Expand Down
6 changes: 3 additions & 3 deletions Model/TpayPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function getTpayFormData(?string $orderId = null): array
{
$order = $this->getOrder($orderId);
$billingAddress = $order->getBillingAddress();
$amount = number_format((float) $order->getGrandTotal(), 2, '.', '');
$amount = number_format((float) $order->getBaseGrandTotal(), 2, '.', '');
$crc = base64_encode($orderId);
$name = $billingAddress->getData('firstname').' '.$billingAddress->getData('lastname');
$phone = $billingAddress->getData('telephone');
Expand All @@ -210,7 +210,7 @@ public function getTpayFormData(?string $orderId = null): array
'phone' => $phone,
'online' => $this->configurationProvider->onlyOnlineChannels() ? 1 : 0,
'module' => 'Magento '.$this->configurationProvider->getMagentoVersion(),
'currency' => $this->getISOCurrencyCode($order->getOrderCurrencyCode()),
'currency' => $this->getISOCurrencyCode($order->getBaseCurrencyCode()),
'language' => $language,
];
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public function getCheckoutTotal()
if (!$amount) {
$orderId = $this->getCheckout()->getLastRealOrderId();
$order = $this->orderRepository->getByIncrementId($orderId);
$amount = $order->getGrandTotal();
$amount = $order->getBaseGrandTotal();
}

return $amount;
Expand Down
16 changes: 15 additions & 1 deletion Provider/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;

class ConfigurationProvider implements TpayConfigInterface
Expand All @@ -21,10 +22,14 @@ class ConfigurationProvider implements TpayConfigInterface
/** @var ProductMetadataInterface */
protected $productMetadataInterface;

public function __construct(ScopeConfigInterface $scopeConfig, ProductMetadataInterface $productMetadataInterface)
/** @var StoreManagerInterface */
private $storeManager;

public function __construct(ScopeConfigInterface $scopeConfig, ProductMetadataInterface $productMetadataInterface, StoreManagerInterface $storeManager)
{
$this->scopeConfig = $scopeConfig;
$this->productMetadataInterface = $productMetadataInterface;
$this->storeManager = $storeManager;
}

public function isTpayActive(): bool
Expand Down Expand Up @@ -214,6 +219,15 @@ public function getConfigData($field, $storeId = null)
return $this->scopeConfig->getValue(self::BASE_CONFIG_PATH.$field, ScopeInterface::SCOPE_STORE, $storeId);
}

public function isPlnPayment(): bool
{
if ($this->getConfigData('sale_settings/bank_payments_view')) {
return 'PLN' == $this->storeManager->getStore()->getBaseCurrencyCode();
}

return 'PLN' == $this->storeManager->getStore()->getBaseCurrencyCode() && 'PLN' == $this->storeManager->getStore()->getCurrentCurrencyCode();
}

private function getPackagesVersions(): array
{
$dir = __DIR__.'/../composer.json';
Expand Down
10 changes: 5 additions & 5 deletions Service/TpayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function setOrderStatePendingPayment(string $orderId, bool $sendEmail): O
{
$order = $this->orderRepository->getByIncrementId($orderId);
$order
->setTotalDue($order->getGrandTotal())
->setTotalDue($order->getBaseGrandTotal())
->setTotalPaid(0.00)
->setBaseTotalPaid(0.00)
->setBaseTotalDue($order->getBaseGrandTotal())
Expand Down Expand Up @@ -105,7 +105,7 @@ public function setOrderStatus(string $orderId, array $validParams, TpayConfigIn
}

$sendNewInvoiceMail = (bool) $tpayConfig->getInvoiceSendMail();
$orderAmount = (float) number_format((float) $order->getGrandTotal(), 2, '.', '');
$orderAmount = (float) number_format((float) $order->getBaseGrandTotal(), 2, '.', '');
$trStatus = $validParams['tr_status'];
$emailNotify = false;

Expand All @@ -114,7 +114,7 @@ public function setOrderStatus(string $orderId, array $validParams, TpayConfigIn
$emailNotify = true;
}
$status = Order::STATE_PROCESSING;
$this->registerCaptureNotificationTpay($order->getPayment(), $order->getGrandTotal(), $validParams);
$this->registerCaptureNotificationTpay($order->getPayment(), $order->getBaseGrandTotal(), $validParams);
} elseif ('CHARGEBACK' === $trStatus) {
$order->addCommentToStatusHistory($this->getTransactionDesc($validParams));
$this->orderRepository->save($order);
Expand Down Expand Up @@ -161,7 +161,7 @@ public function setCardOrderStatus($orderId, array $validParams, TpayConfigInter

$sendNewInvoiceMail = (bool) $tpayConfig->getInvoiceSendMail();
$transactionDesc = $this->getCardTransactionDesc($validParams);
$orderAmount = (float) number_format((float) $order->getGrandTotal(), 2, '.', '');
$orderAmount = (float) number_format((float) $order->getBaseGrandTotal(), 2, '.', '');
$emailNotify = false;

$order = $this->updateTransactionId($order, $validParams);
Expand All @@ -174,7 +174,7 @@ public function setCardOrderStatus($orderId, array $validParams, TpayConfigInter
if (Order::STATE_PROCESSING != $order->getState()) {
$emailNotify = true;
}
$this->registerCardCaptureNotificationTpay($order->getPayment(), $order->getGrandTotal(), $validParams);
$this->registerCardCaptureNotificationTpay($order->getPayment(), $order->getBaseGrandTotal(), $validParams);
}

if ($emailNotify) {
Expand Down
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@
<comment>Leave empty for no limit</comment>
<validate>validate-number</validate>
</field>
<field id="bank_payments_view" translate="label" type="select" sortOrder="18" showInDefault="1" showInWebsite="1" showInStore="1">
<label>View bank payments</label>
<comment>If you use other currencies visible on the website and pay in PLN, turn it on/off</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</group>
</section>
Expand Down
2 changes: 2 additions & 0 deletions i18n/pl_PL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"Minimal amount","Minimalna kwota"
"Maximal amount for method","Maksymalna kwota"
"Leave empty for no limit","Puste pole bez ograniczeń"
"View bank payments","Widok płatności bankowych"
"If you use other currencies visible on the website and pay in PLN, turn it on/off","W przypadku korzystania z innych walut widocznych na stronie i płatności w PLN należy włączyć/wyłączyć"
"Type in 6 digit code and press pay to commit blik payment.", "Wpisz 6 cyfrowy kod aby powiązać transakcję blik."
"Choose a payment method","Wybierz jeden ze sposobów płatności"
"I accept the regulations of Tpay.","Akceptuje regulamin Tpay."
Expand Down

0 comments on commit 0217d19

Please sign in to comment.