1 | <?php |
||||||
2 | |||||||
3 | namespace Bgultekin\CashierFastspring; |
||||||
4 | |||||||
5 | use Bgultekin\CashierFastspring\Exceptions\NotImplementedException; |
||||||
6 | use Bgultekin\CashierFastspring\Fastspring\Fastspring; |
||||||
7 | use Exception; |
||||||
8 | |||||||
9 | trait Billable |
||||||
10 | { |
||||||
11 | /** |
||||||
12 | * Make a "one off" charge on the customer for the given amount. |
||||||
13 | * |
||||||
14 | * @param int $amount |
||||||
15 | * @param array $options |
||||||
16 | * |
||||||
17 | * @throws \InvalidArgumentException |
||||||
18 | * @throws Exceptions\NotImplementedException |
||||||
19 | */ |
||||||
20 | 1 | public function charge($amount, array $options = []) |
|||||
0 ignored issues
–
show
The parameter
$amount is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
21 | { |
||||||
22 | 1 | throw new NotImplementedException(); |
|||||
23 | } |
||||||
24 | |||||||
25 | /** |
||||||
26 | * Refund a customer for a charge. |
||||||
27 | * |
||||||
28 | * @param string $charge |
||||||
29 | * @param array $options |
||||||
30 | * |
||||||
31 | * @throws \InvalidArgumentException |
||||||
32 | * @throws Exceptions\NotImplementedException |
||||||
33 | */ |
||||||
34 | 1 | public function refund($charge, array $options = []) |
|||||
0 ignored issues
–
show
The parameter
$charge is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$options is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
35 | { |
||||||
36 | 1 | throw new NotImplementedException(); |
|||||
37 | } |
||||||
38 | |||||||
39 | /** |
||||||
40 | * Begin creating a new subscription. |
||||||
41 | * |
||||||
42 | * @param string $subscription |
||||||
43 | * @param string $plan |
||||||
44 | * |
||||||
45 | * @return \Bgultekin\CashierFastspring\SubscriptionBuilder |
||||||
46 | */ |
||||||
47 | 4 | public function newSubscription($subscription, $plan) |
|||||
48 | { |
||||||
49 | 4 | return new SubscriptionBuilder($this, $subscription, $plan); |
|||||
50 | } |
||||||
51 | |||||||
52 | /** |
||||||
53 | * Determine if the subscription is on trial. |
||||||
54 | * |
||||||
55 | * @param string $subscription |
||||||
56 | * @param string|null $plan |
||||||
57 | * |
||||||
58 | * @return bool |
||||||
59 | */ |
||||||
60 | 1 | public function onTrial($subscription = 'default', $plan = null) |
|||||
61 | { |
||||||
62 | 1 | $subscription = $this->subscription($subscription); |
|||||
63 | |||||||
64 | 1 | if (is_null($plan)) { |
|||||
65 | 1 | return $subscription && $subscription->onTrial(); |
|||||
66 | } |
||||||
67 | |||||||
68 | 1 | return $subscription && $subscription->onTrial() && |
|||||
69 | 1 | $subscription->plan === $plan; |
|||||
70 | } |
||||||
71 | |||||||
72 | /** |
||||||
73 | * Determine if the model has a given subscription. |
||||||
74 | * |
||||||
75 | * @param string $subscription |
||||||
76 | * @param string|null $plan |
||||||
77 | * |
||||||
78 | * @return bool |
||||||
79 | */ |
||||||
80 | 1 | public function subscribed($subscription = 'default', $plan = null) |
|||||
81 | { |
||||||
82 | 1 | $subscription = $this->subscription($subscription); |
|||||
83 | |||||||
84 | 1 | if (is_null($subscription)) { |
|||||
85 | 1 | return false; |
|||||
86 | } |
||||||
87 | |||||||
88 | 1 | if (is_null($plan)) { |
|||||
89 | 1 | return $subscription->valid(); |
|||||
90 | } |
||||||
91 | |||||||
92 | 1 | return $subscription->valid() && |
|||||
93 | 1 | $subscription->plan === $plan; |
|||||
94 | } |
||||||
95 | |||||||
96 | /** |
||||||
97 | * Get a subscription instance by name. |
||||||
98 | * |
||||||
99 | * @param string $subscription |
||||||
100 | * |
||||||
101 | * @return \Bgultekin\CashierFastspring\Subscription|null |
||||||
102 | */ |
||||||
103 | 2 | public function subscription($subscription = 'default') |
|||||
104 | { |
||||||
105 | 2 | return $this->subscriptions() |
|||||
106 | 2 | ->where('name', $subscription) |
|||||
107 | 2 | ->orderBy('created_at', 'desc') |
|||||
108 | 2 | ->first(); |
|||||
109 | } |
||||||
110 | |||||||
111 | /** |
||||||
112 | * Get all of the subscriptions for the model. |
||||||
113 | * |
||||||
114 | * @return \Illuminate\Database\Eloquent\Collection |
||||||
115 | */ |
||||||
116 | 26 | public function subscriptions() |
|||||
117 | { |
||||||
118 | 26 | return $this->hasMany(Subscription::class, $this->getForeignKey())->orderBy('created_at', 'desc'); |
|||||
0 ignored issues
–
show
It seems like
getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
119 | } |
||||||
120 | |||||||
121 | /** |
||||||
122 | * Get all of the FastSpring invoices for the current user. |
||||||
123 | * |
||||||
124 | * @return object |
||||||
125 | */ |
||||||
126 | 1 | public function invoices() |
|||||
127 | { |
||||||
128 | 1 | return $this->hasMany(Invoice::class, $this->getForeignKey())->orderBy('created_at', 'desc'); |
|||||
129 | } |
||||||
130 | |||||||
131 | /** |
||||||
132 | * Determine if the model is actively subscribed to one of the given plans. |
||||||
133 | * |
||||||
134 | * @param array|string $plans |
||||||
135 | * @param string $subscription |
||||||
136 | * |
||||||
137 | * @return bool |
||||||
138 | */ |
||||||
139 | 1 | public function subscribedToPlan($plans, $subscription = 'default') |
|||||
140 | { |
||||||
141 | 1 | $subscription = $this->subscription($subscription); |
|||||
142 | |||||||
143 | 1 | if (!$subscription || !$subscription->valid()) { |
|||||
144 | 1 | return false; |
|||||
145 | } |
||||||
146 | |||||||
147 | 1 | foreach ((array) $plans as $plan) { |
|||||
148 | 1 | if ($subscription->plan === $plan) { |
|||||
149 | 1 | return true; |
|||||
150 | } |
||||||
151 | } |
||||||
152 | |||||||
153 | 1 | return false; |
|||||
154 | } |
||||||
155 | |||||||
156 | /** |
||||||
157 | * Determine if the entity is on the given plan. |
||||||
158 | * |
||||||
159 | * @param string $plan |
||||||
160 | * |
||||||
161 | * @return bool |
||||||
162 | */ |
||||||
163 | 1 | public function onPlan($plan) |
|||||
164 | { |
||||||
165 | return !is_null($this->subscriptions->first(function ($value) use ($plan) { |
||||||
166 | 1 | return $value->plan === $plan && $value->valid(); |
|||||
167 | 1 | })); |
|||||
168 | } |
||||||
169 | |||||||
170 | /** |
||||||
171 | * Determine if the entity has a Fastspring customer ID. |
||||||
172 | * |
||||||
173 | * @return bool |
||||||
174 | */ |
||||||
175 | 5 | public function hasFastspringId() |
|||||
176 | { |
||||||
177 | 5 | return !is_null($this->fastspring_id); |
|||||
178 | } |
||||||
179 | |||||||
180 | /** |
||||||
181 | * Generate authenticated url of fastspring account management panel. |
||||||
182 | * |
||||||
183 | * @return bool |
||||||
184 | */ |
||||||
185 | 1 | public function accountManagementURI() |
|||||
186 | { |
||||||
187 | 1 | $response = Fastspring::getAccountManagementURI($this->fastspring_id); |
|||||
0 ignored issues
–
show
The method
getAccountManagementURI() does not exist on Bgultekin\CashierFastspring\Fastspring\Fastspring . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
188 | |||||||
189 | 1 | return $response->accounts[0]->url; |
|||||
190 | } |
||||||
191 | |||||||
192 | /** |
||||||
193 | * Create a Fastspring customer for the given user model. |
||||||
194 | * |
||||||
195 | * @param array $options |
||||||
196 | * |
||||||
197 | * @return object |
||||||
198 | */ |
||||||
199 | 3 | public function createAsFastspringCustomer(array $options = []) |
|||||
200 | { |
||||||
201 | 3 | $options = empty($options) ? [ |
|||||
202 | 'contact' => [ |
||||||
203 | 3 | 'first' => $this->extractFirstName(), |
|||||
204 | 3 | 'last' => $this->extractLastName(), |
|||||
205 | 3 | 'email' => $this->email, |
|||||
206 | 3 | 'company' => $this->company, |
|||||
207 | 3 | 'phone' => $this->phone, |
|||||
208 | ], |
||||||
209 | 3 | 'language' => $this->language, |
|||||
210 | 3 | 'country' => $this->country, |
|||||
211 | 3 | ] : $options; |
|||||
212 | |||||||
213 | // Here we will create the customer instance on Fastspring and store the ID of the |
||||||
214 | // user from Fastspring. This ID will correspond with the Fastspring user instances |
||||||
215 | // and allow us to retrieve users from Fastspring later when we need to work. |
||||||
216 | 3 | $account = Fastspring::createAccount($options); |
|||||
0 ignored issues
–
show
The method
createAccount() does not exist on Bgultekin\CashierFastspring\Fastspring\Fastspring . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
217 | |||||||
218 | 2 | $this->fastspring_id = $account->account; |
|||||
0 ignored issues
–
show
|
|||||||
219 | |||||||
220 | 2 | $this->save(); |
|||||
0 ignored issues
–
show
It seems like
save() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
221 | |||||||
222 | 2 | return $account; |
|||||
223 | } |
||||||
224 | |||||||
225 | /** |
||||||
226 | * Update the related account on the Fastspring-side the given user model. |
||||||
227 | * |
||||||
228 | * @param array $options |
||||||
229 | * |
||||||
230 | * @return object |
||||||
231 | */ |
||||||
232 | 2 | public function updateAsFastspringCustomer(array $options = []) |
|||||
233 | { |
||||||
234 | // check the fastspring_id first |
||||||
235 | // if there is non, no need to try |
||||||
236 | 2 | if (!$this->hasFastspringId()) { |
|||||
237 | 1 | throw new Exception('User has no fastspring_id'); |
|||||
238 | } |
||||||
239 | |||||||
240 | 1 | $options = empty($options) ? [ |
|||||
241 | 'contact' => [ |
||||||
242 | 1 | 'first' => $this->extractFirstName(), |
|||||
243 | 1 | 'last' => $this->extractLastName(), |
|||||
244 | 1 | 'email' => $this->email, |
|||||
245 | 1 | 'company' => $this->company, |
|||||
246 | 1 | 'phone' => $this->phone, |
|||||
247 | ], |
||||||
248 | 1 | 'language' => $this->language, |
|||||
249 | 1 | 'country' => $this->country, |
|||||
250 | 1 | ] : $options; |
|||||
251 | |||||||
252 | // update |
||||||
253 | 1 | $response = Fastspring::updateAccount($this->fastspring_id, $options); |
|||||
0 ignored issues
–
show
The method
updateAccount() does not exist on Bgultekin\CashierFastspring\Fastspring\Fastspring . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
254 | |||||||
255 | 1 | return $response; |
|||||
256 | } |
||||||
257 | |||||||
258 | /** |
||||||
259 | * Get the Fastspring customer for the model. |
||||||
260 | * |
||||||
261 | * @return object |
||||||
262 | */ |
||||||
263 | 2 | public function asFastspringCustomer() |
|||||
264 | { |
||||||
265 | // check the fastspring_id first |
||||||
266 | // if there is non, no need to try |
||||||
267 | 2 | if (!$this->hasFastspringId()) { |
|||||
268 | 1 | throw new Exception('User has no fastspring_id'); |
|||||
269 | } |
||||||
270 | |||||||
271 | 1 | return Fastspring::getAccount($this->fastspring_id); |
|||||
0 ignored issues
–
show
The method
getAccount() does not exist on Bgultekin\CashierFastspring\Fastspring\Fastspring . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
272 | } |
||||||
273 | |||||||
274 | /** |
||||||
275 | * Get the first name of the customer for the Fastspring API. |
||||||
276 | * |
||||||
277 | * @return object |
||||||
278 | */ |
||||||
279 | 5 | public function extractFirstName() |
|||||
280 | { |
||||||
281 | 5 | $parted = explode(' ', $this->name); |
|||||
282 | 5 | $parted = array_filter($parted); |
|||||
283 | |||||||
284 | 5 | if (count($parted) == 1) { |
|||||
285 | 1 | return $parted[0]; |
|||||
286 | } |
||||||
287 | |||||||
288 | // get rid of the lastname |
||||||
289 | 5 | array_pop($parted); |
|||||
290 | |||||||
291 | // implode rest of it, so there may be more than one name |
||||||
292 | 5 | return implode(' ', $parted); |
|||||
0 ignored issues
–
show
|
|||||||
293 | } |
||||||
294 | |||||||
295 | /** |
||||||
296 | * Get the last name of the customer for the Fastspring API. |
||||||
297 | * |
||||||
298 | * @return object |
||||||
299 | */ |
||||||
300 | 5 | public function extractLastName() |
|||||
301 | { |
||||||
302 | 5 | $parted = explode(' ', $this->name); |
|||||
303 | 5 | $parted = array_filter($parted); |
|||||
304 | |||||||
305 | 5 | if (count($parted) == 1) { |
|||||
306 | // unfortunately we should do this |
||||||
307 | // because Fastspring create account API doesn't work without last name |
||||||
308 | 1 | return 'Unknown'; |
|||||
0 ignored issues
–
show
|
|||||||
309 | } |
||||||
310 | |||||||
311 | // return last element |
||||||
312 | 5 | return array_pop($parted); |
|||||
313 | } |
||||||
314 | } |
||||||
315 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.