bebeboutik/modules/paybox/direct_paiement.php

396 lines
12 KiB
PHP
Raw Normal View History

2016-07-15 17:49:13 +02:00
<?php
require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php');
require_once(dirname(__FILE__).'/paybox.php');
global $cart, $smarty;
$module = new Paybox();
$controller->preProcess();
if (!$cookie->isLogged())
Tools::redirect('authentication.php');
2017-06-23 10:11:06 +02:00
// verif if there's one order already then redirect to the order detail
2017-06-22 16:53:29 +02:00
if ($id_order = (int)Order::getOrderByCartId($cart->id))
{
// log
$content = '';
if (file_exists('logs.txt')) {
$content = file_get_contents('logs.txt');
}
$line = 'SECONDE TENTATIVE ['.(int)$cart->id.'] '.date('Y-m-d H:i:s').': Redirect id_order='.$id_order."\n";
$content .= $line;
file_put_contents('logs.txt', $content);
// end log
2017-06-23 10:11:06 +02:00
2017-06-22 16:53:29 +02:00
Tools::redirect('order-detail.php?id_order='.$id_order);
}
2016-09-08 17:48:51 +02:00
// verif if there's at least one order with this delivery address
$orders_with_delivery_address = Db::getInstance()->getValue('
SELECT COUNT(`id_order`)
2016-09-08 17:51:04 +02:00
FROM `'._DB_PREFIX_.'orders`
2016-09-08 17:48:51 +02:00
WHERE `id_address_delivery` = '.(int)$cart->id_address_delivery.'
AND `id_customer` = '.(int)$cart->id_customer.'
');
if ($orders_with_delivery_address == 0) {
Tools::redirect('order.php?step=3&cgv=1&paybox_error=1');
}
2016-07-18 11:45:18 +02:00
$id_paybox_card = Tools::getValue('id_paybox_card', false);
if ($id_paybox_card) {
$paybox_card = Db::getInstance()->getRow('
2016-07-15 17:49:13 +02:00
SELECT *
FROM `ps_paybox_customer_agreement`
2016-07-18 11:45:18 +02:00
WHERE `id_customer` = '.(int)$cart->id_customer.'
AND `id_paybox_card` = '.(int)$id_paybox_card.'
2016-07-15 17:49:13 +02:00
');
2016-07-18 12:46:58 +02:00
if(empty($paybox_card) || (int)$paybox_card['id_customer']!=(int)$cart->id_customer) {
2016-07-18 11:45:18 +02:00
Tools::redirect('order.php?step=3&cgv=1&paybox_error=1');
2016-07-15 17:49:13 +02:00
}
2016-08-24 17:48:50 +02:00
} else {
Tools::redirect('order.php?step=3&cgv=1&paybox_error=1');
2016-07-15 17:49:13 +02:00
}
2016-08-24 17:48:50 +02:00
2016-07-15 17:49:13 +02:00
include(dirname(__FILE__).'/../../header.php');
if(Configuration::get('PBX_DEMO_MODE') == 0) {
$curl = curl_init('https://preprod-ppps.paybox.com/PPPS.php');
} else {
2016-09-26 11:46:37 +02:00
$curl = curl_init('https://ppps.paybox.com/PPPS.php');
2016-07-15 17:49:13 +02:00
}
2016-08-24 17:48:50 +02:00
2016-07-15 17:49:13 +02:00
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
$pbx_site = trim(Configuration::get('PBX_SITE'));
$pbx_rang = trim(Configuration::get('PBX_RANG'));
$pbx_id = trim(Configuration::get('PBX_ID'));
$num_question = Configuration::get('NUM_QUESTION_PAYBOX') + 1;
Configuration::updateValue('NUM_QUESTION_PAYBOX', $num_question);
2016-09-14 16:00:50 +02:00
$montant = "".(Tools::convertPrice($cart->getOrderTotal(), null, false) * 100)."";
2016-08-24 17:48:50 +02:00
$devise = 978;
$numbers = urldecode($paybox_card['handle']);
$dateval = $paybox_card['date'];
$customer = new Customer((int)$cart->id_customer);
$refabonne = $paybox_card['refabonne'];
2016-09-08 16:47:36 +02:00
$language = new Language((int)$cart->id_lang);
2016-09-08 16:32:14 +02:00
switch ($language->iso_code)
{
case 'fr':
$country = 'FRA';
break;
case 'es':
$country = 'ESP';
break;
case 'de':
$country = 'DEU';
break;
case 'it':
$country = 'ITA';
break;
case 'nl':
$country = 'NLD';
break;
case 'sv':
$country = 'SWE';
break;
case 'en-us':
default:
$country = 'GBR';
break;
}
unset($language);
2016-07-15 17:49:13 +02:00
$postfields = array(
'VERSION' => '00104',
'TYPE' => '00053',
'SITE' => $pbx_site,
'RANG' => $pbx_rang,
'CLE' => 'EFNLJKFB',
'NUMQUESTION' => $num_question,
2016-09-14 15:54:25 +02:00
'MONTANT' => str_pad($montant, 10, '0', STR_PAD_LEFT),
2016-07-15 17:49:13 +02:00
'DEVISE' => $devise,
'REFERENCE' => (int)$cart->id,
2016-07-18 12:46:58 +02:00
'REFABONNE' => $refabonne,
2016-07-18 11:45:18 +02:00
'PORTEUR' => $numbers,
'DATEVAL' => $dateval,
'ACTIVITE' => '027',
2016-09-08 16:32:14 +02:00
'PAYS' => $country,
/*'CVV' => Configuration::get('PBX_DEMO_MODE') == 0 ?'000':$cvv,*/
2016-07-15 17:49:13 +02:00
'DATEQ' => date('dmYHis')
);
2016-08-24 17:48:50 +02:00
2016-09-28 10:49:22 +02:00
$authorized_ip = array(
'88.163.22.99',
'90.63.178.63',
);
2016-07-15 17:49:13 +02:00
$trame = http_build_query($postfields, '', '&');
2016-09-28 10:49:22 +02:00
if (in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
mail('marion@antadis.com', '[BBB] Envoi paybox direct', $trame);
}
2016-08-24 17:48:50 +02:00
2017-05-17 11:32:39 +02:00
// log
$content = '';
if (file_exists('logs.txt')) {
$content = file_get_contents('logs.txt');
}
$line = 'SEND ['.(int)$cart->id.']['.$num_question.'] '.date('Y-m-d H:i:s').': '.$trame."\n";
$content .= $line;
file_put_contents('logs.txt', $content);
// end log
2016-07-15 17:49:13 +02:00
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $trame);
$response = curl_exec($curl);
curl_close($curl);
2017-05-17 11:32:39 +02:00
// log
$content = '';
if (file_exists('logs.txt')) {
$content = file_get_contents('logs.txt');
}
$line = 'RESPONSE ['.(int)$cart->id.']['.$num_question.'] '.date('Y-m-d H:i:s').': '.$response."\n";
$content .= $line;
file_put_contents('logs.txt', $content);
// end log
2016-07-15 17:49:13 +02:00
$data = explode('&', $response);
2017-05-04 14:03:15 +02:00
foreach ($data as $d) {
$d = explode('=', $d);
if($d[0] == 'NUMTRANS') {
$num_trans = $d[1];
} elseif($d[0] == 'NUMAPPEL') {
$num_appel = $d[1];
} elseif($d[0] == 'AUTORISATION') {
$autorisation = $d[1];
} elseif($d[0] == 'CODEREPONSE') {
$code_reponse = $d[1];
} elseif($d[0] == 'COMMENTAIRE') {
$commentaire = $d[1];
} elseif($d[0] == 'PAYS') {
$pays = $d[1];
}
}
2016-09-08 14:51:03 +02:00
2016-09-28 13:02:55 +02:00
$data[7] = utf8_encode($data[7]);
2016-09-28 10:49:22 +02:00
if (in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
mail('marion@antadis.com', '[BBB] Reponse paybox direct', $response);
}
2016-07-15 17:49:13 +02:00
2016-09-08 16:32:14 +02:00
$error = '';
$paybox = new Paybox();
$test_mode = (int)Configuration::get('PBX_DEMO_MODE');
2016-09-08 16:47:36 +02:00
$values = array(
'm' => (int) $montant,
'r' => (int) $cart->id,
2017-05-04 14:03:15 +02:00
't' => (int) $num_appel,
'p' => $autorisation,
2016-09-08 16:47:36 +02:00
'c' => $paybox_card['payment_type'],
'a' => 'CARTE',
2017-05-04 14:03:15 +02:00
's' => (int) $num_trans,
'e' => $code_reponse,
2016-09-08 16:47:36 +02:00
'd' => $paybox_card['date'],
2017-05-04 14:03:15 +02:00
'i' => isset($pays)?$pays:null,
'saved' => 1
2016-09-08 16:47:36 +02:00
);
2016-09-08 17:07:17 +02:00
// payment success
2016-09-08 16:32:14 +02:00
if ((!isset($autorisation) || empty($autorisation)))
2016-09-08 17:07:17 +02:00
{
2016-09-28 13:02:55 +02:00
if (in_array($_SERVER['REMOTE_ADDR'], $authorized_ip)) {
mail('marion@antadis.com', '[BBB] ERREUR Reponse paybox direct', http_build_query($data, '', '&'));
}
2016-09-08 17:07:17 +02:00
Logger::addLog('No autorisation number', 4);
die('No autorisation number');
}
// Wrong authorization number in live mode
if ($autorisation == 'XXXXXX' && $test_mode === 1) {
2016-09-08 17:07:17 +02:00
$error .= '- error payment - wrong authorization number <br>'."\n";
}
if ($id_order = (int)Order::getOrderByCartId($cart->id))
{
2017-06-22 16:53:29 +02:00
// log
$content = '';
if (file_exists('logs.txt')) {
$content = file_get_contents('logs.txt');
}
$line = 'SECONDE RESPONSE ['.(int)$cart->id.']['.$num_question.'] '.date('Y-m-d H:i:s').': '.$response."\n";
$content .= $line;
file_put_contents('logs.txt', $content);
// end log
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
if (empty($error))
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$order = new Order($id_order);
$order->valid = 1;
$order->total_paid_real = $values['m'] / 100;
$order->update();
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
if ($order->getCurrentState() != _PS_OS_PAYMENT_)
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$products = $cart->getProducts();
foreach ($products as $product)
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
Product::updateQuantity($product);
Hook::updateQuantity($product, $order);
2016-09-08 16:32:14 +02:00
}
2016-09-08 17:07:17 +02:00
}
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
$history = new OrderHistory();
$history->id_order = $id_order;
$history->changeIdOrderState((int)_PS_OS_PAYMENT_, $id_order);
$history->addWithemail(true, array());
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
$order_message = '';
foreach ($values as $key => $value) {
$order_message .= $key.': '.$value.'<br>';
}
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
if (isset($order_message) && !empty($order_message))
{
$msg = new Message();
$message = $order_message;
if (Validate::isCleanHtml($order_message))
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$msg->message = $order_message;
$msg->id_order = (int)$order->id;
$msg->private = 1;
$msg->add();
2016-09-08 16:32:14 +02:00
}
}
2016-07-15 17:49:13 +02:00
}
2016-09-08 17:07:17 +02:00
die($error);
}
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
$total_paid = $values['m'] / 100;
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
if ($test_mode === 0)
{
if (empty($error) && $code_reponse == '00000')
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$error = '***TEST*** : Validated Payment <br>'."\n";
$statut = _PS_OS_PAYMENT_;
2016-09-08 16:32:14 +02:00
}
2016-09-08 17:07:17 +02:00
else
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$error = '***TEST*** : Invalid Payment <br>'."\n".$error;
$statut = _PS_OS_ERROR_;
2016-09-08 16:32:14 +02:00
}
2016-09-08 17:07:17 +02:00
}
elseif ($test_mode === 1)
{
if ($code_reponse == '00000' && empty($error)){
2016-09-08 17:07:17 +02:00
$statut = _PS_OS_PAYMENT_;
}
else
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$statut = _PS_OS_ERROR_;
2016-09-08 16:32:14 +02:00
}
2016-09-08 17:07:17 +02:00
}
switch ($code_reponse)
2016-09-08 17:07:17 +02:00
{
case '00001':
$error .= 'Connection to the authorization center failed or an internal error occurred <br>'."\n";
break;
case '00003':
$error .= 'Paybox error<br>'."\n";
break;
case '00004':
$error .= 'Card number invalid or visual cryptogram invalid <br>'."\n";
break;
case '00006':
$error .= 'Access refused or site/rank/identifier incorrect <br>'."\n";
break;
case '00008':
$error .= 'Incorrect expiry date.<br>'."\n";
break;
case '00009':
$error .= 'Error when during subscriber creation<br>'."\n";
break;
case '00010':
$error .= 'Unknown currency<br>'."\n";
break;
case '00011':
$error .= 'Amount incorrect<br>'."\n";
break;
case '00015':
$error .= 'Payment already done<br>'."\n";
break;
case '00105':
$error .= 'Error 00105<br>'."\n";
break;
2016-09-08 17:07:17 +02:00
case '00016':
$error .= 'Subscriber already exists<br>'."\n";
break;
case '00021':
$error .= 'Not authorized bin card<br>'."\n";
break;
case '00029':
$error .= 'Not the same card used for the first payment.<br>'."\n";
break;
case '00030':
$error .= 'Time-out > 15 mn before validation by the buyer when the buyer is on the page of payments of PAYBOX<br>'."\n";
break;
case '00031':
case '00032':
$error .= 'Reserved<br>'."\n";
break;
case '00033':
$error .= 'Unauthorized country code of the IP address of the cardholders browser<br>'."\n";
break;
case '00040':
$error .= 'Operation without 3DSecure authentication, blocked by the fraud filter.<br>'."\n";
break;
case '99999':
$error .= 'Payment waiting confirmation from the issuer<br>'."\n";
break;
case '00000':
default:
$error .= '<b>Successful operation</b><br>'."\n\n";
break;
}
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
$error .= 'PayBox version: '.$paybox->version."\n".'<br> POST '.print_r($postfields, true)."\n".'<br>GET '.print_r($data, true)."\n";
2017-05-04 14:03:15 +02:00
$paybox->validateOrder((int)$cart->id, $statut, $total_paid, $paybox->displayName, $error, array(), NULL, false, $customer->secure_key, $values);
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
// save info paiement BDD
$paybox->saveInformationPaiement($values);
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
if (version_compare(_PS_VERSION_, '1.5.0.0') >= '0')
{
$order_id = Order::getOrderByCartId((int)$cart->id);
$order = new Order((int)$order_id);
if ($order_state == _PS_OS_PAYMENT_)
2016-09-08 16:32:14 +02:00
{
2016-09-08 17:07:17 +02:00
$order->valid = 1;
$order->save();
}
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
$id_order_payment = Db::getInstance()->getValue('SELECT id_order_payment
FROM `'._DB_PREFIX_.'order_payment`
WHERE `order_reference` LIKE \'%'.pSQL($order->reference).'%\'');
2016-09-08 16:32:14 +02:00
2016-09-08 17:07:17 +02:00
if ($id_order_payment == false)
$order->addOrderPayment($total_paid, null, trim($values['t']));
else
{
$order_payment = new OrderPayment((int)$id_order_payment);
$order_payment->transaction_id = trim($values['t']);
$order_payment->save();
2016-09-08 16:32:14 +02:00
}
}
2016-09-08 17:07:17 +02:00
Tools::redirect('order-confirmation.php?id_cart='.$cart->id.'&id_module='.$paybox->id.'&key='.$customer->secure_key);
2016-09-08 16:32:14 +02:00
include(dirname(__FILE__).'/../../footer.php');