Skip to content

Commit

Permalink
[13.x] Stripe SDK refactor (#1169)
Browse files Browse the repository at this point in the history
* Implement StripeClient

* Refactor to StripeClient

* Fix checkout with discounts

* Fix checkout

* Styling

* Fix call

* Remove delay

* Fixes
  • Loading branch information
driesvints authored May 25, 2021
1 parent 8b58a90 commit 36ef105
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 361 deletions.
13 changes: 7 additions & 6 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;
use NumberFormatter;
use Stripe\StripeClient;

class Cashier
{
Expand Down Expand Up @@ -89,17 +90,17 @@ public static function findBillable($stripeId)
}

/**
* Get the default Stripe API options.
* Get the Stripe SDK client.
*
* @param array $options
* @return array
* @return \Stripe\StripeClient
*/
public static function stripeOptions(array $options = [])
public static function stripe(array $options = [])
{
return array_merge([
'api_key' => config('cashier.secret'),
return new StripeClient(array_merge([
'api_key' => $options['api_key'] ?? config('cashier.secret'),
'stripe_version' => static::STRIPE_VERSION,
], $options);
], $options));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public static function create($owner, array $sessionOptions = [], array $custome
{
$customer = $owner->createOrGetStripeCustomer($customerOptions);

$session = Session::create(array_merge([
$session = $owner->stripe()->checkout->sessions->create(array_merge([
'customer' => $customer->id,
'mode' => 'payment',
'success_url' => $sessionOptions['success_url'] ?? route('home').'?checkout=success',
'cancel_url' => $sessionOptions['cancel_url'] ?? route('home').'?checkout=cancelled',
'payment_method_types' => ['card'],
], $sessionOptions), Cashier::stripeOptions());
], $sessionOptions));

return new static($owner, $session);
}
Expand Down
45 changes: 20 additions & 25 deletions src/Concerns/ManagesCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Exceptions\CustomerAlreadyCreated;
use Laravel\Cashier\Exceptions\InvalidCustomer;
use Stripe\BillingPortal\Session as StripeBillingPortalSession;
use Stripe\Customer as StripeCustomer;
use Stripe\Exception\InvalidRequestException as StripeInvalidRequestException;

Expand Down Expand Up @@ -68,9 +67,7 @@ public function createAsStripeCustomer(array $options = [])
// Here we will create the customer instance on Stripe and store the ID of the
// 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, $this->stripeOptions()
);
$customer = $this->stripe()->customers->create($options);

$this->stripe_id = $customer->id;

Expand All @@ -87,8 +84,8 @@ public function createAsStripeCustomer(array $options = [])
*/
public function updateStripeCustomer(array $options = [])
{
return StripeCustomer::update(
$this->stripe_id, $options, $this->stripeOptions()
return $this->stripe()->customers->update(
$this->stripe_id, $options
);
}

Expand Down Expand Up @@ -117,8 +114,8 @@ public function asStripeCustomer(array $expand = [])
{
$this->assertCustomerExists();

return StripeCustomer::retrieve(
['id' => $this->stripe_id, 'expand' => $expand], $this->stripeOptions()
return $this->stripe()->customers->retrieve(
$this->stripe_id, ['expand' => $expand]
);
}

Expand All @@ -142,11 +139,9 @@ public function applyCoupon($coupon)
{
$this->assertCustomerExists();

$customer = $this->asStripeCustomer();

$customer->coupon = $coupon;

$customer->save();
$this->updateStripeCustomer([
'coupon' => $coupon,
]);
}

/**
Expand All @@ -170,10 +165,10 @@ public function billingPortalUrl($returnUrl = null, array $options = [])
{
$this->assertCustomerExists();

return StripeBillingPortalSession::create(array_merge([
return $this->stripe()->billingPortal->sessions->create(array_merge([
'customer' => $this->stripeId(),
'return_url' => $returnUrl ?? route('home'),
], $options), $this->stripeOptions())['url'];
], $options))['url'];
}

/**
Expand All @@ -200,7 +195,7 @@ public function taxIds(array $options = [])
$this->assertCustomerExists();

return new Collection(
StripeCustomer::allTaxIds($this->stripe_id, $options, $this->stripeOptions())->data
$this->stripe()->customers->allTaxIds($this->stripe_id, $options)->data
);
}

Expand All @@ -214,8 +209,8 @@ public function findTaxId($id)
$this->assertCustomerExists();

try {
return StripeCustomer::retrieveTaxId(
$this->stripe_id, $id, [], $this->stripeOptions()
return $this->stripe()->customers->retrieveTaxId(
$this->stripe_id, $id, []
);
} catch (StripeInvalidRequestException $exception) {
//
Expand All @@ -233,10 +228,10 @@ public function createTaxId($type, $value)
{
$this->assertCustomerExists();

return StripeCustomer::createTaxId($this->stripe_id, [
return $this->stripe()->customers->createTaxId($this->stripe_id, [
'type' => $type,
'value' => $value,
], $this->stripeOptions());
]);
}

/**
Expand All @@ -250,7 +245,7 @@ public function deleteTaxId($id)
$this->assertCustomerExists();

try {
StripeCustomer::deleteTaxId($this->stripe_id, $id, [], $this->stripeOptions());
$this->stripe()->customers->deleteTaxId($this->stripe_id, $id);
} catch (StripeInvalidRequestException $exception) {
//
}
Expand Down Expand Up @@ -287,13 +282,13 @@ public function reverseChargeApplies()
}

/**
* Get the default Stripe API options for the current customer model.
* Get the Stripe SDK client.
*
* @param array $options
* @return array
* @return \Stripe\StripeClient
*/
public function stripeOptions(array $options = [])
public static function stripe(array $options = [])
{
return Cashier::stripeOptions($options);
return Cashier::stripe($options);
}
}
23 changes: 10 additions & 13 deletions src/Concerns/ManagesInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Stripe\Exception\CardException as StripeCardException;
use Stripe\Exception\InvalidRequestException as StripeInvalidRequestException;
use Stripe\Invoice as StripeInvoice;
use Stripe\InvoiceItem as StripeInvoiceItem;
use Stripe\PaymentIntent as StripePaymentIntent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand Down Expand Up @@ -40,7 +38,7 @@ public function tab($description, $amount, array $options = [])
$options['amount'] = $amount;
}

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

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

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

if ($stripeInvoice->collection_method === StripeInvoice::COLLECTION_METHOD_CHARGE_AUTOMATICALLY) {
$stripeInvoice = $stripeInvoice->pay();
Expand All @@ -90,9 +88,9 @@ public function invoice(array $options = [])
return false;
} catch (StripeCardException $exception) {
$payment = new Payment(
StripePaymentIntent::retrieve(
['id' => $stripeInvoice->refresh()->payment_intent, 'expand' => ['invoice.subscription']],
$this->stripeOptions()
$this->stripe()->paymentIntents->retrieve(
$stripeInvoice->refresh()->payment_intent,
['expand' => ['invoice.subscription']]
)
);

Expand All @@ -113,9 +111,9 @@ public function upcomingInvoice(array $options = [])
}

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

return new Invoice($this, $stripeInvoice);
} catch (StripeInvalidRequestException $exception) {
Expand All @@ -134,7 +132,7 @@ public function findInvoice($id)
$stripeInvoice = null;

try {
$stripeInvoice = StripeInvoice::retrieve($id, $this->stripeOptions());
$stripeInvoice = $this->stripe()->invoices->retrieve($id);
} catch (StripeInvalidRequestException $exception) {
//
}
Expand Down Expand Up @@ -198,9 +196,8 @@ public function invoices($includePending = false, $parameters = [])

$parameters = array_merge(['limit' => 24], $parameters);

$stripeInvoices = StripeInvoice::all(
['customer' => $this->stripe_id] + $parameters,
$this->stripeOptions()
$stripeInvoices = $this->stripe()->invoices->all(
['customer' => $this->stripe_id] + $parameters
);

// Here we will loop through the Stripe invoices and create our own custom Invoice
Expand Down
32 changes: 10 additions & 22 deletions src/Concerns/ManagesPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Exception;
use Illuminate\Support\Collection;
use Laravel\Cashier\PaymentMethod;
use Stripe\Customer as StripeCustomer;
use Stripe\PaymentMethod as StripePaymentMethod;
use Stripe\SetupIntent as StripeSetupIntent;

trait ManagesPaymentMethods
{
Expand All @@ -19,9 +17,7 @@ trait ManagesPaymentMethods
*/
public function createSetupIntent(array $options = [])
{
return StripeSetupIntent::create(
$options, $this->stripeOptions()
);
return $this->stripe()->setupIntents->create($options);
}

/**
Expand Down Expand Up @@ -61,9 +57,8 @@ public function paymentMethods($type = 'card', $parameters = [])
$parameters = array_merge(['limit' => 24], $parameters);

// "type" is temporarily required by Stripe...
$paymentMethods = StripePaymentMethod::all(
['customer' => $this->stripe_id, 'type' => $type] + $parameters,
$this->stripeOptions()
$paymentMethods = $this->stripe()->paymentMethods->all(
['customer' => $this->stripe_id, 'type' => $type] + $parameters
);

return Collection::make($paymentMethods->data)->map(function ($paymentMethod) {
Expand All @@ -85,7 +80,7 @@ public function addPaymentMethod($paymentMethod)

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

Expand All @@ -112,7 +107,7 @@ public function removePaymentMethod($paymentMethod)

$defaultPaymentMethod = $customer->invoice_settings->default_payment_method;

$stripePaymentMethod->detach(null, $this->stripeOptions());
$stripePaymentMethod->detach();

// If the payment method was the default payment method, we'll remove it manually...
if ($stripePaymentMethod->id === $defaultPaymentMethod) {
Expand All @@ -134,12 +129,7 @@ public function defaultPaymentMethod()
return;
}

$customer = StripeCustomer::retrieve([
'id' => $this->stripe_id,
'expand' => [
'invoice_settings.default_payment_method',
],
], $this->stripeOptions());
$customer = $this->asStripeCustomer(['invoice_settings.default_payment_method']);

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

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

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

$customer->save($this->stripeOptions());
$this->updateStripeCustomer([
'invoice_settings' => ['default_payment_method' => $paymentMethod->id],
]);

// 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 @@ -271,8 +261,6 @@ protected function resolveStripePaymentMethod($paymentMethod)
return $paymentMethod;
}

return StripePaymentMethod::retrieve(
$paymentMethod, $this->stripeOptions()
);
return $this->stripe()->paymentMethods->retrieve($paymentMethod);
}
}
Loading

0 comments on commit 36ef105

Please sign in to comment.