3
3
* Magento 2 extensions for Afterpay Payment
4
4
*
5
5
* @author Afterpay
6
- * @copyright 2016-2020 Afterpay https://www.afterpay.com
6
+ * @copyright 2016-2021 Afterpay https://www.afterpay.com
7
7
*/
8
8
namespace Afterpay \Afterpay \Block \Cart ;
9
9
13
13
use Magento \Customer \Model \Session as CustomerSession ;
14
14
use Magento \Framework \View \Element \Template \Context ;
15
15
use Magento \Framework \Locale \Resolver as Resolver ;
16
+ use Magento \Framework \Serialize \Serializer \Json as JsonHelper ;
16
17
17
18
18
19
class Button extends \Afterpay \Afterpay \Block \JsConfig
@@ -24,6 +25,10 @@ class Button extends \Afterpay\Afterpay\Block\JsConfig
24
25
protected $ afterpayPayovertime ;
25
26
protected $ checkoutSession ;
26
27
protected $ customerSession ;
28
+ /**
29
+ * @var JsonHelper
30
+ */
31
+ protected $ _jsonHelper ;
27
32
28
33
/**
29
34
* Button constructor.
@@ -32,6 +37,7 @@ class Button extends \Afterpay\Afterpay\Block\JsConfig
32
37
* @param AfterpayPayovertime $afterpayPayovertime
33
38
* @param CheckoutSession $checkoutSession
34
39
* @param CustomerSession $customerSession
40
+ * @param JsonHelper $jsonHelper
35
41
* @param array $data
36
42
* @param Resolver $localeResolver
37
43
*/
@@ -41,14 +47,16 @@ public function __construct(
41
47
AfterpayPayovertime $ afterpayPayovertime ,
42
48
CheckoutSession $ checkoutSession ,
43
49
CustomerSession $ customerSession ,
50
+ JsonHelper $ jsonHelper ,
44
51
array $ data =[],
45
52
Resolver $ localeResolver
46
53
) {
47
54
$ this ->afterpayConfig = $ afterpayConfig ;
48
55
$ this ->afterpayPayovertime = $ afterpayPayovertime ;
49
56
$ this ->checkoutSession = $ checkoutSession ;
50
57
$ this ->customerSession = $ customerSession ;
51
- parent ::__construct ($ afterpayConfig ,$ context , $ localeResolver ,$ data );
58
+ parent ::__construct ($ afterpayConfig ,$ afterpayPayovertime ,$ context , $ localeResolver ,$ jsonHelper ,$ data );
59
+
52
60
}
53
61
54
62
/**
@@ -58,37 +66,37 @@ protected function _getPaymentIsActive()
58
66
{
59
67
return $ this ->afterpayConfig ->isActive ();
60
68
}
61
-
69
+
62
70
/**
63
71
* @return bool
64
72
*/
65
73
public function canShow ()
66
74
{
67
- // check if payment is active
75
+ // check if payment is active
68
76
if (!$ this ->_getPaymentIsActive ()) {
69
77
return false ;
70
78
}
71
79
else {
72
80
//Check for Supported currency
73
81
if ($ this ->afterpayConfig ->getCurrencyCode ()){
74
-
82
+
75
83
$ quote = $ this ->checkoutSession ->getQuote ();
76
84
// get grand total (final amount need to be paid)
77
85
$ grandTotal =$ quote ->getGrandTotal ();
78
86
$ excluded_categories =$ this ->afterpayConfig ->getExcludedCategories ();
79
-
80
- if ($ this ->afterpayPayovertime ->canUseForCurrency ($ this ->afterpayConfig ->getCurrencyCode ()) ){
81
-
87
+
88
+ if ($ this ->afterpayPayovertime ->canUseForCurrency ($ this ->afterpayConfig ->getCurrencyCode ()) ){
89
+
82
90
if ($ excluded_categories !="" ){
83
91
$ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
84
92
$ productRepository = $ objectManager ->get ('\Magento\Catalog\Model\ProductRepository ' );
85
93
$ excluded_categories_array = explode (", " ,$ excluded_categories );
86
-
94
+
87
95
foreach ($ quote ->getAllVisibleItems () as $ item ) {
88
96
$ productid = $ item ->getProductId ();
89
97
$ product =$ productRepository ->getById ($ productid );
90
98
$ categoryids = $ product ->getCategoryIds ();
91
-
99
+
92
100
foreach ($ categoryids as $ k )
93
101
{
94
102
if (in_array ($ k ,$ excluded_categories_array )){
@@ -102,25 +110,25 @@ public function canShow()
102
110
else {
103
111
return false ;
104
112
}
105
- }
113
+ }
106
114
else {
107
115
return false ;
108
116
}
109
117
}
110
118
}
111
-
119
+
112
120
/**
113
121
* @return string
114
122
*/
115
123
public function getFinalAmount ()
116
124
{
117
-
125
+
118
126
$ grandTotal = $ this ->checkoutSession ->getQuote ()->getGrandTotal ();
119
-
127
+
120
128
return !empty ($ grandTotal )?number_format ($ grandTotal , 2 ,". " ,"" ):"0.00 " ;
121
-
129
+
122
130
}
123
- /*
131
+ /*
124
132
* @return boolean
125
133
*/
126
134
public function canUseCurrency ()
@@ -131,8 +139,27 @@ public function canUseCurrency()
131
139
{
132
140
$ canUse = $ this ->afterpayPayovertime ->canUseForCurrency ($ this ->afterpayConfig ->getCurrencyCode ());
133
141
}
134
-
142
+
135
143
return $ canUse ;
136
-
144
+
145
+ }
146
+ /*
147
+ * @return boolean
148
+ */
149
+ public function isWithinLimits ()
150
+ {
151
+ $ isWithinLimits =false ;
152
+ $ grandTotal = $ this ->checkoutSession ->getQuote ()->getGrandTotal ();
153
+ if ($ grandTotal > 0 && $ this ->afterpayConfig ->getMaxOrderLimit () >= $ grandTotal && $ this ->afterpayConfig ->getMinOrderLimit () <= $ grandTotal ){
154
+ $ isWithinLimits = true ;
155
+ }
156
+ return $ isWithinLimits ;
157
+ }
158
+ /*
159
+ * @return boolean
160
+ **/
161
+ public function isQuoteVirtual ()
162
+ {
163
+ return $ this ->checkoutSession ->getQuote ()->isVirtual ();
137
164
}
138
165
}
0 commit comments