Example #1
0
 /**
  * [action_buy] Pay for ad, and set new order 
  *
  */
 public function action_buy()
 {
     if (Core::config('general.subscriptions') == FALSE) {
         throw HTTP_Exception::factory(404, __('Page not found'));
     }
     //getting the user that wants to buy now
     if (!Auth::instance()->logged_in()) {
         Alert::set(Alert::INFO, __('To buy this product you need to register first.'));
         $this->redirect(Route::url('oc-panel'));
     }
     //check plan exists
     $plan = new Model_Plan();
     $plan->where('seoname', '=', $this->request->param('id'))->where('status', '=', 1)->find();
     //loaded published and with stock if we control the stock.
     if ($plan->loaded() and $plan->status == 1) {
         //free plan can not be renewed
         if ($plan->price == 0 and $this->user->subscription()->id_plan == $plan->id_plan) {
             Alert::set(Alert::WARNING, __('Free plan can not be renewed, before expired'));
             HTTP::redirect(Route::url('pricing'));
         }
         $order = Model_Order::new_order(NULL, $this->user, $plan->id_plan, $plan->price, core::config('payment.paypal_currency'), __('Subscription to ') . $plan->name);
         //free plan no checkout
         if ($plan->price == 0) {
             $order->confirm_payment('cash');
             $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders')));
         } else {
             $this->redirect(Route::url('default', array('controller' => 'plan', 'action' => 'checkout', 'id' => $order->id_order)));
         }
     } else {
         throw HTTP_Exception::factory(404, __('Page not found'));
     }
 }