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

Allow configuring names for the stripe_id column on Cashier models. #1657

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion database/factories/SubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion database/factories/SubscriptionItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,7 @@
trait Billable
{
use HandlesTaxes;
use HasStripeId;
use ManagesCustomer;
use ManagesInvoices;
use ManagesPaymentMethods;
Expand Down
2 changes: 1 addition & 1 deletion src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Concerns/HasStripeId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Laravel\Cashier\Concerns;

trait HasStripeId
{
/**
* Get the name of the model's "stripe_id" column.
*
* @return string
*/
public static function stripeIdColumn()
{
return 'stripe_id';
}

/**
* Get the Stripe ID for the model.
*
* @return null|string
*/
public function stripeId()
{
return $this->{static::stripeIdColumn()};
}
}
30 changes: 10 additions & 20 deletions src/Concerns/ManagesCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@

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.
*
* @return bool
*/
public function hasStripeId()
{
return ! is_null($this->stripe_id);
return ! is_null($this->stripeId());
}

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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
);
}

Expand Down Expand Up @@ -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]
);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
);
}

Expand All @@ -477,7 +467,7 @@ public function findTaxId($id)

try {
return static::stripe()->customers->retrieveTaxId(
$this->stripe_id, $id, []
$this->stripeId(), $id, []
);
} catch (StripeInvalidRequestException $exception) {
//
Expand All @@ -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,
]);
Expand All @@ -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) {
//
}
Expand Down
10 changes: 5 additions & 5 deletions src/Concerns/ManagesInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Concerns/ManagesPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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()]
);
}

Expand All @@ -118,7 +118,7 @@ public function deletePaymentMethod($paymentMethod)

$stripePaymentMethod = $this->resolveStripePaymentMethod($paymentMethod);

if ($stripePaymentMethod->customer !== $this->stripe_id) {
if ($stripePaymentMethod->customer !== $this->stripeId()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/PerformsCharges.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/CustomerBalanceTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CustomerAlreadyCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}.");
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidCustomerBalanceTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`.");
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`.");
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`."
);
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/SubscriptionUpdateFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
}

Expand All @@ -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()}\"."
);
}

Expand All @@ -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."
);
}
}
Loading
Loading