3232class Atos extends AbstractPaymentModule
3333{
3434 const MODULE_DOMAIN = 'atos ' ;
35-
35+
3636 /**
3737 * The confirmation message identifier
3838 */
3939 const CONFIRMATION_MESSAGE_NAME = 'atos_payment_confirmation ' ;
40-
40+
4141 private $ parameters ;
42-
42+
4343 public function postActivation (ConnectionInterface $ con = null )
4444 {
4545 // Setup some default values
@@ -49,30 +49,28 @@ public function postActivation(ConnectionInterface $con = null)
4949 Atos::setConfigValue ('maximum_amount ' , 0 );
5050 Atos::setConfigValue ('send_payment_confirmation_message ' , 1 );
5151 }
52-
52+
5353 // Try to chmod binaries if they're not executables
5454 $ binFile = Atos::getBinDirectory () . 'request ' ;
5555 if (! is_executable ($ binFile )) {
5656 @chmod ($ binFile , 0755 );
5757 }
58-
58+
5959 $ binFile = Atos::getBinDirectory () . 'response ' ;
6060 if (! is_executable ($ binFile )) {
6161 @chmod ($ binFile , 0755 );
6262 }
63-
63+
6464 $ database = new Database ($ con );
65-
65+
6666 $ database ->insertSql (null , array (
6767 __DIR__ . DS . 'Config ' .DS .'thelia.sql '
6868 ));
69-
69+
7070 // Create payment confirmation message from templates, if not already defined
71- $ email_templates_dir = __DIR__ .DS .'I18n ' .DS .'email-templates ' .DS ;
72-
7371 if (null === MessageQuery::create ()->findOneByName (Atos::CONFIRMATION_MESSAGE_NAME )) {
7472 $ message = new Message ();
75-
73+
7674 $ message
7775 ->setName (Atos::CONFIRMATION_MESSAGE_NAME )
7876 ->setHtmlTemplateFileName ('atos-payment-confirmation.html ' )
@@ -86,10 +84,10 @@ public function postActivation(ConnectionInterface $con = null)
8684 ->save ()
8785 ;
8886 }
89-
87+
9088 $ this ->replacePath ();
9189 }
92-
90+
9391 public function update ($ currentVersion , $ newVersion , ConnectionInterface $ con = null )
9492 {
9593 // Migrate old configuration
@@ -98,35 +96,35 @@ public function update($currentVersion, $newVersion, ConnectionInterface $con =
9896 /** @var Config $atosConfig */
9997 foreach ($ atosConfigs as $ atosConfig ) {
10098 Atos::setConfigValue ($ atosConfig ->getName (), $ atosConfig ->getValue ());
101-
99+
102100 $ atosConfig ->delete ($ con );
103101 }
104102 }
105103 }
106-
104+
107105 parent ::update ($ currentVersion , $ newVersion , $ con );
108106 }
109-
107+
110108 public function destroy (ConnectionInterface $ con = null , $ deleteModuleData = false )
111109 {
112110 if ($ deleteModuleData ) {
113111 $ database = new Database ($ con );
114-
112+
115113 $ database ->execute ('drop table `atos_currency` ' );
116-
114+
117115 MessageQuery::create ()->findOneByName (Atos::CONFIRMATION_MESSAGE_NAME )->delete ();
118116 }
119117 }
120-
118+
121119 protected function replacePath ()
122120 {
123121 $ pathfile = $ this ->getPathfilePath ();
124-
122+
125123 $ pathfileContent = @file_get_contents ($ pathfile . '.dist ' );
126-
124+
127125 if ($ pathfileContent ) {
128126 $ pathfileContent = str_replace ('__PATH__ ' , __DIR__ , $ pathfileContent );
129-
127+
130128 if (! file_put_contents ($ this ->getConfigDirectory () . 'pathfile ' , $ pathfileContent )) {
131129 throw new \RuntimeException (
132130 Translator::getInstance ()->trans (
@@ -146,7 +144,7 @@ protected function replacePath()
146144 );
147145 }
148146 }
149-
147+
150148 /**
151149 * @param string $key atos key parameter
152150 * @param string $value parameter value
@@ -155,15 +153,15 @@ protected function replacePath()
155153 protected function addParam ($ key , $ value )
156154 {
157155 $ this ->parameters = sprintf ("%s %s=%s " , $ this ->parameters , $ key , $ value );
158-
156+
159157 return $ this ;
160158 }
161-
159+
162160 protected function getParameters ()
163161 {
164162 return trim ($ this ->parameters );
165163 }
166-
164+
167165 /**
168166 *
169167 * generate a transaction id for atos solution
@@ -173,18 +171,18 @@ protected function getParameters()
173171 private function generateTransactionID ()
174172 {
175173 $ transId = Atos::getConfigValue ('atos_transactionId ' , 1 );
176-
174+
177175 $ transId = 1 + intval ($ transId );
178-
176+
179177 if (strlen ($ transId ) > 6 ) {
180178 $ transId = 1 ;
181179 }
182-
180+
183181 Atos::setConfigValue ('atos_transactionId ' , $ transId );
184-
182+
185183 return sprintf ("%06d " , $ transId );
186184 }
187-
185+
188186 /**
189187 *
190188 * Method used by payment gateway.
@@ -201,11 +199,11 @@ private function generateTransactionID()
201199 public function pay (Order $ order )
202200 {
203201 $ pathBin = Atos::getBinDirectory () .'request ' ;
204-
202+
205203 $ atosCurrency = AtosCurrencyQuery::create ()->findPk (
206204 $ order ->getCurrency ()->getCode ()
207205 );
208-
206+
209207 if (null == $ atosCurrency ) {
210208 throw new \InvalidArgumentException (
211209 sprintf (
@@ -214,17 +212,17 @@ public function pay(Order $order)
214212 )
215213 );
216214 }
217-
215+
218216 $ amount = $ order ->getTotalAmount ();
219217 $ amount = number_format ($ amount , $ atosCurrency ->getDecimals (), '' , '' );
220-
218+
221219 $ transactionId = $ this ->generateTransactionID ();
222-
220+
223221 $ order ->setTransactionRef ($ transactionId )->save ();
224-
222+
225223 /** @var Router $router */
226224 $ router = $ this ->getContainer ()->get ('router.atos ' );
227-
225+
228226 $ this
229227 ->addParam ('pathfile ' , Atos::getPathfilePath ())
230228 ->addParam ('merchant_id ' , Atos::getConfigValue ('atos_merchantId ' ))
@@ -237,13 +235,15 @@ public function pay(Order $order)
237235 ->addParam ('automatic_response_url ' , URL ::getInstance ()->absoluteUrl ($ router ->generate ('atos.payment.confirmation ' )))
238236 ->addParam ('cancel_return_url ' , URL ::getInstance ()->absoluteUrl ($ router ->generate ('atos.payment.cancel ' , [ 'orderId ' => $ order ->getId () ])))
239237 ->addParam ('normal_return_url ' , $ this ->getPaymentSuccessPageUrl ($ order ->getId ()))
238+ // base64 encode the ref to prevent problems with unallowed characters
239+ ->addParam ('return_context ' , base64_encode ($ order ->getRef ()))
240240 ;
241-
241+
242242 $ encrypt = exec (sprintf ("%s %s " , $ pathBin , $ this ->getParameters ()));
243-
243+
244244 if (! empty ($ encrypt )) {
245245 $ datas = explode ('! ' , $ encrypt );
246-
246+
247247 if ($ datas [1 ] == '' && $ datas [2 ] == '' ) {
248248 throw new \RuntimeException (
249249 Translator::getInstance ()->trans ('Request binary not found in "%path" ' , ['%path ' => $ pathBin ])
@@ -253,19 +253,18 @@ public function pay(Order $order)
253253 } else {
254254 /** @var ParserInterface $parser */
255255 $ parser = $ this ->getContainer ()->get ('thelia.parser ' );
256-
256+
257257 $ parser ->setTemplateDefinition (
258- $ parser ->getTemplateHelper ()->getActiveFrontTemplate (),
259- true
258+ $ parser ->getTemplateHelper ()->getActiveFrontTemplate ()
260259 );
261-
260+
262261 $ content = $ parser ->render ('atos/payment.html ' , [
263262 'site_name ' => self ::getConfigValue ('store_name ' ),
264263 'form ' => $ datas [3 ],
265264 'order_id ' => $ order ->getId ()
266265 ]);
267-
268- return Response:: create ($ content );
266+
267+ return new Response ($ content );
269268 }
270269 } else {
271270 throw new \RuntimeException (
@@ -278,39 +277,39 @@ public function pay(Order $order)
278277 // FIXME : show something to the customer
279278 }
280279 }
281-
280+
282281 /**
283282 * @return boolean true to allow usage of this payment module, false otherwise.
284283 */
285284 public function isValidPayment ()
286285 {
287286 $ valid = false ;
288-
287+
289288 // Check config files
290289 $ parmcomFile = Atos::getConfigDirectory () . 'parmcom. ' . Atos::getConfigValue ('atos_merchantId ' , '0 ' );
291290 $ certifFile = Atos::getConfigDirectory () . 'certif.fr. ' . Atos::getConfigValue ('atos_merchantId ' , '0 ' );
292-
291+
293292 if (is_readable ($ parmcomFile ) && is_readable ($ certifFile )) {
294293 $ mode = Atos::getConfigValue ('atos_mode ' , false );
295-
294+
296295 // If we're in test mode, do not display Payzen on the front office, except for allowed IP addresses.
297296 if ('TEST ' == $ mode ) {
298297 $ raw_ips = explode ("\n" , Atos::getConfigValue ('atos_allowed_ip_list ' , '' ));
299-
298+
300299 $ allowed_client_ips = array ();
301-
300+
302301 foreach ($ raw_ips as $ ip ) {
303302 $ allowed_client_ips [] = trim ($ ip );
304303 }
305-
304+
306305 $ client_ip = $ this ->getRequest ()->getClientIp ();
307-
306+
308307 $ valid = in_array ($ client_ip , $ allowed_client_ips );
309-
308+
310309 } elseif ('PRODUCTION ' == $ mode ) {
311310 $ valid = true ;
312311 }
313-
312+
314313 if ($ valid ) {
315314 // Check if total order amount is in the module's limits
316315 $ valid = $ this ->checkMinMaxAmount ();
@@ -326,7 +325,7 @@ public function isValidPayment()
326325 }
327326 return $ valid ;
328327 }
329-
328+
330329 /**
331330 * Check if total order amount is in the module's limits
332331 *
@@ -336,26 +335,26 @@ protected function checkMinMaxAmount()
336335 {
337336 // Check if total order amount is in the module's limits
338337 $ order_total = $ this ->getCurrentOrderTotalAmount ();
339-
338+
340339 $ min_amount = Atos::getConfigValue ('atos_minimum_amount ' , 0 );
341340 $ max_amount = Atos::getConfigValue ('atos_maximum_amount ' , 0 );
342-
341+
343342 return
344343 $ order_total > 0
345344 &&
346345 ($ min_amount <= 0 || $ order_total >= $ min_amount ) && ($ max_amount <= 0 || $ order_total <= $ max_amount );
347346 }
348-
347+
349348 public static function getBinDirectory ()
350349 {
351350 return __DIR__ . DS . 'bin ' . DS ;
352351 }
353-
352+
354353 public static function getConfigDirectory ()
355354 {
356355 return __DIR__ . DS . 'Config ' . DS ;
357356 }
358-
357+
359358 public static function getPathfilePath ()
360359 {
361360 return Atos::getConfigDirectory () . 'pathfile ' ;
0 commit comments