Create Payment Modules
Let’s take the operation of the module BankWire as an example.
As it is written in the first chapter of the article, all payment-modules are inherited from the abstract class PаymentMоdule. Apart from the settings related to the current method of payment, the BankWire module constructor has two properties set up:
- $this- >сurrenсies = true; //binding this method of payment to a specific currency
- $this- >сurrenсies_mоde = ‘checkbox’; //ability to bind to multiple currencies
This module uses only two hooks:
1. Payment – this hook is used to output the current method of payment to the choice list of available methods on the checkout pages.
Hook code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function hookPayment($params) { if (!$this->active) return; if (!$this->checkCurrency($params['cart'])) return; $this->smarty->assign(array( 'this_path' => $this->_path, 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/' )); return $this->display(__FILE__, 'payment.tpl'); } |
2. pаymentreturn – this one is called when a customer has chosen this method of payment
Hook code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public function hookPaymentReturn($params) { if (!$this->active) return; $state = $params['objOrder']->getCurrentState(); if ($state == Configuration::get('PS_OS_BANKWIRE') || $state == Configuration::get('PS_OS_OUTOFSTOCK')) { $this->smarty->assign(array( 'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false), 'bankwireDetails' => Tools::nl2br($this->details), 'bankwireAddress' => Tools::nl2br($this->address), 'bankwireOwner' => $this->owner, 'status' => 'ok', 'id_order' => $params['objOrder']->id )); if (isset($params['objOrder']->reference) && !empty($params['objOrder']->reference)) $this->smarty->assign('reference', $params['objOrder']->reference); } else $this->smarty->assign('status', 'failed'); return $this->display(__FILE__, 'payment_return.tpl'); } |
The following hooks are installed in the install method:
1 2 3 4 5 6 |
public function install() { if (!parent::install() || !$this->registerHook('payment') || !$this->registerHook('paymentReturn')) return false; return true; } |
Templates for the above-mentioned hooks are located in the directory modules/bankwire/views/templates/hook.
The module BankWire has two front-controllers for validation and for the payment process itself:
1. modules/bankwire/controllers/front/payment.php // is used for the order payment process. The controller has an overridden method BаnkwirePаymentMоduleFrоntCоntrоller:: initContent()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public function initContent() { $this->display_column_left = false; parent::initContent(); $cart = $this->context->cart; if (!$this->module->checkCurrency($cart)) Tools::redirect('index.php?controller=order'); $this->context->smarty->assign(array( 'nbProducts' => $cart->nbProducts(), 'cust_currency' => $cart->id_currency, 'currencies' => $this->module->getCurrency((int)$cart->id_currency), 'total' => $cart->getOrderTotal(true, Cart::BOTH), 'this_path' => $this->module->getPathUri(), 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->module->name.'/' )); $this->setTemplate('payment_execution.tpl'); } |
2. modules/bankwire/controllers/front/validation.php // is used for validation and generation of a purchase order. The controller has an overridden method BankwireValidationModuleFrontController::postProcess()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public function postProcess() { $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'); // Check that this payment option is still available in case the customer changed his address just before the end of the checkout process $authorized = false; foreach (Module::getPaymentModules() as $module) if ($module['name'] == 'bankwire') { $authorized = true; break; } if (!$authorized) die($this->module->l('This payment method is not available.', 'validation')); $customer = new Customer($cart->id_customer); if (!Validate::isLoadedObject($customer)) Tools::redirect('index.php?controller=order&step=1'); $currency = $this->context->currency; $total = (float)$cart->getOrderTotal(true, Cart::BOTH); $mailVars = array( '{bankwire_owner}' => Configuration::get('BANK_WIRE_OWNER'), '{bankwire_details}' => nl2br(Configuration::get('BANK_WIRE_DETAILS')), '{bankwire_address}' => nl2br(Configuration::get('BANK_WIRE_ADDRESS')) ); $this->module->validateOrder($cart->id, Configuration::get('PS_OS_BANKWIRE'), $total, $this->module->displayName, NULL, $mailVars, (int)$currency->id, false, $customer->secure_key); Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key); } |
Controllers’ templates are located here: modules/bankwire/views/templates/front.