ciao,
ho un carrello multilingua in cui il cliente al momento del checkout, deve scegliere la modalità di pagamento.
Per il contrassegno/bonifico/bollettino al submit del checkout faccio eseguire una funzione in cui svuoto il carrello e inserisco l'ordine nel DB. e fin qua tutto bene.
il problema arriva con il pagamento tramite carta di credito.
mi spiego.
al submit del checkout passo tutti dati necessari al sito della banca ( uso bankpass), ma non posso far eseguire la funzione al submit del checkout perchè nel caso in cui il cliente decide di cambiare il metodo di pagamento, ritornando nella pagina del carrello, si troverebbe il carrello vuoto.
allora ho modificato la funzione in modo che se il cliente paga con carta di credito, finchè non è avvenuta la transazione il carrello non viene svuotato.
Dal sito di bankpass a transazione avvenuta, devo specificare un "URLDONE" cioè un url di ritorno al sito.
è a questo punto che, specificato un url di ritorno che punta ad una pagina, ho pensato di utilizzare quella funzione per svuotare il carrello e inserire l'ordine, ma qui avviene il problema, perchè mi perde le sessioni e quindi mi genera degli errori.
posto il file di checkout con lo switch per la modalità di pagamento:
<?php
require_once 'library/config.php';
require_once 'library/cart-functions.php';
require_once 'library/checkout-functions.php';
if (isCartEmpty()) {
// the shopping cart is still empty
// so checkout is not allowed
header('Location: home.php?page=cart');
} else if (isset($_GET['step']) && (int)$_GET['step'] > 0 && (int)$_GET['step'] <= 3) {
$step = (int)$_GET['step'];
$includeFile = '';
if ($step == 1) {
$includeFile = 'shippingAndPaymentInfo.php';
$pageTitle = 'Checkout - Step 1 of 2';
} else if ($step == 2) {
$includeFile = 'checkoutConfirmation.php';
$pageTitle = 'Checkout - Step 2 of 2';
} else if ($step == 3) {
switch($_SESSION['id_pag']) {
case '2':
case '3':
case '4':
$orderId = saveOrder();
$orderAmount = getOrderAmount($orderId);
$_SESSION['orderId'] = $orderId;
echo"<meta http-equiv=\"Refresh\" content=\"0;url=home.php?page=success\">";
break;
case '1':
$orderId = saveOrder2();
$orderAmount = getOrderAmount($orderId);
$_SESSION['orderId'] = $orderId;
$includeFile = 'bankpass.php';
break;
}
}
} else {
// missing or invalid step number, just redirect
header('Location: home.php?page=cart');
}
?>
<script type="text/javascript" src="library/checkout.js"></script>
<?php
require_once "../include/$includeFile";
?>
e la funzione che esegue lo svuotamento del carrello e l'inserimento dell'ordine nel DB:
<?php
function saveOrder()
{
$cartContent = getCartContent();
$numItem = count($cartContent);
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
extract($cartContent[$i]);
$orderId = 10;
$shippingCost = $shipping;
}
$requiredField = array('hidShippingFirstName', 'hidShippingLastName', 'hidShippingAddress1', 'hidShippingCity', 'hidShippingState',
'hidShippingPostalCode', 'hidShippingPhone', 'hidShippingEmail');
if (checkRequiredPost($requiredField)) {
extract($_POST);
// make sure the first character in the
// customer and city name are properly upper cased
$hidShippingFirstName = ucwords($hidShippingFirstName);
$hidShippingLastName = ucwords($hidShippingLastName);
$hidShippingCity = ucwords($hidShippingCity);
//$cartContent = getCartContent();
//$numItem = count($cartContent);
if (!empty($_SESSION['comunicati'])) {
$press = 'S';
} else {$press = 'N'; }
$query = mysql_query("SELECT id_pag, pagamento FROM tbl_pagamento WHERE id_pag = {$_SESSION['id_pag']}") or die(mysql_error());
$row = dbFetchAssoc($query);
extract($row);
// save order & get order id
$sql = "INSERT INTO tbl_order(od_date, od_last_update, od_shipping_first_name, od_shipping_last_name, od_shipping_address1,
od_shipping_phone, od_shipping_state, od_shipping_city, od_shipping_postal_code, od_shipping_cost, od_shipping_email, od_payment, od_press )
VALUES (NOW(), NOW(), '$hidShippingFirstName', '$hidShippingLastName', '$hidShippingAddress1',
'$hidShippingPhone', '$hidShippingState', '$hidShippingCity', '$hidShippingPostalCode', '$shippingCost', '$hidShippingEmail', '$pagamento', '$press')";
$result = dbQuery($sql)or die(mysql_error());
// get the order id
$orderId = dbInsertId();
if ($orderId) {
// save order items
for ($i = 0; $i < $numItem; $i++) {
$sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty, od_lang_name)
VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']}, '{$cartContent[$i]['lang_name']}' )";
$result = dbQuery($sql)or die(mysql_error());
}
// update product stock
for ($i = 0; $i < $numItem; $i++) {
$sql = "UPDATE tbl_product
SET pd_qty = pd_qty - {$cartContent[$i]['ct_qty']}
WHERE pd_id = {$cartContent[$i]['pd_id']}";
$result = dbQuery($sql);
}
// then remove the ordered items from cart
for ($i = 0; $i < $numItem; $i++) {
$sql = "DELETE FROM tbl_cart
WHERE ct_id = {$cartContent[$i]['ct_id']}";
$result = dbQuery($sql);
}
}
}
return $orderId;
}
?>
spero mi possiate dare un idea di come risolvere...
Grazie