/**
  * 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
 }
Example #2
0
 /**
  * [action_form] generates the form to pay at paypal
  */
 public function action_3d()
 {
     $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)->limit(1)->find();
     if ($order->loaded()) {
         //dr($_GET);
         if (Core::get('status') == 'succeeded' and Core::get('id') != NULL and ($customer_id = Session::instance()->get('customer_id')) != NULL) {
             try {
                 StripeKO::init();
                 // Create the charge on Stripe's servers - this will charge the user's card
                 $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, 'customer' => $customer_id, "description" => $order->description, "metadata" => array("id_order" => $order->id_order)));
             } catch (Exception $e) {
                 // The card has been declined
                 Kohana::$log->add(Log::ERROR, 'Stripe The card has been declined');
                 Alert::set(Alert::ERROR, 'The card has been declined');
                 $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
             }
             //its a plan product
             if ($order->id_product >= 100) {
                 //save the stripe user id to be able to charge them later on renewal
                 $order->user->stripe_agreement = $customer_id;
                 $order->user->save();
             }
             //mark as paid
             $order->confirm_payment('stripe', $charge->id);
             //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)));
     }
 }