Jump to content

How to store an array of values in prestashop cookie ?


Nayana Chandran

Recommended Posts

 

are you doing this in php or in a template file?  using {$payment.'_response'} would suggest you are trying to use smarty for this

$this->context->cookie->{$payment.'_response'} = serialize($response);

Hi,

 

I am doing in my module.php file. 

 

Also tried     $this->context->cookie->__set($payment.'_response', serialize($response));

 

In both case, I can use the above cookie value with in the function, or within the class, After I redirect to any controller the the serialized cookie value is not getting (not even empty). If am set a single value to cookie, it will getting in controller.

 

I need to pass the entire array, I can I make it possible ? Suggest any idea ..

Link to comment
Share on other sites

well you can't do this in a module

$this->context->cookie->{$payment.'_response'} = serialize($response);

you can try to read the Cookie class code and figure something out

1) processing will die if an array is passed as the value

2) an exception will be thrown if the value contains invalid characters, which may occur if you try to serialize the array

 

i don't know what the contents of your array is, but it is likely a really BAD idea to pass payment details in a cookie, it is really easy for a client to manipulate that data and be malicious with your module.  if you need to pass the payment information from one request to the next, then you should store that data in a database, or perhaps retrieve the data when it is needed.  but passing it using a cookie is a bad practice

    public function __set($key, $value)
    {
        if (is_array($value)) {
            die(Tools::displayError());
        }
        if (preg_match('/¤|\|/', $key.$value)) {
            throw new Exception('Forbidden chars in cookie');
        }
        if (!$this->_modified && (!isset($this->_content[$key]) || (isset($this->_content[$key]) && $this->_content[$key] != $value))) {
            $this->_modified = true;
        }
        $this->_content[$key] = $value;
    }

 

Link to comment
Share on other sites

 

well you can't do this in a module

$this->context->cookie->{$payment.'_response'} = serialize($response);

you can try to read the Cookie class code and figure something out

1) processing will die if an array is passed as the value

2) an exception will be thrown if the value contains invalid characters, which may occur if you try to serialize the array

 

i don't know what the contents of your array is, but it is likely a really BAD idea to pass payment details in a cookie, it is really easy for a client to manipulate that data and be malicious with your module.  if you need to pass the payment information from one request to the next, then you should store that data in a database, or perhaps retrieve the data when it is needed.  but passing it using a cookie is a bad practice

    public function __set($key, $value)
    {
        if (is_array($value)) {
            die(Tools::displayError());
        }
        if (preg_match('/¤|\|/', $key.$value)) {
            throw new Exception('Forbidden chars in cookie');
        }
        if (!$this->_modified && (!isset($this->_content[$key]) || (isset($this->_content[$key]) && $this->_content[$key] != $value))) {
            $this->_modified = true;
        }
        $this->_content[$key] = $value;
    }

Thank you sir,

 

First of all I didn't store any payment details in $cookie. The response variable hold only Transaction id, date, and success/failure status. The customer sensitive data like customer card details,  cvc, iban in case of Credit card and other payments like Paypal, SEPA will be masked and extract from response before save to cookie.

 

I am verifying fraud check by "callback sms", so my need is to get Transaction id and status after redirecting to controller. 

I thought like store these limited array data in cookie instead of store each variable like :

 

$cookie->__set($payment.'_tid', $response['tid']);

$cookie->__set($payment.'_date', $response['date']);

$cookie->__set($payment.'_status', $response['status']);

 

Because it will optime in a single line via using array.

 

I realised that the array data can't set into a cookie variable, so only proceed with serialize the array. That also get failed. 

Now I use another way to store array of data like , first I encode the data then serialize..

 

Now my coding looking like :

 

     $this->context->cookie->{$payment.'_response'} = serialize(json_encode($response));

 

and am getting the above serialized data from cookie like : 

  

     json_decode(unserialize($this->context->cookie->{$payment.'_response'}))

 

 

:)   It's now working correctly. Is it a better idea ??? I hope  :)

 

Again thanks for your sharing sir.

Link to comment
Share on other sites

Hi sir, 

 

Good morning 

 

I am storing the essential information in the database about the payment only after fraud verification, I need to check the transaction details with transaction id, date, success status & etc. And the process is calling inside a custom controller, there I need the above mentioned details .... 

 

I hope that you got my process  :rolleyes: 

Edited by Nayana Chandran (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...