Jump to content

Hook and module's controller


Xtense

Recommended Posts

Hi there,

 

I'm trying to display a payment module directly on the payment page (which is /index.php?controller=order-opc because I'm using a one page checkout page).

 

The payment system is Paymill you can find the module here : https://github.com/paymill/paymill-prestashop-1.5

 

I don't understand how I can do it with the Hook system. Cause in this case, I would need to place the hookPayment() in the module's controller (payment.php). Can someone around here help me using "hook" in module's controllers ?

 

PS : I've tried to avoid the issue using jquery load() but it's not doing the trick because I've got to use some javascript embbed with php.

 

 

Thanks a lot guys :)

Link to comment
Share on other sites

Yep I'm sorry I've been coding 14h today so I was not really clear. I want to avoid the step in the payment process where the user click the URL of the selected mean of Payment and is redirected to the correct paying module page.

 

I want to integrate Paymill module but directly in the order-opc template. I don't want to propose different mean of payments to my customers. I just want them to be able to pay directly on the checkout page with the Paymill payment module.

 

Add a phone case to your cart here : 

http://extraverso.com/beta/prestashop/index.php?fc=module&module=testmodule&controller=displaybasics

 

And then go on the order-opc page clicking on the tiny cart appearing on the top menu bar. You'll see what I need I think. I want to implement the credit card form in the bottom of that page.

 

Thanks for reading and trying to help, that's really nice from you :)

Link to comment
Share on other sites

I've come to a solution: I've totaly inserted Paymill controller into OPCcontroller but I hate doing things like this. Can someone give a sight at this post and explain what's the best way to make a module being displayed in another template than the one in its folder?

Link to comment
Share on other sites

Not really. The hook function is not in the module's controller but in the module's controller called payment.php.

 

I don't think it would work, in fact I don't really know how to use Hook to display module's template inside another template.

Link to comment
Share on other sites

Hi,

 

I haven't used the Paymill module before but after a quick look at the module files and your website it appears you may have a problem with your current setup. In the source code the #submitButton click event handler appears to create a token using the total price that's inherited from the server when the page is created. Since you can still add and delete products on your page or change a shipping option this token value could be incorrect at credit card submission. There's also a javascript file pulled from https://bridge.paymill.com/ that's minifed and thus makes no sense but may be important if you'd like to customise some features.

 

If you'd like to give it a go you could modify the click event handler (to include a total price variable that updates with price changes) and transplant desired aspects of the payment-form.tpl in the pigmbhpaymill module folder \views\templates\front and merge it with the usual payment.tpl hook found in \views\templates\hook. Note that Paymill multiplies final amounts by 100 to remove the decimal point.

 

Anyway, after a look at your site it appears there are other bugs that need fixing on the one page checkout page (which you may be aware of anyway). Firstly, selecting the alternate shipping method does not appear to update the total price. Secondly, only a handful of countries in the account setup list actually have their list of states loaded within the webpage. Thirdly, month and year are switched for the credit card expiry field.

 

Cheers

Link to comment
Share on other sites

In fact, you might not even need to change this total price variable once you move it to payment.tpl since once your page is working normally (i.e. refreshing) the payment hook that calls payment.tpl in the one-page checkout should be refreshed automatically and (hopefully) intval($cart->getOrderTotal(true, Cart::BOTH) * 100); will be updated automatically from payment.php

 

Unfortunately, there's probably other changes to you'll need to make in the payment.php controller (and maybe elsewhere) to re-assign variables to payment.tpl

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

Thanks for your feedbacks, you're right, I may need to change something to ask for the token to be generated on each cart change.

 

In fact I'm going to store the total in a div's rel attribute, which will be updated by a function on the last payment click. The callback of the function will ask to send the paymill request once everything with the defined div's rel attribute as the total amount.

 

Another question, Do you know how I can avoid a customer registered who didn't complete his address info yet to be redirected to the address controller on cart click even before having seen the content of his cart.

 

 

I don't know why Prestashop handle this, this way. I've read deeply orderopccontroller.php and I just can't find where this redirection is made this is insane...

Link to comment
Share on other sites

It's working for the token generation, I've done that : 

 

 $("#submitButton").click(function(event) {
var total = $("#total_price").text();
var total = parseFloat(total) * 100;
if (validate()) {
            try {
                {if $payment == 'creditcard'}
                paymill.createToken({
                    number: $('#card-number').val(),
                    cardholder:  $('#account-holder').val(),
                    exp_month: $('#card-expiry-month').val(),
                    exp_year: $('#card-expiry-year').val(),
                    cvc: $('#card-cvc').val(),
                    amount_int: total,
                    currency: '{$currency_iso}'
                }, PaymillResponseHandler);
                {elseif $payment == 'debit'}
                paymill.createToken({
                    number: $('#paymill_accountnumber').val(),
                    bank: $('#paymill_banknumber').val(),
                    accountholder: $('#paymill_accountholder').val()
                }, PaymillResponseHandler);
 
                {/if}
            } catch (e) {
                alert("Ein Fehler ist aufgetreten: " + e);
            }
        }
        return false;
    });
Link to comment
Share on other sites

Not really. The hook function is not in the module's controller but in the module's controller called payment.php.

 

I don't think it would work, in fact I don't really know how to use Hook to display module's template inside another template.

 

 

in your modules install function...

$this->registerHook('payment')

then you create a function called hookPayment

    public function hookPayment($params)
    {
          // your code here
    }

Now you would normally have something like this which would present a link to your payment methods form.

return $this->display(__FILE__, 'payment.tpl');

However there is nothing stopping you from doing this.

Tools::redirect($this->context->link->getModuleLink('modulename', 'payment'));
Link to comment
Share on other sites

Sure. You could handle the update yourself but like I said before the payment hook is refreshed by default anyway on any changes when the page works correctly.

 

Although you'd need to look more closesly, I'd steal the variables I need from the $data array in the payment.php controller and copy them to pigmbhpaymill.php (in the root of the module's folder) within the function hookPayment($params) (adding them to $this->smarty->assign(array(... etc). In your case since you're only wanting the credit card form you'd make a few extra changes. (Note: bellini13 beat me to it :) - pigmbhpaymill.php is the install file in this case that he's referring to).

 

That re-direct in order-opc is called from the inherited ParentOrderController in the _assignAddress() function -

else if (!Customer::getAddressesTotalById($this->context->customer->id))

Tools::redirect('index.php?controller=address&back='.urlencode('order.php?step=1&multi-shipping='.(int)Tools::getValue('multi-shipping')));
 
_assignAddress() is used quite a bit in different functions and I don't know how safe it is to remove the re-direct or add another condition to it. Maybe someone else knows.
Edited by Emzed (see edit history)
  • Like 1
Link to comment
Share on other sites

Cool. Me too.

 

In case you didn't know, just make you sure you put modified classes and controllers in the override folder rather than make direct changes to the core files.

 

And the module intro is pleasant enough - http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module

 

From my limited experience the biggest headache has been getting different modules to integrate smoothly with each other. There are some v1.4.x modules that have been quickly jigged to work with v1.5.x and may fail when new features weren't accounted for.

Link to comment
Share on other sites

I've never used the override folder. Each time I tried, it just didn't work.

 

Do I have to create an entire other class (copy pasting the original and adding some changes), or just create a class with a list off addtionnal methods in it to override a class ?

Link to comment
Share on other sites

Yeah I just copy and paste the whole file and make the necessary changes.

 

Then, you must delete cache/class_index.php for the new class in the override folder to take over. Never forget that one :)

 

While we're at it, make sure any form of caching is deactivated for now as well during development (force compile for smarty, turn off all that CSS/Javascript compression etc). And keep clearing your browser cache or turn it off completely (else you'll keep wondering why your recent javascript changes aren't taking effect :)).

 

Just in case you were wondering, module's tpl templates, css and js can be overridden in their respective folders in your theme folder (which I find kind of annoying since the structure has to be mimicked and isn't always consistent).

 

And I just remembered other annoyances. I always forget to re-build my product index for my search function to work properly. And if you're using CSV import, don't try and import more than 50 images at a time (I don't know if this was a server time-out issue or Prestashop limitation to be honest).

 

Don't forget to set define('_PS_MODE_DEV_', true); in config/defines.inc.php at least every now and then on your development server to see if your site really works. And turn on debug mode in the back office. Other than knowing what variables were loaded, it makes updating your smarty templates much easier.

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

Thanks for all those tips, I knew them all except for the overiding thing ! Damn If only I had discovered that when I started this project.

 

What do you think about the website btw ? It's not responsive yet but it will become later on.

Link to comment
Share on other sites

Yep the footer is different in local. I have to reassign the order of the blocks in the footer diving in the database or maybe somewhere in the back office. Also there's an HTML mistake in my online footer template as you can see ^^

 

 

Thanks for your time, great talk, nice time :)

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...