Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Re-add tax percentage #916

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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