Skip to content

Commit ffb8c7c

Browse files
author
Igor Chepurnoy
authored
Merge pull request #11 from yii2mod/feature-package-refactoring
rename tables, require php 7.1, update tests
2 parents 09e4db5 + 6cfa1b0 commit ffb8c7c

12 files changed

+262
-176
lines changed

Billable.php

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use Stripe\Charge;
99
use Stripe\Customer;
10+
use Stripe\Error\Card;
1011
use Stripe\Error\InvalidRequest;
1112
use Stripe\Invoice as StripeInvoice;
1213
use Stripe\InvoiceItem as StripeInvoiceItem;
@@ -37,20 +38,20 @@ trait Billable
3738
* @param int $amount
3839
* @param array $options
3940
*
40-
* @return \Stripe\Charge
41+
* @return Charge
4142
*
42-
* @throws \Stripe\Error\Card
43+
* @throws Card
4344
*/
44-
public function charge($amount, array $options = [])
45+
public function charge($amount, array $options = []): Charge
4546
{
4647
$options = array_merge([
4748
'currency' => $this->preferredCurrency(),
4849
], $options);
4950

5051
$options['amount'] = $amount;
5152

52-
if (!array_key_exists('source', $options) && $this->stripeId) {
53-
$options['customer'] = $this->stripeId;
53+
if (!array_key_exists('source', $options) && $this->stripe_id) {
54+
$options['customer'] = $this->stripe_id;
5455
}
5556

5657
if (!array_key_exists('source', $options) && !array_key_exists('customer', $options)) {
@@ -68,7 +69,7 @@ public function charge($amount, array $options = [])
6869
*
6970
* @return StripeRefund
7071
*/
71-
public function refund($charge, array $options = [])
72+
public function refund($charge, array $options = []): StripeRefund
7273
{
7374
$options['charge'] = $charge;
7475

@@ -80,9 +81,9 @@ public function refund($charge, array $options = [])
8081
*
8182
* @return bool
8283
*/
83-
public function hasCardOnFile()
84+
public function hasCardOnFile(): bool
8485
{
85-
return (bool)$this->cardBrand;
86+
return (bool) $this->card_brand;
8687
}
8788

8889
/**
@@ -92,18 +93,18 @@ public function hasCardOnFile()
9293
* @param int $amount
9394
* @param array $options
9495
*
95-
* @return bool
96+
* @return bool|StripeInvoice
9697
*
97-
* @throws \Stripe\Error\Card
98+
* @throws Card
9899
*/
99100
public function invoiceFor($description, $amount, array $options = [])
100101
{
101-
if (!$this->stripeId) {
102+
if (!$this->stripe_id) {
102103
throw new InvalidArgumentException('User is not a customer. See the createAsStripeCustomer method.');
103104
}
104105

105106
$options = array_merge([
106-
'customer' => $this->stripeId,
107+
'customer' => $this->stripe_id,
107108
'amount' => $amount,
108109
'currency' => $this->preferredCurrency(),
109110
'description' => $description,
@@ -124,7 +125,7 @@ public function invoiceFor($description, $amount, array $options = [])
124125
*
125126
* @return SubscriptionBuilder
126127
*/
127-
public function newSubscription($subscription, $plan)
128+
public function newSubscription(string $subscription, string $plan): SubscriptionBuilder
128129
{
129130
return new SubscriptionBuilder($this, $subscription, $plan);
130131
}
@@ -137,7 +138,7 @@ public function newSubscription($subscription, $plan)
137138
*
138139
* @return bool
139140
*/
140-
public function onTrial($subscription = 'default', $plan = null)
141+
public function onTrial(string $subscription = 'default', ?string $plan = null): bool
141142
{
142143
if (func_num_args() === 0 && $this->onGenericTrial()) {
143144
return true;
@@ -148,17 +149,17 @@ public function onTrial($subscription = 'default', $plan = null)
148149
}
149150

150151
return $subscription && $subscription->onTrial() &&
151-
$subscription->stripePlan === $plan;
152+
$subscription->stripePlan === $plan;
152153
}
153154

154155
/**
155156
* Determine if the user is on a "generic" trial at the user level.
156157
*
157158
* @return bool
158159
*/
159-
public function onGenericTrial()
160+
public function onGenericTrial(): bool
160161
{
161-
return $this->trialEndAt && Carbon::now()->lt(Carbon::createFromFormat('Y-m-d H:i:s', $this->trialEndAt));
162+
return $this->trial_ends_at && Carbon::now()->lt(Carbon::createFromFormat('Y-m-d H:i:s', $this->trial_ends_at));
162163
}
163164

164165
/**
@@ -169,18 +170,20 @@ public function onGenericTrial()
169170
*
170171
* @return bool
171172
*/
172-
public function subscribed($subscription = 'default', $plan = null)
173+
public function subscribed(string $subscription = 'default', ?string $plan = null): bool
173174
{
174175
$subscription = $this->subscription($subscription);
176+
175177
if (is_null($subscription)) {
176178
return false;
177179
}
180+
178181
if (is_null($plan)) {
179182
return $subscription->valid();
180183
}
181184

182185
return $subscription->valid() &&
183-
$subscription->stripePlan === $plan;
186+
$subscription->stripe_plan === $plan;
184187
}
185188

186189
/**
@@ -190,7 +193,7 @@ public function subscribed($subscription = 'default', $plan = null)
190193
*
191194
* @return SubscriptionModel|null
192195
*/
193-
public function subscription($subscription = 'default')
196+
public function subscription(string $subscription = 'default'): ?SubscriptionModel
194197
{
195198
return $this->getSubscriptions()->where(['name' => $subscription])->one();
196199
}
@@ -200,19 +203,19 @@ public function subscription($subscription = 'default')
200203
*/
201204
public function getSubscriptions()
202205
{
203-
return $this->hasMany(SubscriptionModel::className(), ['userId' => 'id'])->orderBy(['createdAt' => SORT_DESC]);
206+
return $this->hasMany(SubscriptionModel::class, ['user_id' => 'id'])->orderBy(['created_at' => SORT_DESC]);
204207
}
205208

206209
/**
207210
* Invoice the billable entity outside of regular billing cycle.
208211
*
209-
* @return bool
212+
* @return bool|StripeInvoice
210213
*/
211214
public function invoice()
212215
{
213-
if ($this->stripeId) {
216+
if ($this->stripe_id) {
214217
try {
215-
return StripeInvoice::create(['customer' => $this->stripeId], $this->getStripeKey())->pay();
218+
return StripeInvoice::create(['customer' => $this->stripe_id], $this->getStripeKey())->pay();
216219
} catch (InvalidRequest $e) {
217220
return false;
218221
}
@@ -226,11 +229,11 @@ public function invoice()
226229
*
227230
* @return Invoice|null
228231
*/
229-
public function upcomingInvoice()
232+
public function upcomingInvoice(): ?Invoice
230233
{
231234
try {
232235
$stripeInvoice = StripeInvoice::upcoming(
233-
['customer' => $this->stripeId], ['api_key' => $this->getStripeKey()]
236+
['customer' => $this->stripe_id], ['api_key' => $this->getStripeKey()]
234237
);
235238

236239
return new Invoice($this, $stripeInvoice);
@@ -245,7 +248,7 @@ public function upcomingInvoice()
245248
*
246249
* @return Invoice|null
247250
*/
248-
public function findInvoice($id)
251+
public function findInvoice(string $id): Invoice
249252
{
250253
try {
251254
return new Invoice($this, StripeInvoice::retrieve($id, $this->getStripeKey()));
@@ -262,7 +265,7 @@ public function findInvoice($id)
262265
*
263266
* @throws NotFoundHttpException
264267
*/
265-
public function findInvoiceOrFail($id)
268+
public function findInvoiceOrFail(string $id)
266269
{
267270
$invoice = $this->findInvoice($id);
268271

@@ -281,7 +284,7 @@ public function findInvoiceOrFail($id)
281284
*
282285
* @return Response
283286
*/
284-
public function downloadInvoice($id, array $data)
287+
public function downloadInvoice(string $id, array $data)
285288
{
286289
return $this->findInvoiceOrFail($id)->download($data);
287290
}
@@ -294,7 +297,7 @@ public function downloadInvoice($id, array $data)
294297
*
295298
* @return array
296299
*/
297-
public function invoices($includePending = false, $parameters = [])
300+
public function invoices(bool $includePending = false, array $parameters = []): array
298301
{
299302
$invoices = [];
300303

@@ -323,7 +326,7 @@ public function invoices($includePending = false, $parameters = [])
323326
*
324327
* @return array
325328
*/
326-
public function invoicesIncludingPending(array $parameters = [])
329+
public function invoicesIncludingPending(array $parameters = []): array
327330
{
328331
return $this->invoices(true, $parameters);
329332
}
@@ -333,7 +336,7 @@ public function invoicesIncludingPending(array $parameters = [])
333336
*
334337
* @param string $token
335338
*/
336-
public function updateCard($token)
339+
public function updateCard(string $token): void
337340
{
338341
$customer = $this->asStripeCustomer();
339342
$token = Token::retrieve($token, ['api_key' => $this->getStripeKey()]);
@@ -376,8 +379,8 @@ public function updateCardFromStripe()
376379
if ($defaultCard) {
377380
$this->fillCardDetails($defaultCard)->save();
378381
} else {
379-
$this->cardBrand = null;
380-
$this->cardLastFour = null;
382+
$this->card_brand = null;
383+
$this->card_last_four = null;
381384
$this->update(false);
382385
}
383386

@@ -394,8 +397,8 @@ public function updateCardFromStripe()
394397
protected function fillCardDetails($card)
395398
{
396399
if ($card) {
397-
$this->cardBrand = $card->brand;
398-
$this->cardLastFour = $card->last4;
400+
$this->card_brand = $card->brand;
401+
$this->card_last_four = $card->last4;
399402
}
400403

401404
return $this;
@@ -423,16 +426,16 @@ public function applyCoupon($coupon)
423426
*
424427
* @return bool
425428
*/
426-
public function subscribedToPlan($plans, $subscription = 'default')
429+
public function subscribedToPlan($plans, $subscription = 'default'): bool
427430
{
428431
$subscription = $this->subscription($subscription);
429432

430433
if (!$subscription || !$subscription->valid()) {
431434
return false;
432435
}
433436

434-
foreach ((array)$plans as $plan) {
435-
if ($subscription->stripePlan === $plan) {
437+
foreach ((array) $plans as $plan) {
438+
if ($subscription->stripe_plan === $plan) {
436439
return true;
437440
}
438441
}
@@ -447,9 +450,9 @@ public function subscribedToPlan($plans, $subscription = 'default')
447450
*
448451
* @return bool
449452
*/
450-
public function onPlan($plan)
453+
public function onPlan($plan): bool
451454
{
452-
$plan = $this->getSubscriptions()->where(['stripePlan' => $plan])->one();
455+
$plan = $this->getSubscriptions()->where(['stripe_plan' => $plan])->one();
453456

454457
return !is_null($plan) && $plan->valid();
455458
}
@@ -459,9 +462,9 @@ public function onPlan($plan)
459462
*
460463
* @return bool
461464
*/
462-
public function hasStripeId()
465+
public function hasStripeId(): bool
463466
{
464-
return !is_null($this->stripeId);
467+
return !is_null($this->stripe_id);
465468
}
466469

467470
/**
@@ -472,7 +475,7 @@ public function hasStripeId()
472475
*
473476
* @return Customer
474477
*/
475-
public function createAsStripeCustomer($token, array $options = [])
478+
public function createAsStripeCustomer(string $token, array $options = []): Customer
476479
{
477480
$options = array_key_exists('email', $options)
478481
? $options : array_merge($options, ['email' => $this->email]);
@@ -482,7 +485,7 @@ public function createAsStripeCustomer($token, array $options = [])
482485
// and allow us to retrieve users from Stripe later when we need to work.
483486
$customer = Customer::create($options, $this->getStripeKey());
484487

485-
$this->stripeId = $customer->id;
488+
$this->stripe_id = $customer->id;
486489

487490
$this->save();
488491

@@ -499,19 +502,19 @@ public function createAsStripeCustomer($token, array $options = [])
499502
/**
500503
* Get the Stripe customer for the user.
501504
*
502-
* @return \Stripe\Customer
505+
* @return Customer
503506
*/
504-
public function asStripeCustomer()
507+
public function asStripeCustomer(): Customer
505508
{
506-
return Customer::retrieve($this->stripeId, $this->getStripeKey());
509+
return Customer::retrieve($this->stripe_id, $this->getStripeKey());
507510
}
508511

509512
/**
510513
* Get the Stripe supported currency used by the entity.
511514
*
512515
* @return string
513516
*/
514-
public function preferredCurrency()
517+
public function preferredCurrency(): string
515518
{
516519
return Cashier::usesCurrency();
517520
}
@@ -521,7 +524,7 @@ public function preferredCurrency()
521524
*
522525
* @return int
523526
*/
524-
public function taxPercentage()
527+
public function taxPercentage(): int
525528
{
526529
return 0;
527530
}
@@ -531,17 +534,17 @@ public function taxPercentage()
531534
*
532535
* @return string
533536
*/
534-
public static function getStripeKey()
537+
public static function getStripeKey(): string
535538
{
536539
return static::$stripeKey ?: Yii::$app->params['stripe']['apiKey'];
537540
}
538541

539542
/**
540543
* Set the Stripe API key.
541544
*
542-
* @param string $key
545+
* @param string $key
543546
*/
544-
public static function setStripeKey($key)
547+
public static function setStripeKey($key): void
545548
{
546549
static::$stripeKey = $key;
547550
}

0 commit comments

Comments
 (0)