@@ -54,54 +54,7 @@ public function execute()
54
54
try {
55
55
$ post = $ this ->getRequest ()->getContent ();
56
56
$ callbackToken = $ this ->getRequest ()->getHeader ('X-CALLBACK-TOKEN ' );
57
- $ decodedPost = json_decode ($ post , true );
58
- $ isEwallet = false ;
59
-
60
- if (!empty ($ decodedPost ['ewallet_type ' ])) {
61
- $ isEwallet = true ;
62
-
63
- if (!$ decodedPost ['external_id ' ]) {
64
- $ result = $ this ->jsonResultFactory ->create ();
65
- /** You may introduce your own constants for this custom REST API */
66
- $ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
67
- $ result ->setData ([
68
- 'status ' => __ ('ERROR ' ),
69
- 'message ' => 'Callback external_id is invalid '
70
- ]);
71
-
72
- return $ result ;
73
- }
74
- } else if (!isset ($ decodedPost ['description ' ]) || !isset ($ decodedPost ['id ' ])) {
75
- $ result = $ this ->jsonResultFactory ->create ();
76
- /** You may introduce your own constants for this custom REST API */
77
- $ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
78
- $ result ->setData ([
79
- 'status ' => __ ('ERROR ' ),
80
- 'message ' => 'Callback body is invalid '
81
- ]);
82
-
83
- return $ result ;
84
- }
85
-
86
- if ($ isEwallet ) {
87
- // default code if API doesn't send failure_code
88
- $ failureCode = 'UNKNOWN_ERROR ' ;
89
- if (isset ($ decodedPost ['failure_code ' ])) {
90
- $ failureCode = $ decodedPost ['failure_code ' ];
91
- }
92
-
93
- $ extIdPrefix = $ this ->dataHelper ->getExternalIdPrefix ();
94
- $ orderId = ltrim ($ decodedPost ['external_id ' ], $ extIdPrefix );
95
-
96
- // standalone OVO can only be single checkout
97
- $ orderIds = [$ orderId ];
98
- } else {
99
- $ orderId = $ decodedPost ['description ' ];
100
- $ orderIds = explode ("- " , $ orderId );
101
- }
102
-
103
- $ transactionId = $ decodedPost ['id ' ];
104
- $ isMultishipping = (count ($ orderIds ) > 1 ) ? true : false ;
57
+ $ callbackPayload = json_decode ($ post , true );
105
58
106
59
if (!empty ($ callbackToken )) {
107
60
$ result = $ this ->jsonResultFactory ->create ();
@@ -115,20 +68,14 @@ public function execute()
115
68
return $ result ;
116
69
}
117
70
118
- $ invoice = $ this ->getXenditInvoice ($ transactionId );
119
-
120
- if ( $ isMultishipping ) {
121
- foreach ($ orderIds as $ key => $ value ) {
122
- $ result = $ this ->checkOrder ($ value , $ isEwallet , $ decodedPost , $ invoice , $ orderId );
123
- }
124
-
125
- return $ result ;
71
+ if (!empty ($ callbackPayload ['invoice_url ' ])) {
72
+ return $ this ->handleInvoiceCallback ($ callbackPayload );
126
73
} else {
127
- return $ this ->checkOrder ( $ orderId , $ isEwallet , $ decodedPost , $ invoice , $ orderId );
74
+ return $ this ->handleEwalletCallback ( $ callbackPayload );
128
75
}
129
76
} catch (\Exception $ e ) {
130
- $ message = "Error invoice callback " . $ e ->getMessage ();
131
- $ this ->logDNA ->log (LogDNALevel::ERROR , $ message , $ decodedPost );
77
+ $ message = "Error invoice callback: " . $ e ->getMessage ();
78
+ $ this ->logDNA ->log (LogDNALevel::ERROR , $ message , $ callbackPayload );
132
79
133
80
$ result = $ this ->jsonResultFactory ->create ();
134
81
/** You may introduce your own constants for this custom REST API */
@@ -141,11 +88,79 @@ public function execute()
141
88
return $ result ;
142
89
}
143
90
}
144
-
145
- private function checkOrder ($ orderId , $ isEwallet , $ decodedPost , $ invoice , $ callbackDescription ) {
146
- $ order = $ this ->orderFactory ->create ();
147
- $ order ->load ($ orderId );
148
- $ transactionId = $ decodedPost ['id ' ];
91
+
92
+ public function handleInvoiceCallback ($ callbackPayload ) {
93
+ if (!isset ($ callbackPayload ['description ' ]) || !isset ($ callbackPayload ['id ' ])) {
94
+ $ result = $ this ->jsonResultFactory ->create ();
95
+ /** You may introduce your own constants for this custom REST API */
96
+ $ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
97
+ $ result ->setData ([
98
+ 'status ' => __ ('ERROR ' ),
99
+ 'message ' => 'Callback body is invalid '
100
+ ]);
101
+
102
+ return $ result ;
103
+ }
104
+
105
+ // Invoice description is Magento's order ID
106
+ $ description = $ callbackPayload ['description ' ];
107
+ // in case of multishipping, we separate order IDs with `-`
108
+ $ orderIds = explode ("- " , $ description );
109
+
110
+ $ transactionId = $ callbackPayload ['id ' ];
111
+ $ isMultishipping = (count ($ orderIds ) > 1 ) ? true : false ;
112
+
113
+ $ invoice = $ this ->getXenditInvoice ($ transactionId );
114
+
115
+ if ( $ isMultishipping ) {
116
+ foreach ($ orderIds as $ key => $ value ) {
117
+ $ order = $ this ->orderFactory ->create ();
118
+ $ order ->load ($ value );
119
+
120
+ $ result = $ this ->checkOrder ($ order , false , $ callbackPayload , $ invoice , $ description );
121
+ }
122
+
123
+ return $ result ;
124
+ } else {
125
+ $ order = $ this ->getOrderById ($ description );
126
+
127
+ if (!$ order ) {
128
+ $ order = $ this ->orderFactory ->create ();
129
+ $ order ->load ($ description );
130
+ }
131
+
132
+ return $ this ->checkOrder ($ order , false , $ callbackPayload , $ invoice , $ description );
133
+ }
134
+ }
135
+
136
+ public function handleEwalletCallback ($ callbackPayload ) {
137
+ if (!$ callbackPayload ['external_id ' ]) {
138
+ $ result = $ this ->jsonResultFactory ->create ();
139
+ /** You may introduce your own constants for this custom REST API */
140
+ $ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
141
+ $ result ->setData ([
142
+ 'status ' => __ ('ERROR ' ),
143
+ 'message ' => 'Callback external_id is invalid '
144
+ ]);
145
+
146
+ return $ result ;
147
+ }
148
+
149
+ $ failureCode = 'UNKNOWN_ERROR ' ;
150
+ if (isset ($ callbackPayload ['failure_code ' ])) {
151
+ $ failureCode = $ callbackPayload ['failure_code ' ];
152
+ }
153
+
154
+ $ extIdPrefix = $ this ->dataHelper ->getExternalIdPrefix ();
155
+ // Trimmed external ID from prefix is Magento's order ID
156
+ $ orderId = ltrim ($ callbackPayload ['external_id ' ], $ extIdPrefix );
157
+ $ order = $ this ->getOrderById ($ orderId );
158
+
159
+ return $ this ->checkOrder ($ order , true , $ callbackPayload , null , $ orderId );
160
+ }
161
+
162
+ private function checkOrder ($ order , $ isEwallet , $ callbackPayload , $ invoice , $ callbackDescription ) {
163
+ $ transactionId = $ callbackPayload ['id ' ];
149
164
150
165
if (!$ order ) {
151
166
$ result = $ this ->jsonResultFactory ->create ();
@@ -159,26 +174,22 @@ private function checkOrder($orderId, $isEwallet, $decodedPost, $invoice, $callb
159
174
return $ result ;
160
175
}
161
176
162
- if ($ isEwallet ) {
163
- $ order = $ this ->getOrderById ($ orderId );
177
+ if (!$ order ->canInvoice ()) {
178
+ $ result = $ this ->jsonResultFactory ->create ();
179
+ $ result ->setData ([
180
+ 'status ' => __ ('SUCCESS ' ),
181
+ 'message ' => 'Order is already processed '
182
+ ]);
164
183
165
- if ($ order ->getState () === Order::STATE_PENDING_PAYMENT || $ order ->getState () === Order::STATE_PAYMENT_REVIEW ) {
166
- //get ewallet payment status
167
- $ paymentStatus = $ this ->getEwalletStatus ($ decodedPost ['ewallet_type ' ], $ decodedPost ['external_id ' ]);
168
- } else {
169
- $ result = $ this ->jsonResultFactory ->create ();
170
- $ result ->setData ([
171
- 'status ' => __ ('SUCCESS ' ),
172
- 'message ' => 'eWallet transaction has been completed successfully '
173
- ]);
184
+ return $ result ;
185
+ }
174
186
175
- return $ result ;
176
- }
187
+ if ( $ isEwallet ) {
188
+ $ paymentStatus = $ this -> getEwalletStatus ( $ callbackPayload [ ' ewallet_type ' ], $ callbackPayload [ ' external_id ' ]);
177
189
} else {
178
190
$ paymentStatus = $ invoice ['status ' ];
179
- $ invoiceOrderId = $ invoice ['description ' ];
180
191
181
- if ($ invoiceOrderId !== $ callbackDescription ) {
192
+ if ($ invoice [ ' description ' ] !== $ callbackDescription ) {
182
193
$ result = $ this ->jsonResultFactory ->create ();
183
194
/** You may introduce your own constants for this custom REST API */
184
195
$ result ->setHttpResponseCode (\Magento \Framework \Webapi \Exception::HTTP_BAD_REQUEST );
0 commit comments