From d721769dc29acb2d5edb9b661cce20a9599a41f8 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 16 Apr 2020 15:25:17 +0200 Subject: [PATCH] Re-add tax percentage (#916) --- src/Concerns/ManagesSubscriptions.php | 11 +++++++++ src/Subscription.php | 15 ++++++++++++ src/SubscriptionBuilder.php | 33 +++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/Concerns/ManagesSubscriptions.php b/src/Concerns/ManagesSubscriptions.php index 7de21ccd..0ec87a34 100644 --- a/src/Concerns/ManagesSubscriptions.php +++ b/src/Concerns/ManagesSubscriptions.php @@ -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. * diff --git a/src/Subscription.php b/src/Subscription.php index 98e066ce..5900f62b 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -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. * diff --git a/src/SubscriptionBuilder.php b/src/SubscriptionBuilder.php index c36ec2dd..204518c2 100644 --- a/src/SubscriptionBuilder.php +++ b/src/SubscriptionBuilder.php @@ -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; } @@ -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; } /** @@ -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. *