Skip to content

Commit 1e993a6

Browse files
authored
Merge pull request #44 from kielsoft/master
Cleanup for marketplace submission
2 parents e1054de + ad0a4ab commit 1e993a6

7 files changed

Lines changed: 125 additions & 65 deletions

File tree

Controller/Payment/AbstractPaystackStandard.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ abstract class AbstractPaystackStandard extends \Magento\Framework\App\Action\Ac
6666
* @var \Psr\Log\LoggerInterface
6767
*/
6868
protected $logger;
69+
70+
/**
71+
*
72+
* @var \Magento\Framework\App\Request\Http
73+
*/
74+
protected $request;
6975

7076
/**
7177
* Constructor
@@ -83,6 +89,7 @@ public function __construct(
8389
\Magento\Framework\Message\ManagerInterface $messageManager,
8490
\Pstk\Paystack\Model\Ui\ConfigProvider $configProvider,
8591
\Magento\Framework\Event\Manager $eventManager,
92+
\Magento\Framework\App\Request\Http $request,
8693
\Psr\Log\LoggerInterface $logger
8794
) {
8895
$this->resultPageFactory = $resultPageFactory;
@@ -93,11 +100,12 @@ public function __construct(
93100
$this->messageManager = $messageManager;
94101
$this->configProvider = $configProvider;
95102
$this->eventManager = $eventManager;
103+
$this->request = $request;
96104
$this->logger = $logger;
97105

98106
$this->paystack = $this->initPaystackPHP();
99107

100-
108+
101109
parent::__construct($context);
102110
}
103111

Controller/Payment/Callback.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Callback extends AbstractPaystackStandard {
3131
*/
3232
public function execute() {
3333

34-
$reference = isset($_GET['reference']) ? $_GET['reference'] : '';
34+
$reference = $this->request->get('reference');
3535
$message = "";
3636

3737
if (!$reference) {
@@ -44,7 +44,7 @@ public function execute() {
4444
]);
4545

4646
$reference = explode('_', $transactionDetails->data->reference, 2);
47-
$reference = @$reference[0];
47+
$reference = ($reference[0])?: 0;
4848

4949
$order = $this->orderInterface->loadByIncrementId($reference);
5050

Controller/Payment/Webhook.php

Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,80 +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() {
29+
$finalMessage = "failed";
30+
31+
$resultFactory = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
32+
try {
3233

33-
// Retrieve the request's body and parse it as JSON
34-
$event = \Yabacon\Paystack\Event::capture();
35-
http_response_code(200);
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}");
3641

37-
/* It is a important to log all events received. Add code *
38-
* here to log the signature and body to db or file */
39-
$this->logger->debug("PAYSTACK_LOG: {$event->raw}");
42+
/* Verify that the signature matches one of your keys */
43+
$secretKey = $this->configProvider->getSecretKeyArray();
44+
$owner = $event->discoverOwner($secretKey);
4045

41-
/* Verify that the signature matches one of your keys */
42-
$secretKey = $this->configProvider->getSecretKeyArray();
43-
$owner = $event->discoverOwner($secretKey);
44-
45-
if (!$owner) {
46-
// None of the keys matched the event's signature
47-
die("auth failed");
48-
}
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;
65+
66+
$order = $this->orderInterface->loadByIncrementId($reference);
4967

50-
// Do something with $event->obj
51-
// Give value to your customer but don't give any output
52-
// Remember that this is a call from Paystack's servers and
53-
// Your customer is not seeing the response here at all
54-
switch ($event->obj->event) {
55-
// charge.success
56-
case 'charge.success':
57-
if ('success' === $event->obj->data->status) {
58-
$transactionDetails = $this->paystack->transaction->verify([
59-
'reference' => $event->obj->data->reference
60-
]);
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)){
6170

62-
$reference = $transactionDetails->data->reference;
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+
}
6378

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

79-
if ($order && $order->getId()) {
80-
// dispatch the `payment_verify_after` event to update the order status
81-
$this->eventManager->dispatch('paystack_payment_verify_after', [
82-
"paystack_order" => $order,
83-
]);
84-
die("success");
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+
}
8590
}
86-
}
87-
break;
91+
break;
92+
}
93+
} catch (Exception $exc) {
94+
$finalMessage = $exc->getMessage();
8895
}
8996

90-
die("failed");
91-
}
92-
93-
public function createCsrfValidationException(\Magento\Framework\App\RequestInterface $request): ?\Magento\Framework\App\Request\InvalidRequestException {
94-
return null;
97+
$resultFactory->setContents($finalMessage);
98+
return $resultFactory;
9599
}
96-
97-
public function validateForCsrf(\Magento\Framework\App\RequestInterface $request): ?bool {
98-
return true;
99-
}
100-
101100
}

Model/Ui/ConfigProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Class ConfigProvider
1010
*/
11-
final class ConfigProvider implements ConfigProviderInterface
11+
class ConfigProvider implements ConfigProviderInterface
1212
{
1313

1414
protected $method;

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+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "pstk/paystack-magento2-module",
33
"description": "Paystack Magento2 Module using \\Magento\\Payment\\Model\\Method\\AbstractMethod",
4+
"version": "2.3.5",
45
"require": {
56
"yabacon/paystack-php": "2.*"
67
},

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)