Skip to content

Commit 4116ba0

Browse files
committed
ACP2E-3028: Paylater message not showing in PDP for Canadian paypal merchant account
1 parent 4be117b commit 4116ba0

File tree

7 files changed

+82
-64
lines changed

7 files changed

+82
-64
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-
public const ARRAY_PAYLATER_SUPPORTED_COUNTRIES = ['US','GB','DE','FR','AU','IT','ES'];
32+
private 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: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
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;
1412
use Magento\Paypal\Model\PayLaterConfig;
1513
use Magento\Paypal\Model\SdkUrl;
1614
use Magento\Paypal\Model\Config as PaypalConfig;
1715
use Magento\Framework\App\ObjectManager;
18-
use Magento\Customer\Model\Session as CustomerSession;
1916

2017
/**
2118
* PayPal PayLater component block
@@ -49,30 +46,18 @@ class Banner extends Template
4946
private $paypalConfig;
5047

5148
/**
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
49+
* @param Template\Context $context
6350
* @param PayLaterConfig $payLaterConfig
6451
* @param SdkUrl $sdkUrl
6552
* @param array $data
66-
* @param PaypalConfig|null $paypalConfig
67-
* @param CustomerSession|null $session
53+
* @param PaypalConfig $paypalConfig
6854
*/
6955
public function __construct(
7056
Template\Context $context,
7157
PayLaterConfig $payLaterConfig,
7258
SdkUrl $sdkUrl,
7359
array $data = [],
74-
?PaypalConfig $paypalConfig = null,
75-
?CustomerSession $session = null
60+
PaypalConfig $paypalConfig = null
7661
) {
7762
parent::__construct($context, $data);
7863
$this->payLaterConfig = $payLaterConfig;
@@ -81,7 +66,6 @@ public function __construct(
8166
$this->position = $data['position'] ?? '';
8267
$this->paypalConfig = $paypalConfig ?: ObjectManager::getInstance()
8368
->get(PaypalConfig::class);
84-
$this->session = $session ?: ObjectManager::getInstance()->get(CustomerSession::class);
8569
}
8670

8771
/**
@@ -121,7 +105,6 @@ public function getJsLayout()
121105
$componentAttributes = $this->jsLayout['components']['payLater']['config']['attributes'] ?? [];
122106
$config['attributes'] = array_replace($this->getStyleAttributesConfig(), $componentAttributes);
123107
$config['attributes']['data-pp-placement'] = $this->placement;
124-
$config['attributes']['data-pp-buyercountry'] = $this->getBuyerCountry();
125108

126109
$this->jsLayout = [
127110
'components' => [
@@ -164,44 +147,7 @@ private function isEnabled(): bool
164147
{
165148
$enabled = $this->payLaterConfig->isEnabled($this->placement);
166149
return $enabled &&
167-
$this->isBuyerCountryAvailable() &&
168150
$this->payLaterConfig->getSectionConfig($this->placement, PayLaterConfig::CONFIG_KEY_POSITION) ===
169151
$this->position;
170152
}
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-
}
207153
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Paypal\CustomerData;
20+
21+
use Magento\Customer\Api\AddressRepositoryInterface;
22+
use Magento\Customer\CustomerData\SectionSourceInterface;
23+
use Magento\Customer\Helper\Session\CurrentCustomer;
24+
use Magento\Framework\Exception\NoSuchEntityException;
25+
26+
class BuyerCountry implements SectionSourceInterface
27+
{
28+
/**
29+
* @param CurrentCustomer $currentCustomer
30+
* @param AddressRepositoryInterface $addressRepository
31+
*/
32+
public function __construct(
33+
private readonly CurrentCustomer $currentCustomer,
34+
private readonly AddressRepositoryInterface $addressRepository
35+
) {
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getSectionData()
42+
{
43+
$country = null;
44+
try {
45+
$customer = $this->currentCustomer->getCustomer();
46+
$addressId = $customer->getDefaultBilling() ?
47+
$customer->getDefaultBilling() :
48+
$customer->getDefaultShipping();
49+
50+
if ($addressId) {
51+
$address = $this->addressRepository->getById($addressId);
52+
$country = $address->getCountryId();
53+
}
54+
} catch (NoSuchEntityException $e) {
55+
}
56+
57+
return [
58+
'code' => $country
59+
];
60+
}
61+
}

app/code/Magento/Paypal/etc/frontend/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<arguments>
120120
<argument name="sectionSourceMap" xsi:type="array">
121121
<item name="paypal-billing-agreement" xsi:type="string">Magento\Paypal\CustomerData\BillingAgreement</item>
122+
<item name="paypal-buyer-country" xsi:type="string">Magento\Paypal\CustomerData\BuyerCountry</item>
122123
</argument>
123124
</arguments>
124125
</type>

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" cacheable="false">
18+
template="Magento_Paypal::paylater/banner.phtml">
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" cacheable="false">
47+
template="Magento_Paypal::paylater/banner.phtml">
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" cacheable="false">
70+
template="Magento_Paypal::paylater/banner.phtml">
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ define([
99
'uiElement',
1010
'uiLayout',
1111
'Magento_Paypal/js/in-context/paypal-sdk',
12+
'Magento_Customer/js/customer-data',
1213
'domReady!'
1314
], function (
1415
$,
1516
ko,
1617
Component,
1718
layout,
18-
paypalSdk
19+
paypalSdk,
20+
customerData
1921
) {
2022
'use strict';
2123

@@ -37,21 +39,29 @@ define([
3739
},
3840
paypal: null,
3941
amount: null,
42+
buyerCountry: null,
4043

4144
/**
4245
* Initialize
4346
*
4447
* @returns {*}
4548
*/
4649
initialize: function () {
50+
let buyerCountry = customerData.get('paypal-buyer-country');
51+
this.buyerCountry = buyerCountry().code;
52+
53+
if (!this.buyerCountry) {
54+
return this;
55+
}
56+
4757
this._super()
4858
.observe(['amount']);
4959

5060
if (this.displayAmount) {
5161
layout([this.amountComponentConfig]);
5262
}
5363

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

app/code/Magento/Paypal/view/frontend/web/template/paylater.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
'data-pp-style-text-size': getAttribute('data-pp-style-text-size'),
1616
'data-pp-style-color': getAttribute('data-pp-style-color'),
1717
'data-pp-style-ratio': getAttribute('data-pp-style-ratio'),
18-
'data-pp-buyercountry': getAttribute('data-pp-buyercountry')
18+
'data-pp-buyercountry': buyerCountry
1919
}" ></div>
2020

0 commit comments

Comments
 (0)