DVD Order Form

SOS DVD

// Kunaki product order script
// Author: Elimar Green
// Date: 2010-03-10

// Configuration values
global $cfg, $Countries;

$cfg = array(
‘Script’ => ‘http://Kunaki.com/HTTPService.ASP’,
‘Product’ => ‘PX00Z2ZPWH’,
‘MaxQuantity’ => 50,
‘Price’ => 43.0,
‘Title’ => ‘Marché Sprouting Method and Living Oils DVD’,
‘TaxVal’ => 0.0825,
‘TaxState’ => ‘NY’,
‘DBServer’ => ‘localhost’,
‘DBUser’ => ‘dcavallaro’,
‘DBPassword’ => ‘pennywise’,
‘DBDatabase’ => ‘dcavallaro_dlguard’,
‘DBTable’ => ‘KunakiOrders’
);

$Countries = array(‘Argentina’,'Australia’,'Austria’,'Bahrain’,'Belarus’,'Belgium’,'Brazil’,'Bulgaria’,'Canada’,'Chile’,'China’,'Costa Rica’,'Croatia’,'Cyprus’,'Czech Republic’,'Denmark’,'Estonia’,'Finland’,'France’,'Germany’,'Gibraltar’,'Greece’,'Greenland’,'Hong Kong’,'Hungary’,'Iceland’,'Ireland’,'Israel’,'Italy’,'Japan’,'Liechtenstein’,'Lithuania’,'Luxembourg’,'Macedonia’,'Malta’,'Mexico’,'Netherlands’,'New Zealand’,'Norway’,'Poland’,'Portugal’,'Qatar’,'Romania’,'Saudi Arabia’,'Serbia-Montenegro’,'Singapore’,'Slovak Republic’,'Slovenia’,'South Africa’,'South Korea’,'Spain’,'Sweden’,'Switzerland’,'Taiwan’,'Thailand’,'Turkey’,'United Arab Emirates’,'United Kingdom’,'United States’,'Vatican City’,”);

// Stage 0: Shipping calculation form
function shipping_form()
{
global $cfg, $Countries;

echo <<

Quantity
Country
State/Province US or Canada
Postal Code
 

 

HTML;
}

// Stage 1: Checkout form, processes shipping form variables, queries Kunaki and shows checkout form
function checkout_form()
{
global $cfg;

// Form variables
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Country = $_POST['Country'];
$Quantity = $_POST['Quantity'];

$url = $cfg['Script'] . ‘?RequestType=ShippingOptions’
. ‘&State_Province=’ . urlencode($State)
. ‘&PostalCode=’ . urlencode($PostalCode)
. ‘&Country=’ . urlencode($Country)
. ‘&ProductId=’ . urlencode($cfg['Product'])
. ‘&Quantity=’ . urlencode($Quantity)
. ‘&ResponseType=xml’;

$resp = file_get_contents($url);
$xml = simplexml_load_string($resp);

$SubTotal = $Quantity * $cfg['Price'];

if ($Country == “United States” && $State == $cfg['TaxState'])
$SalesTax = round ($SubTotal * $cfg['TaxVal'], 2);
else $SalesTax = 0.0;

$Price = sprintf (“$%0.2f”, $cfg['Price']);
$SubTotal = sprintf (“$%0.2f”, $SubTotal);
$SalesTax = sprintf (“$%0.2f”, $SalesTax);
$Title = htmlspecialchars($cfg['Title']);

if ($xml->ErrorCode == 0)
{
echo ‘

HTML;

$i = 0;
foreach ($xml->Option as $option)
{
echo ‘

Shipping Options Shipping Cost Delivery Time
Total
{$Title} {$Price} {$Quantity} {$SubTotal}

NY Sales Tax {$SalesTax}
Total

 

Shipping Address
Recipient
Organization
Address
Address
City
State/Province Postal Code

Country

Email

 

HTML;

}
else
{
echo “Error occurred: ” . $xml->ErrorText . ‘
‘;
}
}

// Stage 2: Checkout submit, inserts data into database, redirects to Paypal with POST data
function checkout_submit()
{
global $cfg;

$handle = mysql_connect ($cfg['DBServer'], $cfg['DBUser'], $cfg['DBPassword']);

if (!$handle)
{
echo “Internal database error, please try again later”;
return;
}

if (!mysql_select_db ($cfg['DBDatabase']))
{
mysql_close ($handle);
echo “Internal database error, please try again later”;
return;
}

$query = sprintf(‘INSERT INTO ‘ . $cfg['DBTable'] . ‘ (Name,Company,Address1,Address2,City,’
. ‘State,PostalCode,Country,ShippingOption,ProductId,Quantity)’
. ‘ VALUES(“%s”,”%s”,”%s”,”%s”,”%s”,”%s”,”%s”,”%s”,”%s”,”%s”,”%s”)’, $_POST['Name'], $_POST['Company'],
$_POST['Address1'], $_POST['Address2'], $_POST['City'], $_POST['State'], $_POST['PostalCode'],
$_POST['Country'], $_POST['ShippingOption'], $cfg['Product'], $_POST['Quantity']);

if (!mysql_query ($query))
{
mysql_close ($handle);
echo “Internal database error, please try again later”;
return;
}

mysql_close ($handle);

echo “Successfully added order to database!”;
}

if ($_POST['Stage'] == 1)
checkout_form();
else if ($_POST['Stage'] == 2)
checkout_submit();
else shipping_form();

?>

Comments on this entry are closed.