public function cart()
 {
     $this->setTemplate('cart.tpl');
     $c = new ShoppingCart();
     if (isset($_POST['update'])) {
         foreach ($_POST['qty'] as $v => $qty) {
             if (is_numeric($qty) && $qty > 0) {
                 $c->update($v, $qty);
             } elseif (is_numeric($qty) && $qty == 0) {
                 $c->remove($v);
                 // vendor locking
                 if ($c->isEmpty()) {
                     Session::clear('vendor_lock');
                 }
             }
         }
         $this->redirect('store/cart');
     } elseif (isset($_POST['checkout'])) {
         $this->redirect('store/checkout');
     }
     $items = $c->loadFromCart($this);
     if (sizeof($items) > 0) {
         $ids = array();
         foreach ($items as $item) {
             array_push($ids, $item['id']);
         }
         $v = Model::load('ProductVariant');
         $cat_ids = $v->getCategories($ids);
         $cat = Model::load('CategoryItem');
         $calc = new ShippingCalculator($c->calcTotal($items), $cat_ids, $cat, sizeof($items), false);
         $shipping = $calc->getFee();
         $shipping = 0;
         $this->assign('shipping', $shipping);
         $this->assign('total', $c->calcTotal($items) + $shipping);
         $this->assign('items', $items);
     }
     if (isset($this->vendor_lock)) {
         $this->assign('vendor_id', $this->vendor_lock);
     }
     $this->assign('last_cat', Session::get('last_cat'));
 }
 public function default_event()
 {
     $c = new ShoppingCart();
     if ($c->isEmpty()) {
         $this->redirect('paypal/cancel');
     }
     $items = $c->loadFromCart($this);
     $co = new Checkout($items, $this);
     $invoice_no = $co->getInvoiceNo();
     $o = Model::load('OrderItem');
     $o->id = $invoice_no;
     $o->load();
     $products = array();
     //$product[0]['alias'] = 'Some product';
     //$product[0]['price'] = 1.99;
     //$product[0]['code'] = 23;
     $interface = 'http://' . WEB_ROOT . PUBLIC_DIR . '/paypal/';
     $message = '';
     $p = new PaypalClass();
     $p->ipn_log = false;
     $p->add_field('cmd', '_cart');
     $p->add_field('upload', '1');
     // address
     $p->add_field('first_name', $o->first_name);
     $p->add_field('last_name', $o->last_name);
     $p->add_field('address1', $o->address1);
     $p->add_field('address2', $o->address2);
     $p->add_field('city', $o->city);
     $p->add_field('state', $o->state);
     $p->add_field('zip', $o->zip);
     $p->add_field('country', $o->country);
     $p->add_field('address_override', 1);
     $p->add_field('no_shipping', 1);
     // shipping
     $ids = array();
     foreach ($items as $item) {
         array_push($ids, $item['id']);
     }
     $v = Model::load('ProductVariant');
     $cat_ids = $v->getCategories($ids);
     $cat = Model::load('CategoryItem');
     if ($o->country != 'GB') {
         $intl = true;
     } else {
         $intl = false;
     }
     $calc = new ShippingCalculator($c->calcTotal($items), $cat_ids, $cat, sizeof($items), $intl);
     $shipping = $calc->getFee();
     $shipping = $this->getShipping();
     $p->add_field('shipping_1', $shipping);
     $o->shipping = $shipping;
     $o->save(Model::getTable('OrderItem'), array(), 1);
     $i = 1;
     foreach ($items as $index => $item) {
         $p->add_field('item_name_' . $i, $item['name']);
         $p->add_field('amount_' . $i, $item['price']);
         $p->add_field('item_number_' . $i, $this->getItemNumber($item['id']));
         $p->add_field('quantity_' . $i, $item['qty']);
         $o = explode(', ', $item['options']);
         $pr = explode(', ', $item['properties']);
         foreach ($o as $index => $item) {
             $p->add_field('os' . $index . '_' . $i, $o[$index]);
             $p->add_field('on' . $index . '_' . $i, $pr[$index]);
         }
         $i++;
     }
     //    $p->add_field('image_url', 'http://'.WEB_ROOT.PUBLIC_DIR.'/img/pier.png');
     $p->add_field('invoice', $this->getInvoiceNumber($invoice_no));
     $p->add_field('no_shipping', 1);
     $p->add_field('currency_code', 'GBP');
     $p->add_field('business', '*****@*****.**');
     //$p->add_field('business', '*****@*****.**');
     //$p->add_field('business', $this->getBusiness());
     $p->add_field('return', $interface . 'success');
     $p->add_field('notify_url', $interface . 'ipn');
     $p->add_field('cancel_return', $interface . 'cancel');
     $p->paypal_url = $this->getPayPalURL();
     $c->emptyCart();
     $this->presenter->assign('paypal_url', $p->paypal_url);
     $this->presenter->assign('fields', $p->fields);
     //$p->dump_fields();
 }