Skip to content

Commit 5a143eb

Browse files
committed
ACP2E-3028: Paylater message not showing in PDP for Canadian paypal merchant account
1 parent dcdf6e6 commit 5a143eb

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

app/code/Magento/Paypal/Block/Adminhtml/System/Config/PayLaterLink.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PayLaterLink extends Field
2929
/**
3030
* @var array.
3131
*/
32-
private const ARRAY_PAYLATER_SUPPORTED_COUNTRIES = ['US','GB','DE','FR','AU','IT','ES'];
32+
public const ARRAY_PAYLATER_SUPPORTED_COUNTRIES = ['US','GB','DE','FR','AU','IT','ES'];
3333

3434
/**
3535
* @var ScopeConfigInterface

app/code/Magento/Paypal/Block/PayLater/Banner.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
namespace Magento\Paypal\Block\PayLater;
1010

1111
use Magento\Framework\View\Element\Template;
12+
use Magento\Framework\View\Element\Template\Context;
13+
use Magento\Paypal\Block\Adminhtml\System\Config\PayLaterLink;
1214
use Magento\Paypal\Model\PayLaterConfig;
1315
use Magento\Paypal\Model\SdkUrl;
1416
use Magento\Paypal\Model\Config as PaypalConfig;
1517
use Magento\Framework\App\ObjectManager;
18+
use Magento\Customer\Model\Session as CustomerSession;
1619

1720
/**
1821
* PayPal PayLater component block
@@ -46,18 +49,30 @@ class Banner extends Template
4649
private $paypalConfig;
4750

4851
/**
49-
* @param Template\Context $context
52+
* @var string|null
53+
*/
54+
private ?string $buyerCountry = null;
55+
56+
/**
57+
* @var CustomerSession
58+
*/
59+
private CustomerSession $session;
60+
61+
/**
62+
* @param Context $context
5063
* @param PayLaterConfig $payLaterConfig
5164
* @param SdkUrl $sdkUrl
5265
* @param array $data
53-
* @param PaypalConfig $paypalConfig
66+
* @param PaypalConfig|null $paypalConfig
67+
* @param CustomerSession|null $session
5468
*/
5569
public function __construct(
5670
Template\Context $context,
5771
PayLaterConfig $payLaterConfig,
5872
SdkUrl $sdkUrl,
5973
array $data = [],
60-
PaypalConfig $paypalConfig = null
74+
?PaypalConfig $paypalConfig = null,
75+
?CustomerSession $session = null
6176
) {
6277
parent::__construct($context, $data);
6378
$this->payLaterConfig = $payLaterConfig;
@@ -66,6 +81,7 @@ public function __construct(
6681
$this->position = $data['position'] ?? '';
6782
$this->paypalConfig = $paypalConfig ?: ObjectManager::getInstance()
6883
->get(PaypalConfig::class);
84+
$this->session = $session ?: ObjectManager::getInstance()->get(CustomerSession::class);
6985
}
7086

7187
/**
@@ -105,7 +121,7 @@ public function getJsLayout()
105121
$componentAttributes = $this->jsLayout['components']['payLater']['config']['attributes'] ?? [];
106122
$config['attributes'] = array_replace($this->getStyleAttributesConfig(), $componentAttributes);
107123
$config['attributes']['data-pp-placement'] = $this->placement;
108-
$config['attributes']['data-pp-buyercountry'] = 'US'; // this should be dynamic. Possible values listed here: https://developer.paypal.com/limited-release/sdk-pay-later-messaging-cross-border/#link-renderthemessageswiththebuyercountryparameter
124+
$config['attributes']['data-pp-buyercountry'] = $this->getBuyerCountry();
109125

110126
$this->jsLayout = [
111127
'components' => [
@@ -148,7 +164,44 @@ private function isEnabled(): bool
148164
{
149165
$enabled = $this->payLaterConfig->isEnabled($this->placement);
150166
return $enabled &&
167+
$this->isBuyerCountryAvailable() &&
151168
$this->payLaterConfig->getSectionConfig($this->placement, PayLaterConfig::CONFIG_KEY_POSITION) ===
152169
$this->position;
153170
}
171+
172+
/**
173+
* The pay later message should be displayed only if the buyer country is available
174+
*
175+
* @return bool
176+
*/
177+
private function isBuyerCountryAvailable(): bool
178+
{
179+
return (bool)$this->getBuyerCountry();
180+
}
181+
182+
/**
183+
* Buyer country should be extracted from logged-in user billing or shipping address
184+
*
185+
* @return string
186+
*/
187+
private function getBuyerCountry(): string
188+
{
189+
if ($this->buyerCountry === null) {
190+
$country = null;
191+
if ($this->session->isLoggedIn()) {
192+
$address = $this->session->getCustomer()->getPrimaryBillingAddress() ?
193+
$this->session->getCustomer()->getPrimaryBillingAddress() :
194+
$this->session->getCustomer()->getDefaultShippingAddress();
195+
$country = $address->getCountryId();
196+
}
197+
198+
if (in_array($country, PayLaterLink::ARRAY_PAYLATER_SUPPORTED_COUNTRIES)) {
199+
$this->buyerCountry = $country;
200+
} else {
201+
$this->buyerCountry = '';
202+
}
203+
}
204+
205+
return $this->buyerCountry;
206+
}
154207
}

app/code/Magento/Paypal/view/frontend/layout/catalog_product_view.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</arguments>
1616
</block>
1717
<block class="Magento\Paypal\Block\PayLater\Banner" name="top.container.paylater"
18-
template="Magento_Paypal::paylater/banner.phtml">
18+
template="Magento_Paypal::paylater/banner.phtml" cacheable="false">
1919
<arguments>
2020
<argument name="placement" xsi:type="string">product</argument>
2121
<argument name="position" xsi:type="string">header</argument>
@@ -44,7 +44,7 @@
4444
</arguments>
4545
</block>
4646
<block class="Magento\Paypal\Block\PayLater\Banner" name="product.info.addtocart.paylater"
47-
template="Magento_Paypal::paylater/banner.phtml">
47+
template="Magento_Paypal::paylater/banner.phtml" cacheable="false">
4848
<arguments>
4949
<argument name="placement" xsi:type="string">product</argument>
5050
<argument name="position" xsi:type="string">near_pp_button</argument>
@@ -67,7 +67,7 @@
6767
</referenceContainer>
6868
<referenceBlock name="product.info.addtocart.additional">
6969
<block class="Magento\Paypal\Block\PayLater\Banner" name="product.info.addtocart.additional.paylater"
70-
template="Magento_Paypal::paylater/banner.phtml">
70+
template="Magento_Paypal::paylater/banner.phtml" cacheable="false">
7171
<arguments>
7272
<argument name="placement" xsi:type="string">product</argument>
7373
<argument name="position" xsi:type="string">near_pp_button</argument>

app/code/Magento/Paypal/view/frontend/web/js/view/paylater.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ define([
5151
layout([this.amountComponentConfig]);
5252
}
5353

54-
if (this.sdkUrl !== '') {
54+
if (this.sdkUrl !== '') { // we should probably make a check here if we should load it or not
5555
this.loadPayPalSdk(this.sdkUrl, this.dataAttributes)
5656
.then(this._setPayPalObject.bind(this));
5757
}

0 commit comments

Comments
 (0)