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

[13.x] Implement new calculateTaxes call #1198

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 additions & 0 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class Cashier
*/
public static $deactivatePastDue = true;

/**
* Indicates if Cashier will automatically calculate taxes using Strip Tax.
*
* @var bool
*/
public static $calculatesTaxes = false;

/**
* The default customer model class name.
*
Expand Down Expand Up @@ -173,6 +180,18 @@ public static function keepPastDueSubscriptionsActive()
return new static;
}

/**
* Configure Cashier to automatically calculate taxes using Stripe Tax.
*
* @return static
*/
public static function calculateTaxes()
{
static::$calculatesTaxes = true;

return new static;
}

/**
* Set the customer model class name.
*
Expand Down
10 changes: 7 additions & 3 deletions src/Concerns/HandlesTaxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace Laravel\Cashier\Concerns;

use Laravel\Cashier\Cashier;

trait HandlesTaxes
{
/**
* Indicates if Cashier should automatically calculate tax for the new subscription.
*
* @var bool
* @deprecated Use the new Cashier::calculateTaxes() method instead.
*/
protected $automaticTax = false;

/**
* The IP address of the customer used to determine tax location.
* The IP address of the customer used to determine the tax location.
*
* @var string|null
*/
Expand All @@ -36,6 +39,7 @@ trait HandlesTaxes
* Allow taxes to be automatically calculated by Stripe.
*
* @return $this
* @deprecated Use the new Cashier::calculateTaxes() method instead.
*/
public function withTax()
{
Expand All @@ -45,7 +49,7 @@ public function withTax()
}

/**
* Set the The IP address of the customer used to determine tax location.
* Set the The IP address of the customer used to determine the tax location.
*
* @return $this
*/
Expand Down Expand Up @@ -84,7 +88,7 @@ protected function automaticTaxPayload()
{
return array_filter([
'customer_ip_address' => $this->customerIpAddress,
'enabled' => $this->automaticTax,
'enabled' => $this->automaticTax ?: Cashier::$calculatesTaxes,
'estimation_billing_address' => $this->estimationBillingAddress,
]);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Concerns/PerformsCharges.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Cashier\Concerns;

use Illuminate\Support\Collection;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Checkout;
use Laravel\Cashier\Payment;

Expand All @@ -23,7 +24,6 @@ trait PerformsCharges
public function charge($amount, $paymentMethod, array $options = [])
{
$options = array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because it's not possible to use automatic tax with payment intents atm it seems.

'confirmation_method' => 'automatic',
'confirm' => true,
'currency' => $this->preferredCurrency(),
Expand Down Expand Up @@ -63,7 +63,6 @@ public function refund($paymentIntent, array $options = [])
* Begin a new checkout session for existing prices.
*
* @param array|string $items
* @param int $quantity
* @param array $sessionOptions
* @param array $customerOptions
* @return \Laravel\Cashier\Checkout
Expand All @@ -85,7 +84,7 @@ public function checkout($items, array $sessionOptions = [], array $customerOpti

return $item;
})->values()->all(),
'tax_id_collection' => $this->collectsTaxIds,
'tax_id_collection' => Cashier::$calculatesTaxes ?: $this->collectTaxIds,
]);

return Checkout::create($this, array_merge($payload, $sessionOptions), $customerOptions);
Expand Down
4 changes: 0 additions & 4 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use Laravel\Cashier\Concerns\HandlesTaxes;
use Laravel\Cashier\Concerns\InteractsWithPaymentBehavior;
use Laravel\Cashier\Concerns\Prorates;
use Laravel\Cashier\Database\Factories\SubscriptionFactory;
Expand All @@ -23,7 +22,6 @@
*/
class Subscription extends Model
{
use HandlesTaxes;
use HasFactory;
use InteractsWithPaymentBehavior;
use Prorates;
Expand Down Expand Up @@ -791,7 +789,6 @@ protected function mergeItemsThatShouldBeDeletedDuringSwap(Collection $items)
protected function getSwapOptions(Collection $items, array $options = [])
{
$payload = [
'automatic_tax' => $this->automaticTaxPayload(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because disabling automatic taxes during a swap is unlikely to happen. People can still do this by passing options explicitly if they want.

'items' => $items->values()->all(),
'payment_behavior' => $this->paymentBehavior(),
'proration_behavior' => $this->prorateBehavior(),
Expand Down Expand Up @@ -1132,7 +1129,6 @@ public function latestInvoice()
public function upcomingInvoice(array $options = [])
{
return $this->owner->upcomingInvoice(array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because the underlying upcomingInvoice already adds this.

'subscription' => $this->stripe_id,
], $options));
}
Expand Down
3 changes: 2 additions & 1 deletion src/SubscriptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ public function checkout(array $sessionOptions = [], array $customerOptions = []
}

$payload = array_filter([
'automatic_tax' => $this->automaticTaxPayload(),
'mode' => 'subscription',
'line_items' => Collection::make($this->items)->values()->all(),
'allow_promotion_codes' => $this->allowPromotionCodes,
Expand All @@ -332,7 +333,7 @@ public function checkout(array $sessionOptions = [], array $customerOptions = []
'trial_end' => $trialEnd ? $trialEnd->getTimestamp() : null,
'metadata' => array_merge($this->metadata, ['name' => $this->name]),
]),
'tax_id_collection' => $this->collectTaxIds,
'tax_id_collection' => Cashier::$calculatesTaxes ?: $this->collectTaxIds,
]);

return Checkout::create($this->owner, array_merge($payload, $sessionOptions), $customerOptions);
Expand Down