/** * 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()); } }
/** * 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); }
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'); }
/** * @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) { } } }