9
9
namespace Magento \Paypal \Block \PayLater ;
10
10
11
11
use Magento \Framework \View \Element \Template ;
12
+ use Magento \Framework \View \Element \Template \Context ;
13
+ use Magento \Paypal \Block \Adminhtml \System \Config \PayLaterLink ;
12
14
use Magento \Paypal \Model \PayLaterConfig ;
13
15
use Magento \Paypal \Model \SdkUrl ;
14
16
use Magento \Paypal \Model \Config as PaypalConfig ;
15
17
use Magento \Framework \App \ObjectManager ;
18
+ use Magento \Customer \Model \Session as CustomerSession ;
16
19
17
20
/**
18
21
* PayPal PayLater component block
@@ -46,18 +49,30 @@ class Banner extends Template
46
49
private $ paypalConfig ;
47
50
48
51
/**
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
50
63
* @param PayLaterConfig $payLaterConfig
51
64
* @param SdkUrl $sdkUrl
52
65
* @param array $data
53
- * @param PaypalConfig $paypalConfig
66
+ * @param PaypalConfig|null $paypalConfig
67
+ * @param CustomerSession|null $session
54
68
*/
55
69
public function __construct (
56
70
Template \Context $ context ,
57
71
PayLaterConfig $ payLaterConfig ,
58
72
SdkUrl $ sdkUrl ,
59
73
array $ data = [],
60
- PaypalConfig $ paypalConfig = null
74
+ ?PaypalConfig $ paypalConfig = null ,
75
+ ?CustomerSession $ session = null
61
76
) {
62
77
parent ::__construct ($ context , $ data );
63
78
$ this ->payLaterConfig = $ payLaterConfig ;
@@ -66,6 +81,7 @@ public function __construct(
66
81
$ this ->position = $ data ['position ' ] ?? '' ;
67
82
$ this ->paypalConfig = $ paypalConfig ?: ObjectManager::getInstance ()
68
83
->get (PaypalConfig::class);
84
+ $ this ->session = $ session ?: ObjectManager::getInstance ()->get (CustomerSession::class);
69
85
}
70
86
71
87
/**
@@ -105,7 +121,7 @@ public function getJsLayout()
105
121
$ componentAttributes = $ this ->jsLayout ['components ' ]['payLater ' ]['config ' ]['attributes ' ] ?? [];
106
122
$ config ['attributes ' ] = array_replace ($ this ->getStyleAttributesConfig (), $ componentAttributes );
107
123
$ 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 ();
109
125
110
126
$ this ->jsLayout = [
111
127
'components ' => [
@@ -148,7 +164,44 @@ private function isEnabled(): bool
148
164
{
149
165
$ enabled = $ this ->payLaterConfig ->isEnabled ($ this ->placement );
150
166
return $ enabled &&
167
+ $ this ->isBuyerCountryAvailable () &&
151
168
$ this ->payLaterConfig ->getSectionConfig ($ this ->placement , PayLaterConfig::CONFIG_KEY_POSITION ) ===
152
169
$ this ->position ;
153
170
}
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
+ }
154
207
}
0 commit comments