This commit is contained in:
Michael RICOIS 2018-01-26 10:50:02 +01:00
parent 063ce25d01
commit 67b9dd85be
2 changed files with 78 additions and 45 deletions

View File

@ -684,7 +684,7 @@ class Paybox extends PaymentModule
$transaction = $this->loadTransaction($order->id_cart);
if(empty($transaction)) {
if (empty($transaction)) {
return false;
} else {
$montant = $order->total_shipping;

View File

@ -679,35 +679,40 @@ class PayPal extends PaymentModule
public function hookCancelProduct($params)
{
// if (Tools::isSubmit('generateDiscount') || !$this->isPayPalAPIAvailable())
// return false;
// // delete test sur la génération d'avoir
if (!$this->isPayPalAPIAvailable())
if (!$this->isPayPalAPIAvailable()) {
return false;
elseif ($params['order']->module != $this->name || !($order = $params['order']) || !Validate::isLoadedObject($order))
}
elseif ($params['order']->module != $this->name
|| !($order = $params['order']) || !Validate::isLoadedObject($order)) {
return false;
elseif (!$order->hasBeenPaid())
}
elseif (!$order->hasBeenPaid()) {
return false;
}
$order_detail = new OrderDetail((int)$params['id_order_detail']);
if (!$order_detail || !Validate::isLoadedObject($order_detail))
return false;
$paypal_order = PayPalOrder::getOrderById((int)$order->id);
if (!$paypal_order)
if (!$paypal_order) {
return false;
}
$products = $order->getProducts();
$cancel_quantity = Tools::getValue('cancelQuantity');
$message = $this->l('Cancel products result:').'<br>';
// Get product amount to refund (HT)
$amount = (float)($products[(int)$order_detail->id]['product_price_wt'] * (int)$cancel_quantity[(int)$order_detail->id]);
if($amount > 0){
if ($amount > 0){
$refund = $this->_makeRefund($paypal_order['id_transaction'], (int)$order->id, $amount);
} else {
$refund = array();
$message .= $this->l('Transaction error because of the amount of the cancel product!').'<br>';
}
$this->formatMessage($refund, $message);
$this->_addNewPrivateMessage((int)$order->id, $message);
}
@ -1081,22 +1086,33 @@ class PayPal extends PaymentModule
private function _makeRefund($id_transaction, $id_order, $amt = false)
{
if (!$this->isPayPalAPIAvailable())
if (!$this->isPayPalAPIAvailable()) {
die(Tools::displayError('Fatal Error: no API Credentials are available'));
elseif (!$id_transaction)
}
elseif (!$id_transaction) {
die(Tools::displayError('Fatal Error: id_transaction is null'));
}
if (!$amt)
$params = array('TRANSACTIONID' => $id_transaction, 'REFUNDTYPE' => 'Full');
else
{
// No amount make a total refund
if (!$amt) {
$params = array(
'TRANSACTIONID' => $id_transaction,
'REFUNDTYPE' => 'Full'
);
}
else {
$iso_currency = Db::getInstance()->getValue('
SELECT `iso_code`
FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'currency` c ON (o.`id_currency` = c.`id_currency`)
WHERE o.`id_order` = '.(int)$id_order);
$params = array('TRANSACTIONID' => $id_transaction, 'REFUNDTYPE' => 'Partial', 'AMT' => (float)$amt, 'CURRENCYCODE' => Tools::strtoupper($iso_currency));
$params = array(
'TRANSACTIONID' => $id_transaction,
'REFUNDTYPE' => 'Partial',
'AMT' => (float)$amt,
'CURRENCYCODE' => Tools::strtoupper($iso_currency)
);
}
$paypal_lib = new PaypalLib();
@ -1122,53 +1138,69 @@ class PayPal extends PaymentModule
return $new_message->add();
}
public function generateTotalRefund($id_order){
public function generateTotalRefund($id_order)
{
$this->_doTotalRefund($id_order);
}
private function _doTotalRefund($id_order)
{
$paypal_order = PayPalOrder::getOrderById((int)$id_order);
if (!$this->isPayPalAPIAvailable() || !$paypal_order)
if (!$this->isPayPalAPIAvailable() || !$paypal_order) {
return false;
}
$order = new Order((int)$id_order);
if (!Validate::isLoadedObject($order))
if (!Validate::isLoadedObject($order)) {
return false;
}
$products = $order->getProducts();
$currency = new Currency((int)$order->id_currency);
if (!Validate::isLoadedObject($currency))
if (!Validate::isLoadedObject($currency)) {
$this->_errors[] = $this->l('Not a valid currency');
}
if (count($this->_errors))
if (count($this->_errors)) {
return false;
}
$decimals = (is_array($currency) ? (int)$currency['decimals'] : (int)$currency->decimals) * _PS_PRICE_DISPLAY_PRECISION_;
// Amount for refund
$amt = 0.00;
foreach ($products as $product)
// Add product price * ( quantity total - quantity already refunded )
foreach ($products as $product) {
$amt += (float)($product['product_price_wt']) * ($product['product_quantity'] - $product['product_quantity_refunded']);
}
// Add shipping, add wrapping, remove discount
$amt += (float)($order->total_shipping) + (float)($order->total_wrapping) - (float)($order->total_discounts);
if($amt > 0){
// @todo : add frais de port
if ($amt > 0) {
// check if total or partial
if (Tools::ps_round($order->total_paid_real, $decimals) == Tools::ps_round($amt, $decimals))
if (Tools::ps_round($order->total_paid_real, $decimals) == Tools::ps_round($amt, $decimals)) {
$response = $this->_makeRefund($paypal_order['id_transaction'], $id_order);
else
}
else {
$response = $this->_makeRefund($paypal_order['id_transaction'], $id_order, (float)($amt));
}
$message = $this->l('Refund operation result:').'<br>';
foreach ($response as $key => $value)
foreach ($response as $key => $value) {
$message .= $key.': '.$value.'<br>';
}
if (array_key_exists('ACK', $response) && $response['ACK'] == 'Success' && $response['REFUNDTRANSACTIONID'] != '')
{
$message .= $this->l('PayPal refund successful!');
if (!Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'paypal_order` SET `payment_status` = \'Refunded\' WHERE `id_order` = '.(int)$id_order))
if (!Db::getInstance()->Execute(
'UPDATE `'._DB_PREFIX_.'paypal_order` SET `payment_status` = \'Refunded\'
WHERE `id_order` = '.(int)$id_order)) {
die(Tools::displayError('Error when updating PayPal database'));
}
$history = new OrderHistory();
$history->id_order = (int)$id_order;
@ -1176,8 +1208,9 @@ class PayPal extends PaymentModule
$history->addWithemail();
$history->save();
}
else
else {
$message .= $this->l('Transaction error!');
}
} else {
$message = $this->l('Refund operation result:').'<br>';
$message .= $this->l('Transaction error because of the amount (amt)!');