Skip to content

Commit

Permalink
Re-add tax percentage (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Apr 16, 2020
1 parent 202ea1c commit d721769
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/Concerns/ManagesSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ public function onPlan($plan)
}));
}

/**
* Get the tax percentage to apply to the subscription.
*
* @return int|float
* @deprecated Please migrate to the new Tax Rates API.
*/
public function taxPercentage()
{
return 0;
}

/**
* Get the tax rates to apply to the subscription.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,21 @@ public function invoice(array $options = [])
}
}

/**
* Sync the tax percentage of the user to the subscription.
*
* @return void
* @deprecated Please migrate to the new Tax Rates API.
*/
public function syncTaxPercentage()
{
$subscription = $this->asStripeSubscription();

$subscription->tax_percent = $this->user->taxPercentage();

$subscription->save();
}

/**
* Sync the tax rates of the user to the subscription.
*
Expand Down
33 changes: 29 additions & 4 deletions src/SubscriptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ public function __construct($owner, $name, $plans = null)
*/
public function plan($plan, $quantity = 1)
{
$this->items[$plan] = [
$options = [
'plan' => $plan,
'quantity' => $quantity,
'tax_rates' => $this->getPlanTaxRatesForPayload($plan),
];

if ($taxRates = $this->getPlanTaxRatesForPayload($plan)) {
$options['tax_rates'] = $taxRates;
}

$this->items[$plan] = $options;

return $this;
}

Expand Down Expand Up @@ -304,16 +309,23 @@ protected function getStripeCustomer($paymentMethod = null, array $options = [])
*/
protected function buildPayload()
{
return array_filter([
$payload = array_filter([
'billing_cycle_anchor' => $this->billingCycleAnchor,
'coupon' => $this->coupon,
'expand' => ['latest_invoice.payment_intent'],
'metadata' => $this->metadata,
'items' => collect($this->items)->values()->all(),
'default_tax_rates' => $this->getTaxRatesForPayload(),
'trial_end' => $this->getTrialEndForPayload(),
'off_session' => true,
]);

if ($taxRates = $this->getTaxRatesForPayload()) {
$payload['default_tax_rates'] = $taxRates;
} elseif ($taxPercentage = $this->getTaxPercentageForPayload()) {
$payload['tax_percent'] = $taxPercentage;
}

return $payload;
}

/**
Expand All @@ -332,6 +344,19 @@ protected function getTrialEndForPayload()
}
}

/**
* Get the tax percentage for the Stripe payload.
*
* @return int|float|null
* @deprecated Please migrate to the new Tax Rates API.
*/
protected function getTaxPercentageForPayload()
{
if ($taxPercentage = $this->owner->taxPercentage()) {
return $taxPercentage;
}
}

/**
* Get the tax rates for the Stripe payload.
*
Expand Down

0 comments on commit d721769

Please sign in to comment.