Skip to content

Commit

Permalink
v 2.2.0
Browse files Browse the repository at this point in the history
+ Assigned cart rule to payment
+ Added multistore
  • Loading branch information
prestalab committed Mar 31, 2016
1 parent d89edcf commit ba969bb
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 31 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Creates an unlimited number of fully customizable methods of payment
Allows you to bind to a payment to carrier
It does not require programming skills

v 2.2.0
+ Assigned cart rule to payment
+ Added multistore

v 2.1.0
+ Added AdvancedPaymentOptions hook
+ Added french translation
Expand Down
5 changes: 4 additions & 1 deletion classes/UniPaySystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class UniPaySystem extends ObjectModel
public $id;
public $active = 1;
public $id_order_state = 3;
public $id_cart_rule = 0;
public $position;
public $date_add;
public $date_upd;
Expand All @@ -39,6 +40,7 @@ class UniPaySystem extends ObjectModel
'date_add' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),
'date_upd' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),
'id_order_state' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_cart_rule' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

/* Lang fields */
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
Expand All @@ -52,13 +54,13 @@ public function __construct($id = null, $id_lang = null)
{
$this->image_dir = _PS_IMG_DIR_.'pay/';
return parent::__construct($id, $id_lang);

}

public static function getPaySystems($id_lang, $active = true, $id_carrier = false, $groups = array())
{
if (!Validate::isBool($active))
die(Tools::displayError());

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'universalpay_system` us
Expand All @@ -67,6 +69,7 @@ public static function getPaySystems($id_lang, $active = true, $id_carrier = fal
ON (us.`id_universalpay_system` = usc.`id_universalpay_system` AND usc.`id_carrier`='.(int)$id_carrier.')':'').'
'.(!empty($groups)?'JOIN `'._DB_PREFIX_.'universalpay_system_group` usg
ON (us.`id_universalpay_system` = usg.`id_universalpay_system` AND usg.`id_group` IN ('.implode(',', array_map('intval', $groups)).'))':'').
Shop::addSqlAssociation('universalpay_system', 'us').
'WHERE `id_lang` = '.(int)$id_lang.
($active ? ' AND `active` = 1' : '').'
GROUP BY us.`id_universalpay_system`
Expand Down
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>universalpay</name>
<displayName><![CDATA[Universal Payment Module]]></displayName>
<version><![CDATA[2.1.0]]></version>
<version><![CDATA[2.2.0]]></version>
<description><![CDATA[Payment methods creating.]]></description>
<author><![CDATA[PrestaLab.Ru]]></author><author_uri><![CDATA[http://addons.prestashop.com/ru/payments-gateways/5507-universal-payment-module.html]]></author_uri>
<tab><![CDATA[payments_gateways]]></tab>
Expand Down
64 changes: 52 additions & 12 deletions controllers/admin/AdminUniPaySystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,55 @@ public function renderForm()
'name' => 'name'
),
'desc' => $this->l('The carriers in which this paysystem is to be used')
),
array(
'type' => 'group',
'label' => $this->l('Groups:'),
'name' => 'groupBox',
'values' => Group::getGroups($this->context->language->id),
'desc' => $this->l('The customer groups in which this paysystem is to be used')
)
),
'submit' => array(
'title' => $this->l('Save'),
)
);

$cart_rules = array(0 => array('id_cart_rule' => 0, 'name' => $this->l('No discount')));

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT cr.id_cart_rule, crl.name, cr.reduction_percent, cr.reduction_amount
FROM `'._DB_PREFIX_.'cart_rule` cr, `'._DB_PREFIX_.'cart_rule_lang` crl
WHERE
cr.id_cart_rule = crl.id_cart_rule
AND `id_lang` = '.(int)$this->context->language->id.
' AND `active` = 1');

foreach ($result as $discount)
$cart_rules[$discount['id_cart_rule']] = array('id_cart_rule' => $discount['id_cart_rule'], 'name' => $discount['name'].' - '.($discount['reduction_percent'] >0 ? $discount['reduction_percent'].'%' : $discount['reduction_amount']));

if (CartRule::isFeatureActive())
$this->fields_form['input'][] = array(
'type' => 'select',
'label' => $this->l('Order discount:'),
'name' => 'id_cart_rule',
'desc' =>$this->l('Select cart rule'),
'options' => array(
'query' => $cart_rules,
'name' => 'name',
'id' => 'id_cart_rule'
)
);

if (Group::isFeatureActive())
$this->fields_form['input'][] = array(
'type' => 'group',
'label' => $this->l('Groups:'),
'name' => 'groupBox',
'values' => Group::getGroups($this->context->language->id),
'desc' => $this->l('The customer groups in which this paysystem is to be used')
);

if (Shop::isFeatureActive())
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association'),
'name' => 'checkBoxShopAsso',
);

if (!($obj = $this->loadObject(true)))
return;

Expand All @@ -168,11 +203,13 @@ public function renderForm()

$universalpay_system_group_ids = $obj->getGroups();

$groups = Group::getGroups($this->context->language->id);
if (Group::isFeatureActive()) {
$groups = Group::getGroups($this->context->language->id);

foreach ($groups as $group)
$this->fields_value['groupBox_'.$group['id_group']] = Tools::getValue('groupBox_'.$group['id_group'],
(in_array($group['id_group'], $universalpay_system_group_ids)));
foreach ($groups as $group)
$this->fields_value['groupBox_' . $group['id_group']] = Tools::getValue('groupBox_' . $group['id_group'],
(in_array($group['id_group'], $universalpay_system_group_ids)));
}

return parent::renderForm();
}
Expand All @@ -190,7 +227,10 @@ public function postProcess()
if (Tools::getIsset('carrierBox_'.$carrier['id_carrier']))
$carrier_box[] = $carrier['id_carrier'];
$return->updateCarriers($carrier_box);
$return->updateGroups(Tools::getValue('groupBox'));
if (Group::isFeatureActive())
$return->updateGroups(Tools::getValue('groupBox'));
if (Shop::isFeatureActive())
$this->updateAssoShop($return->id);
}
return $return;
}
Expand Down
11 changes: 10 additions & 1 deletion controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ public function initContent()
if (!Validate::isLoadedObject($paysistem))
return;

$total = $cart->getOrderTotal(true, Cart::BOTH);
/*if ($paysistem->id_cart_rule) {
$cart_rule = new CartRule($paysistem->id_cart_rule);
if ($cart_rule->reduction_percent > 0)
$total = $total - $total * $cart_rule->reduction_percent / 100;
else
$total = $total - $cart_rule->reduction_amount;
}*/

$paysistem->description = str_replace(array('%total%'),
array(Tools::DisplayPrice($cart->getOrderTotal(true, Cart::BOTH))),
array(Tools::DisplayPrice($total)),
$paysistem->description);

$this->context->smarty->assign(array(
Expand Down
34 changes: 19 additions & 15 deletions controllers/front/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function postProcess()
// 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'] == 'universalpay')
{
if ($module['name'] == 'universalpay') {
$authorized = true;
break;
}
Expand All @@ -36,28 +35,33 @@ public function postProcess()
if (!Validate::isLoadedObject($customer))
Tools::redirect('index.php?controller=order&step=1');

$currency = $this->context->currency;
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);

require_once(dirname(__FILE__).'/../../classes/UniPaySystem.php');
require_once(dirname(__FILE__) . '/../../classes/UniPaySystem.php');
$paysistem = new UniPaySystem((int)Tools::getValue('id_universalpay_system'), $this->context->cookie->id_lang);
if (!Validate::isLoadedObject($paysistem))
return;

$up_fields = array();
foreach ($_POST as $key => $val)
{
$key_parts = explode('_', $key);
if ($key_parts[0] == 'up')
$up_fields[$key_parts[1]] = $val;
}
if ($paysistem->id_cart_rule)
$cart->addCartRule($paysistem->id_cart_rule);

$currency = $this->context->currency;
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);

$up_fields = array();
foreach ($_POST as $key => $val) {
$key_parts = explode('_', $key);
if ($key_parts[0] == 'up')
$up_fields[$key_parts[1]] = $val;
}

$mail_vars = array(
'{paysistem_name}' => $paysistem->name
);

foreach ($up_fields as $key => $val)
$mail_vars['{up_'.$key.'}'] = $val;
foreach ($up_fields as $key => $val)
$mail_vars['{up_' . $key . '}'] = $val;

if ($paysistem->id_cart_rule)
$cart->addCartRule($paysistem->id_cart_rule);

$this->module->validateOrder((int)$cart->id, $paysistem->id_order_state, $total, $paysistem->name,
null, $mail_vars, (int)$currency->id, false, $customer->secure_key);
Expand Down
11 changes: 10 additions & 1 deletion universalpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct()
{
$this->name = 'universalpay';
$this->tab = 'payments_gateways';
$this->version = '2.1.0';
$this->version = '2.2.0';
$this->author = 'PrestaLab.Ru';
$this->need_instance = 1;
$this->module_key = 'a4e3c26ec6e4316dccd6d7da5ca30411';
Expand All @@ -32,6 +32,7 @@ public function __construct()

$this->displayName = $this->l('Universal Payment Module');
$this->description = $this->l('Payment methods creating.');
Shop::addTableAssociation('universalpay_system', array('type' => 'shop'));
}

public function install()
Expand All @@ -41,6 +42,7 @@ public function install()
`id_order_state` INT( 10 ) NOT NULL DEFAULT \''.Configuration::get('PS_OS_PREPARATION').'\',
`active` TINYINT(1) UNSIGNED NOT NULL DEFAULT \'0\',
`position` INT(10) UNSIGNED NOT NULL DEFAULT \'0\',
`id_cart_rule` INT(10) UNSIGNED NOT NULL DEFAULT \'0\',
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
PRIMARY KEY (`id_universalpay_system`)
Expand All @@ -65,6 +67,13 @@ public function install()
UNIQUE KEY `id_universalpay_system` (`id_universalpay_system`,`id_group`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
Db::getInstance()->Execute('ALTER TABLE `'._DB_PREFIX_.'orders` ADD `up_fields` VARCHAR( 255 ) NOT NULL DEFAULT ""');
Db::getInstance()->Execute('CREATE TABLE `'._DB_PREFIX_.'universalpay_system_shop` (
`id_universalpay_system` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
UNIQUE KEY `id_universalpay_system` (`id_universalpay_system`,`id_shop`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');

return parent::install()
&& $this->registerHook('displayPayment')
Expand Down

0 comments on commit ba969bb

Please sign in to comment.