Reading list Switch to dark mode

    how to create payment modules in Prestashop 1.7

    Updated 22 April 2017

    In this blog, we will learn how to develop Payment modules in Prestashop 1.7 .

    In Prestashop v1.6 we use hook displayPayment to show payment option of our payment module on the checkout page. But now it won’t work in Prestashop 1.7 .

    Now you have to use hook paymentOptions in place of hook displayPayment to show your payment module’s payment option.

    In hook paymentOptions we can set our payment module’s form and other details like the example below –

    public function hookPaymentOptions()
    {
        $newOption = new PaymentOption();
        $paymentForm = $this->fetch('module:mymodule/views/templates/hook/payment.tpl');
        $newOption->setCallToActionText($this->trans('Pay by MyModule', array(), 'Modules.MyModule.Shop'))
            ->setForm($paymentForm)
            ->setLogo(_MODULE_DIR_.'mymodule/views/img/logo-mymodule.png')
            ->setAdditionalInformation('your additional Information')
            ->setAction($this->context->link->getModuleLink($this->name, 'payment'));
        return [$newOption];
    }
    
    

    To show form when clicking on the checkbox of your payment option,

    Searching for an experienced
    Prestashop Company ?
    Find out More

    use setForm($form) method of paymentOption class

    Here $form is custom HTML to display e.g. a form where a customer will fill his payment details. The HTML must not contain a submit button, as  the Core will submit the form.

    Note – The HTML must not contain a submit button, as  the Core will submit the form.

    You also can use other options provided by PrestaShop. Some are described below-

    setForm($form) – To set form on checking your payment option.

    setLogo($logo) – To set your payment logo. $logo is the path of your logo image.

    setAdditionalInformation($callToActionText) – If your module need any additional information. $callToActionText is the path of your tpl file contains additional information about your payment option.

    setAction() – Link to which you payment form will be submitted.

    Note : Please have a look to prestashop1.7/src/Core/Payment/paymentOptions.php class file for payment options provided by Prestashop.

    For your JavaScript validations, you can use:

    $(‘#payment-confirmation > .ps-shown-by-js > button’).click(function(e) {
        var myPaymentMethodSelected = $(‘.payment-options’).find(“input[data-module-name=’myModuleName’]”).is(‘:checked’);
        
        if (myPaymentMethodSelected){
        //Your validations or other checks.
        }
        
    });
    
    

    You can take a reference for paymentReturn hook code from the below code as some keys are changed in the parameter $params of this hook –

    public function hookPaymentReturn($params) 
    { 
        if (!$this->active) {
            return;
        }
        if (!isset($params['order']) || ($params['order']->module != $this->name)) {
            return false;
        }
        if (isset($params['order']) && Validate::isLoadedObject($params['order']) && isset($params['order']->valid)) {
            $this->smarty->assign(array(
                'id_order' => $params['order']->id,
                'valid' => $params['order']->valid,
                ));
        }
        if (isset($params['order']->reference) && !empty($params['order']->reference)) {
            $this->smarty->assign('reference', $params['order']->reference);
        }
        $this->smarty->assign(array(
            'shop_name' => $this->context->shop->name,
            'reference' => $params['order']->reference,
            'contact_url' => $this->context->link->getPageLink('contact', true)
            ));
        return $his->fetch('mymodule/views/templates/hook/payment_return.tpl'); 
    }

    You can take a reference for payment_return.tpl which is returned from hookPaymentReturn

    {if $valid == 1}
     // write your code what you want to show to the customer if order is valid.
    {else}
     // write your code what you want to show to the customer if some error occurred while creating order.
    {/if}
    
    

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    15 comments

  • nyko_k
    • nyko_k
  • Jota McCready
    • sumit
  • Attila Szabo
  • SnipWeb Dezone
    • Webkul Support
  • SnipWeb Dezone
    • Webkul Support
      • SnipWeb Dezone
        • Webkul Support
  • Andresa
    • Daniele
  • Sabelo Simelane
    • sumit
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home