Skip to content

Commit e2a8683

Browse files
authored
Merge branch 'master' into master
2 parents b86e815 + 1e993a6 commit e2a8683

7 files changed

Lines changed: 121 additions & 49 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: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,58 @@
2222

2323
namespace Pstk\Paystack\Controller\Payment;
2424

25+
2526
use Magento\Sales\Model\Order;
2627
use Magento\Framework\App\CsrfAwareActionInterface;
2728

2829
include_once realpath(dirname(__FILE__)) . '/class-paystack-plugin-tracker.php';
2930
class Webhook extends AbstractPaystackStandard implements CsrfAwareActionInterface
31+
3032
{
3133

3234
public function execute() {
35+
$finalMessage = "failed";
36+
37+
$resultFactory = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
38+
try {
3339

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

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}");
48+
/* Verify that the signature matches one of your keys */
49+
$secretKey = $this->configProvider->getSecretKeyArray();
50+
$owner = $event->discoverOwner($secretKey);
4151

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-
die("auth failed");
49-
}
52+
if (!$owner) {
53+
// None of the keys matched the event's signature
54+
$resultFactory->setContents("auth failed");
55+
return $resultFactory;
56+
}
57+
58+
// Do something with $event->obj
59+
// Give value to your customer but don't give any output
60+
// Remember that this is a call from Paystack's servers and
61+
// Your customer is not seeing the response here at all
62+
switch ($event->obj->event) {
63+
// charge.success
64+
case 'charge.success':
65+
if ('success' === $event->obj->data->status) {
66+
$transactionDetails = $this->paystack->transaction->verify([
67+
'reference' => $event->obj->data->reference
68+
]);
69+
70+
$reference = $transactionDetails->data->reference;
71+
72+
$order = $this->orderInterface->loadByIncrementId($reference);
73+
74+
//if is popup mode, reference is generated by Paystack and we provided quoteId instead
75+
if((!$order || !$order->getId()) && isset($event->obj->data->metadata->quoteId)){
5076

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

6378
$reference = $transactionDetails->data->reference;
6479
//PSTK LOGGER HERE
@@ -76,30 +91,26 @@ public function execute() {
7691
$items = $this->orderRepository->getList($searchCriteria);
7792
if($items->getTotalCount() == 1){
7893
$order = $items->getFirstItem();
94+
7995
}
80-
81-
}
8296

83-
if ($order && $order->getId()) {
84-
// dispatch the `payment_verify_after` event to update the order status
85-
$this->eventManager->dispatch('paystack_payment_verify_after', [
86-
"paystack_order" => $order,
87-
]);
88-
die("success");
97+
if ($order && $order->getId()) {
98+
// dispatch the `payment_verify_after` event to update the order status
99+
$this->eventManager->dispatch('paystack_payment_verify_after', [
100+
"paystack_order" => $order,
101+
]);
102+
103+
$resultFactory->setContents("success");
104+
return $resultFactory;
105+
}
89106
}
90-
}
91-
break;
107+
break;
108+
}
109+
} catch (Exception $exc) {
110+
$finalMessage = $exc->getMessage();
92111
}
93112

94-
die("failed");
113+
$resultFactory->setContents($finalMessage);
114+
return $resultFactory;
95115
}
96-
97-
public function createCsrfValidationException(\Magento\Framework\App\RequestInterface $request): ?\Magento\Framework\App\Request\InvalidRequestException {
98-
return null;
99-
}
100-
101-
public function validateForCsrf(\Magento\Framework\App\RequestInterface $request): ?bool {
102-
return true;
103-
}
104-
105116
}

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)