|
| 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\Controller\Payment; |
| 24 | + |
| 25 | +use Magento\Sales\Model\Order; |
| 26 | +use Magento\Framework\App\CsrfAwareActionInterface; |
| 27 | + |
| 28 | +class Webhook extends AbstractPaystackStandard implements CsrfAwareActionInterface |
| 29 | +{ |
| 30 | + |
| 31 | + 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); |
| 36 | + |
| 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}"); |
| 40 | + |
| 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 | + } |
| 49 | + |
| 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 | + ]); |
| 61 | + |
| 62 | + $reference = $transactionDetails->data->reference; |
| 63 | + |
| 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(); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 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"); |
| 85 | + } |
| 86 | + } |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + die("failed"); |
| 91 | + } |
| 92 | + |
| 93 | + public function createCsrfValidationException(\Magento\Framework\App\RequestInterface $request): ?\Magento\Framework\App\Request\InvalidRequestException { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + public function validateForCsrf(\Magento\Framework\App\RequestInterface $request): ?bool { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments