2222
2323namespace Pstk \Paystack \Controller \Payment ;
2424
25+
2526use Magento \Sales \Model \Order ;
2627use Magento \Framework \App \CsrfAwareActionInterface ;
2728
2829include_once realpath (dirname (__FILE__ )) . '/class-paystack-plugin-tracker.php ' ;
2930class 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}
0 commit comments