Exemple #1
0
 public static function getForm()
 {
     $form = parent::getForm();
     $paypalHost = 'https://' . SiteConfig::get('Cart::PaypalHostName') . '/cgi-bin/webscr';
     $form->updateAttributes(array('action' => $paypalHost));
     $form->updateAttributes(array('onSubmit' => "return checkBeforeSendToPaypal()"));
     $form->setConstants(array('cmd' => '_cart'));
     $form->addElement('hidden', 'cmd');
     $form->setConstants(array('upload' => 1));
     $form->addElement('hidden', 'upload');
     //Set the ID of the customer making this order
     $form->setConstants(array('custom' => session_id()));
     $form->addElement('hidden', 'custom');
     $form->setConstants(array('currency_code' => "CAD"));
     $form->addElement('hidden', 'currency_code');
     $form->setConstants(array('business' => SiteConfig::get('Cart::PaypalBusinessEmailAddress')));
     $form->addElement('hidden', 'business');
     $form->setConstants(array('return' => "http://" . $_SERVER['HTTP_HOST'] . "/store/orderComplete"));
     $form->addElement('hidden', 'return');
     //		<input type="hidden" name="return" value="ordercomplete.php?req=success">
     $items = CartBasket::getUserCartBaskets($_SESSION['authenticated_user']->getId());
     $count = 0;
     foreach ($items as $item) {
         $form->setConstants(array('item_name_' . ++$count => $item->getProduct()->getName()));
         $form->addElement('hidden', 'item_name_' . $count);
         $form->setConstants(array('item_number_' . $count => $item->getProduct()->getModel()));
         $form->addElement('hidden', 'item_number_' . $count);
         $form->setConstants(array('amount_' . $count => round($item->getPrice(), 2)));
         $form->addElement('hidden', 'amount_' . $count);
         $form->setConstants(array('quantity_' . $count => $item->getQuantity()));
         $form->addElement('hidden', 'quantity_' . $count);
         //The tax will be passed as one value
         //$taxRate = CartTaxRate::getTaxRate($item->getProduct()->getTaxClass(), $_SESSION['cart_checkout']['address']['shipping_address'])->getRate();
         //$taxValue = $taxRate * $item->getPrice();//Do not multiply by the quantity because paypal does it automatically
         //$taxValue = ceil($taxValue);
         //$taxValue = $taxValue / 100;
         //$form->setConstants( array ( 'tax_' . $count => $taxValue ) );
         //$form->addElement( 'hidden', 'tax_' . $count );
         //Charge the shipping cost only for the first item because the shipping cost will apply on all the items
         $shippingCost = 0;
         if ($count == 1) {
             $shipping = @$_SESSION['cart_checkout']['shipping'];
             if ($shipping) {
                 $shippingCost = number_format($_SESSION['cart_checkout']['shipping']->getCost(), 2);
             }
             $shippingCost = ceil($shippingCost * 100) / 100;
         }
         $form->setConstants(array('shipping_' . $count => $shippingCost));
         $form->addElement('hidden', 'shipping_' . $count);
     }
     $temp = new Module_Cart();
     $form->setConstants(array('tax_cart' => $temp->getTax()));
     $form->addElement('hidden', 'tax_cart');
     //$form->setConstants( array ( 'shipping' => number_format($_SESSION['cart_checkout']['shipping']->getCost(), 2) ) );
     //$form->addElement( 'hidden', 'shipping' );
     $form->addElement('image', 'cart_submit', 'https://www.paypal.com/en_US/i/btn/x-click-but23.gif');
     return $form;
 }
Exemple #2
0
 public function validateOrder()
 {
     //The following function checks to ses if the user paid for what they ordered or not
     //First, make sure that the receiver is us:
     $this->log("Receiver is: " . $_POST["business"] . ", Our account is: " . $this->accountEmail);
     if ($_POST["business"] != $this->accountEmail) {
         $_SESSION['cart_checkout']['orderFailureReason'] = "The money was paid to another user";
         return false;
     }
     $sessionID = $_POST["custom"];
     //Switch to the user's session. To do so, first we have to close the currenct session with Paypal.
     session_write_close();
     session_id($sessionID);
     //Then we have to assign the user's session ID
     session_start();
     //Then we can start a new session.
     $this->log("The ID of the session is: " . $sessionID);
     $this->log("The ID of the customer is: " . $_SESSION['authenticated_user']->getId());
     $this->log("Amount: " . $_POST["mc_gross"] . ", " . $_POST["mc_currency"]);
     $cartitems = CartBasket::getUserCartBaskets($_SESSION['authenticated_user']->getId());
     //Calculate the total amount of the client's order
     $tmpModule = new Module_Cart();
     $totalAmount = $tmpModule->getTotal();
     /*
     $totalAmount = 0.00;
     $tax = 0.00;
     foreach ($cartitems as $item) {
     	$rate = CartTaxRate::getTaxRate($item->getProduct()->getTaxClass(), $_SESSION['cart_checkout']['address']['shipping_address'])->getRate();
     	$taxValue = $rate * ($item->getPrice() * $item->getQuantity());
     	$taxValue = ceil($taxValue);
     	$taxValue = $taxValue / 100;
     	$totalAmount += $item->getPrice() * $item->getQuantity() + $taxValue;
     }
     $shipping = @$_SESSION['cart_checkout']['shipping'];
     if ($shipping){
     	$shippingCost = $shipping->getCost();
     	$shippingCost = ceil($shippingCost * 100) / 100;
     	$totalAmount += $shippingCost;
     }
     $totalAmount = ceil($totalAmount * 100) / 100;//Account for numbers such as: 19.6421 such amount will be rounded to 19.65
     */
     //The currency of the client's order is always in Canadian Dollar. This needs to be tweaked so the admin will be able to set the currencies
     $currency = "CAD";
     $this->log("The order amount is: " . $totalAmount . ", " . $currency);
     //The reason why we're using the ceil function here is to account for the difference in calculating the taxes(if any)
     //For example, if paypal rounds the tax down (2.3487 becomes 2.34) and we round it up (2.3487 becomes 2.35), there should be no difference
     if (ceil($totalAmount) == ceil($_POST["mc_gross"]) && $currency == $_POST["mc_currency"]) {
         $this->log("The client has paid for what they ordered");
         return true;
     } else {
         $this->log("The client has NOT paid for what they ordered");
         $_SESSION['cart_checkout']['orderFailureReason'] = "The client has NOT paid for what they ordered";
         return false;
     }
 }
Exemple #3
0
 public function getUserInterface($params)
 {
     //$this->setPageTitle('Feed Store');//Foreign Affair
     $this->addCSS('/modules/Cart/css/cart.css');
     $this->addJS('/modules/Cart/js/cart.js');
     switch (@$params['action']) {
         case 'add':
             require_once 'include/CartBasket.php';
             require_once 'include/CartProduct.php';
             $item = new CartBasket();
             $product = new CartProduct($params['productId']);
             $price = 0;
             if (isset($_REQUEST['att'])) {
                 $uniqid = uniqid(rand(), true);
                 $item->setProduct($params['productId'] . ':' . $uniqid);
                 $price = $item->getPrice();
                 foreach ($_REQUEST['att'] as $key => $newatt) {
                     $attribute = new CartProductAttribute($newatt);
                     $att = new CartBasketAttribute();
                     $att->setProduct($params['productId'] . ':' . $uniqid);
                     $att->setOptionsId($attribute->getOptionsId()->getId());
                     $att->setValueId($attribute->getValue()->getId());
                     $att->setValueText($attribute->getOptionsId()->getName() . ': ' . $attribute->getValue()->getName());
                     $price = $price + $attribute->getValuesPrice();
                     if (isset($_SESSION['authenticated_user'])) {
                         $att->setUser($_SESSION['authenticated_user']->getId());
                         $att->save();
                     }
                 }
             } else {
                 $item->setProduct($params['productId']);
             }
             if ($product->getSpecials()) {
                 $price += $product->getSpecials()->getNew_products_price();
             } else {
                 $price += $product->getPrice();
             }
             $item->setPrice($price);
             if (isset($params['productQuantity'])) {
                 $item->setQuantity($params['productQuantity']);
             } else {
                 $item->setQuantity(1);
             }
             if (isset($_SESSION['authenticated_user'])) {
                 $item->setUser($_SESSION['authenticated_user']->getId());
                 $item->save();
             } else {
                 $_SESSION['cart_basket'][] = $item;
             }
             break;
         case 'remove':
             $item = @new CartBasket(@$_REQUEST['cartbasket_id']);
             if (is_null($item->delete())) {
                 $tmp = array();
                 foreach ($_SESSION['cart_basket'] as &$item) {
                     if ($item->getProduct()->getId() != $_REQUEST['product_id']) {
                         $tmp[] = $item;
                     }
                 }
                 $_SESSION['cart_basket'] = $tmp;
             }
             break;
     }
     //var_dump($_REQUEST);
     //echo $params['section'];exit;
     switch ($params['section']) {
         case 'search':
             $items = CartProduct::searchProducts(@$_REQUEST["selSupplier"], @$_REQUEST["selCategory"], @$_REQUEST["selProductType"]);
             $this->smarty->assign('products', $items);
             return $this->smarty->fetch('store.tpl');
             break;
         case 'canCheckout':
             /*Check to see if the user is ready to go to Paypal
              * We should check the following:
              * - The user is actually logged in
              * - The price of the order is at least $250
              * - The shipping address is present
              * - The billing address is present
              * - The shipping information is there (Canada Post or FedEx)
              */
             $canCheckout = Module_Cart::canUserCheckout();
             foreach ($canCheckout as $key => $value) {
                 $this->smarty->assign($key, $value);
             }
             $_SESSION['cart_checkout']['delivery_direction'] = @$_REQUEST["delivery_direction"];
             return $this->smarty->fetch("canDoCheckOut.tpl");
             break;
         case 'cartdetail':
             if (isset($_REQUEST['ship_type'])) {
                 $_SESSION['cart_checkout']['shipping'] = Shipping::factory($_REQUEST['ship_type']);
             }
             $this->setUpCartDetail();
             return $this->smarty->fetch('cart_detail.tpl');
             break;
         case 'cart':
             if (!isset($_SESSION['authenticated_user'])) {
                 //Reset all the shipping variables and addresses
                 $_SESSION['cart_checkout']['shipping'] = null;
                 $_SESSION['cart_checkout']['address']['billing_address'] = null;
                 $_SESSION['cart_checkout']['address']['shipping_address'] = null;
             }
             $this->setUpCartDetail();
             $user = new User();
             $form = $user->getUserAddEditForm('/store/checkout');
             $form->removeElement('section');
             $form->setConstants(array('account' => 'create'));
             $form->addElement('hidden', 'account');
             $this->smarty->assign('user_form', $form);
             $this->smarty->assign('usernameexists', @$_REQUEST["usernameexists"]);
             return $this->smarty->fetch('cart.tpl');
             break;
         case 'buyOrder':
             //DO NOT UNCOMMENT THE FOLLOWING LINE UNLESS YOU WANT TO TEST THE PURCHASE OPERATION
             //The following line buys the products that are in session. In other words, it bypasses Paypal.
             $_SESSION['cart_checkout']['payment']->process();
         case 'deliverydirections':
             $_SESSION['cart_checkout']['delivery_direction'] = @$_REQUEST["delivery_direction"];
             return $_SESSION['cart_checkout']['delivery_direction'];
             break;
         case 'checkout':
             $_SESSION['cart_checkout']['order'] = null;
             //Make sure to remove the old order (if any) from the session
             $_SESSION['cart_checkout']['orderFailureReason'] = null;
             //AND delete the previous failure reason
             Module_Cart::initSessionVariables();
             switch (@$_REQUEST['account']) {
                 case 'create':
                     $user = new User();
                     $form = $user->getUserAddEditForm();
                     if (@$_REQUEST["user_created"] == 1) {
                         $_POST["username"] = $_REQUEST["a_username"];
                         $_POST["password"] = $_REQUEST["a_password"];
                         $_POST["doLogin"] = "******";
                         $auth_container = new CMSAuthContainer();
                         $auth = new Auth($auth_container, null, 'authInlineHTML');
                         $auth->start();
                     }
                     if (@$_REQUEST["username_already_exists"]) {
                         header('Location: /store/cart&usernameexists=1');
                         exit;
                     }
                     $_SESSION['authenticated_user'] = $user;
                     break;
             }
             //Only logged in users can view this page
             if (!isset($_SESSION['authenticated_user'])) {
                 header('Location: /store/cart');
                 exit;
             }
             //echo $_SESSION['authenticated_user']->getAddress()->getId() . "))))";exit;
             $_SESSION['cart_checkout']['shipping'] = Shipping::factory('EAndA');
             //Always set the shipping to EAndA
             //The billing address of the order will be the addres of the user
             $_SESSION['cart_checkout']['address']['billing_address'] = @$_SESSION['authenticated_user']->getAddress();
             $_SESSION['cart_checkout']['address']['shipping_address'] = @$_SESSION['authenticated_user']->getShippingAddress();
             $this->setUpCartDetail();
             $this->addJS('/modules/Cart/js/cart.js');
             $shipping = Shipping::getAllShippings();
             if (isset($_SESSION['cart_checkout']['payment'])) {
                 $this->smarty->assign('payment_types', $_SESSION['cart_checkout']['payment']->getForm(Payment::getForm()));
             } else {
                 $this->smarty->assign('payment_types', Payment::getForm());
             }
             if (isset($_SESSION['cart_checkout']['shipping'])) {
                 $this->smarty->assign('ship_types', $_SESSION['cart_checkout']['shipping']->getForm());
             } else {
                 $this->smarty->assign('ship_types', Shipping::getForm());
             }
             if (!isset($_SESSION['cart_checkout']['address']['shipping_address'])) {
                 $_SESSION['cart_checkout']['address']['shipping_address'] = new Address();
             }
             $this->smarty->assign('ship_address', $_SESSION['cart_checkout']['address']['shipping_address']);
             if (!isset($_SESSION['cart_checkout']['address']['billing_address'])) {
                 $_SESSION['cart_checkout']['address']['billing_address'] = new Address();
             }
             $this->smarty->assign('bill_address', $_SESSION['cart_checkout']['address']['billing_address']);
             $this->smarty->assign('shipping_types', $shipping);
             $delivery_direction = @$_SESSION['cart_checkout']['delivery_direction'];
             $this->smarty->assign('delivery_direction', $delivery_direction);
             return $this->smarty->fetch('cart_checkout_address.tpl');
             break;
         case 'payment':
             //$this->addJS('/modules/Cart/js/cart.js');
             $payment = $_SESSION['cart_checkout']['payment'];
             $form = $payment->getForm(Payment::getForm());
             if ($form->validate() && $form->isSubmitted() && isset($_REQUEST['cart_submit'])) {
                 return $payment->complete($this->smarty);
             } else {
                 return '<div id="pay_form">' . $form->display() . '</div>';
             }
             break;
         case 'payform':
             if (!isset($_REQUEST['pay_type'])) {
                 $_REQUEST['pay_type'] = "Paypal";
             }
             if (isset($_REQUEST['pay_type'])) {
                 $_SESSION['cart_checkout']['payment'] = Payment::factory($_REQUEST['pay_type']);
             }
             $form = Payment::getForm();
             return $_SESSION['cart_checkout']['payment']->getForm($form)->display();
             //return Payment::getForm($form)->display();
             break;
         case 'address':
             //No need to set the ID of the address to null.
             //$_SESSION['cart_checkout']['address'][$_REQUEST['adr_type']]->setId(null);
             if (@$_REQUEST["sameAsBilling"]) {
                 //The user has clicked on the link: "The shipping address is the same as the billing address"
                 //Copy the billing address object to the shipping address object
                 //Make sure we're not assigning pointers
                 $_SESSION['cart_checkout']['address']["shipping_address"]->copy($_SESSION['cart_checkout']['address']["billing_address"]);
             }
             if ($_REQUEST['adr_type'] == "shipping_address") {
                 $this->smarty->assign('sameAsBilling', "1");
             }
             $form = $_SESSION['cart_checkout']['address'][$_REQUEST['adr_type']]->getAddEditForm($_REQUEST['adr_type']);
             $form->addElement('submit', 'submit', 'Submit');
             $form->updateAttributes(array('action' => '/store/address'));
             $form->setConstants(array('adr_type' => $_REQUEST['adr_type']));
             $form->addElement('hidden', 'adr_type');
             if (isset($_REQUEST['submit'])) {
                 $this->smarty->assign('address', $_SESSION['cart_checkout']['address'][$_REQUEST['adr_type']]);
                 $this->smarty->assign('adr_type', $_REQUEST['adr_type']);
                 /***************************************
                  * The following lines are important.
                  * Even though the billing address ID is stored in the user's object and there is no need to re-assign it
                  * The old users have that ID set to zero. So, we need to change that to the ID of the billing address
                  */
                 if ($_REQUEST['adr_type'] == "billing_address") {
                     $_SESSION['authenticated_user']->setAddress($_SESSION['cart_checkout']['address']["billing_address"]);
                     $_SESSION['authenticated_user']->save();
                 }
                 if ($_REQUEST['adr_type'] == "shipping_address") {
                     $_SESSION['authenticated_user']->setShippingAddress($_SESSION['cart_checkout']['address']["shipping_address"]);
                     $_SESSION['authenticated_user']->save();
                 }
                 return $this->smarty->fetch('cart_address_format.tpl');
             } else {
                 return $form->display();
             }
             break;
         case 'product':
             $this->addJS('/modules/Cart/js/cart.js');
             $this->addCSS('/modules/Cart/css/product.css');
             $product = new CartProduct($params['page']);
             /*
             switch (@$_REQUEST['subsection']) {
             	case 'accessories':
             		$this->smarty->assign('products', $product->getAccessories());
             		$this->smarty->assign('section', 'accessories');
             		break;
             	default:
             }
             */
             $this->smarty->assign('product', $product);
             return $this->smarty->fetch('cart_product.tpl');
             break;
         case 'productform':
             $product = new CartProduct($params['productId']);
             $form = $product->getAddToCartForm();
             return $form->display();
             break;
         case 'manufacturer':
             $this->addJS('/modules/Cart/js/cart.js');
             $this->smarty->assign('threecol', true);
             //$products = CartManufacturer::getProductsByManufacturer($params['page']);
             require_once 'Pager.php';
             $pagerOptions = array('mode' => 'Sliding', 'delta' => 3, 'perPage' => 8, 'append' => false, 'path' => '/store/manufacturer', 'fileName' => $params['page'] . "/%d", 'totalItems' => CartManufacturer::getCountCartManufacturer($params['page']));
             $pager =& Pager::factory($pagerOptions);
             list($from, $to) = $pager->getOffsetByPageId();
             //$items = CartManufacturer::getCategoriesByManufacturer($params['page']);
             $items = CartManufacturer::getProductsByManufacturer($params['page'], $from, $to);
             $this->smarty->assign('pager_links', $pager->links);
             $this->smarty->assign('page_numbers', array('current' => $pager->getCurrentPageID(), 'total' => $pager->numPages()));
             $this->smarty->assign('products', $items);
             $this->smarty->assign('manufacturer', new CartManufacturer($params['page']));
             //$this->smarty->assign('products', $products);
             return $this->smarty->fetch('store.tpl');
             break;
         case 'IPN':
             Module_Cart::initSessionVariables();
             require_once 'include/PaypalIPN.php';
             require_once 'include/PaypalLog.php';
             $pp = new PaypalIPN();
             $process = $pp->checkOrder();
             //This method returns either true in case the client actually paid for the products they asked for, or false in case the request didn't come from paypal OR the client didn't pay the right amount of money
             if ($process) {
                 //Store the order
                 $_SESSION['cart_checkout']['payment']->process();
                 $this->sendEmail(true);
             } else {
                 $this->sendEmail(false);
                 //Log a false IPN for security purposes
             }
             exit;
             break;
         case 'orderComplete':
             if (@$_SESSION['cart_checkout']['order']->getId()) {
                 $this->smarty->assign('order', $_SESSION['cart_checkout']['order']);
                 $this->smarty->assign('address', $_SESSION['cart_checkout']['address']['shipping_address']);
                 $this->smarty->assign('shippingCost', $this->getShipping());
                 return $this->smarty->fetch('orderComplete.tpl');
             } else {
                 $this->smarty->assign('reason', @$_SESSION['cart_checkout']['orderFailureReason']);
                 return $this->smarty->fetch('orderNotComplete.tpl');
             }
             break;
         case 'suppliers':
             $items = CartManufacturer::getAllCartManufacturers();
             $this->smarty->assign('suppliers', $items);
             return $this->smarty->fetch('store.tpl');
             break;
         case 'myorders':
             //Only logged in users can view this page
             if (!isset($_SESSION['authenticated_user'])) {
                 header('Location: /user/');
                 exit;
             }
             $this->addCSS('/modules/Cart/css/cart.css');
             $this->addCSS('/css/facebox.css');
             $this->addJS('/modules/Cart/js/cart.js');
             $this->addJS('/js/facebox.js');
             $myOrders = CartOrder::getAllCartOrders($_SESSION['authenticated_user']->getId());
             $this->smarty->assign('orders', $myOrders);
             return $this->smarty->fetch('my_orders.tpl');
             break;
         case 'orderDetails':
             //Only logged in users can view this page
             if (!isset($_SESSION['authenticated_user'])) {
                 header('Location: /user/');
                 exit;
             }
             $order = new CartOrder(@$_REQUEST['cartorder_orders_id']);
             if ($order->getCustomer()->getId() == $_SESSION['authenticated_user']->getId()) {
                 $this->smarty->assign('order', $order);
                 return $this->smarty->fetch('admin/order_details.tpl');
             }
             return "You have to login to see this order";
             break;
         case 'category':
         default:
             $this->addJS('/modules/Cart/js/cart.js');
             $this->smarty->assign('threecol', true);
             if (!isset($params['page'])) {
                 $cat_id = 0;
             } else {
                 $cat_id = $params['page'];
             }
             $cats = CartCategory::getCartCategorys(array('parent_id' => $cat_id));
             if (!isset($_REQUEST['subsection'])) {
                 //					require_once 'Pager.php';
                 //
                 //					$pagerOptions = array(
                 //					    'mode'     => 'Sliding',
                 //					    'delta'    => 3,
                 //					    'perPage'  => 10,
                 //						'append'   => false,
                 //						'path'		=> '/store/category/',
                 //						'fileName'  => $cat_id . "/%d",
                 //						'totalItems' => CartProduct::getCountCartProductsByCat($cat_id)
                 //					);
                 //					$pager =& Pager::factory($pagerOptions);
                 //					list($from, $to) = $pager->getOffsetByPageId();
                 $items = CartProduct::getCategoryProducts($cat_id);
                 //					$this->smarty->assign('pager_links', $pager->links);
                 //					$this->smarty->assign(
                 //					    'page_numbers', array(
                 //					        'current' => $pager->getCurrentPageID(),
                 //					        'total'   => $pager->numPages()
                 //					    )
                 //					);
             } else {
                 if ($_REQUEST['subsection'] == 'manufacturer') {
                     $items = CartProduct::getCategoryProducts($cat_id);
                     $arr = array();
                     foreach ($items as $item) {
                         if ($item->getManufacturer()->getId() == $_REQUEST['subpage']) {
                             $arr[] = $item;
                         }
                     }
                     $items = $arr;
                 }
                 $this->smarty->assign('manufacturer', new CartManufacturer($_REQUEST['subpage']));
             }
             $arr = array();
             //foreach ($items as $itm) {
             //	if (count($itm->getAccessoryOf()) == 0) {
             //		$arr[] = $itm;
             //	}
             //}
             //$items = $arr;
             $this->smarty->assign('products', $items);
             // stuff
             //$products = CartProduct::getCategoryProducts($cat_id);
             $this->smarty->assign('categories', $cats);
             $this->smarty->assign('cur_cat', new CartCategory($cat_id));
             //$this->smarty->assign('products', $products);
             return $this->smarty->fetch('store.tpl');
     }
 }