Search code examples
phpprestashopsmarty

How to populate POST data from postProcess to smarty tpl via hookPaymentReturn


I'm trying to change the behavior of a payment module in Prestashop 1.6.x, in order to create a customer receipt from the approval source to the order confirmation page.

I would like a suggestion concerning the best method and some guidance to achieve it a the correct way.

More specifically, when the user completes the transaction using a specific payment module, I need to populate the order confirmation page with POST data from the external banking source which is available in the postProcess() function, in order to use it as a kind of receipt.

As far as I can understand, after payment has been made:

  1. the module validates the order and elaborates the external source's POST data through a PostProcess() function (part of an extended ModuleFrontController class) and found in a controllers/front/validation.php file.

  2. Within PostProcess(), if external data is OK (i.e. transaction approved), it redirects to the order confirmation controller with the following:

public function postProcess() {

(...)

$somePostData = '';

//this is the variable that is populated from POST data and i need to show in the confirmation.tpl
$somePostData = Tools::getValue('postdata'); 

Tools::redirect('index.php?controller=order-confirmation&id_cart=' .
                    $this->context->cart->id . '&id_module=' .
                    $this->module->id . '&id_order=' .
                    $this->module->currentOrder . '&key=' .
                    $customer->secure_key
                );

(...)

}
  1. At some point hookPaymentReturn() is being called (resided in the main module php file), which loads a specific module template file related to the order confirmation page.

  2. In order to show some variables through the tpl file, the only solution i've found is to use a smarty variable just before returning the populated tpl as shown below:

public function hookPaymentReturn()
    {
        if (!$this->active) {
            return;
        }

        //this is the variable that I want to populate from the above-mentioned $somePostData found in postProcess()
        $receipt_display = 'some data';

        $this->context->smarty->assign('receipt_display', $receipt_display);

        return $this->display(__FILE__, 'views/templates/hook/confirmation.tpl');
    }

So my question is how can I populate $receipt_display with data from $somePostData as shown in the two above-mentioned code sections?

Is there a different methodology that you could suggest if the above-mentioned is wrong?

Thank you, mmystery


Solution

  • It depends if $_POST['somePostData'] is just a simple string, if so, add it to Tools::redirect as next param in URL, if this is most complex data you have two options which i see:

    1. create some table with simple mapping: ps_yourpaymentmethod_data: id_order | data and get data by id in hookPaymentReturn

    2. set value in cookie:

    $this->context->cookie->someVar = Tools::getValue('postData'); $this->context->cookie->write();

    read it in hookPaymentReturn:

    if ($this->context->cookie->someVar) {
        $someVar = $this->context->cookie->someVar;
        $this->context->cookie->someVar = null;
        $this->context->cookie->write();
    }