balian

checkoutphp

Feb 1st, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.36 KB | None | 0 0
  1. <?php
  2. require("../init2.php");
  3.  
  4.  
  5. // Include jcart before session start
  6. include_once('jcart.php');
  7.  
  8. $config = $jcart->config;
  9.  
  10. // The update and empty buttons are displayed when javascript is disabled
  11. // Re-display the cart if the visitor has clicked either button
  12. if ($_POST['jcartUpdateCart'] || $_POST['jcartEmpty']) {
  13.  
  14.     // Update the cart
  15.     if ($_POST['jcartUpdateCart']) {
  16.         if ($jcart->update_cart() !== true) {
  17.             $_SESSION['quantityError'] = true;
  18.         }
  19.     }
  20.  
  21.     // Empty the cart
  22.     if ($_POST['jcartEmpty']) {
  23.         $jcart->empty_cart();
  24.     }
  25.  
  26.     // Redirect back to the checkout page
  27.     $protocol = 'http://';
  28.     if (!empty($_SERVER['HTTPS'])) {
  29.         $protocol = 'https://';
  30.     }
  31.  
  32.     header('Location: ' . $protocol . $_SERVER['HTTP_HOST'] . $config['checkoutPath']);
  33.     exit;
  34. }
  35.  
  36. // The visitor has clicked the PayPal checkout button
  37. else {
  38.         $user = $_SESSION['username'];
  39.         $sqlquery = "SELECT idcliente,email FROM clientes WHERE username='".$user."' ";
  40.         $idcliente = mysql_result(mysql_query($sqlquery),0,idcliente);
  41.         $email = mysql_result(mysql_query($sqlquery),0,email);
  42.  
  43.         $precoportes=$_GET['portes'];
  44.                                            
  45.         $total=$jcart->display_total();
  46.         $total2=$total+$precoportes;
  47.  
  48.          $sqlquery = "SELECT morada, codigopostal, cidade, pais  FROM moradatemp WHERE username='".$user."' ";
  49.         if(mysql_num_rows($sqlquery)>0) {
  50.             $morada = mysql_result(mysql_query($sqlquery),0,morada);
  51.             $codigopostal = mysql_result(mysql_query($sqlquery),0,codigopostal);
  52.             $cidade = mysql_result(mysql_query($sqlquery),0,cidade);
  53.             $pais = mysql_result(mysql_query($sqlquery),0,pais);
  54.         }else{
  55.             $sqlquery = "SELECT morada, codigopostal, cidade, pais  FROM clientes WHERE username='".$user."' ";
  56.             $morada = mysql_result(mysql_query($sqlquery),0,morada);
  57.             $codigopostal = mysql_result(mysql_query($sqlquery),0,codigopostal);
  58.             $cidade = mysql_result(mysql_query($sqlquery),0,cidade);
  59.             $pais = mysql_result(mysql_query($sqlquery),0,pais);
  60.         }
  61.  
  62.         $txtsql = "INSERT INTO encomendas(idencomenda, idtipoenvio, idcliente, tipopagamento, total, totalcportes, data, idestado, idpai, idfilho, modificado, notificado, morada, codigopostal, cidade, pais) ".
  63.         "VALUES(NULL, '3', '$idcliente', 'Paypal', '$total', '$total2', now(), '1', '0', '0', '0', '0', '$morada', '$codigopostal', '$cidade', '$pais')";
  64.         //echo $txtsql;
  65.         mysql_query($txtsql);
  66.  
  67.  
  68.  
  69.         $sql = "SHOW TABLE STATUS LIKE 'encomendas'";
  70.         $res = mysql_query($sql)or die(mysql_error());
  71.         $status = mysql_fetch_array($res);
  72.         $idencomendas=$status['Auto_increment'];
  73.         $idencomendas=$idencomendas-1;
  74.  
  75.  
  76.         $carrinho=$jcart->get_contents();                                        
  77.         foreach ($carrinho as $items){
  78.             //echo $items['id'];
  79.             //echo $idencomenda;// depois tenho de ver o codigo para ir buscar isto testa 1º assim                                            
  80.             //print_r($carrinho);
  81.  
  82.              
  83.  
  84.         //INSERIR NA TABELA ENCOMENDAS_PRODUTOS O ID $items['id'] e o $idencomenda;
  85.         $txtsql = "INSERT INTO encomendas_produtos(idencomenda, idproduto, quantidade) ".
  86.           "VALUES('$idencomendas', '".$items['id']."', '".$items['qty']."' )";
  87.                                    
  88.         mysql_query($txtsql);
  89.  
  90.         $txtquery = "delete from moradatemp where username = '".$user."' ";
  91.         mysql_query($txtquery);
  92.         }
  93.  
  94.         $txtquery = "delete from encomendas where total = '0' ";
  95.         mysql_query($txtquery);
  96.  
  97.         $to = $email;
  98.         $subject = "[Nutrinow] Confirmação de encomenda";
  99.         $body = "<html><body><img src='http://img571.imageshack.us/img571/9484/nutrilogo.png' height='40px'/><br />Olá , ".$nome." ".$apelido."<br /><br />
  100.            A sua encomenda foi registada com sucesso.<br /><br />
  101.            Escolheu o pagamento por <strong>Paypal</strong>.<br /><br />
  102.  
  103.            <strong>Valor:</strong> ".$total2."€<br /><br />  
  104.  
  105.            Assim que confirmemos o seu pagamento, procederemos ao envio da encomenda.<br />
  106.            Pode consultar os detalhes da sua encomenda no nosso site em <strong>Perfil > Histórico de Encomendas </strong><br />
  107.            Para qualquer esclarecimento adicional não hesite em contactar-nos.   <br /><br />    
  108.  
  109.            Com os melhores cumprimentos,<br />Nutrinow</body></html>";
  110.         $email = "[email protected]";
  111.         $headers = "Content-type: text/html; charset=iso-8859-1\r\n";
  112.         $headers .= "From: nutrinow <" . $email . ">\r\n";
  113.         mail($to,$subject,$body,$headers);
  114.  
  115.     ////////////////////////////////////////////////////////////////////////////
  116.     /*
  117.  
  118.     A malicious visitor may try to change item prices before checking out.
  119.  
  120.     Here you can add PHP code that validates the submitted prices against
  121.     your database or validates against hard-coded prices.
  122.  
  123.     The cart data has already been sanitized and is available thru the
  124.     $jcart->get_contents() method. For example:
  125.  
  126.     foreach ($jcart->get_contents() as $item) {
  127.         $itemId     = $item['id'];
  128.         $itemName   = $item['name'];
  129.         $itemPrice  = $item['price'];
  130.         $itemQty    = $item['qty'];
  131.     }
  132.  
  133.     */
  134.     ////////////////////////////////////////////////////////////////////////////
  135.  
  136.     // For now we assume prices are valid
  137.     $validPrices = true;
  138.  
  139.     ////////////////////////////////////////////////////////////////////////////
  140.  
  141.     // If the submitted prices are not valid, exit the script with an error message
  142.     if ($validPrices !== true)  {
  143.         die($config['text']['checkoutError']);
  144.     }
  145.  
  146.     // Price validation is complete
  147.     // Send cart contents to PayPal using their upload method, for details see: http://j.mp/h7seqw
  148.     elseif ($validPrices === true) {
  149.         // Paypal count starts at one instead of zero
  150.         $count = 1;
  151.        
  152.         // Build the query string
  153.         $queryString  = "?cmd=_cart";
  154.         $queryString .= "&upload=1";
  155.         $queryString .= "&charset=utf-8";
  156.         $queryString .= "&currency_code=" . urlencode($config['currencyCode']);
  157.         $queryString .= "&business=" . urlencode($config['paypal']['id']);
  158.         $queryString .= "&return=" . urlencode($config['paypal']['returnUrl']);
  159.         $queryString .= '&notify_url=' . urlencode($config['paypal']['notifyUrl']);
  160.        
  161.         foreach ($jcart->get_contents() as $item) {
  162.  
  163.             $queryString .= '&item_number_' . $count . '=' . urlencode($item['id']);
  164.             $queryString .= '&item_name_' . $count . '=' . urlencode($item['name']);
  165.             $queryString .= '&amount_' . $count . '=' . urlencode($item['price']);
  166.             $queryString .= '&quantity_' . $count . '=' . urlencode($item['qty']);
  167.  
  168.             // Increment the counter
  169.             ++$count;
  170.         }
  171.  
  172.         // Empty the cart
  173.         $jcart->empty_cart();
  174.  
  175.         // Confirm that a PayPal id is set in config.php
  176.         if ($config['paypal']['id']) {
  177.  
  178.             // Add the sandbox subdomain if necessary
  179.             $sandbox = '';
  180.             if ($config['paypal']['sandbox'] === true) {
  181.                 $sandbox = '.sandbox';
  182.             }
  183.  
  184.             // Use HTTPS by default
  185.             $protocol = 'https://';
  186.             if ($config['paypal']['https'] == false) {
  187.                 $protocol = 'http://';
  188.             }
  189.  
  190.             // Send the visitor to PayPal
  191.             @header('Location: ' . $protocol . 'www' . $sandbox . '.paypal.com/cgi-bin/webscr' . $queryString);
  192.         }
  193.         else {
  194.             die('Couldn&rsquo;t find a PayPal ID in <strong>config.php</strong>.');
  195.         }
  196.     }
  197. }
  198.  
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment