Skip to content

Commit 77c4b28

Browse files
Merge pull request #1342 from buckaroo-it/BP-3526-Update-Push-Plugins
BP-3526-Update-Push-Plugins
2 parents 9c2ff9e + dd5f8a8 commit 77c4b28

File tree

3 files changed

+118
-57
lines changed

3 files changed

+118
-57
lines changed

Model/Service/Plugin/Mpi/Push.php renamed to Plugin/DefaultProcessorPlugin.php

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
* @copyright Copyright (c) Buckaroo B.V.
1818
* @license https://tldrlegal.com/license/mit-license
1919
*/
20+
declare(strict_types=1);
2021

21-
namespace Buckaroo\Magento2\Model\Service\Plugin\Mpi;
22+
namespace Buckaroo\Magento2\Plugin;
2223

2324
use Buckaroo\Magento2\Model\ConfigProvider\Method\Creditcard;
2425
use Buckaroo\Magento2\Model\Method\BuckarooAdapter;
26+
use Buckaroo\Magento2\Model\Push\DefaultProcessor;
2527
use Magento\Framework\Exception\LocalizedException;
2628

27-
class Push
29+
class DefaultProcessorPlugin
2830
{
2931
/**
3032
* @var Creditcard
@@ -43,27 +45,35 @@ public function __construct(
4345
/**
4446
* After Process Succeeded Push
4547
*
46-
* @param \Buckaroo\Magento2\Model\Push $push
48+
* @param DefaultProcessor $subject
4749
* @param boolean $result
50+
* @param string $newStatus
51+
* @param string $message
4852
* @return boolean
4953
*
54+
* @SuppressWarnings(PHPMD.NPathComplexity)
5055
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5156
* @throws LocalizedException
5257
*/
5358
public function afterProcessSucceededPush(
54-
\Buckaroo\Magento2\Model\Push $push,
59+
DefaultProcessor $subject,
5560
$result
5661
) {
57-
$payment = $push->order->getPayment();
62+
$order = $this->getProtectedProperty($subject, 'order');
63+
$pushRequest = $this->getProtectedProperty($subject, 'pushRequest');
64+
65+
if (!$order || !$pushRequest) {
66+
return $result;
67+
}
68+
69+
$payment = $order->getPayment();
5870
$method = $payment->getMethod();
5971

6072
if (strpos($method, 'buckaroo_magento2') === false) {
6173
return $result;
6274
}
6375

64-
/**
65-
* @var BuckarooAdapter $paymentMethodInstance
66-
*/
76+
/** @var BuckarooAdapter $paymentMethodInstance */
6777
$paymentMethodInstance = $payment->getMethodInstance();
6878
$card = $paymentMethodInstance->getInfoInstance()->getAdditionalInformation('card_type');
6979

@@ -74,13 +84,20 @@ public function afterProcessSucceededPush(
7484
$authenticationFunction = 'getService' . ucfirst($card) . 'Authentication';
7585
$enrolledFunction = 'getService' . ucfirst($card) . 'Enrolled';
7686

77-
if (empty($push->pushRequest->$authenticationFunction())
78-
|| empty($push->pushRequest->$enrolledFunction())
87+
if (!\is_object($pushRequest)
88+
|| !\method_exists($pushRequest, $authenticationFunction)
89+
|| !\method_exists($pushRequest, $enrolledFunction)
90+
) {
91+
return $result;
92+
}
93+
94+
if (empty($pushRequest->$authenticationFunction())
95+
|| empty($pushRequest->$enrolledFunction())
7996
) {
8097
return $result;
8198
}
8299

83-
$authentication = $push->pushRequest->$authenticationFunction();
100+
$authentication = $pushRequest->$authenticationFunction();
84101

85102
if ($authentication == 'U' || $authentication == 'N') {
86103
switch ($card) {
@@ -99,24 +116,46 @@ public function afterProcessSucceededPush(
99116
}
100117

101118
if ($putOrderOnHold) {
102-
$push->order
119+
$order
103120
->hold()
104121
->addCommentToStatusHistory(
105122
__('Order has been put on hold, because it is unsecure.')
106123
);
107124

108-
$push->order->save();
125+
$order->save();
109126
}
110127
}
111128

112129
$paymentMethodInstance->getInfoInstance()->setAdditionalInformation(
113130
'buckaroo_mpi_status',
114131
[
115-
'enrolled' => $push->pushRequest->$enrolledFunction(),
116-
'authentication' => $push->pushRequest->$authenticationFunction(),
132+
'enrolled' => $pushRequest->$enrolledFunction(),
133+
'authentication' => $pushRequest->$authenticationFunction(),
117134
]
118135
);
119136

120137
return $result;
121138
}
139+
140+
/**
141+
* @param object $subject
142+
* @param string $name
143+
* @return mixed|null
144+
*/
145+
private function getProtectedProperty(object $subject, string $name)
146+
{
147+
$ref = new \ReflectionClass($subject);
148+
do {
149+
if ($ref->hasProperty($name)) {
150+
$prop = $ref->getProperty($name);
151+
$prop->setAccessible(true);
152+
return $prop->getValue($subject);
153+
}
154+
$ref = $ref->getParentClass();
155+
} while ($ref);
156+
return null;
157+
}
122158
}
159+
160+
161+

Model/Service/Plugin/PaypalSellersProtection/Push.php renamed to Plugin/PaypalProcessorPlugin.php

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717
* @copyright Copyright (c) Buckaroo B.V.
1818
* @license https://tldrlegal.com/license/mit-license
1919
*/
20+
declare(strict_types=1);
2021

21-
namespace Buckaroo\Magento2\Model\Service\Plugin\PaypalSellersProtection;
22+
namespace Buckaroo\Magento2\Plugin;
2223

2324
use Buckaroo\Magento2\Model\ConfigProvider\Method\Paypal;
25+
use Buckaroo\Magento2\Model\Push\DefaultProcessor;
26+
use Buckaroo\Magento2\Model\Push\PaypalProcessor;
2427
use Magento\Sales\Model\Order;
2528

26-
class Push
29+
class PaypalProcessorPlugin
2730
{
28-
/**#@+
29-
* PayPal Seller's Protection eligibility types.
30-
*/
3131
public const ELIGIBILITY_INELIGIBLE = 'Ineligible';
3232
public const ELIGIBILITY_TYPE_ELIGIBLE = 'Eligible';
3333
public const ELIGIBILITY_TYPE_ITEM_NOT_RECEIVED = 'ItemNotReceivedEligible';
3434
public const ELIGIBILITY_TYPE_UNAUTHORIZED_PAYMENT = 'UnauthorizedPaymentEligible';
3535
public const ELIGIBILITY_TYPE_NONE = 'None';
36-
/**#@-*/
3736

3837
/**
3938
* @var Paypal
@@ -50,43 +49,48 @@ public function __construct(
5049
}
5150

5251
/**
53-
* Change status on order by PayPal status
52+
* Append Seller Protection info after success processing
5453
*
55-
* @param \Buckaroo\Magento2\Model\Push $push
54+
* @param DefaultProcessor|PaypalProcessor $subject
5655
* @param boolean $result
56+
* @param string $newStatus
57+
* @param string $message
5758
* @return bool
58-
* @throws \InvalidArgumentException
5959
*/
6060
public function afterProcessSucceededPush(
61-
\Buckaroo\Magento2\Model\Push $push,
61+
DefaultProcessor $subject,
6262
$result
6363
) {
64+
if (!$subject instanceof PaypalProcessor) {
65+
return $result;
66+
}
67+
68+
$pushRequest = $this->getProtectedProperty($subject, 'pushRequest');
69+
$order = $this->getProtectedProperty($subject, 'order');
70+
6471
if (!$this->configProviderPaypal->getSellersProtection()
65-
|| empty($push->pushRequest->getServicePaypalProtectioneligibility())
66-
|| empty($push->pushRequest->getServicePaypalProtectioneligibilitytype())
72+
|| empty($pushRequest) || empty($order)
6773
) {
6874
return $result;
6975
}
7076

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();
7579

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);
8389

8490
return $result;
8591
}
8692

8793
/**
88-
* Proxy the handling of eligibility types.
89-
*
9094
* @param string|string[] $eligibilityTypes
9195
* @param Order $order
9296
* @return void
@@ -97,53 +101,68 @@ protected function handleEligibilityTypes($eligibilityTypes, $order)
97101
$eligibilityTypes = [$eligibilityTypes];
98102
}
99103

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.
103104
array_walk($eligibilityTypes, function ($eligibilityType) use ($order) {
104105
$this->handleEligibilityType($eligibilityType, $order);
105106
});
106107
}
107108

108109
/**
109-
* Handle the specified eligibility type.
110-
*
111110
* @param string $eligibilityType
112111
* @param Order $order
113112
* @return void
114-
* @throws \InvalidArgumentException
115113
*/
116114
protected function handleEligibilityType($eligibilityType, $order)
117115
{
118116
switch ($eligibilityType) {
119-
case static::ELIGIBILITY_TYPE_ELIGIBLE:
117+
case self::ELIGIBILITY_TYPE_ELIGIBLE:
120118
$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.'
123121
);
124122

125123
$status = $this->configProviderPaypal->getSellersProtectionEligible();
126124
break;
127-
case static::ELIGIBILITY_TYPE_ITEM_NOT_RECEIVED:
125+
case self::ELIGIBILITY_TYPE_ITEM_NOT_RECEIVED:
128126
$comment = __('Merchant is protected by Paypal Seller Protection Policy for Item Not Received.');
129127

130128
$status = $this->configProviderPaypal->getSellersProtectionItemnotreceivedEligible();
131129
break;
132-
case static::ELIGIBILITY_TYPE_UNAUTHORIZED_PAYMENT:
130+
case self::ELIGIBILITY_TYPE_UNAUTHORIZED_PAYMENT:
133131
$comment = __('Merchant is protected by Paypal Seller Protection Policy for Unauthorized Payment.');
134132

135133
$status = $this->configProviderPaypal->getSellersProtectionUnauthorizedpaymentEligible();
136134
break;
137-
case static::ELIGIBILITY_TYPE_NONE:
135+
case self::ELIGIBILITY_TYPE_NONE:
138136
$comment = __('Merchant is not protected under the Seller Protection Policy.');
139137

140138
$status = $this->configProviderPaypal->getSellersProtectionIneligible();
141139
break;
142140
default:
143141
throw new \InvalidArgumentException('Invalid eligibility type(s): ' . $eligibilityType);
144-
//phpcs:ignore:Squiz.PHP.NonExecutableCode
145-
break;
146142
}
143+
147144
$order->addCommentToStatusHistory($comment, $status ?: false);
148145
}
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+
}
149165
}
166+
167+
168+

etc/di.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@
142142
<plugin name="myParcelNLBuckarooPlugin" type="Buckaroo\Magento2\Plugin\MyParcelNLBuckarooPlugin" />
143143
</type>
144144

145-
<type name="Buckaroo\Magento2\Model\Push">
146-
<plugin name="buckaroo_magento2_paypal_sellersprotection_push" type="Buckaroo\Magento2\Model\Service\Plugin\PaypalSellersProtection\Push" />
147-
<plugin name="buckaroo_magento2_mpi_push" type="Buckaroo\Magento2\Model\Service\Plugin\Mpi\Push" />
145+
<type name="Buckaroo\Magento2\Model\Push\DefaultProcessor">
146+
<plugin name="buckaroo_magento2_mpi_push" type="Buckaroo\Magento2\Plugin\DefaultProcessorPlugin" />
147+
</type>
148+
<type name="Buckaroo\Magento2\Model\Push\PaypalProcessor">
149+
<plugin name="buckaroo_magento2_paypal_sellersprotection_push"
150+
type="Buckaroo\Magento2\Plugin\PaypalProcessorPlugin" />
148151
</type>
149152
<!-- END PLUGINS-->
150153

0 commit comments

Comments
 (0)