Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require("../init2.php");
- // Include jcart before session start
- include_once('jcart.php');
- $config = $jcart->config;
- // The update and empty buttons are displayed when javascript is disabled
- // Re-display the cart if the visitor has clicked either button
- if ($_POST['jcartUpdateCart'] || $_POST['jcartEmpty']) {
- // Update the cart
- if ($_POST['jcartUpdateCart']) {
- if ($jcart->update_cart() !== true) {
- $_SESSION['quantityError'] = true;
- }
- }
- // Empty the cart
- if ($_POST['jcartEmpty']) {
- $jcart->empty_cart();
- }
- // Redirect back to the checkout page
- $protocol = 'http://';
- if (!empty($_SERVER['HTTPS'])) {
- $protocol = 'https://';
- }
- header('Location: ' . $protocol . $_SERVER['HTTP_HOST'] . $config['checkoutPath']);
- exit;
- }
- // The visitor has clicked the PayPal checkout button
- else {
- $user = $_SESSION['username'];
- $sqlquery = "SELECT idcliente,email FROM clientes WHERE username='".$user."' ";
- $idcliente = mysql_result(mysql_query($sqlquery),0,idcliente);
- $email = mysql_result(mysql_query($sqlquery),0,email);
- $precoportes=$_GET['portes'];
- $total=$jcart->display_total();
- $total2=$total+$precoportes;
- $sqlquery = "SELECT morada, codigopostal, cidade, pais FROM moradatemp WHERE username='".$user."' ";
- if(mysql_num_rows($sqlquery)>0) {
- $morada = mysql_result(mysql_query($sqlquery),0,morada);
- $codigopostal = mysql_result(mysql_query($sqlquery),0,codigopostal);
- $cidade = mysql_result(mysql_query($sqlquery),0,cidade);
- $pais = mysql_result(mysql_query($sqlquery),0,pais);
- }else{
- $sqlquery = "SELECT morada, codigopostal, cidade, pais FROM clientes WHERE username='".$user."' ";
- $morada = mysql_result(mysql_query($sqlquery),0,morada);
- $codigopostal = mysql_result(mysql_query($sqlquery),0,codigopostal);
- $cidade = mysql_result(mysql_query($sqlquery),0,cidade);
- $pais = mysql_result(mysql_query($sqlquery),0,pais);
- }
- $txtsql = "INSERT INTO encomendas(idencomenda, idtipoenvio, idcliente, tipopagamento, total, totalcportes, data, idestado, idpai, idfilho, modificado, notificado, morada, codigopostal, cidade, pais) ".
- "VALUES(NULL, '3', '$idcliente', 'Paypal', '$total', '$total2', now(), '1', '0', '0', '0', '0', '$morada', '$codigopostal', '$cidade', '$pais')";
- //echo $txtsql;
- mysql_query($txtsql);
- $sql = "SHOW TABLE STATUS LIKE 'encomendas'";
- $res = mysql_query($sql)or die(mysql_error());
- $status = mysql_fetch_array($res);
- $idencomendas=$status['Auto_increment'];
- $idencomendas=$idencomendas-1;
- $carrinho=$jcart->get_contents();
- foreach ($carrinho as $items){
- //echo $items['id'];
- //echo $idencomenda;// depois tenho de ver o codigo para ir buscar isto testa 1º assim
- //print_r($carrinho);
- //INSERIR NA TABELA ENCOMENDAS_PRODUTOS O ID $items['id'] e o $idencomenda;
- $txtsql = "INSERT INTO encomendas_produtos(idencomenda, idproduto, quantidade) ".
- "VALUES('$idencomendas', '".$items['id']."', '".$items['qty']."' )";
- mysql_query($txtsql);
- $txtquery = "delete from moradatemp where username = '".$user."' ";
- mysql_query($txtquery);
- }
- $txtquery = "delete from encomendas where total = '0' ";
- mysql_query($txtquery);
- $to = $email;
- $subject = "[Nutrinow] Confirmação de encomenda";
- $body = "<html><body><img src='http://img571.imageshack.us/img571/9484/nutrilogo.png' height='40px'/><br />Olá , ".$nome." ".$apelido."<br /><br />
- A sua encomenda foi registada com sucesso.<br /><br />
- Escolheu o pagamento por <strong>Paypal</strong>.<br /><br />
- <strong>Valor:</strong> ".$total2."€<br /><br />
- Assim que confirmemos o seu pagamento, procederemos ao envio da encomenda.<br />
- Pode consultar os detalhes da sua encomenda no nosso site em <strong>Perfil > Histórico de Encomendas </strong><br />
- Para qualquer esclarecimento adicional não hesite em contactar-nos. <br /><br />
- Com os melhores cumprimentos,<br />Nutrinow</body></html>";
- $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
- $headers .= "From: nutrinow <" . $email . ">\r\n";
- mail($to,$subject,$body,$headers);
- ////////////////////////////////////////////////////////////////////////////
- /*
- A malicious visitor may try to change item prices before checking out.
- Here you can add PHP code that validates the submitted prices against
- your database or validates against hard-coded prices.
- The cart data has already been sanitized and is available thru the
- $jcart->get_contents() method. For example:
- foreach ($jcart->get_contents() as $item) {
- $itemId = $item['id'];
- $itemName = $item['name'];
- $itemPrice = $item['price'];
- $itemQty = $item['qty'];
- }
- */
- ////////////////////////////////////////////////////////////////////////////
- // For now we assume prices are valid
- $validPrices = true;
- ////////////////////////////////////////////////////////////////////////////
- // If the submitted prices are not valid, exit the script with an error message
- if ($validPrices !== true) {
- die($config['text']['checkoutError']);
- }
- // Price validation is complete
- // Send cart contents to PayPal using their upload method, for details see: http://j.mp/h7seqw
- elseif ($validPrices === true) {
- // Paypal count starts at one instead of zero
- $count = 1;
- // Build the query string
- $queryString = "?cmd=_cart";
- $queryString .= "&upload=1";
- $queryString .= "&charset=utf-8";
- $queryString .= "¤cy_code=" . urlencode($config['currencyCode']);
- $queryString .= "&business=" . urlencode($config['paypal']['id']);
- $queryString .= "&return=" . urlencode($config['paypal']['returnUrl']);
- $queryString .= '¬ify_url=' . urlencode($config['paypal']['notifyUrl']);
- foreach ($jcart->get_contents() as $item) {
- $queryString .= '&item_number_' . $count . '=' . urlencode($item['id']);
- $queryString .= '&item_name_' . $count . '=' . urlencode($item['name']);
- $queryString .= '&amount_' . $count . '=' . urlencode($item['price']);
- $queryString .= '&quantity_' . $count . '=' . urlencode($item['qty']);
- // Increment the counter
- ++$count;
- }
- // Empty the cart
- $jcart->empty_cart();
- // Confirm that a PayPal id is set in config.php
- if ($config['paypal']['id']) {
- // Add the sandbox subdomain if necessary
- $sandbox = '';
- if ($config['paypal']['sandbox'] === true) {
- $sandbox = '.sandbox';
- }
- // Use HTTPS by default
- $protocol = 'https://';
- if ($config['paypal']['https'] == false) {
- $protocol = 'http://';
- }
- // Send the visitor to PayPal
- @header('Location: ' . $protocol . 'www' . $sandbox . '.paypal.com/cgi-bin/webscr' . $queryString);
- }
- else {
- die('Couldn’t find a PayPal ID in <strong>config.php</strong>.');
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment