Skip to content

Commit

Permalink
Merge pull request #754 from laravel/multiple-stripe-accounts
Browse files Browse the repository at this point in the history
[10.0] Multiple stripe accounts
  • Loading branch information
taylorotwell authored Aug 19, 2019
2 parents cb837d1 + b662a08 commit 1110cbe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
49 changes: 30 additions & 19 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function charge($amount, $paymentMethod, array $options = [])
}

$payment = new Payment(
StripePaymentIntent::create($options, Cashier::stripeOptions())
StripePaymentIntent::create($options, $this->stripeOptions())
);

$payment->validate();
Expand All @@ -61,7 +61,7 @@ public function charge($amount, $paymentMethod, array $options = [])
*/
public function refund($paymentIntent, array $options = [])
{
$intent = StripePaymentIntent::retrieve($paymentIntent, Cashier::stripeOptions());
$intent = StripePaymentIntent::retrieve($paymentIntent, $this->stripeOptions());

return $intent->charges->data[0]->refund($options);
}
Expand All @@ -85,7 +85,7 @@ public function tab($description, $amount, array $options = [])
'description' => $description,
], $options);

return StripeInvoiceItem::create($options, Cashier::stripeOptions());
return StripeInvoiceItem::create($options, $this->stripeOptions());
}

/**
Expand Down Expand Up @@ -226,7 +226,7 @@ public function invoice(array $options = [])

try {
/** @var \Stripe\Invoice $invoice */
$stripeInvoice = StripeInvoice::create($parameters, Cashier::stripeOptions());
$stripeInvoice = StripeInvoice::create($parameters, $this->stripeOptions());

$stripeInvoice = $stripeInvoice->pay();

Expand All @@ -237,7 +237,7 @@ public function invoice(array $options = [])
$payment = new Payment(
StripePaymentIntent::retrieve(
['id' => $stripeInvoice->refresh()->payment_intent, 'expand' => ['invoice.subscription']],
Cashier::stripeOptions()
$this->stripeOptions()
)
);

Expand All @@ -255,7 +255,7 @@ public function upcomingInvoice()
$this->assertCustomerExists();

try {
$stripeInvoice = StripeInvoice::upcoming(['customer' => $this->stripe_id], Cashier::stripeOptions());
$stripeInvoice = StripeInvoice::upcoming(['customer' => $this->stripe_id], $this->stripeOptions());

return new Invoice($this, $stripeInvoice);
} catch (StripeErrorInvalidRequest $e) {
Expand All @@ -273,10 +273,10 @@ public function findInvoice($id)
{
try {
$stripeInvoice = StripeInvoice::retrieve(
$id, Cashier::stripeOptions()
$id, $this->stripeOptions()
);

$stripeInvoice->lines = StripeInvoice::retrieve($id, Cashier::stripeOptions())
$stripeInvoice->lines = StripeInvoice::retrieve($id, $this->stripeOptions())
->lines
->all(['limit' => 1000]);

Expand Down Expand Up @@ -370,7 +370,7 @@ public function invoicesIncludingPending(array $parameters = [])
public function createSetupIntent(array $options = [])
{
return StripeSetupIntent::create(
$options, Cashier::stripeOptions()
$options, $this->stripeOptions()
);
}

Expand Down Expand Up @@ -399,7 +399,7 @@ public function paymentMethods($parameters = [])
// "type" is temporarily required by Stripe...
$paymentMethods = StripePaymentMethod::all(
['customer' => $this->stripe_id, 'type' => 'card'] + $parameters,
Cashier::stripeOptions()
$this->stripeOptions()
);

return collect($paymentMethods->data)->map(function ($paymentMethod) {
Expand All @@ -421,7 +421,7 @@ public function addPaymentMethod($paymentMethod)

if ($stripePaymentMethod->customer !== $this->stripe_id) {
$stripePaymentMethod = $stripePaymentMethod->attach(
['customer' => $this->stripe_id], Cashier::stripeOptions()
['customer' => $this->stripe_id], $this->stripeOptions()
);
}

Expand All @@ -441,15 +441,15 @@ public function removePaymentMethod($paymentMethod)
$stripePaymentMethod = $this->resolveStripePaymentMethod($paymentMethod);

if ($stripePaymentMethod->customer === $this->stripe_id) {
$stripePaymentMethod->detach(null, Cashier::stripeOptions());
$stripePaymentMethod->detach(null, $this->stripeOptions());

$customer = $this->asStripeCustomer();

// If payment method was the default payment method, we'll remove it manually...
if ($stripePaymentMethod->id === $customer->invoice_settings->default_payment_method) {
$customer->invoice_settings = ['default_payment_method' => null];

$customer->save(Cashier::stripeOptions());
$customer->save($this->stripeOptions());

$this->forceFill([
'card_brand' => null,
Expand All @@ -476,7 +476,7 @@ public function defaultPaymentMethod()
'invoice_settings.default_payment_method',
'default_source',
],
], Cashier::stripeOptions());
], $this->stripeOptions());

if ($customer->invoice_settings->default_payment_method) {
return new PaymentMethod($this, $customer->invoice_settings->default_payment_method);
Expand Down Expand Up @@ -511,7 +511,7 @@ public function updateDefaultPaymentMethod($paymentMethod)

$customer->invoice_settings = ['default_payment_method' => $paymentMethod->id];

$customer->save(Cashier::stripeOptions());
$customer->save($this->stripeOptions());

// Next we will get the default payment method for this user so we can update the
// payment method details on the record in the database. This will allow us to
Expand Down Expand Up @@ -614,7 +614,7 @@ protected function resolveStripePaymentMethod($paymentMethod)
}

return StripePaymentMethod::retrieve(
$paymentMethod, Cashier::stripeOptions()
$paymentMethod, $this->stripeOptions()
);
}

Expand Down Expand Up @@ -712,7 +712,7 @@ public function createAsStripeCustomer(array $options = [])
// user from Stripe. This ID will correspond with the Stripe user instances
// and allow us to retrieve users from Stripe later when we need to work.
$customer = StripeCustomer::create(
$options, Cashier::stripeOptions()
$options, $this->stripeOptions()
);

$this->stripe_id = $customer->id;
Expand All @@ -731,7 +731,7 @@ public function createAsStripeCustomer(array $options = [])
public function updateStripeCustomer(array $options = [])
{
return StripeCustomer::update(
$this->stripe_id, $options, Cashier::stripeOptions()
$this->stripe_id, $options, $this->stripeOptions()
);
}

Expand All @@ -757,7 +757,7 @@ public function createOrGetStripeCustomer(array $options = [])
*/
public function asStripeCustomer()
{
return StripeCustomer::retrieve($this->stripe_id, Cashier::stripeOptions());
return StripeCustomer::retrieve($this->stripe_id, $this->stripeOptions());
}

/**
Expand All @@ -779,4 +779,15 @@ public function taxPercentage()
{
return 0;
}

/**
* Get the default Stripe API options for the current Billable model.
*
* @param array $options
* @return array
*/
public function stripeOptions(array $options = [])
{
return Cashier::stripeOptions($options);
}
}
3 changes: 1 addition & 2 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Payment;
use Illuminate\Support\Carbon;
use Laravel\Cashier\Subscription;
Expand Down Expand Up @@ -182,7 +181,7 @@ protected function handleInvoicePaymentActionRequired(array $payload)
if (in_array(Notifiable::class, class_uses_recursive($user))) {
$payment = new Payment(StripePaymentIntent::retrieve(
$payload['data']['object']['payment_intent'],
Cashier::stripeOptions()
$user->stripeOptions()
));

$user->notify(new $notification($payment));
Expand Down
2 changes: 1 addition & 1 deletion src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public function asStripeSubscription(array $expand = [])
{
return StripeSubscription::retrieve(
['id' => $this->stripe_id, 'expand' => $expand],
Cashier::stripeOptions()
$this->owner->stripeOptions()
);
}
}
11 changes: 5 additions & 6 deletions tests/Integration/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laravel\Cashier\Tests\Integration;

use Laravel\Cashier\Cashier;
use Stripe\Card as StripeCard;
use Laravel\Cashier\PaymentMethod;
use Stripe\SetupIntent as StripeSetupIntent;
Expand Down Expand Up @@ -101,10 +100,10 @@ public function test_we_can_retrieve_all_payment_methods()
$user = $this->createCustomer('we_can_retrieve_all_payment_methods');
$customer = $user->createAsStripeCustomer();

$paymentMethod = StripePaymentMethod::retrieve('pm_card_visa', Cashier::stripeOptions());
$paymentMethod = StripePaymentMethod::retrieve('pm_card_visa', $user->stripeOptions());
$paymentMethod->attach(['customer' => $customer->id]);

$paymentMethod = StripePaymentMethod::retrieve('pm_card_mastercard', Cashier::stripeOptions());
$paymentMethod = StripePaymentMethod::retrieve('pm_card_mastercard', $user->stripeOptions());
$paymentMethod->attach(['customer' => $customer->id]);

$paymentMethods = $user->paymentMethods();
Expand All @@ -119,7 +118,7 @@ public function test_we_can_sync_the_default_payment_method_from_stripe()
$user = $this->createCustomer('we_can_sync_the_payment_method_from_stripe');
$customer = $user->createAsStripeCustomer();

$paymentMethod = StripePaymentMethod::retrieve('pm_card_visa', Cashier::stripeOptions());
$paymentMethod = StripePaymentMethod::retrieve('pm_card_visa', $user->stripeOptions());
$paymentMethod->attach(['customer' => $customer->id]);

$customer->invoice_settings = ['default_payment_method' => $paymentMethod->id];
Expand All @@ -142,10 +141,10 @@ public function test_we_delete_all_payment_methods()
$user = $this->createCustomer('we_delete_all_payment_methods');
$customer = $user->createAsStripeCustomer();

$paymentMethod = StripePaymentMethod::retrieve('pm_card_visa', Cashier::stripeOptions());
$paymentMethod = StripePaymentMethod::retrieve('pm_card_visa', $user->stripeOptions());
$paymentMethod->attach(['customer' => $customer->id]);

$paymentMethod = StripePaymentMethod::retrieve('pm_card_mastercard', Cashier::stripeOptions());
$paymentMethod = StripePaymentMethod::retrieve('pm_card_mastercard', $user->stripeOptions());
$paymentMethod->attach(['customer' => $customer->id]);

$paymentMethods = $user->paymentMethods();
Expand Down

0 comments on commit 1110cbe

Please sign in to comment.