|
22 | 22 |
|
23 | 23 | namespace Pstk\Paystack\Controller\Payment; |
24 | 24 |
|
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 |
29 | 26 | { |
30 | 27 |
|
31 | 28 | public function execute() { |
| 29 | + $finalMessage = "failed"; |
| 30 | + |
| 31 | + $resultFactory = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW); |
| 32 | + try { |
32 | 33 |
|
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}"); |
36 | 41 |
|
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); |
40 | 45 |
|
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); |
49 | 67 |
|
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)){ |
61 | 70 |
|
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 | + } |
63 | 78 |
|
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 | 79 | } |
76 | | - |
77 | | - } |
78 | 80 |
|
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 | + } |
85 | 90 | } |
86 | | - } |
87 | | - break; |
| 91 | + break; |
| 92 | + } |
| 93 | + } catch (Exception $exc) { |
| 94 | + $finalMessage = $exc->getMessage(); |
88 | 95 | } |
89 | 96 |
|
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; |
95 | 99 | } |
96 | | - |
97 | | - public function validateForCsrf(\Magento\Framework\App\RequestInterface $request): ?bool { |
98 | | - return true; |
99 | | - } |
100 | | - |
101 | 100 | } |
0 commit comments