17
17
* @copyright Copyright (c) Buckaroo B.V.
18
18
* @license https://tldrlegal.com/license/mit-license
19
19
*/
20
+ declare (strict_types=1 );
20
21
21
- namespace Buckaroo \Magento2 \Model \ Service \ Plugin \ PaypalSellersProtection ;
22
+ namespace Buckaroo \Magento2 \Plugin ;
22
23
23
24
use Buckaroo \Magento2 \Model \ConfigProvider \Method \Paypal ;
25
+ use Buckaroo \Magento2 \Model \Push \DefaultProcessor ;
26
+ use Buckaroo \Magento2 \Model \Push \PaypalProcessor ;
24
27
use Magento \Sales \Model \Order ;
25
28
26
- class Push
29
+ class PaypalProcessorPlugin
27
30
{
28
- /**#@+
29
- * PayPal Seller's Protection eligibility types.
30
- */
31
31
public const ELIGIBILITY_INELIGIBLE = 'Ineligible ' ;
32
32
public const ELIGIBILITY_TYPE_ELIGIBLE = 'Eligible ' ;
33
33
public const ELIGIBILITY_TYPE_ITEM_NOT_RECEIVED = 'ItemNotReceivedEligible ' ;
34
34
public const ELIGIBILITY_TYPE_UNAUTHORIZED_PAYMENT = 'UnauthorizedPaymentEligible ' ;
35
35
public const ELIGIBILITY_TYPE_NONE = 'None ' ;
36
- /**#@-*/
37
36
38
37
/**
39
38
* @var Paypal
@@ -50,43 +49,48 @@ public function __construct(
50
49
}
51
50
52
51
/**
53
- * Change status on order by PayPal status
52
+ * Append Seller Protection info after success processing
54
53
*
55
- * @param \Buckaroo\Magento2\Model\Push $push
54
+ * @param DefaultProcessor|PaypalProcessor $subject
56
55
* @param boolean $result
56
+ * @param string $newStatus
57
+ * @param string $message
57
58
* @return bool
58
- * @throws \InvalidArgumentException
59
59
*/
60
60
public function afterProcessSucceededPush (
61
- \ Buckaroo \ Magento2 \ Model \ Push $ push ,
61
+ DefaultProcessor $ subject ,
62
62
$ result
63
63
) {
64
+ if (!$ subject instanceof PaypalProcessor) {
65
+ return $ result ;
66
+ }
67
+
68
+ $ pushRequest = $ this ->getProtectedProperty ($ subject , 'pushRequest ' );
69
+ $ order = $ this ->getProtectedProperty ($ subject , 'order ' );
70
+
64
71
if (!$ this ->configProviderPaypal ->getSellersProtection ()
65
- || empty ($ push ->pushRequest ->getServicePaypalProtectioneligibility ())
66
- || empty ($ push ->pushRequest ->getServicePaypalProtectioneligibilitytype ())
72
+ || empty ($ pushRequest ) || empty ($ order )
67
73
) {
68
74
return $ result ;
69
75
}
70
76
71
- $ eligibilityTypes =
72
- static ::ELIGIBILITY_INELIGIBLE !== $ push ->pushRequest ->getServicePaypalProtectioneligibility ()
73
- ? $ push ->pushRequest ->getServicePaypalProtectioneligibilitytype ()
74
- : static ::ELIGIBILITY_TYPE_NONE ;
77
+ $ eligibility = $ pushRequest ->getServicePaypalProtectioneligibility ();
78
+ $ eligibilityType = $ pushRequest ->getServicePaypalProtectioneligibilitytype ();
75
79
76
- // Handle the given eligibility types separately,
77
- // since we know Buckaroo can provide us with
78
- // multiple types in a single response.
79
- $ this ->handleEligibilityTypes (
80
- explode (', ' , $ eligibilityTypes ),
81
- $ push ->order
82
- );
80
+ if (empty ($ eligibility ) || empty ($ eligibilityType )) {
81
+ return $ result ;
82
+ }
83
+
84
+ $ eligibilityTypes = self ::ELIGIBILITY_INELIGIBLE !== $ eligibility
85
+ ? $ eligibilityType
86
+ : self ::ELIGIBILITY_TYPE_NONE ;
87
+
88
+ $ this ->handleEligibilityTypes (explode (', ' , $ eligibilityTypes ), $ order );
83
89
84
90
return $ result ;
85
91
}
86
92
87
93
/**
88
- * Proxy the handling of eligibility types.
89
- *
90
94
* @param string|string[] $eligibilityTypes
91
95
* @param Order $order
92
96
* @return void
@@ -97,53 +101,68 @@ protected function handleEligibilityTypes($eligibilityTypes, $order)
97
101
$ eligibilityTypes = [$ eligibilityTypes ];
98
102
}
99
103
100
- // Append multiple status updates to the order,
101
- // this way the merchant has a more detailed
102
- // log of what is happening with payments.
103
104
array_walk ($ eligibilityTypes , function ($ eligibilityType ) use ($ order ) {
104
105
$ this ->handleEligibilityType ($ eligibilityType , $ order );
105
106
});
106
107
}
107
108
108
109
/**
109
- * Handle the specified eligibility type.
110
- *
111
110
* @param string $eligibilityType
112
111
* @param Order $order
113
112
* @return void
114
- * @throws \InvalidArgumentException
115
113
*/
116
114
protected function handleEligibilityType ($ eligibilityType , $ order )
117
115
{
118
116
switch ($ eligibilityType ) {
119
- case static ::ELIGIBILITY_TYPE_ELIGIBLE :
117
+ case self ::ELIGIBILITY_TYPE_ELIGIBLE :
120
118
$ comment = __ (
121
- 'Merchant is protected by PayPal Seller Protection Policy for both Unauthorized Payment and Item ' .
122
- ' Not Received. '
119
+ 'Merchant is protected by PayPal Seller Protection Policy for both Unauthorized Payment and Item '
120
+ . ' Not Received. '
123
121
);
124
122
125
123
$ status = $ this ->configProviderPaypal ->getSellersProtectionEligible ();
126
124
break ;
127
- case static ::ELIGIBILITY_TYPE_ITEM_NOT_RECEIVED :
125
+ case self ::ELIGIBILITY_TYPE_ITEM_NOT_RECEIVED :
128
126
$ comment = __ ('Merchant is protected by Paypal Seller Protection Policy for Item Not Received. ' );
129
127
130
128
$ status = $ this ->configProviderPaypal ->getSellersProtectionItemnotreceivedEligible ();
131
129
break ;
132
- case static ::ELIGIBILITY_TYPE_UNAUTHORIZED_PAYMENT :
130
+ case self ::ELIGIBILITY_TYPE_UNAUTHORIZED_PAYMENT :
133
131
$ comment = __ ('Merchant is protected by Paypal Seller Protection Policy for Unauthorized Payment. ' );
134
132
135
133
$ status = $ this ->configProviderPaypal ->getSellersProtectionUnauthorizedpaymentEligible ();
136
134
break ;
137
- case static ::ELIGIBILITY_TYPE_NONE :
135
+ case self ::ELIGIBILITY_TYPE_NONE :
138
136
$ comment = __ ('Merchant is not protected under the Seller Protection Policy. ' );
139
137
140
138
$ status = $ this ->configProviderPaypal ->getSellersProtectionIneligible ();
141
139
break ;
142
140
default :
143
141
throw new \InvalidArgumentException ('Invalid eligibility type(s): ' . $ eligibilityType );
144
- //phpcs:ignore:Squiz.PHP.NonExecutableCode
145
- break ;
146
142
}
143
+
147
144
$ order ->addCommentToStatusHistory ($ comment , $ status ?: false );
148
145
}
146
+
147
+ /**
148
+ * @param object $subject
149
+ * @param string $name
150
+ * @return mixed|null
151
+ */
152
+ private function getProtectedProperty (object $ subject , string $ name )
153
+ {
154
+ $ ref = new \ReflectionClass ($ subject );
155
+ do {
156
+ if ($ ref ->hasProperty ($ name )) {
157
+ $ prop = $ ref ->getProperty ($ name );
158
+ $ prop ->setAccessible (true );
159
+ return $ prop ->getValue ($ subject );
160
+ }
161
+ $ ref = $ ref ->getParentClass ();
162
+ } while ($ ref );
163
+ return null ;
164
+ }
149
165
}
166
+
167
+
168
+
0 commit comments