/**
  * Check stripe data.
  *
  * @access public
  * @return void
  */
 public function run()
 {
     $paymentGateway = Payment_gateways::findOneActiveBySlug('stripe');
     if ($paymentGateway->exists()) {
         \Stripe\Stripe::setApiKey($paymentGateway->getFieldValue('apiKey'));
         $subscriptions = new Subscription();
         $allSubscriptions = $subscriptions->where('status', Subscription::STATUS_ACTIVE)->get();
         /* @var Subscription $_subscription */
         foreach ($allSubscriptions as $_subscription) {
             $end = DateTime::createFromFormat('Y-m-d', $_subscription->end_date);
             if ($end->getTimestamp() > strtotime('now')) {
                 $paymentTransaction = $_subscription->payment_transaction->get();
                 if ($paymentTransaction->system == 'stripe') {
                     $user = new User($_subscription->user_id);
                     try {
                         $customer = \Stripe\Customer::retrieve($user->stripe_id);
                         $subscription = $customer->subscriptions->retrieve($paymentTransaction->payment_id);
                     } catch (Exception $e) {
                         log_message('CRON_ERROR', __FUNCTION__ . ' > ' . $e->getMessage());
                     }
                     if (!isset($subscription) || $subscription->status != 'active') {
                         $_subscription->deactivate();
                         $_subscription->save();
                     }
                 }
             }
         }
         log_message('CRON_SUCCESS', __FUNCTION__);
     }
 }
 /**
  * Check stripe data.
  *
  * @access public
  * @return void
  */
 public function run()
 {
     try {
         $paymentGateway = Payment_gateways::findOneActiveBySlug('stripe');
         if ($paymentGateway->exists()) {
             \Stripe\Stripe::setApiKey($paymentGateway->getFieldValue('apiKey'));
             $subscriptions = new Subscription();
             $allSubscriptions = $subscriptions->get();
             /* @var Subscription $_subscription */
             foreach ($allSubscriptions as $_subscription) {
                 if ($_subscription->end_date <= strtotime('now')) {
                     $paymentTransaction = $_subscription->payment_transaction->get();
                     if ($paymentTransaction->system == 'stripe') {
                         $user = new User($_subscription->user_id);
                         $customer = \Stripe\Customer::retrieve($user->stripe_id);
                         $subscription = $customer->subscriptions->retrieve($paymentTransaction->payment_id);
                         if ($subscription->status == 'active') {
                             $date = new DateTime();
                             $date->setTimestamp($subscription->current_period_end);
                             $_subscription->end_date = $date->format('Y-m-d');
                             $_subscription->activate();
                             $_subscription->save();
                         }
                     }
                 }
             }
             log_message('CRON_SUCCESS', __FUNCTION__);
         } else {
             log_message('CRON_ERROR', __FUNCTION__ . ' > ' . 'No Stripe Api key.');
         }
     } catch (Exception $e) {
         log_message('CRON_ERROR', __FUNCTION__ . ' > ' . $e->getMessage());
     }
 }
Example #3
0
 /**
  * Try ro complete transaction
  *
  * @param $transactionId
  */
 public function complete($transactionId)
 {
     $transaction = new Payment_transaction($transactionId);
     $this->throwModelNotFoundException($transaction);
     if (!$transaction->isPending()) {
         $this->throwAccessDeniedException('The transaction cannot be processed!');
     }
     $paymentGateway = Payment_gateways::findOneActiveBySlug($transaction->system);
     $this->throwModelNotFoundException($paymentGateway);
     $paymentProvider = $this->get('core.payment.system.provider');
     $paymentProvider->setGateway($paymentGateway);
     $parameters = $this->generateParameters($transaction);
     $response = $paymentProvider->completePurchase($parameters);
     $this->processResponse($response, $transaction);
 }
Example #4
0
 public function cancel_subscription()
 {
     $subscription = $this->c_user->getLastSubscription();
     if ($this->c_user->stripe_id) {
         $paymentGateway = Payment_gateways::findOneActiveBySlug('stripe');
         if ($paymentGateway->exists()) {
             \Stripe\Stripe::setApiKey($paymentGateway->getFieldValue('apiKey'));
             $customer = \Stripe\Customer::retrieve($this->c_user->stripe_id);
             $paymentTransaction = $subscription->payment_transaction->get();
             if ($paymentTransaction->exists()) {
                 $customer->subscriptions->retrieve($paymentTransaction->payment_id)->cancel();
             }
         }
     }
     $subscription->is_stripe_active = false;
     $subscription->save();
     redirect('settings/subscriptions');
 }
 public function gateways($slug)
 {
     $gateway = Payment_gateways::findOneBySlug($slug);
     $this->throwModelNotFoundException($gateway);
     if ($this->isRequestMethod('post')) {
         $data = $this->getRequest()->request->get('gateway');
         if (empty($data['enable'])) {
             $data['enable'] = 0;
         }
         $gateway->handleData($data);
         if ($gateway->save()) {
             $this->addFlash(lang('gateway_update_success'), 'success');
             redirect(current_url());
         }
         $this->addFlash($gateway->errors->string);
     }
     $this->template->set('gateway', $gateway);
     $this->template->render();
 }
Example #6
0
 /**
  * Render View
  *
  * @param int  $userId
  * @param int  $planId
  * @param null|string $inviteCode
  */
 public function subscribe($userId, $planId, $inviteCode = null)
 {
     $user = new User($userId);
     $plan = new Plan($planId);
     if ($plan->special) {
         $specialInvite = new Special_invite();
         if (!$specialInvite->check($planId, $inviteCode)) {
             redirect('subscript/plans');
         }
     }
     //set any errors and display the form
     $message = strip_tags($this->template->message());
     if ($message) {
         $this->addFlash($message);
     }
     $periods = $plan->getPeriods();
     $systems = Payment_gateways::findAllActive()->all_to_single_array('name');
     $this->template->set('user', $user);
     $this->template->set('plan', $plan);
     $this->template->set('periods', $periods);
     $this->template->set('systems', $systems);
     $this->template->set('options', $this->config->config['period_qualifier']);
     $this->template->render();
 }
Example #7
0
 public function gateway($gateway)
 {
     $gateway = Payment_gateways::findOneBySlug($gateway);
     $transactionManager = $this->get('core.payment.transactions.manager');
     $transaction = $transactionManager->createForSubscription(array('amount' => 1000, 'description' => 'test 111'), $this->getUser(), $gateway);
     echo $transaction->id;
 }
Example #8
0
 /**
  * @param null|array $periods
  */
 public function deleteStripePlans($periods = null)
 {
     $paymentGateway = Payment_gateways::findOneActiveBySlug('stripe');
     \Stripe\Stripe::setApiKey($paymentGateway->getFieldValue('apiKey'));
     foreach ($periods as $period) {
         try {
             $plan = \Stripe\Plan::retrieve($period->id);
             $plan->delete();
         } catch (Exception $e) {
         }
     }
 }