<?php
//get shop page's title
$t = $site->pageTitle($s['shop_page']);
$seo_pageTitle = seo_str($t);
$basketPageURL = '/'.$s['shop_page'].'/'.$seo_pageTitle;
//if user doesnt have any items in cart, send back to cart page
$check = $mysql->num_rows($mysql->query("ID", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
if ($check == 0)
{
    header("Location: /{$s['shop_page']}/$seo_pageTitle/Basket");
    exit;
}
//check and update discount
include fileInclude('inc/shop/discount/index.php');
//shipping calc
$type = $site->shop_settings('shipping_method,hide_delivery_section');
if ($type['shipping_method'] == 'postoffice' && $type['hide_delivery_section'] == 'n')
{
    include fileInclude('inc/shop/shipping/postoffice.php');
}
if ($type['shipping_method'] == 'flatrate' && $type['hide_delivery_section'] == 'n')
{
    include fileInclude('inc/shop/shipping/flatrate.php');
}
if ($type['shipping_method'] == 'peritem' && $type['hide_delivery_section'] == 'n')
{
    include fileInclude('inc/shop/shipping/peritem.php');
}
if ($type['shipping_method'] == 'fastway' && $type['hide_delivery_section'] == 'n')
{
    include fileInclude('inc/shop/shipping/fastway.php');
}
if ($type['shipping_method'] == 'ego' && $type['hide_delivery_section'] == 'n')
{
    include fileInclude('inc/shop/shipping/ego.php');
}
if ($type['shipping_method'] == 'custom')
{
    include fileInclude('inc/shop/shipping/custom.php');
}
include fileInclude('inc/shop/tax/sales_tax.php'); //sales tax
//crumbs
//order details
$order = $mysql->get_array($mysql->query("company_name,dev_firstname,dev_lastname,address_line1,address_line2,town,state,postcode,country,shipping,sales_tax,discount_value", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
$order['address_line1'] .= (!empty($order['address_line2'])) ? '<br />'.$order['address_line2'] : '';
//cart subtotal
$stockIssue = false;
$subtotal = 0;
$q = $mysql->query("pid,`option`,price,qty", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' ORDER BY ID");
while ($d = $mysql->get_array($q))
{
    $subtotal += ($d['price'] * $d['qty']);
    $d['option'] = addslashes($d['option']);
    $st = $mysql->get_array($mysql->query("stock", "fs_shop_stock", "pid = {$d['pid']} AND `option` = '{$d['option']}' LIMIT 1"));
    if ($st['stock'] <= 0 || $d['qty'] > $st['stock'])
    {
        $stockIssue = true;
    }
}
//stock alert
if ($stockIssue)
{
    header("Location: $basketPageURL/Basket");
    exit;
}
//page html
$html = '<h1>Payment</h1><p id="shopCrumbs"><a href="'.$basketPageURL.'">'.$t.'</a> &gt; <a href="'.$basketPageURL.'/Basket">Basket</a> &gt; <a href="'.$basketPageURL.'/Checkout">Your information</a> &gt; <strong>Payment information</strong></p><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>';
if ($type['hide_delivery_section'] == 'n')
{
    $list = $site->countryList();
    $html .= '<strong>Deliver to:</strong><br />';
    if (!empty($order['company_name']))
    {
        $html .= $order['company_name'].'<br />';
    }
    $html .= $order['dev_firstname'].' '.$order['dev_lastname'].'<br />
	'.$order['address_line1'].'<br />
	'.$order['town'].', '.$order['state'].'<br />
	'.$order['postcode'].'<br />
	'.$list['name'][$order['country']];
}
$html .= '</td>
<td valign="top">
<table border="0" cellspacing="0" cellpadding="3">
<tr><td width="50%">Subtotal</td><td>'.$site->currency_symbol($subtotal).'</td></tr>
<tr><td>Shipping</td><td>'.$site->currency_symbol($order['shipping']).'</td></tr>';
if ($order['discount_value'] > 0)
{
    $html .= '<tr><td>Discount</td><td>'.$site->currency_symbol($order['discount_value']).'</td></tr>';
}
$html .= '<tr><td><strong>Total</strong></td><td><strong>'.$site->currency_symbol($subtotal + $order['shipping'] - $order['sales_tax'] - $order['discount_value']).'</strong></td></tr>';
if ($order['sales_tax'] > 0)
{
    $html .= '<tr><td>Sales tax deducted</td><td>'.$site->currency_symbol($order['sales_tax']).'</td></tr>';
}
$html .= '</table></td></tr></table>';
//payment methods
$html .= '<h2>Choose your payment method</h2>';
$paypal = $site->shop_settings('paypal_payment_method,bank_payment_method,cashOnDev_payment_method,paypal_id,paypal_currency,pickUpStore_payment_method,payByPhone_payment_method,payByPhone_payment_method_phonenum,payNowPickLater_payment_method');
//paypal
$host = ($_SERVER['SERVER_NAME'] == 'demo.freesites.com.au') ? 'demo.freesites.com.au' : 'www.'.$_SERVER['SERVER_NAME'];
$host = str_replace("www.www.", "www.", $host);
$html .= '<table border="0" cellspacing="0" cellpadding="0" id="payment-types">';
//paypal
if ($paypal['paypal_payment_method'] == 'y')
{
    $html .= '<tr>
<td>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="submit" name="button" id="button" value="Credit card or PayPal" class="button" style="width:auto" />
<img src="/inc/shop/images/creditcards.gif" style="border:none;background:none;" />
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="'.$paypal['paypal_id'].'" />
<input type="hidden" name="item_name" value="'.$host.' Order - '.$_SESSION['owner'].'" />
<input type="hidden" name="amount" value="'.number_format($subtotal + $order['shipping'] - $order['sales_tax'] - $order['discount_value'], 2, '.', '').'" />
<input type="hidden" name="no_shipping" value="0" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="currency_code" value="'.$paypal['paypal_currency'].'" />
<input type="hidden" name="lc" value="'.$order['country'].'" />
<input type="hidden" name="return" value="'.httpOrHttps().'://'.$host.'/'.$s['shop_page'].'/'.$seo_pageTitle.'/Thankyou" />
<input type="hidden" name="notify_url" value="'.httpOrHttps().'://'.$host.'/inc/shop/paypal.php?order='.$_SESSION['owner'].'" />
<input type="hidden" name="bn" value="PP-BuyNowBF" />
</form></td>
<td><span>Pay now using your credit card, bank account or PayPal account balance. Click \'credit card or PayPal\' to be taken to the PayPal website to process your payment.</span></td>
</tr>';
}
//bank
if ($paypal['bank_payment_method'] == 'y')
{
    $html .= '<tr>
<td><form action="'.$basketPageURL.'/Bank" method="post"><input type="submit" name="button2" style="width:auto" id="button2" value="Direct Bank Deposit" class="button" /><input type="hidden" name="type" id="type" value="bank" /></form></td>
<td><span>Pay for your order directly into our bank account through your financial institution\'s internet or phone banking service. Our banking details will be emailed to you. Click the \'Direct Bank Deposit\' button to confirm your order.</span></td>
</tr>';
}
//cash on dev
if ($paypal['cashOnDev_payment_method'] == 'y' && $type['hide_delivery_section'] == 'n')
{
    $html .= '<tr>
<td><form action="'.$basketPageURL.'/Bank" method="post"><input type="submit" name="button3" style="width:auto" id="button3" value="Cash on Delivery" class="button" /><input type="hidden" name="type" id="type" value="cod" /></form></td>
<td><span>Pay for your order when we deliver your items. Click the \'Cash on Delivery\' button to confirm your order.</span></td>
</tr>';
}
//pick up from store
if ($paypal['pickUpStore_payment_method'] == 'y')
{
    $address = $site->shop_settings('shop_address');
    $html .= '<tr>
<td><form action="'.$basketPageURL.'/Bank" method="post"><input type="submit" name="button4" style="width:auto" id="button4" value="Pick up from store" class="button" /><input type="hidden" name="type" id="type" value="pickstore" /></form></td>
<td><span>Pay at our store in person and pick up your order. You will not be charged for shipping. Click the \'Pick up from our store\' button to confirm your order.</span>
<p>Pickup address:<br />'.stripslashes($address['shop_address']).'</p></td>
</tr>';
}
//pay now pick up later
if ($paypal['paypal_payment_method'] == 'y' && $paypal['payNowPickLater_payment_method'] == 'y')
{
    $html .= '<tr>
<td>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="submit" name="button" id="button" value="Pay now pick up later" class="button" />
<img src="/inc/shop/images/creditcards.gif" />
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="'.$paypal['paypal_id'].'" />
<input type="hidden" name="item_name" value="'.$host.' Order - '.$_SESSION['owner'].'" />
<input type="hidden" name="amount" value="'.number_format($subtotal - $order['sales_tax'] - $order['discount_value'], 2, '.', '').'" />
<input type="hidden" name="no_shipping" value="0" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="currency_code" value="'.$paypal['paypal_currency'].'" />
<input type="hidden" name="lc" value="'.$order['country'].'" />
<input type="hidden" name="return" value="'.httpOrHttps().'://'.$host.'/'.$s['shop_page'].'/'.$seo_pageTitle.'/Thankyou" />
<input type="hidden" name="notify_url" value="'.httpOrHttps().'://'.$host.'/inc/shop/paypal.php?order='.$_SESSION['owner'].'&pickuplater=y" />
<input type="hidden" name="bn" value="PP-BuyNowBF" />
</form></td>
<td><span>Pay now using your credit card, bank account or PayPal account balance and pick up your order. You will not be charged for shipping. Click \'Pay now pick up later\' to be taken to the PayPal website to process your payment.</span><p>Pickup address:<br />'.stripslashes($address['shop_address']).'</p></td>
</tr>';
}
//pay by phone
if ($paypal['payByPhone_payment_method'] == 'y')
{
    $html .= '<tr>
<td><form action="'.$basketPageURL.'/Bank" method="post"><input type="submit" name="button4" style="width:auto" id="button4" value="Pay by Phone" class="button" /><input type="hidden" name="type" id="type" value="phonePay" /></form></td>
<td><span>Simply call us on '.$paypal['payByPhone_payment_method_phonenum'].' after placing your order to provide your credit card details to pay for this order. Click the \'Pay by Phone\' button to confirm your order.</span></td>
</tr>';
}
//end
$html .= '</table>';
//merge with template
$mainhtml .= stripslashes($html);
//include this add-ons's overwrite css file from the template being used, if there is one
$t = settings('template');
if (file_exists('templates/'.$t['template'].'/addons/shop.css'))
{
    $mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="/templates/'.$t['template'].'/addons/shop.css" /></head>', $mainhtml);
}
else
{
    $mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="//www.freesites.com.au/addons/shop.css" /></head>', $mainhtml);
}
//end
