File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,18 +24,50 @@ trait UsesSubscriptions
2424 * @param string|array $subscription The subscription to assign
2525 * @return array|null
2626 */
27- public function subscription ()
27+ public function subscription (): ? array
2828 {
29- if (! $ this -> subscription ) {
30- $ this ->subscription = db ()
29+ if (
30+ ! $ this -> subscription && $ this ->subscription = db ()
3131 ->select ('subscriptions ' )
3232 ->where ('user_id ' , $ this ->id ())
33- ->first ();
34-
33+ ->first () ?? null
34+ ) {
3535 $ this ->subscription ['tier ' ] = billing ()->tier ($ this ->subscription ['plan_id ' ]);
3636 }
3737
3838 return $ this ->subscription ;
3939 }
4040
41+ /**
42+ * Check if user has a subscription
43+ * @return bool
44+ */
45+ public function hasSubscription (): bool
46+ {
47+ return $ this ->subscription () && $ this ->subscription ['status ' ] !== \Leaf \Billing \Subscription::STATUS_CANCELLED ;
48+ }
49+
50+ /**
51+ * Check if user has an active subscription
52+ * @return bool
53+ */
54+ public function hasActiveSubscription (): bool
55+ {
56+ return $ this ->subscription () && ($ this ->subscription ['status ' ] === \Leaf \Billing \Subscription::STATUS_ACTIVE || $ this ->subscription ['status ' ] === \Leaf \Billing \Subscription::STATUS_TRIAL );
57+ }
58+
59+ /**
60+ * Cancel current subscription
61+ * @return bool
62+ */
63+ public function cancelSubscription (): bool
64+ {
65+ $ subscription = $ this ->subscription ();
66+
67+ if (!$ subscription ) {
68+ return true ;
69+ }
70+
71+ return billing ()->cancelSubcription ($ subscription ['subscription_id ' ]);
72+ }
4173}
You can’t perform that action at this time.
0 commit comments