Location: PHPKode > projects > Simple PHP Shopping cart - asaancart > asaancart v-0.9/shop/DoExpressCheckoutPayment.php
<?php
session_start();
include("../config/config.php");
include("classes/functions.php");


/**********************************************************
DoExpressCheckoutPayment.php

This functionality is called to complete the payment with
PayPal and display the result to the buyer.

The code constructs and sends the DoExpressCheckoutPayment
request string to the PayPal server.

Called by GetExpressCheckoutDetails.php.

Calls CallerService.php and APIError.php.

**********************************************************/

require_once 'paypal_libs/CallerService.php';
//session_start();
if (session_id() == "") session_start();
$session_id = session_id();
/* Gather the information to make the final call to
   finalize the PayPal payment.  The variable nvpstr
   holds the name value pairs
   */
$token =urlencode( $_SESSION['token']);
$paymentAmount =urlencode ($_SESSION['paymentAmount']);
$paymentType = urlencode($_SESSION['paymentType']);
$currCodeType = urlencode($_SESSION['currCodeType']);
$payerID = urlencode($_SESSION['payer_id']);
$serverName = urlencode($_SERVER['SERVER_NAME']);

$nvpstr='&TOKEN='.$token.'&PAYERID='.$payerID.'&PAYMENTACTION='.$paymentType.'&AMT='.$paymentAmount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName ;

 /* Make the call to PayPal to finalize payment
    If an error occured, show the resulting errors
    */
$resArray=hash_call("DoExpressCheckoutPayment",$nvpstr);

/* Display the API response back to the browser.
   If the response from PayPal was a success, display the response parameters'
   If the response was an error, display the errors received using APIError.php.
   */
$ack = strtoupper($resArray["ACK"]);


if($ack!="SUCCESS"){
	$_SESSION['reshash']=$resArray;
	$location = "APIError.php";
		 header("Location: $location");
               }


$smarty->assign('AMT', $resArray['AMT']);
$smarty->assign('TRANSACTIONID', $resArray['TRANSACTIONID']);

///////////////


include("includes/global_settings.php");

$smarty->assign('shippingTotal',shippingRate(getCartTotalAmount($session_id)));
$cartTotal  = getCartTotalAmount($session_id)+ shippingRate(getCartTotalAmount($session_id));
//end shipping cost

$smarty->assign('cartTotal',$cartTotal);

$smarty->assign('cartItems',getCartItems($session_id));
$smarty->assign('cartSubTotal', getCartTotalAmount($session_id));

$shippingTotal = shippingRate(getCartTotalAmount($session_id));
$cartSubTotal = getCartTotalAmount($session_id);
$customer_name = $_POST['customer_name'];
$_SESSION['customer_name'] = $customer_name;

$business_name = $_POST['business_name'];
$_SESSION['business_name'] = $business_name;
if($business_name==""){$business_name = " ";}
$email_address = $_POST['email_address'];
$_SESSION['email_address'] = $email_address;

$customer_mobile = $_POST['customer_mobile'];
$_SESSION['mobile'] = $customer_mobile;

$customer_phone = $_POST['customer_phone'];
$_SESSION['phone'] = $customer_phone;

$customer_state = $_POST['customer_state'];
$_SESSION['state'] = $customer_state;

$customer_city = $_POST['customer_city'];
$_SESSION['city'] = $customer_city;

$customer_suburb = $_POST['customer_suburb'];
$_SESSION['suburb'] = $customer_suburb;

$customer_postcode = $_POST['customer_postcode'];
$_SESSION['postcode'] = $customer_postcode;

$customer_address = $_POST['customer_address'];
$_SESSION['address'] = $customer_address;

$s_customer_country = $_POST['s_customer_country'];
$_SESSION['shipping_country'] = $s_customer_country;

$s_customer_state = $_POST['s_customer_state'];
$_SESSION['shipping_state'] = $s_customer_state;

$s_customer_city = $_POST['s_customer_city'];
$_SESSION['shipping_city'] = $s_customer_city;

$s_customer_postcode = $_POST['s_customer_postcode'];
$_SESSION['shipping_postcode'] = $s_customer_postcode;

$s_customer_suburb = $_POST['s_customer_suburb'];
$_SESSION['shipping_suburb'] = $s_customer_suburb;

$s_customer_address = $_POST['s_customer_address'];
$_SESSION['shipping_address'] = $s_customer_address;

$shipping_instructions = $_POST['shipping_instructions'];
$_SESSION['shipping_instructions'] = $shipping_instructions;

$mailing_list = $_POST['mailing_list'];
if($mailing_list!="yes"){$mailing_list = "no";}

$payment_option = "PayPal Express Checkout";

$error_msg = "";
if($customer_name==""){
$error_msg .= "Customer Name <br />";
}
if($email_address==""){
$error_msg .= "Email Address <br />";
}
if($customer_phone==""){
$error_msg .= "Phone <br />";
}
if($customer_state==""){
$error_msg .= "State <br />";
}
if($customer_city==""){
$error_msg .= "City <br />";
}
if($customer_postcode==""){
$error_msg .= "Postcode <br />";
}
if($customer_address==""){
$error_msg .= "Address <br />";
}
if($s_customer_country==""){
$error_msg .= "Shipping Country <br />";
}
if($s_customer_state==""){
$error_msg .= "Shipping State <br />";
}
if($s_customer_city==""){
$error_msg .= "Shipping City <br />";
}
if($s_customer_postcode==""){
$error_msg .= "Shipping Postcode <br />";
}
if($s_customer_address==""){
$error_msg .= "Shipping Address <br />";
}


if($error_msg!=""){
	//header("location: checkout.php?error_msg=".$error_msg);
}

if(getCartTotalQty($session_id)>0){
	$max_order_id = getMaxOrderId();
	$new_order_id = $max_order_id + 1;
	$order_prefix = $order_prefix;
	$order_number = $order_prefix.$new_order_id;
	
	$smarty->assign('order_number', $order_number);
	$smarty->assign('customer_name', $customer_name);
	$smarty->assign('bunisess_name', $business_name);
	$smarty->assign('email_address', $email_address);
	$smarty->assign('address', $s_customer_address);
	$smarty->assign('city', $s_customer_city);
	$smarty->assign('state', $s_customer_state);
	$smarty->assign('post_code', $s_customer_postcode);
	$smarty->assign('country', $s_customer_country);
	$smarty->assign('payment_method', $payment_option);
	$smarty->assign('phone_number', $customer_phone);
	$smarty->assign('order_date', date("d-m-Y"));
	$smarty->assign('order_instructions', $shipping_instructions);
	
	$smarty->assign('payment_options_available', getDefaultPaymentOptions());
	
	
	
	
	//add user
	$sql = "INSERT INTO customers (customer_name, business_name, customer_country, customer_state, customer_city, customer_suburb, customer_postcode, customer_mobile, customer_phone, email_address, customer_address, s_customer_country, s_customer_state, s_customer_city, s_customer_suburb, s_customer_postcode, s_customer_address, mailing_list) VALUES ('$customer_name', '$business_name', '$customer_country', '$customer_state', '$customer_city', '$customer_suburb', '$customer_postcode', '$customer_mobile', '$customer_phone', '$email_address', '$customer_address', '$s_customer_country', '$s_customer_state', '$s_customer_city', '$s_customer_suburb', '$s_customer_postcode', '$s_customer_address', '$mailing_list')";
	$results = mysql_query($sql);
	//end add user
	
	//add order
	$sql = "INSERT INTO orders (order_number, customer_name, customer_email, state, city, shipping_address, postcode, country, payment_method, order_comments, order_date, order_status, order_shipping, order_subtotal) VALUES ('$order_number', '$customer_name', '$email_address', '$s_customer_state', '$s_customer_city', '$s_customer_address', '$s_customer_postcode', '$s_customer_country', '$payment_option', '$shipping_instructions', '".date("d-m-Y")."', 'pending', $shippingTotal, $cartSubTotal)";
	$results = mysql_query($sql);
	
	
	//end add order
	
	//add order items
	$sqlCart = "SELECT * FROM my_cart WHERE session_id = '$session_id' ORDER BY product_name";
		$resultsCart = mysql_query($sqlCart);
		while($rowCart = mysql_fetch_array($resultsCart) )
		{
			$product_code = $rowCart['product_code'];
			$product_name = $rowCart['product_name'];
			$product_name = $product_name."<br />".$rowCart['product_description'];
			$product_quantity = $rowCart['product_qty'];
			$product_price = $rowCart['product_price'];
			$sub_total = $rowCart['total'];
			
			$sql = "INSERT INTO order_item (order_number, product_code, product_name, product_quantity, product_price, sub_total) VALUES ('$order_number', '$product_code', '$product_name', $product_quantity, $product_price, $sub_total)";
	$results = mysql_query($sql);
	
	
		}
		
		
	
	//end add items

}

$smarty->assign('site_url', SITE_URL);


$results = mysql_query("DELETE FROM my_cart WHERE session_id = '$session_id'");

session_unset();
session_destroy();
$_SESSION = array();





$smarty->assign('payment_options_available', getDefaultPaymentOptions());



$smarty->assign('template','receipt');

$smarty->display('shop/site_index.tpl');


?>
Return current item: Simple PHP Shopping cart - asaancart