Blog navigation

Blog Rss rss_feed

Developing a Payment Module for PrestaShop: Framework and Key Methods

Developing a Payment Module for PrestaShop: Framework and Key Methods

Creating a payment module for PrestaShop is a crucial step in extending the functionality of your online store and offering customers convenient payment options. In this article, we’ll explore how to create a basic module framework, its architecture, and key methods. We’ll also include an example controller for redirecting to a payment gateway.

Getting Started: Generating the Module Framework

To kick off development, it’s recommended to use the official PrestaShop Module Generator. This tool automatically creates the basic structure of the module, including necessary files and folders, speeding up the process and reducing errors.

Essential Requirements

When developing a payment module, certain rules must be followed:

  • The module must extend the PaymentModule class.
  • Implementation of the hookPaymentOptions() and hookPaymentReturn() methods is required.
  • The module should register the paymentOptions and paymentReturn hooks.
  • No submit buttons should be included in the module’s HTML; these are auto-generated by PrestaShop.

Payment Module Architecture

PrestaShop uses a modular architecture and an event-driven model, allowing seamless integration of additional features. Payment modules are based on the Observer pattern, where modules subscribe to specific events and process them accordingly.

Hook Registration Example

public function install() { return parent::install() && $this->registerHook('paymentOptions') && $this->registerHook('paymentReturn'); }

Implementation of hookPaymentOptions

public function hookPaymentOptions($params) { $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); $paymentOption->setCallToActionText($this->l('Pay via our gateway')) ->setAction($this->context->link->getModuleLink($this->name, 'validation', [], true)) ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/logo.png')); return [$paymentOption]; }

Implementation of hookPaymentReturn

public function hookPaymentReturn($params) { if ($this->active == false) { return; } return $this->context->smarty->fetch('module:' . $this->name . '/views/templates/hook/payment_return.tpl'); }

Example of a Controller for Redirecting to a Payment Gateway

The controller is responsible for validating cart data and redirecting the customer to the payment gateway. Here’s an example:

class Ps_CheckpaymentValidationModuleFrontController extends ModuleFrontController { public function postProcess() { if (!($this->module instanceof Ps_Checkpayment)) { Tools::redirect('index.php?controller=order&step=1'); return; } $cart = $this->context->cart; if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->module->active) { Tools::redirect('index.php?controller=order&step=1'); return; } $authorized = false; foreach (Module::getPaymentModules() as $module) { if ($module['name'] == 'ps_checkpayment') { $authorized = true; break; } } if (!$authorized) { exit($this->trans('This payment method is not available.', [], 'Modules.Checkpayment.Shop')); } $customer = new Customer($cart->id_customer); if (!Validate::isLoadedObject($customer)) { Tools::redirect('index.php?controller=order&step=1'); return; } $currency = $this->context->currency; $total = (float) $cart->getOrderTotal(true, Cart::BOTH); $mailVars = [ '{check_name}' => Configuration::get('CHEQUE_NAME'), '{check_address}' => Configuration::get('CHEQUE_ADDRESS'), '{check_address_html}' => str_replace("\n", '<br />', Configuration::get('CHEQUE_ADDRESS')), ]; // Order confirmation $this->module->validateOrder( (int) $cart->id, (int) Configuration::get('PS_OS_CHEQUE'), $total, $this->module->displayName, null, $mailVars, (int) $currency->id, false, $customer->secure_key ); // Redirect to the payment gateway $paymentUrl = 'https://example-payment-gateway.com/checkout'; Tools::redirect($paymentUrl); } }

Explanation:

  • Cart and Customer Validation: Ensures valid data before redirection.
  • Order Creation: Saves the order using validateOrder().
  • Gateway Redirection: The URL is generated based on the payment system.

Developing a payment module for PrestaShop requires a deep understanding of the platform's architecture. By properly utilizing hooks and patterns, you can create a secure and reliable payment solution.


What payment systems do you prefer for your clients? Share your thoughts in the comments!

Was this blog post helpful to you?

    
👈 Присоединяйтесь к нашему Telegram-каналу!

Будьте в курсе последних новинок и фишек e-commerce: советы, полезные инструменты и эксклюзивные материалы.

No comments at this moment
close

Checkout

close

Favourites