Skip to content

Commit

Permalink
Feature/paypal pricing pdp (#25)
Browse files Browse the repository at this point in the history
* Changed paypal pdp messaging to work with taxes and final price
  • Loading branch information
andrii-onufriichuk authored Mar 27, 2023
1 parent 1575bb9 commit b447860
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
15 changes: 7 additions & 8 deletions Test/Unit/ViewModel/ClearpayMessagingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Checkout\Model\Session;
use Magento\Framework\Locale\Resolver;
use Magento\Store\Model\StoreManagerInterface;
use Rvvup\Payments\ViewModel\Price;

class ClearpayMessagingTest extends TestCase
{
Expand All @@ -26,8 +27,8 @@ class ClearpayMessagingTest extends TestCase
/** @var Clearpay */
private $viewModel;

/** @var Data $taxHelperMock */
private $taxHelperMock;
/** @var Price $priceHelperMock */
private $priceHelperMock;

/**
* @return void
Expand All @@ -52,9 +53,8 @@ protected function setUp(): void
$storeManagerMock->method('getStore')->willReturn($store);
$resolverMock = $this->getMockBuilder(Resolver::class)->disableOriginalConstructor()->getMock();
$loggerMock = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
$this->taxHelperMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
$taxConfigMock = $this->getMockBuilder(TaxConfig::class)->disableOriginalConstructor()->getMock();
$taxConfigMock->method('getPriceDisplayType')->willReturn(TaxConfig::DISPLAY_TYPE_BOTH);
//$this->priceHelperMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
$this->priceHelperMock = $this->getMockBuilder(Price::class)->disableOriginalConstructor()->getMock();
$this->viewModel = new Clearpay(
$this->configMock,
$productRepositoryMock,
Expand All @@ -68,8 +68,7 @@ protected function setUp(): void
"bundle" => 'bundle',
])),
$loggerMock,
$this->taxHelperMock,
$taxConfigMock
$this->priceHelperMock
);
}

Expand Down Expand Up @@ -178,6 +177,6 @@ private function configureProduct(bool $restricted, float $price)
$this->productMock->method('getFinalPrice')
->willReturn($price);

$this->taxHelperMock->method('getTaxPrice')->willReturn($price);
$this->priceHelperMock->method('getPrice')->willReturn($price);
}
}
43 changes: 10 additions & 33 deletions ViewModel/Clearpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Helper\Data;
use Magento\Tax\Model\Config as TaxConfig;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
Expand Down Expand Up @@ -42,20 +40,15 @@ class Clearpay implements ArgumentInterface
*/
private $logger;

/** @var null|bool */
private $isEnabled;

/**
* @var Data
* @var Price
*/
private Data $taxHelper;
private Price $priceViewModel;

public const PROVIDER = 'CLEARPAY';
/** @var null|bool */
private $isEnabled;

/**
* @var TaxConfig
*/
private TaxConfig $taxConfig;
public const PROVIDER = 'CLEARPAY';

/**
* @param Config $config
Expand All @@ -66,8 +59,7 @@ class Clearpay implements ArgumentInterface
* @param Resolver $locale
* @param ComplexProductTypePool $productTypePool
* @param LoggerInterface|RvvupLog $logger
* @param Data $taxHelper
* @param TaxConfig $taxConfig
* @param Price $priceViewModel
*/
public function __construct(
Config $config,
Expand All @@ -78,8 +70,7 @@ public function __construct(
Resolver $locale,
ComplexProductTypePool $productTypePool,
LoggerInterface $logger,
Data $taxHelper,
TaxConfig $taxConfig
Price $priceViewModel
) {
$this->config = $config;
$this->productRepository = $productRepository;
Expand All @@ -89,8 +80,7 @@ public function __construct(
$this->locale = $locale;
$this->productTypePool = $productTypePool;
$this->logger = $logger;
$this->taxHelper = $taxHelper;
$this->taxConfig = $taxConfig;
$this->priceViewModel = $priceViewModel;
}

/**
Expand Down Expand Up @@ -128,7 +118,7 @@ public function showByProduct(ProductInterface $product): bool
if ($isRestricted) {
return false;
}
$price = $this->getPrice($product);
$price = $this->priceViewModel->getPrice($product);

$thresholds = $this->getThresholdsByProviderAndCurrency();

Expand Down Expand Up @@ -258,7 +248,7 @@ public function isInThreshold(ProductInterface $product, array $threshold): bool
if (in_array($product->getTypeId(), $this->productTypePool->getProductTypes())) {
return true;
}
$price = $this->getPrice($product);
$price = $this->priceViewModel->getPrice($product);
return $price >= $threshold['min'] && $price <= $threshold['max'];
}

Expand Down Expand Up @@ -308,17 +298,4 @@ private function getThresholdsByProviderAndCurrency()
return false;
}
}

/**
* @param ProductInterface $product
* @return float
*/
public function getPrice(ProductInterface $product): float
{
if ($this->taxConfig->getPriceDisplayType() == TaxConfig::DISPLAY_TYPE_BOTH) {
return $this->taxHelper->getTaxPrice($product, (float)$product->getFinalPrice(), true);
}

return $this->taxHelper->getTaxPrice($product, (float)$product->getFinalPrice());
}
}
41 changes: 41 additions & 0 deletions ViewModel/Price.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\ViewModel;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Helper\Data;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Tax\Model\Config as TaxConfig;

class Price implements ArgumentInterface
{

/**
* @var Data
*/
private Data $taxHelper;

/**
* @var TaxConfig
*/
private TaxConfig $taxConfig;

public function __construct(
Data $taxHelper,
TaxConfig $taxConfig
) {
$this->taxHelper = $taxHelper;
$this->taxConfig = $taxConfig;
}

public function getPrice(ProductInterface $product): float
{
if ($this->taxConfig->getPriceDisplayType() == TaxConfig::DISPLAY_TYPE_BOTH) {
return $this->taxHelper->getTaxPrice($product, (float)$product->getFinalPrice(), true);
}

return $this->taxHelper->getTaxPrice($product, (float)$product->getFinalPrice());
}
}
1 change: 1 addition & 0 deletions view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
before="product.info">
<arguments>
<argument name="clearpay" xsi:type="object">Rvvup\Payments\ViewModel\Clearpay</argument>
<argument name="price" xsi:type="object">Rvvup\Payments\ViewModel\Price</argument>
<argument name="restrictions" xsi:type="object">Rvvup\Payments\ViewModel\Restrictions</argument>
<argument name="rvvup_payments_paypal_view_model" xsi:type="object">Rvvup\Payments\ViewModel\PayPal</argument>
</arguments>
Expand Down
10 changes: 7 additions & 3 deletions view/frontend/templates/product/view/addtocart.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ if (!isset($escaper)) {

/** @var \Rvvup\Payments\ViewModel\Clearpay $clearpay */
$clearpay = $block->getData('clearpay');

/** @var \Rvvup\Payments\ViewModel\Price $priceViewModel */
$priceViewModel = $block->getData('price');

/** @var \Rvvup\Payments\ViewModel\Restrictions $restrictions */
$restrictions = $block->getData('restrictions');
/** @var \Rvvup\Payments\ViewModel\PayPal $paypalViewModel */
Expand Down Expand Up @@ -37,7 +41,7 @@ $thresholds = $clearpay->getThresholds($product);
<afterpay-placement style="display:none" id="clearpay-summary"
data-locale="<?= $escaper->escapeHtmlAttr($clearpay->getCurrentLocale()) ?>"
data-currency="<?= $escaper->escapeHtmlAttr($clearpay->getCurrentCurrencyCode()) ?>"
data-amount="<?= $escaper->escapeHtmlAttr($clearpay->getPrice($product)) ?>"
data-amount="<?= $escaper->escapeHtmlAttr($priceViewModel->getPrice($product)) ?>"
data-logo-type="<?= $escaper->escapeHtmlAttr($clearpay->getLogoType()) ?>"
data-badge-theme="<?= $escaper->escapeHtmlAttr($clearpay->getBadgeTheme()) ?>"
data-modal-theme="<?= $escaper->escapeHtmlAttr($clearpay->getModalTheme()) ?>"
Expand Down Expand Up @@ -69,14 +73,14 @@ $thresholds = $clearpay->getThresholds($product);
<?php endif; ?>
<?php endif; ?>
<?php if ($paypalViewModel->canUseForProductType($product)
&& $paypalViewModel->isAvailable((string) $product->getFinalPrice())
&& $paypalViewModel->isAvailable((string) $priceViewModel->getPrice($product))
&& $paypalViewModel->getPayLaterMessagingValue('enabled') === true
): ?>
<div>
<div class="rvvup-paypal-paylater-messaging-container"
data-pp-message
data-pp-placement="product"
data-pp-amount="<?= $escaper->escapeHtmlAttr($product->getFinalPrice()) ?>"
data-pp-amount="<?= $escaper->escapeHtmlAttr($priceViewModel->getPrice($product)) ?>"
data-pp-style-layout="<?= $escaper->escapeHtmlAttr($paypalViewModel->getPayLaterMessagingValue('layout')) ?>"
data-pp-style-logo-type="<?= $escaper->escapeHtmlAttr($paypalViewModel->getPayLaterMessagingValue('logoType')) ?>"
data-pp-style-logo-position="<?= $escaper->escapeHtmlAttr($paypalViewModel->getPayLaterMessagingValue('logoPosition')) ?>"
Expand Down

0 comments on commit b447860

Please sign in to comment.