diff --git a/database/factories/SubscriptionFactory.php b/database/factories/SubscriptionFactory.php index b0d48203..16d367a0 100644 --- a/database/factories/SubscriptionFactory.php +++ b/database/factories/SubscriptionFactory.php @@ -31,7 +31,7 @@ public function definition(): array return [ (new $model)->getForeignKey() => ($model)::factory(), 'type' => 'default', - 'stripe_id' => 'sub_'.Str::random(40), + $this->model::stripeIdColumn() => 'sub_'.Str::random(40), 'stripe_status' => StripeSubscription::STATUS_ACTIVE, 'stripe_price' => null, 'quantity' => null, diff --git a/database/factories/SubscriptionItemFactory.php b/database/factories/SubscriptionItemFactory.php index 43d00094..dba6623d 100644 --- a/database/factories/SubscriptionItemFactory.php +++ b/database/factories/SubscriptionItemFactory.php @@ -25,7 +25,7 @@ public function definition(): array { return [ 'subscription_id' => Subscription::factory(), - 'stripe_id' => 'si_'.Str::random(40), + $this->model::stripeIdColumn() => 'si_'.Str::random(40), 'stripe_product' => 'prod_'.Str::random(40), 'stripe_price' => 'price_'.Str::random(40), 'quantity' => null, diff --git a/src/Billable.php b/src/Billable.php index f257fe4e..7b6c1568 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -3,6 +3,7 @@ namespace Laravel\Cashier; use Laravel\Cashier\Concerns\HandlesTaxes; +use Laravel\Cashier\Concerns\HasStripeId; use Laravel\Cashier\Concerns\ManagesCustomer; use Laravel\Cashier\Concerns\ManagesInvoices; use Laravel\Cashier\Concerns\ManagesPaymentMethods; @@ -12,6 +13,7 @@ trait Billable { use HandlesTaxes; + use HasStripeId; use ManagesCustomer; use ManagesInvoices; use ManagesPaymentMethods; diff --git a/src/Cashier.php b/src/Cashier.php index b9320e3c..e52da313 100644 --- a/src/Cashier.php +++ b/src/Cashier.php @@ -107,7 +107,7 @@ public static function findBillable($stripeId) ? $model::withTrashed() : new $model; - return $stripeId ? $builder->where('stripe_id', $stripeId)->first() : null; + return $stripeId ? $builder->where($model::stripeIdColumn(), $stripeId)->first() : null; } /** diff --git a/src/Concerns/HasStripeId.php b/src/Concerns/HasStripeId.php new file mode 100644 index 00000000..e7d9035c --- /dev/null +++ b/src/Concerns/HasStripeId.php @@ -0,0 +1,26 @@ +{static::stripeIdColumn()}; + } +} diff --git a/src/Concerns/ManagesCustomer.php b/src/Concerns/ManagesCustomer.php index c1122439..37df9b84 100644 --- a/src/Concerns/ManagesCustomer.php +++ b/src/Concerns/ManagesCustomer.php @@ -15,16 +15,6 @@ trait ManagesCustomer { - /** - * Retrieve the Stripe customer ID. - * - * @return string|null - */ - public function stripeId() - { - return $this->stripe_id; - } - /** * Determine if the customer has a Stripe customer ID. * @@ -32,7 +22,7 @@ public function stripeId() */ public function hasStripeId() { - return ! is_null($this->stripe_id); + return ! is_null($this->stripeId()); } /** @@ -92,7 +82,7 @@ public function createAsStripeCustomer(array $options = []) // and allow us to retrieve users from Stripe later when we need to work. $customer = static::stripe()->customers->create($options); - $this->stripe_id = $customer->id; + $this->{$this::stripeIdColumn()} = $customer->id; $this->save(); @@ -108,7 +98,7 @@ public function createAsStripeCustomer(array $options = []) public function updateStripeCustomer(array $options = []) { return static::stripe()->customers->update( - $this->stripe_id, $options + $this->stripeId(), $options ); } @@ -138,7 +128,7 @@ public function asStripeCustomer(array $expand = []) $this->assertCustomerExists(); return static::stripe()->customers->retrieve( - $this->stripe_id, ['expand' => $expand] + $this->stripeId(), ['expand' => $expand] ); } @@ -344,7 +334,7 @@ public function balanceTransactions($limit = 10, array $options = []) $transactions = static::stripe() ->customers - ->allBalanceTransactions($this->stripe_id, array_merge(['limit' => $limit], $options)); + ->allBalanceTransactions($this->stripeId(), array_merge(['limit' => $limit], $options)); return Collection::make($transactions->data)->map(function ($transaction) { return new CustomerBalanceTransaction($this, $transaction); @@ -391,7 +381,7 @@ public function applyBalance($amount, $description = null, array $options = []) $transaction = static::stripe() ->customers - ->createBalanceTransaction($this->stripe_id, array_filter(array_merge([ + ->createBalanceTransaction($this->stripeId(), array_filter(array_merge([ 'amount' => $amount, 'currency' => $this->preferredCurrency(), 'description' => $description, @@ -462,7 +452,7 @@ public function taxIds(array $options = []) $this->assertCustomerExists(); return new Collection( - static::stripe()->customers->allTaxIds($this->stripe_id, $options)->data + static::stripe()->customers->allTaxIds($this->stripeId(), $options)->data ); } @@ -477,7 +467,7 @@ public function findTaxId($id) try { return static::stripe()->customers->retrieveTaxId( - $this->stripe_id, $id, [] + $this->stripeId(), $id, [] ); } catch (StripeInvalidRequestException $exception) { // @@ -495,7 +485,7 @@ public function createTaxId($type, $value) { $this->assertCustomerExists(); - return static::stripe()->customers->createTaxId($this->stripe_id, [ + return static::stripe()->customers->createTaxId($this->stripeId(), [ 'type' => $type, 'value' => $value, ]); @@ -512,7 +502,7 @@ public function deleteTaxId($id) $this->assertCustomerExists(); try { - static::stripe()->customers->deleteTaxId($this->stripe_id, $id); + static::stripe()->customers->deleteTaxId($this->stripeId(), $id); } catch (StripeInvalidRequestException $exception) { // } diff --git a/src/Concerns/ManagesInvoices.php b/src/Concerns/ManagesInvoices.php index 22162ddb..374066a5 100644 --- a/src/Concerns/ManagesInvoices.php +++ b/src/Concerns/ManagesInvoices.php @@ -35,7 +35,7 @@ public function tab($description, $amount, array $options = []) $this->assertCustomerExists(); $options = array_merge([ - 'customer' => $this->stripe_id, + 'customer' => $this->stripeId(), 'currency' => $this->preferredCurrency(), 'description' => $description, ], $options); @@ -85,7 +85,7 @@ public function tabPrice($price, $quantity = 1, array $options = []) $this->assertCustomerExists(); $options = array_merge([ - 'customer' => $this->stripe_id, + 'customer' => $this->stripeId(), 'price' => $price, 'quantity' => $quantity, ], $options); @@ -164,7 +164,7 @@ public function createInvoice(array $options = []) $parameters = array_merge([ 'automatic_tax' => $this->automaticTaxPayload(), - 'customer' => $this->stripe_id, + 'customer' => $this->stripeId(), 'currency' => $stripeCustomer->currency ?? config('cashier.currency'), ], $options); @@ -195,7 +195,7 @@ public function upcomingInvoice(array $options = []) $parameters = array_merge([ 'automatic_tax' => $this->automaticTaxPayload(), - 'customer' => $this->stripe_id, + 'customer' => $this->stripeId(), ], $options); try { @@ -283,7 +283,7 @@ public function invoices($includePending = false, $parameters = []) $parameters = array_merge(['limit' => 24], $parameters); $stripeInvoices = static::stripe()->invoices->all( - ['customer' => $this->stripe_id] + $parameters + ['customer' => $this->stripeId()] + $parameters ); // Here we will loop through the Stripe invoices and create our own custom Invoice diff --git a/src/Concerns/ManagesPaymentMethods.php b/src/Concerns/ManagesPaymentMethods.php index 85bb4ae4..44377dde 100644 --- a/src/Concerns/ManagesPaymentMethods.php +++ b/src/Concerns/ManagesPaymentMethods.php @@ -20,7 +20,7 @@ trait ManagesPaymentMethods public function createSetupIntent(array $options = []) { if ($this->hasStripeId()) { - $options['customer'] = $this->stripe_id; + $options['customer'] = $this->stripeId(); } return static::stripe()->setupIntents->create($options); @@ -77,7 +77,7 @@ public function paymentMethods($type = null, $parameters = []) // "type" is temporarily required by Stripe... $paymentMethods = static::stripe()->paymentMethods->all( - array_filter(['customer' => $this->stripe_id, 'type' => $type]) + $parameters + array_filter(['customer' => $this->stripeId(), 'type' => $type]) + $parameters ); return Collection::make($paymentMethods->data)->map(function ($paymentMethod) { @@ -97,9 +97,9 @@ public function addPaymentMethod($paymentMethod) $stripePaymentMethod = $this->resolveStripePaymentMethod($paymentMethod); - if ($stripePaymentMethod->customer !== $this->stripe_id) { + if ($stripePaymentMethod->customer !== $this->stripeId()) { $stripePaymentMethod = $stripePaymentMethod->attach( - ['customer' => $this->stripe_id] + ['customer' => $this->stripeId()] ); } @@ -118,7 +118,7 @@ public function deletePaymentMethod($paymentMethod) $stripePaymentMethod = $this->resolveStripePaymentMethod($paymentMethod); - if ($stripePaymentMethod->customer !== $this->stripe_id) { + if ($stripePaymentMethod->customer !== $this->stripeId()) { return; } diff --git a/src/Concerns/PerformsCharges.php b/src/Concerns/PerformsCharges.php index dc91c8d8..45e52684 100644 --- a/src/Concerns/PerformsCharges.php +++ b/src/Concerns/PerformsCharges.php @@ -86,7 +86,7 @@ public function createPayment($amount, array $options = []) $options['amount'] = $amount; if ($this->hasStripeId()) { - $options['customer'] = $this->stripe_id; + $options['customer'] = $this->stripeId(); } return new Payment( diff --git a/src/CustomerBalanceTransaction.php b/src/CustomerBalanceTransaction.php index 9708f43d..7f2521e2 100644 --- a/src/CustomerBalanceTransaction.php +++ b/src/CustomerBalanceTransaction.php @@ -32,7 +32,7 @@ class CustomerBalanceTransaction */ public function __construct($owner, StripeCustomerBalanceTransaction $transaction) { - if ($owner->stripe_id !== $transaction->customer) { + if ($owner->stripeId() !== $transaction->customer) { throw InvalidCustomerBalanceTransaction::invalidOwner($transaction, $owner); } diff --git a/src/Exceptions/CustomerAlreadyCreated.php b/src/Exceptions/CustomerAlreadyCreated.php index fbe2aedc..ea899660 100644 --- a/src/Exceptions/CustomerAlreadyCreated.php +++ b/src/Exceptions/CustomerAlreadyCreated.php @@ -14,6 +14,6 @@ class CustomerAlreadyCreated extends Exception */ public static function exists($owner) { - return new static(class_basename($owner)." is already a Stripe customer with ID {$owner->stripe_id}."); + return new static(class_basename($owner)." is already a Stripe customer with ID {$owner->stripeId()}."); } } diff --git a/src/Exceptions/InvalidCustomerBalanceTransaction.php b/src/Exceptions/InvalidCustomerBalanceTransaction.php index ac274d08..e9c0209e 100644 --- a/src/Exceptions/InvalidCustomerBalanceTransaction.php +++ b/src/Exceptions/InvalidCustomerBalanceTransaction.php @@ -16,6 +16,6 @@ class InvalidCustomerBalanceTransaction extends Exception */ public static function invalidOwner(StripeCustomerBalanceTransaction $transaction, $owner) { - return new static("The transaction `{$transaction->id}` does not belong to customer `$owner->stripe_id`."); + return new static("The transaction `{$transaction->id}` does not belong to customer `{$owner->stripeId()}`."); } } diff --git a/src/Exceptions/InvalidInvoice.php b/src/Exceptions/InvalidInvoice.php index 3ef99239..febce991 100644 --- a/src/Exceptions/InvalidInvoice.php +++ b/src/Exceptions/InvalidInvoice.php @@ -16,6 +16,6 @@ class InvalidInvoice extends Exception */ public static function invalidOwner(StripeInvoice $invoice, $owner) { - return new static("The invoice `{$invoice->id}` does not belong to this customer `$owner->stripe_id`."); + return new static("The invoice `{$invoice->id}` does not belong to this customer `{$owner->stripeId()}`."); } } diff --git a/src/Exceptions/InvalidPaymentMethod.php b/src/Exceptions/InvalidPaymentMethod.php index f69f25c3..e426fda4 100644 --- a/src/Exceptions/InvalidPaymentMethod.php +++ b/src/Exceptions/InvalidPaymentMethod.php @@ -17,7 +17,7 @@ class InvalidPaymentMethod extends Exception public static function invalidOwner(StripePaymentMethod $paymentMethod, $owner) { return new static( - "The payment method `{$paymentMethod->id}`'s customer `{$paymentMethod->customer}` does not belong to this customer `$owner->stripe_id`." + "The payment method `{$paymentMethod->id}`'s customer `{$paymentMethod->customer}` does not belong to this customer `{$owner->stripeId()}`." ); } } diff --git a/src/Exceptions/SubscriptionUpdateFailure.php b/src/Exceptions/SubscriptionUpdateFailure.php index 3558cf46..edec016d 100644 --- a/src/Exceptions/SubscriptionUpdateFailure.php +++ b/src/Exceptions/SubscriptionUpdateFailure.php @@ -16,7 +16,7 @@ class SubscriptionUpdateFailure extends Exception public static function incompleteSubscription(Subscription $subscription) { return new static( - "The subscription \"{$subscription->stripe_id}\" cannot be updated because its payment is incomplete." + "The subscription \"{$subscription->stripeId()}\" cannot be updated because its payment is incomplete." ); } @@ -30,7 +30,7 @@ public static function incompleteSubscription(Subscription $subscription) public static function duplicatePrice(Subscription $subscription, $price) { return new static( - "The price \"$price\" is already attached to subscription \"{$subscription->stripe_id}\"." + "The price \"$price\" is already attached to subscription \"{$subscription->stripeId()}\"." ); } @@ -43,7 +43,7 @@ public static function duplicatePrice(Subscription $subscription, $price) public static function cannotDeleteLastPrice(Subscription $subscription) { return new static( - "The price on subscription \"{$subscription->stripe_id}\" cannot be removed because it is the last one." + "The price on subscription \"{$subscription->stripeId()}\" cannot be removed because it is the last one." ); } } diff --git a/src/Http/Controllers/WebhookController.php b/src/Http/Controllers/WebhookController.php index d9ec9d76..78cc68f1 100644 --- a/src/Http/Controllers/WebhookController.php +++ b/src/Http/Controllers/WebhookController.php @@ -70,7 +70,7 @@ protected function handleCustomerSubscriptionCreated(array $payload) if ($user) { $data = $payload['data']['object']; - if (! $user->subscriptions->contains('stripe_id', $data['id'])) { + if (! $user->subscriptions->contains(Cashier::$subscriptionModel::stripeIdColumn(), $data['id'])) { if (isset($data['trial_end'])) { $trialEndsAt = Carbon::createFromTimestamp($data['trial_end']); } else { @@ -82,7 +82,7 @@ protected function handleCustomerSubscriptionCreated(array $payload) $subscription = $user->subscriptions()->create([ 'type' => $data['metadata']['type'] ?? $data['metadata']['name'] ?? $this->newSubscriptionType($payload), - 'stripe_id' => $data['id'], + Cashier::$subscriptionModel::stripeIdColumn() => $data['id'], 'stripe_status' => $data['status'], 'stripe_price' => $isSinglePrice ? $firstItem['price']['id'] : null, 'quantity' => $isSinglePrice && isset($firstItem['quantity']) ? $firstItem['quantity'] : null, @@ -92,7 +92,7 @@ protected function handleCustomerSubscriptionCreated(array $payload) foreach ($data['items']['data'] as $item) { $subscription->items()->create([ - 'stripe_id' => $item['id'], + Cashier::$subscriptionItemModel::stripeIdColumn() => $item['id'], 'stripe_product' => $item['price']['product'], 'stripe_price' => $item['price']['id'], 'quantity' => $item['quantity'] ?? null, @@ -131,7 +131,7 @@ protected function handleCustomerSubscriptionUpdated(array $payload) if ($user = $this->getUserByStripeId($payload['data']['object']['customer'])) { $data = $payload['data']['object']; - $subscription = $user->subscriptions()->firstOrNew(['stripe_id' => $data['id']]); + $subscription = $user->subscriptions()->firstOrNew([Cashier::$subscriptionModel::stripeIdColumn() => $data['id']]); if ( isset($data['status']) && @@ -189,7 +189,7 @@ protected function handleCustomerSubscriptionUpdated(array $payload) $subscriptionItemIds[] = $item['id']; $subscription->items()->updateOrCreate([ - 'stripe_id' => $item['id'], + Cashier::$subscriptionItemModel::stripeIdColumn() => $item['id'], ], [ 'stripe_product' => $item['price']['product'], 'stripe_price' => $item['price']['id'], @@ -198,7 +198,7 @@ protected function handleCustomerSubscriptionUpdated(array $payload) } // Delete items that aren't attached to the subscription anymore... - $subscription->items()->whereNotIn('stripe_id', $subscriptionItemIds)->delete(); + $subscription->items()->whereNotIn(Cashier::$subscriptionItemModel::stripeIdColumn(), $subscriptionItemIds)->delete(); } } @@ -215,7 +215,7 @@ protected function handleCustomerSubscriptionDeleted(array $payload) { if ($user = $this->getUserByStripeId($payload['data']['object']['customer'])) { $user->subscriptions->filter(function ($subscription) use ($payload) { - return $subscription->stripe_id === $payload['data']['object']['id']; + return $subscription->stripeId() === $payload['data']['object']['id']; })->each(function ($subscription) { $subscription->skipTrial()->markAsCanceled(); }); @@ -253,7 +253,7 @@ protected function handleCustomerDeleted(array $payload) }); $user->forceFill([ - 'stripe_id' => null, + $user::stripeIdColumn() => null, 'trial_ends_at' => null, 'pm_type' => null, 'pm_last_four' => null, diff --git a/src/Invoice.php b/src/Invoice.php index 204fcab8..b551ddad 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -78,7 +78,7 @@ class Invoice implements Arrayable, Jsonable, JsonSerializable */ public function __construct($owner, StripeInvoice $invoice, array $refreshData = []) { - if ($owner->stripe_id !== $invoice->customer) { + if ($owner->stripeId() !== $invoice->customer) { throw InvalidInvoice::invalidOwner($invoice, $owner); } @@ -590,7 +590,7 @@ protected function refreshWithExpandedData() } else { // If no invoice ID is present then assume this is the customer's upcoming invoice... $this->invoice = $this->owner->stripe()->invoices->upcoming(array_merge($this->refreshData, [ - 'customer' => $this->owner->stripe_id, + 'customer' => $this->owner->stripeId(), 'expand' => $expand, ])); } diff --git a/src/PaymentMethod.php b/src/PaymentMethod.php index 7b2ca488..52b7df55 100644 --- a/src/PaymentMethod.php +++ b/src/PaymentMethod.php @@ -40,7 +40,7 @@ public function __construct($owner, StripePaymentMethod $paymentMethod) throw new LogicException('The payment method is not attached to a customer.'); } - if ($owner->stripe_id !== $paymentMethod->customer) { + if ($owner->stripeId() !== $paymentMethod->customer) { throw InvalidPaymentMethod::invalidOwner($paymentMethod, $owner); } diff --git a/src/Subscription.php b/src/Subscription.php index f18be08e..70665afc 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -11,6 +11,7 @@ use InvalidArgumentException; use Laravel\Cashier\Concerns\AllowsCoupons; use Laravel\Cashier\Concerns\HandlesPaymentFailures; +use Laravel\Cashier\Concerns\HasStripeId; use Laravel\Cashier\Concerns\InteractsWithPaymentBehavior; use Laravel\Cashier\Concerns\Prorates; use Laravel\Cashier\Database\Factories\SubscriptionFactory; @@ -27,6 +28,7 @@ class Subscription extends Model use AllowsCoupons; use HandlesPaymentFailures; use HasFactory; + use HasStripeId; use InteractsWithPaymentBehavior; use Prorates; @@ -694,7 +696,7 @@ public function swap($prices, array $options = []) ); $stripeSubscription = $this->owner->stripe()->subscriptions->update( - $this->stripe_id, $this->getSwapOptions($items, $options) + $this->stripeId(), $this->getSwapOptions($items, $options) ); /** @var \Stripe\SubscriptionItem $firstItem */ @@ -714,7 +716,7 @@ public function swap($prices, array $options = []) $subscriptionItemIds[] = $item->id; $this->items()->updateOrCreate([ - 'stripe_id' => $item->id, + Cashier::$subscriptionItemModel::stripeIdColumn() => $item->id, ], [ 'stripe_product' => $item->price->product, 'stripe_price' => $item->price->id, @@ -723,7 +725,7 @@ public function swap($prices, array $options = []) } // Delete items that aren't attached to the subscription anymore... - $this->items()->whereNotIn('stripe_id', $subscriptionItemIds)->delete(); + $this->items()->whereNotIn(Cashier::$subscriptionItemModel::stripeIdColumn(), $subscriptionItemIds)->delete(); $this->unsetRelation('items'); @@ -860,7 +862,7 @@ public function addPrice($price, $quantity = 1, array $options = []) $stripeSubscriptionItem = $this->owner->stripe()->subscriptionItems ->create(array_filter(array_merge([ - 'subscription' => $this->stripe_id, + 'subscription' => $this->stripeId(), 'price' => $price, 'quantity' => $quantity, 'tax_rates' => $this->getPriceTaxRatesForPayload($price), @@ -869,7 +871,7 @@ public function addPrice($price, $quantity = 1, array $options = []) ], $options))); $this->items()->create([ - 'stripe_id' => $stripeSubscriptionItem->id, + Cashier::$subscriptionItemModel::stripeIdColumn() => $stripeSubscriptionItem->id, 'stripe_product' => $stripeSubscriptionItem->price->product, 'stripe_price' => $stripeSubscriptionItem->price->id, 'quantity' => $stripeSubscriptionItem->quantity ?? null, @@ -1041,7 +1043,7 @@ public function cancelAt($endsAt) */ public function cancelNow() { - $this->owner->stripe()->subscriptions->cancel($this->stripe_id, [ + $this->owner->stripe()->subscriptions->cancel($this->stripeId(), [ 'prorate' => $this->prorateBehavior() === 'create_prorations', ]); @@ -1057,7 +1059,7 @@ public function cancelNow() */ public function cancelNowAndInvoice() { - $this->owner->stripe()->subscriptions->cancel($this->stripe_id, [ + $this->owner->stripe()->subscriptions->cancel($this->stripeId(), [ 'invoice_now' => true, 'prorate' => $this->prorateBehavior() === 'create_prorations', ]); @@ -1132,7 +1134,7 @@ public function pending() public function invoice(array $options = []) { try { - return $this->user->invoice(array_merge($options, ['subscription' => $this->stripe_id])); + return $this->user->invoice(array_merge($options, ['subscription' => $this->stripeId()])); } catch (IncompletePayment $exception) { // Set the new Stripe subscription status immediately when payment fails... $this->fill([ @@ -1170,7 +1172,7 @@ public function upcomingInvoice(array $options = []) } return $this->owner->upcomingInvoice(array_merge([ - 'subscription' => $this->stripe_id, + 'subscription' => $this->stripeId(), ], $options)); } @@ -1220,7 +1222,7 @@ public function previewInvoice($prices, array $options = []) public function invoices($includePending = false, $parameters = []) { return $this->owner->invoices( - $includePending, array_merge($parameters, ['subscription' => $this->stripe_id]) + $includePending, array_merge($parameters, ['subscription' => $this->stripeId()]) ); } @@ -1373,7 +1375,7 @@ public function guardAgainstMultiplePrices() public function updateStripeSubscription(array $options = []) { return $this->owner->stripe()->subscriptions->update( - $this->stripe_id, $options + $this->stripeId(), $options ); } @@ -1386,7 +1388,7 @@ public function updateStripeSubscription(array $options = []) public function asStripeSubscription(array $expand = []) { return $this->owner->stripe()->subscriptions->retrieve( - $this->stripe_id, ['expand' => $expand] + $this->stripeId(), ['expand' => $expand] ); } diff --git a/src/SubscriptionBuilder.php b/src/SubscriptionBuilder.php index 1448c11e..bce1695b 100644 --- a/src/SubscriptionBuilder.php +++ b/src/SubscriptionBuilder.php @@ -294,7 +294,7 @@ public function createAndSendInvoice(array $customerOptions = [], array $subscri */ protected function createSubscription(StripeSubscription $stripeSubscription) { - if ($subscription = $this->owner->subscriptions()->where('stripe_id', $stripeSubscription->id)->first()) { + if ($subscription = $this->owner->subscriptions()->where(Cashier::$subscriptionModel::stripeIdColumn(), $stripeSubscription->id)->first()) { return $subscription; } @@ -305,7 +305,7 @@ protected function createSubscription(StripeSubscription $stripeSubscription) /** @var \Laravel\Cashier\Subscription $subscription */ $subscription = $this->owner->subscriptions()->create([ 'type' => $this->type, - 'stripe_id' => $stripeSubscription->id, + Cashier::$subscriptionModel::stripeIdColumn() => $stripeSubscription->id, 'stripe_status' => $stripeSubscription->status, 'stripe_price' => $isSinglePrice ? $firstItem->price->id : null, 'quantity' => $isSinglePrice ? ($firstItem->quantity ?? null) : null, @@ -316,7 +316,7 @@ protected function createSubscription(StripeSubscription $stripeSubscription) /** @var \Stripe\SubscriptionItem $item */ foreach ($stripeSubscription->items as $item) { $subscription->items()->create([ - 'stripe_id' => $item->id, + Cashier::$subscriptionItemModel::stripeIdColumn() => $item->id, 'stripe_product' => $item->price->product, 'stripe_price' => $item->price->id, 'quantity' => $item->quantity ?? null, diff --git a/src/SubscriptionItem.php b/src/SubscriptionItem.php index 4494c265..adbc4cd6 100644 --- a/src/SubscriptionItem.php +++ b/src/SubscriptionItem.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Laravel\Cashier\Concerns\HandlesPaymentFailures; +use Laravel\Cashier\Concerns\HasStripeId; use Laravel\Cashier\Concerns\InteractsWithPaymentBehavior; use Laravel\Cashier\Concerns\Prorates; use Laravel\Cashier\Database\Factories\SubscriptionItemFactory; @@ -18,6 +19,7 @@ class SubscriptionItem extends Model { use HandlesPaymentFailures; use HasFactory; + use HasStripeId; use InteractsWithPaymentBehavior; use Prorates; @@ -213,7 +215,7 @@ public function reportUsage($quantity = 1, $timestamp = null) { $timestamp = $timestamp instanceof DateTimeInterface ? $timestamp->getTimestamp() : $timestamp; - return $this->subscription->owner->stripe()->subscriptionItems->createUsageRecord($this->stripe_id, [ + return $this->subscription->owner->stripe()->subscriptionItems->createUsageRecord($this->stripeId(), [ 'quantity' => $quantity, 'action' => $timestamp ? 'set' : 'increment', 'timestamp' => $timestamp ?? time(), @@ -229,7 +231,7 @@ public function reportUsage($quantity = 1, $timestamp = null) public function usageRecords($options = []) { return new Collection($this->subscription->owner->stripe()->subscriptionItems->allUsageRecordSummaries( - $this->stripe_id, $options + $this->stripeId(), $options )->data); } @@ -242,7 +244,7 @@ public function usageRecords($options = []) public function updateStripeSubscriptionItem(array $options = []) { return $this->subscription->owner->stripe()->subscriptionItems->update( - $this->stripe_id, $options + $this->stripeId(), $options ); } @@ -255,7 +257,7 @@ public function updateStripeSubscriptionItem(array $options = []) public function asStripeSubscriptionItem(array $expand = []) { return $this->subscription->owner->stripe()->subscriptionItems->retrieve( - $this->stripe_id, ['expand' => $expand] + $this->stripeId(), ['expand' => $expand] ); } diff --git a/tests/Feature/ChargesTest.php b/tests/Feature/ChargesTest.php index 8fb03d80..558a596a 100644 --- a/tests/Feature/ChargesTest.php +++ b/tests/Feature/ChargesTest.php @@ -18,7 +18,7 @@ public function test_customer_can_be_charged() $this->assertInstanceOf(Payment::class, $response); $this->assertEquals(1000, $response->rawAmount()); - $this->assertEquals($user->stripe_id, $response->customer); + $this->assertEquals($user->stripeId(), $response->customer); } public function test_non_stripe_customer_can_be_charged() @@ -43,7 +43,7 @@ public function test_customer_can_pay() $this->assertInstanceOf(Payment::class, $response); $this->assertEquals(1000, $response->rawAmount()); - $this->assertEquals($user->stripe_id, $response->customer); + $this->assertEquals($user->stripeId(), $response->customer); $this->assertTrue($response->requiresPaymentMethod()); $this->assertTrue($response->automatic_payment_methods->enabled); diff --git a/tests/Feature/InvoicesTest.php b/tests/Feature/InvoicesTest.php index bc89d00e..88f848e8 100644 --- a/tests/Feature/InvoicesTest.php +++ b/tests/Feature/InvoicesTest.php @@ -113,7 +113,7 @@ public function test_it_throws_an_exception_if_the_invoice_does_not_belong_to_th $this->expectException(InvalidInvoice::class); $this->expectExceptionMessage( - "The invoice `{$invoice->id}` does not belong to this customer `$otherUser->stripe_id`." + "The invoice `{$invoice->id}` does not belong to this customer `{$otherUser->stripeId()}`." ); $otherUser->findInvoice($invoice->id); diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index 946829d8..86ccddd3 100644 --- a/tests/Feature/SubscriptionsTest.php +++ b/tests/Feature/SubscriptionsTest.php @@ -117,7 +117,7 @@ public function test_subscriptions_can_be_created() ->create('pm_card_visa'); $this->assertEquals(1, count($user->subscriptions)); - $this->assertNotNull(($subscription = $user->subscription('main'))->stripe_id); + $this->assertNotNull(($subscription = $user->subscription('main'))->stripeId()); $this->assertSame($metadata, $subscription->asStripeSubscription()->metadata->toArray()); $this->assertTrue($user->subscribed('main')); diff --git a/tests/Feature/WebhooksTest.php b/tests/Feature/WebhooksTest.php index 4d5fefaa..dfb0b99f 100644 --- a/tests/Feature/WebhooksTest.php +++ b/tests/Feature/WebhooksTest.php @@ -110,7 +110,7 @@ public function test_subscriptions_are_updated() 'type' => 'customer.subscription.updated', 'data' => [ 'object' => [ - 'id' => $subscription->stripe_id, + 'id' => $subscription->stripeId(), 'customer' => 'cus_foo', 'cancel_at_period_end' => false, 'items' => [ @@ -168,7 +168,7 @@ public function test_subscriptions_on_update_cancel_at_date_is_correct() 'type' => 'customer.subscription.updated', 'data' => [ 'object' => [ - 'id' => $subscription->stripe_id, + 'id' => $subscription->stripeId(), 'customer' => 'cus_foo', 'cancel_at' => $cancelDate->timestamp, 'cancel_at_period_end' => false, @@ -216,12 +216,12 @@ public function test_canceled_subscription_is_properly_reactivated() 'type' => 'customer.subscription.updated', 'data' => [ 'object' => [ - 'id' => $subscription->stripe_id, - 'customer' => $user->stripe_id, + 'id' => $subscription->stripeId(), + 'customer' => $user->stripeId(), 'cancel_at_period_end' => false, 'items' => [ 'data' => [[ - 'id' => $subscription->items()->first()->stripe_id, + 'id' => $subscription->items()->first()->stripeId(), 'price' => ['id' => static::$priceId, 'product' => static::$productId], 'quantity' => 1, ]], @@ -245,8 +245,8 @@ public function test_subscription_is_marked_as_canceled_when_deleted_in_stripe() 'type' => 'customer.subscription.deleted', 'data' => [ 'object' => [ - 'id' => $subscription->stripe_id, - 'customer' => $user->stripe_id, + 'id' => $subscription->stripeId(), + 'customer' => $user->stripeId(), 'quantity' => 1, ], ], @@ -267,8 +267,8 @@ public function test_subscription_is_deleted_when_status_is_incomplete_expired() 'type' => 'customer.subscription.updated', 'data' => [ 'object' => [ - 'id' => $subscription->stripe_id, - 'customer' => $user->stripe_id, + 'id' => $subscription->stripeId(), + 'customer' => $user->stripeId(), 'status' => StripeSubscription::STATUS_INCOMPLETE_EXPIRED, 'quantity' => 1, ], @@ -295,7 +295,7 @@ public function test_payment_action_required_email_is_sent() 'data' => [ 'object' => [ 'id' => 'foo', - 'customer' => $user->stripe_id, + 'customer' => $user->stripeId(), 'payment_intent' => $exception->payment->id, ], ], diff --git a/tests/Unit/CustomerTest.php b/tests/Unit/CustomerTest.php index eb60f3ff..243b8707 100644 --- a/tests/Unit/CustomerTest.php +++ b/tests/Unit/CustomerTest.php @@ -67,7 +67,7 @@ public function test_stripe_customer_method_throws_exception_when_stripe_id_is_n public function test_stripe_customer_cannot_be_created_when_stripe_id_is_already_set() { $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $this->expectException(CustomerAlreadyCreated::class); diff --git a/tests/Unit/InvoiceLineItemTest.php b/tests/Unit/InvoiceLineItemTest.php index 7f78fd01..c5dfbce6 100644 --- a/tests/Unit/InvoiceLineItemTest.php +++ b/tests/Unit/InvoiceLineItemTest.php @@ -18,7 +18,7 @@ class InvoiceLineItemTest extends TestCase public function test_we_can_calculate_the_inclusive_tax_percentage() { $customer = new User(); - $customer->stripe_id = 'foo'; + $customer->{$customer::stripeIdColumn()} = 'foo'; $stripeInvoice = new StripeInvoice(); $stripeInvoice->customer_tax_exempt = StripeCustomer::TAX_EXEMPT_NONE; @@ -43,7 +43,7 @@ public function test_we_can_calculate_the_inclusive_tax_percentage() public function test_we_can_calculate_the_exclusive_tax_percentage() { $customer = new User(); - $customer->stripe_id = 'foo'; + $customer->{$customer::stripeIdColumn()} = 'foo'; $stripeInvoice = new StripeInvoice(); $stripeInvoice->customer_tax_exempt = StripeCustomer::TAX_EXEMPT_NONE; diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index a40f747d..953327c9 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -30,7 +30,7 @@ public function test_it_can_return_the_invoice_date() $stripeInvoice->created = 1560541724; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -47,7 +47,7 @@ public function test_it_can_return_the_invoice_date_with_a_timezone() $stripeInvoice->created = 1560541724; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -65,7 +65,7 @@ public function test_it_can_return_its_total() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -82,7 +82,7 @@ public function test_it_can_return_its_raw_total() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -100,7 +100,7 @@ public function test_it_returns_a_lower_total_when_there_was_a_starting_balance( $stripeInvoice->starting_balance = -450; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -117,7 +117,7 @@ public function test_it_can_return_its_subtotal() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -133,7 +133,7 @@ public function test_it_can_determine_when_the_customer_has_a_starting_balance() $stripeInvoice->starting_balance = -450; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -147,7 +147,7 @@ public function test_it_can_determine_when_the_customer_does_not_have_a_starting $stripeInvoice->starting_balance = 0; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -162,7 +162,7 @@ public function test_it_can_return_its_starting_balance() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -178,7 +178,7 @@ public function test_it_can_return_its_ending_balance() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -195,7 +195,7 @@ public function test_it_can_return_its_applied_balance() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -213,7 +213,7 @@ public function test_it_can_return_its_applied_balance_when_depleted() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -238,7 +238,7 @@ public function test_it_can_determine_if_it_has_a_discount_applied() $stripeInvoice->discounts = [$discount, $otherDiscount]; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -256,7 +256,7 @@ public function test_it_can_return_its_tax() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -271,7 +271,7 @@ public function test_it_can_return_its_tax() $stripeInvoice->currency = 'USD'; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -287,7 +287,7 @@ public function test_it_can_determine_if_the_customer_was_exempt_from_taxes() $stripeInvoice->customer_tax_exempt = StripeCustomer::TAX_EXEMPT_EXEMPT; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice); @@ -301,7 +301,7 @@ public function test_it_can_determine_if_reverse_charge_applies() $stripeInvoice->customer_tax_exempt = StripeCustomer::TAX_EXEMPT_REVERSE; $user = new User(); - $user->stripe_id = 'foo'; + $user->{$user::stripeIdColumn()} = 'foo'; $invoice = new Invoice($user, $stripeInvoice);