@@ -61,13 +61,37 @@ public function initContent()
6161 return ;
6262 }
6363
64- $ transaction = $ this ->getTransactionFromRequest ();
64+ $ input = Tools::getValue ('paymentData ' , '' );
65+
66+ if (is_string ($ input )) {
67+ $ input = json_decode ($ input , true );
68+ }
69+
6570 $ cart = $ this ->context ->cart ;
6671
72+ if (empty ($ input ['orderID ' ]) || !$ this ->validateOrderID ($ input ['orderID ' ])) {
73+ return $ this ->redirectToErrorPage (new Exception ('Payment data is not valid. ' ));
74+ }
75+ if (false === Validate::isLoadedObject ($ cart )) {
76+ return $ this ->redirectToErrorPage (new Exception ('Cart is not valid. ' ));
77+ }
78+
79+ $ response = $ this ->client ->execute (new OrdersCaptureRequest ($ input ['orderID ' ]));
80+
81+ if ($ response ->getCode () > 299 || $ response ->getCode () < 200 ) {
82+ return $ this ->redirectToErrorPage (new Exception ('Capture is failed. ' ));
83+ }
84+
85+ if ($ response instanceof HttpJsonResponse) {
86+ $ transaction = $ this ->preapreTransaction ($ response ->toArray ());
87+ } else {
88+ $ transaction = $ this ->preapreTransaction ([]);
89+ }
90+
6791 try {
6892 $ this ->module ->validateOrder (
6993 $ cart ->id ,
70- $ this ->getIdOrderState (),
94+ $ this ->getIdOrderState ($ transaction -> getPaymentStatus () ),
7195 $ transaction ->getTotalPaid (),
7296 $ this ->module ->displayName ,
7397 $ this ->module ->l ('Payment accepted. ' ),
@@ -91,16 +115,14 @@ public function initContent()
91115 Tools::redirect ('index.php? ' . http_build_query ($ queryParams ));
92116 }
93117
94- protected function getTransactionFromRequest ( )
118+ protected function preapreTransaction ( $ paymentData )
95119 {
96120 $ transaction = new Transaction ();
97121
98- if (empty (Tools:: getValue ( ' paymentData ' ) )) {
122+ if (empty ($ paymentData )) {
99123 return $ transaction ;
100124 }
101125
102- $ paymentData = json_decode (Tools::getValue ('paymentData ' ), true );
103-
104126 if (false === empty ($ paymentData ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['id ' ])) {
105127 $ transaction ->setIdTransaction ($ paymentData ['purchase_units ' ][0 ]['payments ' ]['captures ' ][0 ]['id ' ]);
106128 }
@@ -128,9 +150,13 @@ protected function getTransactionFromRequest()
128150 return $ transaction ;
129151 }
130152
131- protected function getIdOrderState ()
153+ protected function getIdOrderState ($ paymentStatus = PayPal:: CAPTURE_STATUS_COMPLETED )
132154 {
133- return (int ) Configuration::get ('PS_OS_PAYMENT ' );
155+ if ($ paymentStatus === PayPal::CAPTURE_STATUS_COMPLETED ) {
156+ return (int ) Configuration::get ('PS_OS_PAYMENT ' );
157+ }
158+
159+ return (int ) Configuration::get ('PS_OS_PAYPAL ' );
134160 }
135161
136162 protected function redirectToErrorPage ($ e )
@@ -172,39 +198,13 @@ public function displayAjaxCreateOrder()
172198 die (json_encode ($ return ));
173199 }
174200
175- public function displayAjaxCaptureOrder ( )
201+ protected function validateOrderID ( $ orderID )
176202 {
177- $ order = json_decode (Tools::getValue ('order ' ), true );
178- $ return = [
179- 'success ' => false ,
180- ];
181-
182- if ($ this ->validateOrderID ($ order )) {
183- $ response = $ this ->client ->execute (new OrdersCaptureRequest ($ order ['orderID ' ]));
184-
185- if ($ response ->getCode () < 300 && $ response ->getCode () > 199 ) {
186- $ return ['success ' ] = true ;
187-
188- if ($ response instanceof HttpJsonResponse) {
189- $ return = array_merge ($ return , $ response ->toArray ());
190- }
191- }
192- }
193-
194- die (json_encode ($ return ));
195- }
196-
197- protected function validateOrderID ($ order )
198- {
199- if (empty ($ order ['orderID ' ])) {
200- return false ;
201- }
202-
203- if (Validate::isCleanHtml ($ order ['orderID ' ]) === false ) {
203+ if (Validate::isCleanHtml ($ orderID ) === false ) {
204204 return false ;
205205 }
206206
207- if (mb_strlen ($ order [ ' orderID ' ] ) > 36 ) {
207+ if (mb_strlen ($ orderID ) > 36 ) {
208208 return false ;
209209 }
210210
0 commit comments