* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 7798 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
include_once(dirname(__FILE__).'/../config/config.inc.php');
abstract class PaymentModuleCore extends Module
{
/** @var integer Current order's id */
public $currentOrder;
public $currencies = true;
public $currencies_mode = 'checkbox';
public function install()
{
if (!parent::install())
return false;
// Insert currencies availability
if ($this->currencies_mode == 'checkbox')
{
if (!Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'module_currency` (id_module, id_currency)
SELECT '.(int)($this->id).', id_currency FROM `'._DB_PREFIX_.'currency` WHERE deleted = 0'))
return false;
}
elseif ($this->currencies_mode == 'radio')
{
if (!Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'module_currency` (id_module, id_currency)
VALUES ('.(int)($this->id).', -2)'))
return false;
}
else
Tools::displayError('No currency mode for payment module');
// Insert countries availability
$return = Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'module_country` (id_module, id_country)
SELECT '.(int)($this->id).', id_country FROM `'._DB_PREFIX_.'country` WHERE active = 1');
// Insert group availability
$return &= Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'module_group` (id_module, id_group)
SELECT '.(int)($this->id).', id_group FROM `'._DB_PREFIX_.'group`');
return $return;
}
public function uninstall()
{
if (!Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'module_country` WHERE id_module = '.(int)($this->id))
OR !Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'module_currency` WHERE id_module = '.(int)($this->id))
OR !Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'module_group` WHERE id_module = '.(int)($this->id)))
return false;
return parent::uninstall();
}
/**
* Validate an order in database
* Function called from a payment module
*
* @param integer $id_cart Value
* @param integer $id_order_state Value
* @param float $amountPaid Amount really paid by customer (in the default currency)
* @param string $paymentMethod Payment method (eg. 'Credit card')
* @param string $message Message to attach to order
*/
public function validateOrder($id_cart, $id_order_state, $amountPaid, $paymentMethod = 'Unknown', $message = NULL, $extraVars = array(), $currency_special = NULL, $dont_touch_amount = false, $secure_key = false)
{
global $cart;
$cart = new Cart((int)($id_cart));
// Does order already exists ?
if (Validate::isLoadedObject($cart) AND $cart->OrderExists() == false)
{
if ($secure_key !== false AND $secure_key != $cart->secure_key)
die(Tools::displayError());
// Copying data from cart
$order = new Order();
$order->id_carrier = (int)($cart->id_carrier);
$order->id_customer = (int)($cart->id_customer);
$order->id_address_invoice = (int)($cart->id_address_invoice);
$order->id_address_delivery = (int)($cart->id_address_delivery);
$vat_address = new Address((int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
$order->id_currency = ($currency_special ? (int)($currency_special) : (int)($cart->id_currency));
$order->id_lang = (int)($cart->id_lang);
$order->id_cart = (int)($cart->id);
$customer = new Customer((int)($order->id_customer));
$order->secure_key = ($secure_key ? pSQL($secure_key) : pSQL($customer->secure_key));
$order->payment = $paymentMethod;
if (isset($this->name))
$order->module = $this->name;
$order->recyclable = $cart->recyclable;
$order->gift = (int)($cart->gift);
$order->gift_message = $cart->gift_message;
$currency = new Currency($order->id_currency);
$order->conversion_rate = $currency->conversion_rate;
$amountPaid = !$dont_touch_amount ? Tools::ps_round((float)($amountPaid), 2) : $amountPaid;
$order->total_paid_real = $amountPaid;
$order->total_products = (float)($cart->getOrderTotal(false, Cart::ONLY_PRODUCTS));
$order->total_products_wt = (float)($cart->getOrderTotal(true, Cart::ONLY_PRODUCTS));
$order->total_discounts = (float)(abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS)));
$order->total_shipping = (float)($cart->getOrderShippingCost());
$order->carrier_tax_rate = (float)Tax::getCarrierTaxRate($cart->id_carrier, (int)$cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$order->total_wrapping = (float)(abs($cart->getOrderTotal(true, Cart::ONLY_WRAPPING)));
$order->total_paid = (float)(Tools::ps_round((float)($cart->getOrderTotal(true, Cart::BOTH)), 2));
$order->invoice_date = '0000-00-00 00:00:00';
$order->delivery_date = '0000-00-00 00:00:00';
// Amount paid by customer is not the right one -> Status = payment error
// We don't use the following condition to avoid the float precision issues : http://www.php.net/manual/en/language.types.float.php
// if ($order->total_paid != $order->total_paid_real)
// We use number_format in order to compare two string
if (number_format($order->total_paid, 2) != number_format($order->total_paid_real, 2))
$id_order_state = Configuration::get('PS_OS_ERROR');
// Creating order
if ($cart->OrderExists() == false)
$result = $order->add();
else
{
$errorMessage = Tools::displayError('An order has already been placed using this cart.');
Logger::addLog($errorMessage, 4, '0000001', 'Cart', intval($order->id_cart));
die($errorMessage);
}
// Next !
if ($result AND isset($order->id))
{
if (!$secure_key)
$message .= $this->l('Warning : the secure key is empty, check your payment account before validation');
// Optional message to attach to this order
if (isset($message) AND !empty($message))
{
$msg = new Message();
$message = strip_tags($message, '
');
if (Validate::isCleanHtml($message))
{
$msg->message = $message;
$msg->id_order = intval($order->id);
$msg->private = 1;
$msg->add();
}
}
// Insert products from cart into order_detail table
$products = $cart->getProducts();
$productsList = '';
$db = Db::getInstance();
$query = 'INSERT INTO `'._DB_PREFIX_.'order_detail`
(`id_order`, `product_id`, `product_attribute_id`, `product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`, `reduction_percent`, `reduction_amount`, `group_reduction`, `product_quantity_discount`, `product_ean13`, `product_upc`, `product_reference`, `product_supplier_reference`, `product_weight`, `tax_name`, `tax_rate`, `ecotax`, `ecotax_tax_rate`, `discount_quantity_applied`, `download_deadline`, `download_hash`)
VALUES ';
$customizedDatas = Product::getAllCustomizedDatas((int)($order->id_cart));
Product::addCustomizationPrice($products, $customizedDatas);
$outOfStock = false;
foreach ($products AS $key => $product)
{
$productQuantity = (int)(Product::getQuantity((int)($product['id_product']), ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL)));
$quantityInStock = ($productQuantity - (int)($product['cart_quantity']) < 0) ? $productQuantity : (int)($product['cart_quantity']);
if ($id_order_state != Configuration::get('PS_OS_CANCELED') AND $id_order_state != Configuration::get('PS_OS_ERROR'))
{
if (Product::updateQuantity($product, (int)$order->id))
$product['stock_quantity'] -= $product['cart_quantity'];
if ($product['stock_quantity'] < 0 && Configuration::get('PS_STOCK_MANAGEMENT'))
$outOfStock = true;
Product::updateDefaultAttribute($product['id_product']);
}
$price = Product::getPriceStatic((int)($product['id_product']), false, ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL), 6, NULL, false, true, $product['cart_quantity'], false, (int)($order->id_customer), (int)($order->id_cart), (int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
$price_wt = Product::getPriceStatic((int)($product['id_product']), true, ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL), 2, NULL, false, true, $product['cart_quantity'], false, (int)($order->id_customer), (int)($order->id_cart), (int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
// Add some informations for virtual products
$deadline = '0000-00-00 00:00:00';
$download_hash = NULL;
if ($id_product_download = ProductDownload::getIdFromIdProduct((int)($product['id_product'])))
{
$productDownload = new ProductDownload((int)($id_product_download));
$deadline = $productDownload->getDeadLine();
$download_hash = $productDownload->getHash();
}
// Exclude VAT
if (Tax::excludeTaxeOption())
{
$product['tax'] = 0;
$product['rate'] = 0;
$tax_rate = 0;
}
else
$tax_rate = Tax::getProductTaxRate((int)($product['id_product']), $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$ecotaxTaxRate = 0;
if (!empty($product['ecotax']))
$ecotaxTaxRate = Tax::getProductEcotaxRate($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$product_price = (float)Product::getPriceStatic((int)($product['id_product']), false, ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL), (Product::getTaxCalculationMethod((int)($order->id_customer)) == PS_TAX_EXC ? 2 : 6), NULL, false, false, $product['cart_quantity'], false, (int)($order->id_customer), (int)($order->id_cart), (int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}), $specificPrice, false);
$quantityDiscount = SpecificPrice::getQuantityDiscount((int)$product['id_product'], Shop::getCurrentShop(), (int)$cart->id_currency, (int)$vat_address->id_country, (int)$customer->id_default_group, (int)$product['cart_quantity']);
$unitPrice = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 2, NULL, false, true, 1, false, (int)$order->id_customer, NULL, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$quantityDiscountValue = $quantityDiscount ? ((Product::getTaxCalculationMethod((int)$order->id_customer) == PS_TAX_EXC ? Tools::ps_round($unitPrice, 2) : $unitPrice) - $quantityDiscount['price'] * (1 + $tax_rate / 100)) : 0.00;
$query .= '('.(int)($order->id).',
'.(int)($product['id_product']).',
'.(isset($product['id_product_attribute']) ? (int)($product['id_product_attribute']) : 'NULL').',
\''.pSQL($product['name'].((isset($product['attributes']) AND $product['attributes'] != NULL) ? ' - '.$product['attributes'] : '')).'\',
'.(int)($product['cart_quantity']).',
'.$quantityInStock.',
'.$product_price.',
'.(float)(($specificPrice AND $specificPrice['reduction_type'] == 'percentage') ? $specificPrice['reduction'] * 100 : 0.00).',
'.(float)(($specificPrice AND $specificPrice['reduction_type'] == 'amount') ? (!$specificPrice['id_currency'] ? Tools::convertPrice($specificPrice['reduction'], $order->id_currency) : $specificPrice['reduction']) : 0.00).',
'.(float)(Group::getReduction((int)($order->id_customer))).',
'.$quantityDiscountValue.',
'.(empty($product['ean13']) ? 'NULL' : '\''.pSQL($product['ean13']).'\'').',
'.(empty($product['upc']) ? 'NULL' : '\''.pSQL($product['upc']).'\'').',
'.(empty($product['reference']) ? 'NULL' : '\''.pSQL($product['reference']).'\'').',
'.(empty($product['supplier_reference']) ? 'NULL' : '\''.pSQL($product['supplier_reference']).'\'').',
'.(float)($product['id_product_attribute'] ? $product['weight_attribute'] : $product['weight']).',
\''.(empty($tax_rate) ? '' : pSQL($product['tax'])).'\',
'.(float)($tax_rate).',
'.(float)Tools::convertPrice(floatval($product['ecotax']), intval($order->id_currency)).',
'.(float)$ecotaxTaxRate.',
'.(($specificPrice AND $specificPrice['from_quantity'] > 1) ? 1 : 0).',
\''.pSQL($deadline).'\',
\''.pSQL($download_hash).'\'),';
$customizationQuantity = 0;
if (isset($customizedDatas[$product['id_product']][$product['id_product_attribute']]))
{
$customizationText = '';
foreach ($customizedDatas[$product['id_product']][$product['id_product_attribute']] AS $customization)
{
if (isset($customization['datas'][_CUSTOMIZE_TEXTFIELD_]))
foreach ($customization['datas'][_CUSTOMIZE_TEXTFIELD_] AS $text)
$customizationText .= $text['name'].':'.' '.$text['value'].'
';
if (isset($customization['datas'][_CUSTOMIZE_FILE_]))
$customizationText .= sizeof($customization['datas'][_CUSTOMIZE_FILE_]) .' '. Tools::displayError('image(s)').'
';
$customizationText .= '---
';
}
$customizationText = rtrim($customizationText, '---
');
$customizationQuantity = (int)($product['customizationQuantityTotal']);
$productsList .=
'