Source of file StripeSubscription.php
Size: 2,745 Bytes - Last Modified: 2021-12-24T05:17:00+00:00
/var/www/docs.ssmods.com/process/src/src/Model/StripeSubscription.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | <?php namespace ilateral\SilverStripe\StripeForms\Model; use \Stripe\Stripe as Stripe; use \Stripe\Subscription as StripeAPISubscription; use \SilverStripe\ORM\DataObject; use \Exception; use ilateral\SilverStripe\StripeForms\StripeForms; /** * An object in Silverstripe that links to a stripe subscription. * * @package stripe-forms * @subpackage model * @author Mo <morven@ilateral.co.uk> */ class StripeSubscription extends DataObject { /** * The status in Stripe to show this is a currently active subscription. * * @var String * @config */ private static $active_status = "active"; /** * Number of times a payment attempt can fail before the subscription * is cancelled. * * @var Int * @config */ private static $failier_attempts = 3; private static $db = [ "StripeID" => "Varchar(255)", "PlanID" => "Varchar(255)", "Status" => "Varchar", "PaymentAttempts" => "Int" ]; private static $has_one = [ "Member" => "Member" ]; private static $summary_fields = [ "Member.Email" => "Member", "Status" => "Status", "StripeID" => "Stripe ID", "PlanID" => "Plan" ]; /** * Simple function to retrieve a subscription by ID * from Stripe. * * If subscription is invalid, then return null * * @return \Stripe\Subscription | null */ public function getSubscription() { Stripe::setApiKey(StripeForms::secret_key()); try { $subscription = StripeAPISubscription::retrieve($this->StripeID); return $subscription; } catch (Exception $e) { error_log($e->getMessage()); return null; } } /** * Set the status of this subscription to the status logged in * stripe and return the current object. * * @return StripeSubscription */ public function updateStatus() { $subscription = $this->getSubscription(); if ($subscription) { $this->Status = $subscription->status; } $this->extend("onAfterUpdate"); return $this; } /** * Cancel the current subscription in stripe and return * the current object. * * @return StripeSubscription */ public function cancel() { $subscription = $this->getSubscription(); if ($subscription) { $subscription->cancel(); $this->Status = $subscription->status; } $this->extend("onAfterCancel"); return $this; } public function onBeforeDelete() { parent::onBeforeDelete(); $this->cancel(); } } |