Skip to content

Commit

Permalink
Merge pull request #11 from yii2mod/feature-package-refactoring
Browse files Browse the repository at this point in the history
rename tables, require php 7.1, update tests
  • Loading branch information
Igor Chepurnoy authored Mar 22, 2018
2 parents 09e4db5 + 6cfa1b0 commit ffb8c7c
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 176 deletions.
109 changes: 56 additions & 53 deletions Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Stripe\Charge;
use Stripe\Customer;
use Stripe\Error\Card;
use Stripe\Error\InvalidRequest;
use Stripe\Invoice as StripeInvoice;
use Stripe\InvoiceItem as StripeInvoiceItem;
Expand Down Expand Up @@ -37,20 +38,20 @@ trait Billable
* @param int $amount
* @param array $options
*
* @return \Stripe\Charge
* @return Charge
*
* @throws \Stripe\Error\Card
* @throws Card
*/
public function charge($amount, array $options = [])
public function charge($amount, array $options = []): Charge
{
$options = array_merge([
'currency' => $this->preferredCurrency(),
], $options);

$options['amount'] = $amount;

if (!array_key_exists('source', $options) && $this->stripeId) {
$options['customer'] = $this->stripeId;
if (!array_key_exists('source', $options) && $this->stripe_id) {
$options['customer'] = $this->stripe_id;
}

if (!array_key_exists('source', $options) && !array_key_exists('customer', $options)) {
Expand All @@ -68,7 +69,7 @@ public function charge($amount, array $options = [])
*
* @return StripeRefund
*/
public function refund($charge, array $options = [])
public function refund($charge, array $options = []): StripeRefund
{
$options['charge'] = $charge;

Expand All @@ -80,9 +81,9 @@ public function refund($charge, array $options = [])
*
* @return bool
*/
public function hasCardOnFile()
public function hasCardOnFile(): bool
{
return (bool)$this->cardBrand;
return (bool) $this->card_brand;
}

/**
Expand All @@ -92,18 +93,18 @@ public function hasCardOnFile()
* @param int $amount
* @param array $options
*
* @return bool
* @return bool|StripeInvoice
*
* @throws \Stripe\Error\Card
* @throws Card
*/
public function invoiceFor($description, $amount, array $options = [])
{
if (!$this->stripeId) {
if (!$this->stripe_id) {
throw new InvalidArgumentException('User is not a customer. See the createAsStripeCustomer method.');
}

$options = array_merge([
'customer' => $this->stripeId,
'customer' => $this->stripe_id,
'amount' => $amount,
'currency' => $this->preferredCurrency(),
'description' => $description,
Expand All @@ -124,7 +125,7 @@ public function invoiceFor($description, $amount, array $options = [])
*
* @return SubscriptionBuilder
*/
public function newSubscription($subscription, $plan)
public function newSubscription(string $subscription, string $plan): SubscriptionBuilder
{
return new SubscriptionBuilder($this, $subscription, $plan);
}
Expand All @@ -137,7 +138,7 @@ public function newSubscription($subscription, $plan)
*
* @return bool
*/
public function onTrial($subscription = 'default', $plan = null)
public function onTrial(string $subscription = 'default', ?string $plan = null): bool
{
if (func_num_args() === 0 && $this->onGenericTrial()) {
return true;
Expand All @@ -148,17 +149,17 @@ public function onTrial($subscription = 'default', $plan = null)
}

return $subscription && $subscription->onTrial() &&
$subscription->stripePlan === $plan;
$subscription->stripePlan === $plan;
}

/**
* Determine if the user is on a "generic" trial at the user level.
*
* @return bool
*/
public function onGenericTrial()
public function onGenericTrial(): bool
{
return $this->trialEndAt && Carbon::now()->lt(Carbon::createFromFormat('Y-m-d H:i:s', $this->trialEndAt));
return $this->trial_ends_at && Carbon::now()->lt(Carbon::createFromFormat('Y-m-d H:i:s', $this->trial_ends_at));
}

/**
Expand All @@ -169,18 +170,20 @@ public function onGenericTrial()
*
* @return bool
*/
public function subscribed($subscription = 'default', $plan = null)
public function subscribed(string $subscription = 'default', ?string $plan = null): bool
{
$subscription = $this->subscription($subscription);

if (is_null($subscription)) {
return false;
}

if (is_null($plan)) {
return $subscription->valid();
}

return $subscription->valid() &&
$subscription->stripePlan === $plan;
$subscription->stripe_plan === $plan;
}

/**
Expand All @@ -190,7 +193,7 @@ public function subscribed($subscription = 'default', $plan = null)
*
* @return SubscriptionModel|null
*/
public function subscription($subscription = 'default')
public function subscription(string $subscription = 'default'): ?SubscriptionModel
{
return $this->getSubscriptions()->where(['name' => $subscription])->one();
}
Expand All @@ -200,19 +203,19 @@ public function subscription($subscription = 'default')
*/
public function getSubscriptions()
{
return $this->hasMany(SubscriptionModel::className(), ['userId' => 'id'])->orderBy(['createdAt' => SORT_DESC]);
return $this->hasMany(SubscriptionModel::class, ['user_id' => 'id'])->orderBy(['created_at' => SORT_DESC]);
}

/**
* Invoice the billable entity outside of regular billing cycle.
*
* @return bool
* @return bool|StripeInvoice
*/
public function invoice()
{
if ($this->stripeId) {
if ($this->stripe_id) {
try {
return StripeInvoice::create(['customer' => $this->stripeId], $this->getStripeKey())->pay();
return StripeInvoice::create(['customer' => $this->stripe_id], $this->getStripeKey())->pay();
} catch (InvalidRequest $e) {
return false;
}
Expand All @@ -226,11 +229,11 @@ public function invoice()
*
* @return Invoice|null
*/
public function upcomingInvoice()
public function upcomingInvoice(): ?Invoice
{
try {
$stripeInvoice = StripeInvoice::upcoming(
['customer' => $this->stripeId], ['api_key' => $this->getStripeKey()]
['customer' => $this->stripe_id], ['api_key' => $this->getStripeKey()]
);

return new Invoice($this, $stripeInvoice);
Expand All @@ -245,7 +248,7 @@ public function upcomingInvoice()
*
* @return Invoice|null
*/
public function findInvoice($id)
public function findInvoice(string $id): Invoice
{
try {
return new Invoice($this, StripeInvoice::retrieve($id, $this->getStripeKey()));
Expand All @@ -262,7 +265,7 @@ public function findInvoice($id)
*
* @throws NotFoundHttpException
*/
public function findInvoiceOrFail($id)
public function findInvoiceOrFail(string $id)
{
$invoice = $this->findInvoice($id);

Expand All @@ -281,7 +284,7 @@ public function findInvoiceOrFail($id)
*
* @return Response
*/
public function downloadInvoice($id, array $data)
public function downloadInvoice(string $id, array $data)
{
return $this->findInvoiceOrFail($id)->download($data);
}
Expand All @@ -294,7 +297,7 @@ public function downloadInvoice($id, array $data)
*
* @return array
*/
public function invoices($includePending = false, $parameters = [])
public function invoices(bool $includePending = false, array $parameters = []): array
{
$invoices = [];

Expand Down Expand Up @@ -323,7 +326,7 @@ public function invoices($includePending = false, $parameters = [])
*
* @return array
*/
public function invoicesIncludingPending(array $parameters = [])
public function invoicesIncludingPending(array $parameters = []): array
{
return $this->invoices(true, $parameters);
}
Expand All @@ -333,7 +336,7 @@ public function invoicesIncludingPending(array $parameters = [])
*
* @param string $token
*/
public function updateCard($token)
public function updateCard(string $token): void
{
$customer = $this->asStripeCustomer();
$token = Token::retrieve($token, ['api_key' => $this->getStripeKey()]);
Expand Down Expand Up @@ -376,8 +379,8 @@ public function updateCardFromStripe()
if ($defaultCard) {
$this->fillCardDetails($defaultCard)->save();
} else {
$this->cardBrand = null;
$this->cardLastFour = null;
$this->card_brand = null;
$this->card_last_four = null;
$this->update(false);
}

Expand All @@ -394,8 +397,8 @@ public function updateCardFromStripe()
protected function fillCardDetails($card)
{
if ($card) {
$this->cardBrand = $card->brand;
$this->cardLastFour = $card->last4;
$this->card_brand = $card->brand;
$this->card_last_four = $card->last4;
}

return $this;
Expand Down Expand Up @@ -423,16 +426,16 @@ public function applyCoupon($coupon)
*
* @return bool
*/
public function subscribedToPlan($plans, $subscription = 'default')
public function subscribedToPlan($plans, $subscription = 'default'): bool
{
$subscription = $this->subscription($subscription);

if (!$subscription || !$subscription->valid()) {
return false;
}

foreach ((array)$plans as $plan) {
if ($subscription->stripePlan === $plan) {
foreach ((array) $plans as $plan) {
if ($subscription->stripe_plan === $plan) {
return true;
}
}
Expand All @@ -447,9 +450,9 @@ public function subscribedToPlan($plans, $subscription = 'default')
*
* @return bool
*/
public function onPlan($plan)
public function onPlan($plan): bool
{
$plan = $this->getSubscriptions()->where(['stripePlan' => $plan])->one();
$plan = $this->getSubscriptions()->where(['stripe_plan' => $plan])->one();

return !is_null($plan) && $plan->valid();
}
Expand All @@ -459,9 +462,9 @@ public function onPlan($plan)
*
* @return bool
*/
public function hasStripeId()
public function hasStripeId(): bool
{
return !is_null($this->stripeId);
return !is_null($this->stripe_id);
}

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

$this->stripeId = $customer->id;
$this->stripe_id = $customer->id;

$this->save();

Expand All @@ -499,19 +502,19 @@ public function createAsStripeCustomer($token, array $options = [])
/**
* Get the Stripe customer for the user.
*
* @return \Stripe\Customer
* @return Customer
*/
public function asStripeCustomer()
public function asStripeCustomer(): Customer
{
return Customer::retrieve($this->stripeId, $this->getStripeKey());
return Customer::retrieve($this->stripe_id, $this->getStripeKey());
}

/**
* Get the Stripe supported currency used by the entity.
*
* @return string
*/
public function preferredCurrency()
public function preferredCurrency(): string
{
return Cashier::usesCurrency();
}
Expand All @@ -521,7 +524,7 @@ public function preferredCurrency()
*
* @return int
*/
public function taxPercentage()
public function taxPercentage(): int
{
return 0;
}
Expand All @@ -531,17 +534,17 @@ public function taxPercentage()
*
* @return string
*/
public static function getStripeKey()
public static function getStripeKey(): string
{
return static::$stripeKey ?: Yii::$app->params['stripe']['apiKey'];
}

/**
* Set the Stripe API key.
*
* @param string $key
* @param string $key
*/
public static function setStripeKey($key)
public static function setStripeKey($key): void
{
static::$stripeKey = $key;
}
Expand Down
Loading

0 comments on commit ffb8c7c

Please sign in to comment.