/**
  * expired featured ads
  * @return void
  */
 public static function renew()
 {
     if (Core::config('general.subscriptions') == TRUE) {
         //get expired subscription that are active
         $subscriptions = new Model_Subscription();
         $subscriptions = $subscriptions->where('status', '=', 1)->where('expire_date', '<=', Date::unix2mysql())->order_by('created', 'desc')->find_all();
         foreach ($subscriptions as $s) {
             //disable the plan
             $s->status = 0;
             try {
                 $s->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $plan = $s->plan;
             if ($plan->loaded() and $plan->status == 1) {
                 //generate a new order
                 $order = Model_Order::new_order(NULL, $s->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');
                 } else {
                     $checkout_url = $s->user->ql('default', array('controller' => 'plan', 'action' => 'checkout', 'id' => $order->id_order));
                     $s->user->email('plan-expired', array('[PLAN.NAME]' => $plan->name, '[URL.CHECKOUT]' => $checkout_url));
                 }
             }
             //if plan loaded
         }
         //end foreach
     }
     //if subscription active
 }
Esempio n. 2
0
 public function action_create()
 {
     try {
         if (!Valid::email(core::request('email'))) {
             $this->_error(__('Invalid email'), 501);
         } elseif (!is_numeric(core::request('id_product'))) {
             $this->_error(__('Invalid product'), 501);
         } else {
             $product = new Model_Product(core::request('id_product'));
             if ($product->loaded()) {
                 $user = Model_User::create_email(core::request('email'), core::request('name'));
                 $order = Model_Order::new_order($user, $product);
                 $order->confirm_payment(core::request('paymethod', 'API'), core::request('txn_id'), core::request('pay_date'), core::request('amount'), core::request('currency'), core::request('fee'));
                 //adding the notes
                 $order->notes = core::request('notes');
                 $order->save();
                 $this->rest_output(array('order' => self::get_order_array($order)));
             } else {
                 $this->_error(__('Something went wrong'), 501);
             }
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
Esempio n. 3
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'));
     }
 }
Esempio n. 4
0
 /**
  * expired featured ads
  * @return void
  */
 public static function renew()
 {
     if (Core::config('general.subscriptions') == TRUE) {
         //get expired subscription that are active
         $subscriptions = new Model_Subscription();
         $subscriptions = $subscriptions->where('status', '=', 1)->where('expire_date', '<=', Date::unix2mysql())->order_by('created', 'desc')->find_all();
         foreach ($subscriptions as $s) {
             //disable the plan
             $s->status = 0;
             try {
                 $s->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
             $plan = $s->plan;
             if ($plan->loaded() and $plan->status == 1) {
                 //generate a new order
                 $order = Model_Order::new_order(NULL, $s->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');
                 } else {
                     $paid = FALSE;
                     //customers who paid with sripe we can charge the recurrency
                     if ($order->user->stripe_agreement != NULL) {
                         StripeKO::init();
                         // Create the charge on Stripe's servers - this will charge the user's card
                         try {
                             $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, 'customer' => $order->user->stripe_agreement, "description" => $order->description, "metadata" => array("id_order" => $order->id_order)));
                             $paid = TRUE;
                         } catch (Exception $e) {
                             // The card has been declined
                             Kohana::$log->add(Log::ERROR, 'Stripe The card has been declined');
                             $paid = FALSE;
                         }
                     }
                     if ($paid === TRUE) {
                         //mark as paid
                         $order->confirm_payment('stripe', $charge->id);
                     } else {
                         $checkout_url = $s->user->ql('default', array('controller' => 'plan', 'action' => 'checkout', 'id' => $order->id_order));
                         $s->user->email('plan-expired', array('[PLAN.NAME]' => $plan->name, '[URL.CHECKOUT]' => $checkout_url));
                     }
                 }
             }
             //if plan loaded
         }
         //end foreach
     }
     //if subscription active
 }
Esempio n. 5
0
 /**
  * creates a new ad
  * @param  array $data 
  * @param  model_user $user 
  * @return array       
  */
 public static function new_ad($data, $user)
 {
     $return_message = '';
     $checkout_url = '';
     //akismet spam filter
     if (isset($data['title']) and isset($data['description']) and core::akismet($data['title'], $user->email, $data['description']) == TRUE) {
         // is user marked as spammer? Make him one :)
         if (core::config('general.black_list')) {
             $user->user_spam();
         }
         return array('error' => __('This post has been considered as spam! We are sorry but we can not publish this advertisement.'), 'error_type' => Alert::ALERT);
     }
     //akismet
     $ad = new Model_Ad();
     $ad->id_user = $user->id_user;
     $ad->values($data);
     $ad->seotitle = $ad->gen_seo_title($ad->title);
     $ad->created = Date::unix2mysql();
     try {
         $ad->save();
     } catch (ORM_Validation_Exception $e) {
         return array('validation_errors' => $e->errors('ad'));
     } catch (Exception $e) {
         return array('error' => $e->getMessage(), 'error_type' => Alert::ALERT);
     }
     /////////// NOTIFICATION Emails,messages to user and Status of the ad
     // depending on user flow (moderation mode), change usecase
     $moderation = core::config('general.moderation');
     //calculate how much he needs to pay in case we have payment on
     if ($moderation == Model_Ad::PAYMENT_ON or $moderation == Model_Ad::PAYMENT_MODERATION) {
         // check category price, if 0 check parent
         if ($ad->category->price == 0) {
             $cat_parent = new Model_Category($ad->category->id_category_parent);
             //category without price
             if ($cat_parent->price == 0) {
                 //swapping moderation since theres no price :(
                 if ($moderation == Model_Ad::PAYMENT_ON) {
                     $moderation = Model_Ad::POST_DIRECTLY;
                 } elseif ($moderation == Model_Ad::PAYMENT_MODERATION) {
                     $moderation = Model_Ad::MODERATION_ON;
                 }
             } else {
                 $amount = $cat_parent->price;
             }
         } else {
             $amount = $ad->category->price;
         }
     }
     //where and what we say to the user depending ont he moderation
     switch ($moderation) {
         case Model_Ad::PAYMENT_ON:
         case Model_Ad::PAYMENT_MODERATION:
             $ad->status = Model_Ad::STATUS_NOPUBLISHED;
             $order = Model_Order::new_order($ad, $user, Model_Order::PRODUCT_CATEGORY, $amount, NULL, Model_Order::product_desc(Model_Order::PRODUCT_CATEGORY) . ' ' . $ad->category->name);
             // redirect to invoice
             $return_message = __('Please pay before we publish your advertisement.');
             $checkout_url = Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order));
             break;
         case Model_Ad::EMAIL_MODERATION:
         case Model_Ad::EMAIL_CONFIRMATION:
             $ad->status = Model_Ad::STATUS_UNCONFIRMED;
             $url_ql = $user->ql('oc-panel', array('controller' => 'myads', 'action' => 'confirm', 'id' => $ad->id_ad));
             $user->email('ads-confirm', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $ad->title));
             $return_message = __('Advertisement is posted but first you need to activate. Please check your email!');
             break;
         case Model_Ad::MODERATION_ON:
             $ad->status = Model_Ad::STATUS_NOPUBLISHED;
             $url_ql = $user->ql('oc-panel', array('controller' => 'myads', 'action' => 'update', 'id' => $ad->id_ad));
             $user->email('ads-notify', array('[URL.QL]' => $url_ql, '[AD.NAME]' => $ad->title));
             // email to notify user of creating, but it is in moderation currently
             $return_message = __('Advertisement is received, but first administrator needs to validate. Thank you for being patient!');
             break;
         case Model_Ad::POST_DIRECTLY:
         default:
             $ad->status = Model_Ad::STATUS_PUBLISHED;
             $ad->published = $ad->created;
             $url_cont = $user->ql('contact');
             $url_ad = $user->ql('ad', array('category' => $ad->category->seoname, 'seotitle' => $ad->seotitle));
             $user->email('ads-user-check', array('[URL.CONTACT]' => $url_cont, '[URL.AD]' => $url_ad, '[AD.NAME]' => $ad->title));
             Model_Subscribe::notify($ad);
             $return_message = __('Advertisement is posted. Congratulations!');
             break;
     }
     //save the last changes on status
     $ad->save();
     //notify admins new ad
     $ad->notify_admins();
     return array('message' => $return_message, 'checkout_url' => $checkout_url, 'ad' => $ad);
 }
Esempio n. 6
0
 /**
  * gets the payment token from stripe and marks order as paid. Methos for application fee
  */
 public function action_payconnect()
 {
     //TODO only if stripe connect enabled
     $this->auto_render = FALSE;
     $id_order = $this->request->param('id');
     //retrieve info for the item in DB
     $order = new Model_Order();
     $order = $order->where('id_order', '=', $id_order)->where('status', '=', Model_Order::STATUS_CREATED)->where('id_product', '=', Model_Order::PRODUCT_AD_SELL)->limit(1)->find();
     if ($order->loaded()) {
         if (isset($_POST['stripeToken'])) {
             //its a fraud...lets let him know
             if ($order->is_fraud() === TRUE) {
                 Alert::set(Alert::ERROR, __('We had, issues with your transaction. Please try paying with another paymethod.'));
                 $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
             }
             StripeKO::init();
             // Get the credit card details submitted by the form
             $token = Core::post('stripeToken');
             // email
             $email = Core::post('stripeEmail');
             // Create the charge on Stripe's servers - this will charge the user's card
             try {
                 //in case memberships the fee may be set on the plan ;)
                 $fee = NULL;
                 if ($order->ad->user->subscription()->loaded()) {
                     $fee = $order->ad->user->subscription()->plan->marketplace_fee;
                 }
                 $application_fee = StripeKO::application_fee($order->amount, $fee);
                 //we charge the fee only if its not admin
                 if (!$order->ad->user->is_admin()) {
                     $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, "card" => $token, "description" => $order->description, "application_fee" => StripeKO::money_format($application_fee)), array('stripe_account' => $order->ad->user->stripe_user_id));
                 } else {
                     $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, "card" => $token, "description" => $order->description));
                 }
             } catch (Exception $e) {
                 // The card has been declined
                 Kohana::$log->add(Log::ERROR, 'Stripe The card has been declined');
                 Alert::set(Alert::ERROR, 'Stripe The card has been declined');
                 $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
             }
             //mark as paid
             $order->confirm_payment('stripe', Core::post('stripeToken'));
             //only if is not admin
             if (!$order->ad->user->is_admin()) {
                 //crete new order for the application fee so we know how much the site owner is earning ;)
                 $order_app = Model_Order::new_order($order->ad, $order->ad->user, Model_Order::PRODUCT_APP_FEE, $application_fee, core::config('payment.paypal_currency'), 'id_order->' . $order->id_order . ' id_ad->' . $order->ad->id_ad);
                 $order_app->confirm_payment('stripe', Core::post('stripeToken'));
             }
             //redirect him to his ads
             Alert::set(Alert::SUCCESS, __('Thanks for your payment!'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders')));
         } else {
             Alert::set(Alert::INFO, __('Please fill your card details.'));
             $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
         }
     } else {
         Alert::set(Alert::INFO, __('Order could not be loaded'));
         $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
     }
 }
Esempio n. 7
0
 /**
  * [action_buy] Pay for ad, and set new order 
  *
  */
 public function action_buy()
 {
     //check pay to featured top is enabled
     if (core::config('payment.paypal_seller') == 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 first need to register'));
         $this->redirect(Route::url('oc-panel'));
     }
     $id_product = Model_Order::PRODUCT_AD_SELL;
     //check ad exists
     $id_ad = $this->request->param('id');
     $ad = new Model_Ad($id_ad);
     //loaded published and with stock if we control the stock.
     if ($ad->loaded() and $ad->status == Model_Ad::STATUS_PUBLISHED and (core::config('payment.stock') == 0 or $ad->stock > 0 and core::config('payment.stock') == 1)) {
         $amount = $ad->price;
         $currency = core::config('payment.paypal_currency');
         if (isset($ad->cf_shipping) and Valid::numeric($ad->cf_shipping) and $ad->cf_shipping > 0) {
             $amount = $ad->price + $ad->cf_shipping;
         }
         $order = Model_Order::new_order($ad, $this->user, $id_product, $amount, $currency, __('Purchase') . ': ' . $ad->seotitle);
         $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
     } else {
         throw HTTP_Exception::factory(404, __('Page not found'));
     }
 }
Esempio n. 8
0
 public function action_buy()
 {
     if (!Auth::instance()->logged_in()) {
         $this->redirect(Route::get('oc-panel')->uri());
     }
     $user = Auth::instance()->get_user();
     $id_product = $this->request->param('id');
     if (is_numeric($id_product)) {
         $product = new Model_Product($id_product);
         if ($product->loaded() and $product->status == Model_Product::STATUS_ACTIVE) {
             //generates a new order if none was existent
             $order = Model_Order::new_order($user, $product);
             //its paid plan?
             if ($product->final_price() > 0) {
                 // redirect to checkout payment
                 $this->redirect(Route::url('default', array('controller' => 'product', 'action' => 'checkout', 'id' => $order->id_order)));
             } else {
                 //mark as paid
                 $order->confirm_payment();
                 //if theres download redirect him to the file
                 if ($product->has_file() == TRUE) {
                     $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'download', 'id' => $order->id_order)));
                 } else {
                     $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders')));
                 }
             }
         }
     }
     //default redirect
     $this->redirect(Route::get('oc-panel')->uri());
 }
Esempio n. 9
0
 public function action_create()
 {
     try {
         if (!is_numeric(core::request('id_ad')) or !is_numeric(core::request('id_product')) or !is_numeric(core::request('id_user'))) {
             $this->_error(__('Missing parameters'), 501);
         } else {
             $user = new Model_User(core::request('id_user'));
             $ad = new Model_Ad(core::request('id_ad'));
             if ($user->loaded() and $ad->loaded()) {
                 $id_product = core::request('id_product');
                 $amount = core::request('amount');
                 //in case not set by request
                 if (!is_numeric($amount)) {
                     //get original price for the product
                     switch ($id_product) {
                         case Model_Order::PRODUCT_CATEGORY:
                             $amount = $ad->category->price;
                             break;
                         case Model_Order::PRODUCT_TO_TOP:
                             $amount = core::config('payment.pay_to_go_on_top');
                             break;
                         case Model_Order::PRODUCT_TO_FEATURED:
                             $amount = Model_Order::get_featured_price(core::request('featured_days'));
                             break;
                         case Model_Order::PRODUCT_AD_SELL:
                             $amount = $ad->price;
                             break;
                         default:
                             $plan = new Model_Plan($id_product);
                             $amount = $plan->loaded() ? $plan->price : 0;
                             break;
                     }
                 }
                 $order = Model_Order::new_order($ad, $user, $id_product, $amount, core::request('currency'), Model_Order::product_desc(core::request('id_product')), core::request('featured_days'));
                 $order->confirm_payment(core::request('paymethod', 'API'), core::request('txn_id'));
                 $order->save();
                 $this->rest_output(array('order' => self::get_order_array($order)));
             } else {
                 $this->_error(__('User or Ad not loaded'), 501);
             }
         }
     } catch (Kohana_HTTP_Exception $khe) {
         $this->_error($khe);
     }
 }
Esempio n. 10
0
 public function action_import()
 {
     if ($this->request->post()) {
         ini_set('auto_detect_line_endings', true);
         $csv = $_FILES['file_source']['tmp_name'];
         if (($handle = fopen($csv, "r")) !== FALSE) {
             $i = 0;
             while (($data = fgetcsv($handle, 0, ";")) !== false) {
                 //avoid first line
                 if ($i != 0) {
                     list($email, $pay_date, $product_seotitle, $amount, $currency) = $data;
                     $pay_date = Date::from_format($pay_date, 'd/m/yy', 'Y-m-d H:i:s');
                     $user = Model_User::create_email($email, substr($email, 0, strpos($email, '@')));
                     $product = new Model_Product();
                     $product->where('seotitle', '=', $product_seotitle)->limit(1)->find();
                     if ($product->loaded()) {
                         $order = Model_Order::new_order($user, $product);
                         $order->confirm_payment('import', NULL, $pay_date, $amount, $currency);
                     }
                 }
                 $i++;
             }
         }
         fclose($handle);
         //redirect to orders
         Alert::set(Alert::SUCCESS, __('Import correct'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'order', 'action' => 'index')));
     }
     //template header
     $this->template->title = __('Import Orders');
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Import Orders')));
     $this->template->content = View::factory('oc-panel/pages/order/import');
 }