Calling Prestashop postProcess method - php

I'm actually developing a payment method module for Prestashop 1.7 but now I'm stuck with the postProcess() method on my payment page, the method is being called immediately when the payment page loads. Do you know why this is happening? My payment.tpl its a simple html form to perform a POST request.
Thanks in advance.

Solved. Its needed a validation because the postProcess runs after init() and before initContent():
public function postProcess()
{
if (!empty($_POST))
{
// Magic here!
}
}

Related

Prestashop 1.7 Hook actionCustomerAccountAdd not fired

I am trying to add a hook in my custom module but it is not fired after I add a customer both in prestashop backoffice or by using the webservice.
The hook name I am trying to register is "actionCustomerAccountAdd".
This is the relevant code of the module. Please could you help me? I am a PHP developer but it is the first time I develop in Prestashop side.
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
return parent::install()
&& $this->createRequiredDBTables()
&& $this->registerHook('actionCustomerAccountAdd');
}
I have this code to check in logs files or page but it is not fired:
public function hookActionCustomerAccountAdd($params)
{
$this->logger->info('Hook action customer account add fired');
echo 'hook fired';
die();
}
Thank you.
The thing is that hook actionCustomerAccountAdd is fired only on front-office, you will need to use actionObjectCustomerAddAfter, dynamic hook executed in classes/ObjectModel.php

Laravel call a controller within a controller

I have 2 controllers, one is responsible for submitting a form to the DB, the other is for PayPal integration such as this one:-
http://laravelcode.com/post/how-to-integrate-paypal-payment-gateway-in-laravel-54
I want it so that when the user presses the submit button, it does its usual DB transactions but then calls the PayPal controller to process the payment.
Is it better merge the 2 controllers into one or to call the PayPal controller as part of the store method??
You can call another controller using the following method.
$controller = app()->make('App\Http\Controllers\PaypalController');
app()->call([$controller, 'process'], [$request]);
Where your controller function is defined as:
public function process(Request $request) {}
While not the greatest practice, I have used this for calling a function referenced in a console command and in a URL.
In the function which do the DB transactions, try redirecting to your paypal function:
public function myDBFunc() {
/* do transactions */
return redirect()->route('paypalRoute');
// or return redirect()->action('PaypalController#paypalFunc');
}
Do not forget to pass your variables to your route/action.

Payment module return URI

I am creating a payment module for our payment gateway...
So far I have 1) set up the back-end and 2) used the hookPayment() method to display a hidden form at part 5 of the checkout process (http://prestashop.dev/order). This will then redirect off to my gateway with all the required information. Good job.
The next part is the part I am struggling with. I don't understand what the return URIs are for payment-confirmation - just to give the customer some information on the status of the order (and maybe also update the back-office?).
For now, I have just a very simple method;
public function hookPaymentReturn()
{
// if (!$this->active) {
// return null;
// }
return $this->display(__FILE__, 'views/templates/front/confirmation.tpl');
}
in my main module file. I just want to get to this on the browser... I'll start worrying about POST'ed values after this. But for now I just don't know the URI. What will it be?? Do I need to register the route somehow?
Most payment modules confirmation.tpl get hooked into the order-confirmation.tpl of your theme which in turn gets called by the OrderConfirmationController.php.
This can be accessed by:
[YOUR_BASE_URL]/index.php?controller=order-confirmation&[some_stuff].
The [some_stuff] part is important though since the controller performs a series of validations and will redirect you somewhere else if these are not met

Callback_handler won't fire WooCommerce

I am building a payment Gateway for WooCommerce where the payment takes place in an offsite URL. I need that page to be able to message back to the WooCommerce plugin, and a "callback" URL is really all I need.
WooCommerce seems to have this, but I can't get it to work. You're supposed to be able to ping:
http://yoursite/wc-api/WC_your_gateway
And then you're supposed to add
add_action( 'woocommerce_api_callback', 'callback_handler' );
And then it's supposed to fire a function like this
public function callback_handler() {}
But when I go to that URL, all I see is a 1 on my page - my handler should be redirecting to another page (that's what I set it to do to make it obvious). What I'd LOVE is if anyone has an example of this working. I've tried placing the add_action and the handler function lots of places, no luck.
I have the same problem. Try to add exit; or wp_die(); in the end of your callback function.
This works for me.
I had the same problem, so, this is what worked for me:
class WC_mygateway extends WC_Payment_Gateway {
public function __construct() {
//'woocommerce_api_'.strtolower(get_class($this)) will result in 'woocommerce_api_wc_mygateway'
add_action('woocommerce_api_'.strtolower(get_class($this)), array(&$this, 'handle_callback'));
}
function handle_callback() {
//Handle the thing here!
}
}
function woocommerce_mygateway_add_gateway( $methods ) {
$methods[] = 'WC_mygateway';
return $methods
}
add_filter( 'woocommerce_payment_gateways', 'woocommerce_mygateway_add_gateway');
Make sure you are not missing any of those details, other wise it wont work. Also you can call it using http://example.com/?wc-api=wc_mygateway or http://example.com/wc-api/wc_mygateway
Hope this work for everyone getting stuck with this issue!
Have you tried using http://yoursite/wc-api/WC_your_gateway/ (add slash at the end)?
Also the add_action should be "woocommerce_api_{class_name}" instead of" woocommerce_api_callback". So for your example, it should be "woocommerce_api_wc_your_gateway".

How to use form validation with the CRUD component in AgileToolkit4?

everyone. I've started using atk4 in a personal project a couple weeks ago and have been facing some difficulties since then. This specific question I want to ask is about how to make form validations when using the CRUD component shipped with the atk4 framework.
I have already tried several different solutions, none of them solving my problem.
I have a feeling that the problem here might be that the form validation happens within the call of the method $form->isSubmitted() (am I correct?). Since when using a CRUD component within a Page we don't use that way of processing the form submission, we'd have to find alternatives to it. For example, let's say I have a Page with the following init() function:
function init() {
parent::init();
// create a CRUD and set a model to it
$crud = $this->add('CRUD');
$m = $crud->setModel('Person');
if ($crud->form) {
$fn = $crud->form->getField('first_name');
$fn->validateNotNull('The first name must not be empty.');
}
}
Even though I've added the validation to the first name field, it won't be validated. I've tried several things, unsuccessfully. I tried to extend the CRUD class and reimplement the formSubmit($form) function, adding the validation there. Even if I do it, it doesn't work.
Originally (in the CRUD class), there is the function:
function formSubmit($form){
$form->update();
$this->api->addHook('pre-render',array($this,'formSubmitSuccess'));
}
I tried to iterate through the form's fields and call its validate() method, but it didn't work. Also, if I try to do alter the function (in a MyCRUD class, let's say) like below,
function formSubmit($form){
if ($form->isSubmitted()) {
$form->update();
$this->api->addHook('pre-render',array($this,'formSubmitSuccess'));
}
}
there happens an infinite loop... Could someone help me out?
[EDIT]
One last question intimately related to this one. I've just tried to do the exact same validation proposed by romanish below but, instead of adding a CRUD to a page, I was just adding a Form, and it doesn't work -- though the CRUD does work. Instead, there happens a "Error in AJAX response: SyntaxError: Unexpected token
CRUD component respects the validation you're doing inside the model. When data is entered into the form and button is clicked, $model->update() is called.
The execution continues into beforeUpdate() hook, which is the one you need to intercept.
http://agiletoolkit.org/learn/understand/model/actions
class Model_Book extends Model_Table {
function init(){
parent::init();
// .... more definitions ...
$this->addHook('beforeSave',$this);
}
function beforeSave(){
if(strlen($this['book_name']<10))
throw $this->exception('Name of the book is too short')
->setField('book_name');
}
If model is unable to save itself and will produce exception, Form automatically show it as a field error.

Categories