Untitled diff

Created Diff never expires
7 rimozioni
Linee
Totale
Rimosso
Parole
Totale
Rimosso
Per continuare a utilizzare questa funzione, aggiorna a
Diffchecker logo
Diffchecker Pro
509 linee
34 aggiunte
Linee
Totale
Aggiunto
Parole
Totale
Aggiunto
Per continuare a utilizzare questa funzione, aggiorna a
Diffchecker logo
Diffchecker Pro
534 linee
<?php
<?php
/*
/*
* 2007-2013 PrestaShop
* 2007-2013 PrestaShop
*
*
* NOTICE OF LICENSE
* NOTICE OF LICENSE
*
*
* This source file is subject to the Academic Free License (AFL 3.0)
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
* to license@prestashop.com so we can send you a copy immediately.
*
*
* DISCLAIMER
* DISCLAIMER
*
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
* needs please refer to http://www.prestashop.com for more information.
*
*
* @author PrestaShop SA <contact@prestashop.com>
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
* International Registered Trademark & Property of PrestaShop SA
*/
*/
include_once(_PS_MODULE_DIR_.'paypal/paypal.php');
include_once(_PS_MODULE_DIR_.'paypal/paypal.php');
include_once(_PS_MODULE_DIR_.'paypal/api/paypal_lib.php');
include_once(_PS_MODULE_DIR_.'paypal/api/paypal_lib.php');
//****** Dev only
//****** Dev only
error_reporting(E_ALL);
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_errors', '1');
include_once('Logging.php');
// Logging class initialization
$logger = new Logging();
$logger->lfile('mylog.txt');
class PaypalExpressCheckout extends Paypal
class PaypalExpressCheckout extends Paypal
{
{
public $logs = array();
public $logs = array();
public $method_version = '84';
public $method_version = '84';
public $method;
public $method;
/** @var currency Currency used for the payment process **/
/** @var currency Currency used for the payment process **/
public $currency;
public $currency;
/** @var decimals Used to set prices precision **/
/** @var decimals Used to set prices precision **/
public $decimals;
public $decimals;
/** @var result Contains the last request result **/
/** @var result Contains the last request result **/
public $result;
public $result;
/** @var token Contains the last token **/
/** @var token Contains the last token **/
public $token;
public $token;
// Depending of the type set, id_cart or id_product will be set
// Depending of the type set, id_cart or id_product will be set
public $id_cart;
public $id_cart;
// Depending of the type set, id_cart or id_product will be set
// Depending of the type set, id_cart or id_product will be set
public $id_product;
public $id_product;
public $id_p_attr;
public $id_p_attr;
public $quantity;
public $quantity;
public $payer_id;
public $payer_id;
public $available_type = array('cart', 'product', 'payment_cart');
public $available_type = array('cart', 'product', 'payment_cart');
public $total_different_product;
public $total_different_product;
public $product_list = array();
public $product_list = array();
// Used to know if user can validated his payment after shipping / address selection
// Used to know if user can validated his payment after shipping / address selection
public $ready = false;
public $ready = false;
// Take for now cart or product value
// Take for now cart or product value
public $type = false;
public $type = false;
static public $COOKIE_NAME = 'express_checkout';
static public $COOKIE_NAME = 'express_checkout';
public $cookie_key = array(
public $cookie_key = array(
'token', 'id_product', 'id_p_attr',
'token', 'id_product', 'id_p_attr',
'quantity', 'type', 'total_different_product',
'quantity', 'type', 'total_different_product',
'secure_key', 'ready', 'payer_id'
'secure_key', 'ready', 'payer_id'
);
);
public function __construct($type = false)
public function __construct($type = false)
{
{
parent::__construct();
parent::__construct();
// If type is sent, the cookie has to be delete
// If type is sent, the cookie has to be delete
if ($type)
if ($type)
{
{
unset($this->context->cookie->{self::$COOKIE_NAME});
unset($this->context->cookie->{self::$COOKIE_NAME});
$this->setExpressCheckoutType($type);
$this->setExpressCheckoutType($type);
}
}
// Store back the PayPal data if present under the cookie
// Store back the PayPal data if present under the cookie
if (isset($this->context->cookie->{self::$COOKIE_NAME}))
if (isset($this->context->cookie->{self::$COOKIE_NAME}))
{
{
$paypal = unserialize($this->context->cookie->{self::$COOKIE_NAME});
$paypal = unserialize($this->context->cookie->{self::$COOKIE_NAME});
foreach ($this->cookie_key as $key)
foreach ($this->cookie_key as $key)
$this->{$key} = $paypal[$key];
$this->{$key} = $paypal[$key];
}
}
$this->currency = new Currency((int)$this->context->cart->id_currency);
$this->currency = new Currency((int)$this->context->cart->id_currency);
if (!Validate::isLoadedObject($this->currency))
if (!Validate::isLoadedObject($this->currency))
$this->_errors[] = $this->l('Not a valid currency');
$this->_errors[] = $this->l('Not a valid currency');
if (count($this->_errors))
if (count($this->_errors))
return false;
return false;
$currency_decimals = is_array($this->currency) ? (int)$this->currency['decimals'] : (int)$this->currency->decimals;
$currency_decimals = is_array($this->currency) ? (int)$this->currency['decimals'] : (int)$this->currency->decimals;
$this->decimals = $currency_decimals * _PS_PRICE_DISPLAY_PRECISION_;
$this->decimals = $currency_decimals * _PS_PRICE_DISPLAY_PRECISION_;
}
}
// Will build the product_list depending of the type
// Will build the product_list depending of the type
private function initParameters()
private function initParameters()
{
{
if (!$this->context->cart || !$this->context->cart->id)
if (!$this->context->cart || !$this->context->cart->id)
return false;
return false;
$cart_currency = new Currency((int)$this->context->cart->id_currency);
$cart_currency = new Currency((int)$this->context->cart->id_currency);
$currency_module = $this->getCurrency((int)$this->context->cart->id_currency);
$currency_module = $this->getCurrency((int)$this->context->cart->id_currency);
if ($cart_currency !== $currency_module)
if ($cart_currency !== $currency_module)
{
{
$this->context->cart->id_currency = $currency_module->id;
$this->context->cart->id_currency = $currency_module->id;
$this->context->cart->update();
$this->context->cart->update();
}
}
$this->context->currency = $currency_module;
$this->context->currency = $currency_module;
$this->product_list = $this->context->cart->getProducts();
$this->product_list = $this->context->cart->getProducts();
return (bool)count($this->product_list);
return (bool)count($this->product_list);
}
}
public function setExpressCheckout()
public function setExpressCheckout()
{
{
$this->method = 'SetExpressCheckout';
$this->method = 'SetExpressCheckout';
$this->setCancelUrl($fields);
$this->setCancelUrl($fields);
// Only this call need to get the value from the $_GET / $_POST array
// Only this call need to get the value from the $_GET / $_POST array
if (!$this->initParameters(true) || !$fields['CANCELURL'])
if (!$this->initParameters(true) || !$fields['CANCELURL'])
return false;
return false;
// Set payment detail (reference)
// Set payment detail (reference)
$this->_setPaymentDetails($fields);
$this->_setPaymentDetails($fields);
$fields['SOLUTIONTYPE'] = 'Sole';
$fields['SOLUTIONTYPE'] = 'Sole';
$fields['LANDINGPAGE'] = 'Login';
$fields['LANDINGPAGE'] = 'Login';
// Seller informations
// Seller informations
$fields['USER'] = Configuration::get('PAYPAL_API_USER');
$fields['USER'] = Configuration::get('PAYPAL_API_USER');
$fields['PWD'] = Configuration::get('PAYPAL_API_PASSWORD');
$fields['PWD'] = Configuration::get('PAYPAL_API_PASSWORD');
$fields['SIGNATURE'] = Configuration::get('PAYPAL_API_SIGNATURE');
$fields['SIGNATURE'] = Configuration::get('PAYPAL_API_SIGNATURE');
$this->callAPI($fields);
$this->callAPI($fields);
$this->_storeToken();
$this->_storeToken();
}
}
public function setCancelUrl(&$fields)
public function setCancelUrl(&$fields)
{
{
$parsed_data = parse_url(Tools::getValue('current_shop_url'));
$parsed_data = parse_url(Tools::getValue('current_shop_url'));
$parsed_data['scheme'] .= '://';
$parsed_data['scheme'] .= '://';
$parsed_data['path'] .= '?';
$parsed_data['path'] .= '?';
$parsed_data['query'] = '&paypal_ec_canceled=1';
$parsed_data['query'] = '&paypal_ec_canceled=1';
$fields['CANCELURL'] = implode($parsed_data);
$fields['CANCELURL'] = implode($parsed_data);
}
}
public function getExpressCheckout()
public function getExpressCheckout()
{
{
$this->method = 'GetExpressCheckoutDetails';
$this->method = 'GetExpressCheckoutDetails';
$fields['TOKEN'] = $this->token;
$fields['TOKEN'] = $this->token;
$this->initParameters();
$this->initParameters();
$this->callAPI($fields);
$this->callAPI($fields);
// The same token of SetExpressCheckout
// The same token of SetExpressCheckout
$this->_storeToken();
$this->_storeToken();
}
}
public function doExpressCheckout()
public function doExpressCheckout()
{
{
$this->method = 'DoExpressCheckoutPayment';
$this->method = 'DoExpressCheckoutPayment';
$fields['TOKEN'] = $this->token;
$fields['TOKEN'] = $this->token;
$fields['PAYERID'] = $this->payer_id;
$fields['PAYERID'] = $this->payer_id;
if (count($this->product_list) <= 0)
if (count($this->product_list) <= 0)
$this->initParameters();
$this->initParameters();
// Set payment detail (reference)
// Set payment detail (reference)
$this->_setPaymentDetails($fields);
$this->_setPaymentDetails($fields);
$this->callAPI($fields);
$this->callAPI($fields);
$this->result += $fields;
$this->result += $fields;
}
}
private function callAPI($fields)
private function callAPI($fields)
{
{
$this->logs = array();
$this->logs = array();
$paypal_lib = new PaypalLib();
$paypal_lib = new PaypalLib();
$this->result = $paypal_lib->makeCall($this->getAPIURL(), $this->getAPIScript(), $this->method, $fields, $this->method_version);
$this->result = $paypal_lib->makeCall($this->getAPIURL(), $this->getAPIScript(), $this->method, $fields, $this->method_version);
$this->logs = array_merge($this->logs, $paypal_lib->getLogs());
$this->logs = array_merge($this->logs, $paypal_lib->getLogs());
$this->_storeToken();
$this->_storeToken();
}
}
private function _setPaymentDetails(&$fields)
private function _setPaymentDetails(&$fields)
{
{
// Required field
// Required field
$fields['RETURNURL'] = PayPal::getShopDomainSsl(true, true)._MODULE_DIR_.$this->name.'/express_checkout/payment.php';
$fields['RETURNURL'] = PayPal::getShopDomainSsl(true, true)._MODULE_DIR_.$this->name.'/express_checkout/payment.php';
$fields['REQCONFIRMSHIPPING'] = '0';
$fields['REQCONFIRMSHIPPING'] = '0';
$fields['NOSHIPPING'] = '1';
$fields['NOSHIPPING'] = '1';
$fields['BUTTONSOURCE'] = $this->getTrackingCode();
$fields['BUTTONSOURCE'] = $this->getTrackingCode();
// Products
// Products
$taxes = $total = 0;
$taxes = $total = 0;
$index = -1;
$index = -1;
global $logger;
// Set cart products list
// Set cart products list
$this->setProductsList($fields, $index, $total, $taxes);
$logger->lwrite( 'Total pre-setProductsList = '.$total );
$this->setProductsList($fields, $index, $total, $taxes);
$logger->lwrite( 'Total pre-setDiscountsList = '.$total );
$this->setDiscountsList($fields, $index, $total, $taxes);
$this->setDiscountsList($fields, $index, $total, $taxes);
$logger->lwrite( 'Total pre-setGiftWrapping = '.$total );
$this->setGiftWrapping($fields, $index, $total);
$this->setGiftWrapping($fields, $index, $total);
// Payment values
// Payment values
$logger->lwrite( 'Total pre-setPaymentValues = '.$total );
$this->setPaymentValues($fields, $index, $total, $taxes);
$this->setPaymentValues($fields, $index, $total, $taxes);
$id_address = (int)$this->context->cart->id_address_delivery;
$id_address = (int)$this->context->cart->id_address_delivery;
if (($id_address == 0) && ($this->context->customer))
if (($id_address == 0) && ($this->context->customer))
$id_address = Address::getFirstCustomerAddressId($this->context->customer->id);
$id_address = Address::getFirstCustomerAddressId($this->context->customer->id);
if ($id_address && method_exists($this->context->cart, 'isVirtualCart') && !$this->context->cart->isVirtualCart())
if ($id_address && method_exists($this->context->cart, 'isVirtualCart') && !$this->context->cart->isVirtualCart()) {
$logger->lwrite( 'Total pre-setShippingAddress = '.$total );
$this->setShippingAddress($fields, $id_address);
$this->setShippingAddress($fields, $id_address);
}
else
else
$fields['NOSHIPPING'] = '0';
$fields['NOSHIPPING'] = '0';
foreach ($fields as &$field)
foreach ($fields as &$field)
if (is_numeric($field))
if (is_numeric($field))
$field = str_replace(',', '.', $field);
$field = str_replace(',', '.', $field);
}
}
private function setShippingAddress(&$fields, $id_address)
private function setShippingAddress(&$fields, $id_address)
{
{
$address = new Address($id_address);
$address = new Address($id_address);
$fields['ADDROVERRIDE'] = '1';
$fields['ADDROVERRIDE'] = '1';
$fields['EMAIL'] = $this->context->customer->email;
$fields['EMAIL'] = $this->context->customer->email;
$fields['PAYMENTREQUEST_0_SHIPTONAME'] = $address->firstname.' '.$address->lastname;
$fields['PAYMENTREQUEST_0_SHIPTONAME'] = $address->firstname.' '.$address->lastname;
$fields['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = (empty($address->phone)) ? $address->phone_mobile : $address->phone;
$fields['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = (empty($address->phone)) ? $address->phone_mobile : $address->phone;
$fields['PAYMENTREQUEST_0_SHIPTOSTREET'] = $address->address1;
$fields['PAYMENTREQUEST_0_SHIPTOSTREET'] = $address->address1;
$fields['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $address->address2;
$fields['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $address->address2;
$fields['PAYMENTREQUEST_0_SHIPTOCITY'] = $address->city;
$fields['PAYMENTREQUEST_0_SHIPTOCITY'] = $address->city;
if ($address->id_state)
if ($address->id_state)
{
{
$state = new State((int)$address->id_state);
$state = new State((int)$address->id_state);
$fields['PAYMENTREQUEST_0_SHIPTOSTATE'] = $state->iso_code;
$fields['PAYMENTREQUEST_0_SHIPTOSTATE'] = $state->iso_code;
}
}
$country = new Country((int)$address->id_country);
$country = new Country((int)$address->id_country);
$fields['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $country->iso_code;
$fields['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $country->iso_code;
$fields['PAYMENTREQUEST_0_SHIPTOZIP'] = $address->postcode;
$fields['PAYMENTREQUEST_0_SHIPTOZIP'] = $address->postcode;
}
}
private function setProductsList(&$fields, &$index, &$total)
private function setProductsList(&$fields, &$index, &$total)
{
{
foreach ($this->product_list as $product)
foreach ($this->product_list as $product)
{
{
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = (int)$product['id_product'];
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = (int)$product['id_product'];
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = $product['name'];
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = $product['name'];
$fields['L_PAYMENTREQUEST_0_DESC'.$index] = substr(strip_tags($product['description_short']), 0, 120).'...';
$fields['L_PAYMENTREQUEST_0_DESC'.$index] = substr(strip_tags($product['description_short']), 0, 120).'...';
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($product['price_wt'], $this->decimals);
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($product['price'], $this->decimals);
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = $product['quantity'];
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = $product['quantity'];
$product_tax = Tools::ps_round($product['price_wt'] - $product['price'], $this->decimals);
$total = $total + ($fields['L_PAYMENTREQUEST_0_AMT'.$index] * $product['quantity']);
$total = $total + (($fields['L_PAYMENTREQUEST_0_AMT'.$index] /*+ $product_tax*/)* $product['quantity']);
}
}
}
}
private function setDiscountsList(&$fields, &$index, &$total)
private function setDiscountsList(&$fields, &$index, &$total)
{
{
$discounts = (_PS_VERSION_ < '1.5') ? $this->context->cart->getDiscounts() : $this->context->cart->getCartRules();
$discounts = (_PS_VERSION_ < '1.5') ? $this->context->cart->getDiscounts() : $this->context->cart->getCartRules();
if (count($discounts) > 0)
if (count($discounts) > 0)
foreach ($discounts as $discount)
foreach ($discounts as $discount)
{
{
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = $discount['id_discount'];
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = $discount['id_discount'];
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = $discount['name'];
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = $discount['name'];
if (isset($discount['description']) && !empty($discount['description']))
if (isset($discount['description']) && !empty($discount['description']))
$fields['L_PAYMENTREQUEST_0_DESC'.$index] = substr(strip_tags($discount['description']), 0, 120).'...';
$fields['L_PAYMENTREQUEST_0_DESC'.$index] = substr(strip_tags($discount['description']), 0, 120).'...';
/* It is a discount so we store a negative value */
/* It is a discount so we store a negative value */
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = -1 * Tools::ps_round($discount['value_real'], $this->decimals);
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = -1 * Tools::ps_round($discount['value_real'], $this->decimals);
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$total = Tools::ps_round($total + $fields['L_PAYMENTREQUEST_0_AMT'.$index], $this->decimals);
$total = Tools::ps_round($total + $fields['L_PAYMENTREQUEST_0_AMT'.$index], $this->decimals);
}
}
}
}
private function setGiftWrapping(&$fields, &$index, &$total)
private function setGiftWrapping(&$fields, &$index, &$total)
{
{
if ($this->context->cart->gift == 1)
if ($this->context->cart->gift == 1)
{
{
$gift_wrapping_price = $this->getGiftWrappingPrice();
$gift_wrapping_price = $this->getGiftWrappingPrice();
$fields['L_PAYMENTREQUEST_0_NAME'.++$index] = $this->l('Gift wrapping');
$fields['L_PAYMENTREQUEST_0_NAME'.++$index] = $this->l('Gift wrapping');
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($gift_wrapping_price, $this->decimals);
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($gift_wrapping_price, $this->decimals);
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$total = Tools::ps_round($total + $gift_wrapping_price, $this->decimals);
$total = Tools::ps_round($total + $gift_wrapping_price, $this->decimals);
}
}
}
}
private function setPaymentValues(&$fields, &$index, &$total, &$taxes)
private function setPaymentValues(&$fields, &$index, &$total, &$taxes)
{
{
if (_PS_VERSION_ < '1.5')
if (_PS_VERSION_ < '1.5')
$shipping_cost_wt = $this->context->cart->getOrderShippingCost();
$shipping_cost_wt = $this->context->cart->getOrderShippingCost();
else
else
$shipping_cost_wt = $this->context->cart->getTotalShippingCost();
$shipping_cost_wt = $this->context->cart->getTotalShippingCost(null, false);
if ((bool)Configuration::get('PAYPAL_CAPTURE'))
if ((bool)Configuration::get('PAYPAL_CAPTURE'))
$fields['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Authorization';
$fields['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Authorization';
else
else
$fields['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale';
$fields['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale';
$currency = new Currency((int)$this->context->cart->id_currency);
$currency = new Currency((int)$this->context->cart->id_currency);
$fields['PAYMENTREQUEST_0_CURRENCYCODE'] = $currency->iso_code;
$fields['PAYMENTREQUEST_0_CURRENCYCODE'] = $currency->iso_code;
/**
/**
* If the total amount is lower than 1 we put the shipping cost as an item
* If the total amount is lower than 1 we put the shipping cost as an item
* so the payment could be valid.
* so the payment could be valid.
*/
*/
if ($total <= 1)
if ($total <= 1)
{
{
$carrier = new Carrier($this->context->cart->id_carrier);
$carrier = new Carrier($this->context->cart->id_carrier);
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = $carrier->id_reference;
$fields['L_PAYMENTREQUEST_0_NUMBER'.++$index] = $carrier->id_reference;
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = $carrier->name;
$fields['L_PAYMENTREQUEST_0_NAME'.$index] = $carrier->name;
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['L_PAYMENTREQUEST_0_AMT'.$index] = Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$fields['L_PAYMENTREQUEST_0_QTY'.$index] = 1;
$fields['PAYMENTREQUEST_0_ITEMAMT'] = Tools::ps_round($total, $this->decimals) + Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['PAYMENTREQUEST_0_ITEMAMT'] = Tools::ps_round($total, $this->decimals) + Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['PAYMENTREQUEST_0_AMT'] = $total + Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['PAYMENTREQUEST_0_AMT'] = $total + Tools::ps_round($shipping_cost_wt, $this->decimals);
}
}
else
else
{
{
$fields['PAYMENTREQUEST_0_SHIPPINGAMT'] = Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['PAYMENTREQUEST_0_SHIPPINGAMT'] = Tools::ps_round($shipping_cost_wt, $this->decimals);
$fields['PAYMENTREQUEST_0_ITEMAMT'] = Tools::ps_round($total, $this->decimals);
$fields['PAYMENTREQUEST_0_ITEMAMT'] = Tools::ps_round($total, $this->decimals);
$fields['PAYMENTREQUEST_0_AMT'] = $total + $fields['PAYMENTREQUEST_0_SHIPPINGAMT'];
$fields['PAYMENTREQUEST_0_TAXAMT'] = Tools::ps_round($this->context->cart->getOrderTotal() - $this->context->cart->getOrderTotal(false), $this->decimals);
$fields['PAYMENTREQUEST_0_AMT'] = Tools::ps_round($total + $fields['PAYMENTREQUEST_0_SHIPPINGAMT'] + $fields['PAYMENTREQUEST_0_TAXAMT'], $this->decimals);
}
}
global $logger;
//require_once('Logger.php');
$logger->lwrite( 'setPaymentValues:PAYMENTREQUEST_0_AMT = '.$fields['PAYMENTREQUEST_0_AMT'] );
$logger->lwrite( 'setPaymentValues:PAYMENTREQUEST_0_ITEMAMT = '.$fields['PAYMENTREQUEST_0_ITEMAMT'] );
$logger->lwrite( 'setPaymentValues:PAYMENTREQUEST_0_TAXAMT = '.$fields['PAYMENTREQUEST_0_TAXAMT'] );
$logger->lwrite( 'setPaymentValues:PAYMENTREQUEST_0_SHIPPINGAMT = '.$fields['PAYMENTREQUEST_0_SHIPPINGAMT'] );
$logger->lwrite( var_export($fields, true) );
}
}
public function rightPaymentProcess()
public function rightPaymentProcess()
{
{
$total = $this->getTotalPaid();
$total = $this->getTotalPaid();
// float problem with php, have to use the string cast.
// float problem with php, have to use the string cast.
if ((isset($this->result['AMT']) && ((string)$this->result['AMT'] != (string)$total)) ||
if ((isset($this->result['AMT']) && ((string)$this->result['AMT'] != (string)$total)) ||
(isset($this->result['PAYMENTINFO_0_AMT']) && ((string)$this->result['PAYMENTINFO_0_AMT'] != (string)$total)))
(isset($this->result['PAYMENTINFO_0_AMT']) && ((string)$this->result['PAYMENTINFO_0_AMT'] != (string)$total)))
return false;
return false;
return true;
return true;
}
}
/**
/**
* @return mixed
* @return mixed
*/
*/
public function getTotalPaid()
public function getTotalPaid()
{
{
$total = 0.00;
$total = 0.00;
foreach ($this->product_list as $product)
foreach ($this->product_list as $product)
{
{
$price = Tools::ps_round($product['price_wt'], $this->decimals);
$price = Tools::ps_round($product['price_wt'], $this->decimals);
$quantity = Tools::ps_round($product['quantity'], $this->decimals);
$quantity = Tools::ps_round($product['quantity'], $this->decimals);
$total = Tools::ps_round($total + ($price * $quantity), $this->decimals);
$total = Tools::ps_round($total + ($price * $quantity), $this->decimals);
}
}
if ($this->context->cart->gift == 1)
if ($this->context->cart->gift == 1)
$total = Tools::ps_round($total + $this->getGiftWrappingPrice(), $this->decimals);
$total = Tools::ps_round($total + $this->getGiftWrappingPrice(), $this->decimals);
if (_PS_VERSION_ < '1.5')
if (_PS_VERSION_ < '1.5')
{
{
$discounts = $this->context->cart->getDiscounts();
$discounts = $this->context->cart->getDiscounts();
$shipping_cost = $this->context->cart->getOrderShippingCost();
$shipping_cost = $this->context->cart->getOrderShippingCost();
}
}
else
else
{
{
$discounts = $this->context->cart->getCartRules();
$discounts = $this->context->cart->getCartRules();
$shipping_cost = $this->context->cart->getTotalShippingCost();
$shipping_cost = $this->context->cart->getTotalShippingCost();
}
}
if (count($discounts) > 0)
if (count($discounts) > 0)
foreach ($discounts as $product)
foreach ($discounts as $product)
{
{
$price = -1 * Tools::ps_round($product['value_real'], $this->decimals);
$price = -1 * Tools::ps_round($product['value_real'], $this->decimals);
$total = Tools::ps_round($total + $price, $this->decimals);
$total = Tools::ps_round($total + $price, $this->decimals);
}
}
return Tools::ps_round($shipping_cost, $this->decimals) + $total;
return Tools::ps_round($shipping_cost, $this->decimals) + $total;
}
}
private function _storeToken()
private function _storeToken()
{
{
if (is_array($this->result) && isset($this->result['TOKEN']))
if (is_array($this->result) && isset($this->result['TOKEN']))
$this->token = (string)$this->result['TOKEN'];
$this->token = (string)$this->result['TOKEN'];
}
}
// Store data for the next reloading page
// Store data for the next reloading page
private function _storeCookieInfo()
private function _storeCookieInfo()
{
{
$tab = array();
$tab = array();
foreach ($this->cookie_key as $key)
foreach ($this->cookie_key as $key)
$tab[$key] = $this->{$key};
$tab[$key] = $this->{$key};
$this->context->cookie->{self::$COOKIE_NAME} = serialize($tab);
$this->context->cookie->{self::$COOKIE_NAME} = serialize($tab);
}
}
public function hasSucceedRequest()
public function hasSucceedRequest()
{
{
if (is_array($this->result))
if (is_array($this->result))
foreach(array('ACK', 'PAYMENTINFO_0_ACK') as $key)
foreach(array('ACK', 'PAYMENTINFO_0_ACK') as $key)
if (isset($this->result[$key]) && strtoupper($this->result[$key]) == 'SUCCESS')
if (isset($this->result[$key]) && strtoupper($this->result[$key]) == 'SUCCESS')
return true;
return true;
return false;
return false;
}
}
private function getSecureKey()
private function getSecureKey()
{
{
if (!count($this->product_list))
if (!count($this->product_list))
$this->initParameters();
$this->initParameters();
$key = array();
$key = array();
foreach($this->product_list as $product)
foreach($this->product_list as $product)
{
{
$id_product = $product['id_product'];
$id_product = $product['id_product'];
$id_product_attribute = $product['id_product_attribute'];
$id_product_attribute = $product['id_product_attribute'];
$quantity = $product['quantity'];
$quantity = $product['quantity'];
$key[] = $id_product.$id_product_attribute.$quantity._COOKIE_KEY_;
$key[] = $id_product.$id_product_attribute.$quantity._COOKIE_KEY_;
}
}
return md5(serialize($key));
return md5(serialize($key));
}
}
public function isProductsListStillRight()
public function isProductsListStillRight()
{
{
return $this->secure_key == $this->getSecureKey();
return $this->secure_key == $this->getSecureKey();
}
}
public function setExpressCheckoutType($type)
public function setExpressCheckoutType($type)
{
{
if (in_array($type, $this->available_type))
if (in_array($type, $this->available_type))
{
{
$this->type = $type;
$this->type = $type;
return true;
return true;
}
}
return false;
return false;
}
}
public function redirectToAPI()
public function redirectToAPI()
{
{
$this->secure_key = $this->getSecureKey();
$this->secure_key = $this->getSecureKey();
$this->_storeCookieInfo();
$this->_storeCookieInfo();
if ($this->useMobile())
if ($this->useMobile())
$url = '/cgi-bin/webscr?cmd=_express-checkout-mobile';
$url = '/cgi-bin/webscr?cmd=_express-checkout-mobile';
else
else
$url = '/websc&cmd=_express-checkout';
$url = '/websc&cmd=_express-checkout';
header('Location: https://'.$this->getPayPalURL().$url.'&token='.urldecode($this->token));
header('Location: https://'.$this->getPayPalURL().$url.'&token='.urldecode($this->token));
exit(0);
exit(0);
}
}
public function redirectToCheckout($customer, $redirect = false)
public function redirectToCheckout($customer, $redirect = false)
{
{
$this->ready = true;
$this->ready = true;
$this->_storeCookieInfo();
$this->_storeCookieInfo();
$this->context->cookie->id_customer = (int)$customer->id;
$this->context->cookie->id_customer = (int)$customer->id;
$this->context->cookie->customer_lastname = $customer->lastname;
$this->context->cookie->customer_lastname = $customer->lastname;
$this->context->cookie->customer_firstname = $customer->firstname;
$this->context->cookie->customer_firstname = $customer->firstname;
$this->context->cookie->passwd = $customer->passwd;
$this->context->cookie->passwd = $customer->passwd;
$this->context->cookie->email = $customer->email;
$this->context->cookie->email = $customer->email;
$this->context->cookie->is_guest = $customer->isGuest();
$this->context->cookie->is_guest = $customer->isGuest();
$this->context->cookie->logged = 1;
$this->context->cookie->logged = 1;
if (_PS_VERSION_ < '1.5')
if (_PS_VERSION_ < '1.5')
Module::hookExec('authentication');
Module::hookExec('authentication');
else
else
Hook::exec('authentication');
Hook::exec('authentication');
if ($redirect)
if ($redirect)
{
{
$link = $this->context->link->getPageLink('order.php', false, null, array('step' => '3'));
$link = $this->context->link->getPageLink('order.php', false, null, array('step' => '3'));
Tools::redirectLink($link);
Tools::redirectLink($link);
exit(0);
exit(0);
}
}
}
}
}
}