Skip to content

Commit ad0a4ab

Browse files
committed
Code Sniffer and Varnish fixes
1 parent c707094 commit ad0a4ab

4 files changed

Lines changed: 111 additions & 66 deletions

File tree

Controller/Payment/AbstractPaystackStandard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function __construct(
105105

106106
$this->paystack = $this->initPaystackPHP();
107107

108-
108+
109109
parent::__construct($context);
110110
}
111111

Controller/Payment/Webhook.php

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,86 +22,79 @@
2222

2323
namespace Pstk\Paystack\Controller\Payment;
2424

25-
use Magento\Sales\Model\Order;
26-
use Magento\Framework\App\CsrfAwareActionInterface;
27-
28-
class Webhook extends AbstractPaystackStandard implements CsrfAwareActionInterface
25+
class Webhook extends AbstractPaystackStandard
2926
{
3027

3128
public function execute() {
32-
33-
// Retrieve the request's body and parse it as JSON
34-
$event = \Yabacon\Paystack\Event::capture();
35-
http_response_code(200);
29+
$finalMessage = "failed";
3630

3731
$resultFactory = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
32+
try {
3833

39-
/* It is a important to log all events received. Add code *
40-
* here to log the signature and body to db or file */
41-
$this->logger->debug("PAYSTACK_LOG: {$event->raw}");
34+
// Retrieve the request's body and parse it as JSON
35+
$event = \Yabacon\Paystack\Event::capture();
36+
http_response_code(200);
37+
38+
/* It is a important to log all events received. Add code *
39+
* here to log the signature and body to db or file */
40+
$this->logger->debug("PAYSTACK_LOG: {$event->raw}");
4241

43-
/* Verify that the signature matches one of your keys */
44-
$secretKey = $this->configProvider->getSecretKeyArray();
45-
$owner = $event->discoverOwner($secretKey);
46-
47-
if (!$owner) {
48-
// None of the keys matched the event's signature
49-
$resultFactory->setContents("auth failed");
50-
return $resultFactory;
51-
}
42+
/* Verify that the signature matches one of your keys */
43+
$secretKey = $this->configProvider->getSecretKeyArray();
44+
$owner = $event->discoverOwner($secretKey);
45+
46+
if (!$owner) {
47+
// None of the keys matched the event's signature
48+
$resultFactory->setContents("auth failed");
49+
return $resultFactory;
50+
}
51+
52+
// Do something with $event->obj
53+
// Give value to your customer but don't give any output
54+
// Remember that this is a call from Paystack's servers and
55+
// Your customer is not seeing the response here at all
56+
switch ($event->obj->event) {
57+
// charge.success
58+
case 'charge.success':
59+
if ('success' === $event->obj->data->status) {
60+
$transactionDetails = $this->paystack->transaction->verify([
61+
'reference' => $event->obj->data->reference
62+
]);
63+
64+
$reference = $transactionDetails->data->reference;
5265

53-
// Do something with $event->obj
54-
// Give value to your customer but don't give any output
55-
// Remember that this is a call from Paystack's servers and
56-
// Your customer is not seeing the response here at all
57-
switch ($event->obj->event) {
58-
// charge.success
59-
case 'charge.success':
60-
if ('success' === $event->obj->data->status) {
61-
$transactionDetails = $this->paystack->transaction->verify([
62-
'reference' => $event->obj->data->reference
63-
]);
66+
$order = $this->orderInterface->loadByIncrementId($reference);
6467

65-
$reference = $transactionDetails->data->reference;
68+
//if is popup mode, reference is generated by Paystack and we provided quoteId instead
69+
if((!$order || !$order->getId()) && isset($event->obj->data->metadata->quoteId)){
70+
71+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
72+
$searchCriteriaBuilder = $objectManager->create('Magento\Framework\Api\SearchCriteriaBuilder');
73+
$searchCriteria = $searchCriteriaBuilder->addFilter('quote_id', $event->obj->data->metadata->quoteId, 'eq')->create();
74+
$items = $this->orderRepository->getList($searchCriteria);
75+
if($items->getTotalCount() == 1){
76+
$order = $items->getFirstItem();
77+
}
6678

67-
$order = $this->orderInterface->loadByIncrementId($reference);
68-
69-
//if is popup mode, reference is generated by Paystack and we provided quoteId instead
70-
if((!$order || !$order->getId()) && isset($event->obj->data->metadata->quoteId)){
71-
72-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
73-
$searchCriteriaBuilder = $objectManager->create('Magento\Framework\Api\SearchCriteriaBuilder');
74-
$searchCriteria = $searchCriteriaBuilder->addFilter('quote_id', $event->obj->data->metadata->quoteId, 'eq')->create();
75-
$items = $this->orderRepository->getList($searchCriteria);
76-
if($items->getTotalCount() == 1){
77-
$order = $items->getFirstItem();
7879
}
79-
80-
}
8180

82-
if ($order && $order->getId()) {
83-
// dispatch the `payment_verify_after` event to update the order status
84-
$this->eventManager->dispatch('paystack_payment_verify_after', [
85-
"paystack_order" => $order,
86-
]);
87-
88-
$resultFactory->setContents("success");
89-
return $resultFactory;
81+
if ($order && $order->getId()) {
82+
// dispatch the `payment_verify_after` event to update the order status
83+
$this->eventManager->dispatch('paystack_payment_verify_after', [
84+
"paystack_order" => $order,
85+
]);
86+
87+
$resultFactory->setContents("success");
88+
return $resultFactory;
89+
}
9090
}
91-
}
92-
break;
91+
break;
92+
}
93+
} catch (Exception $exc) {
94+
$finalMessage = $exc->getMessage();
9395
}
9496

95-
$resultFactory->setContents("failed");
97+
$resultFactory->setContents($finalMessage);
9698
return $resultFactory;
9799
}
98-
99-
public function createCsrfValidationException(\Magento\Framework\App\RequestInterface $request) {
100-
return null;
101-
}
102-
103-
public function validateForCsrf(\Magento\Framework\App\RequestInterface $request) {
104-
return true;
105-
}
106-
107100
}

Plugin/CsrfValidatorSkip.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Paystack Magento2 Module using \Magento\Payment\Model\Method\AbstractMethod
5+
* Copyright (C) 2019 Paystack.com
6+
*
7+
* This file is part of Pstk/Paystack.
8+
*
9+
* Pstk/Paystack is free software => you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http =>//www.gnu.org/licenses/>.
21+
*/
22+
23+
namespace Pstk\Paystack\Plugin;
24+
25+
/**
26+
* Description of CsrfValidatorSkip
27+
*
28+
* @author Olayode Ezekiel <kielsoft@gmail.com>
29+
*/
30+
class CsrfValidatorSkip {
31+
/**
32+
* @param \Magento\Framework\App\Request\CsrfValidator $subject
33+
* @param \Closure $proceed
34+
* @param \Magento\Framework\App\RequestInterface $request
35+
* @param \Magento\Framework\App\ActionInterface $action
36+
*/
37+
public function aroundValidate(
38+
$subject,
39+
\Closure $proceed,
40+
$request,
41+
$action
42+
) {
43+
if ("{$request->getModuleName()}/{$request->getActionName()}" == 'paystack/webhook') {
44+
return; // Skip CSRF check
45+
}
46+
$proceed($request, $action); // Proceed Magento 2 core functionalities
47+
}
48+
49+
}

etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
</argument>
88
</arguments>
99
</type>
10+
<type name="Magento\Framework\App\Request\CsrfValidator">
11+
<plugin name="csrf_validator_skip" type="Pstk\Paystack\Plugin\CsrfValidatorSkip" />
12+
</type>
1013
</config>

0 commit comments

Comments
 (0)