From 38e0c922708c1ccb3efb80c145df73e74cef6536 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 15 Apr 2019 18:25:49 +0200 Subject: [PATCH] Set Stripe API Version This commit sets a fixed Stripe API version for all Stripe API calls made from Cashier. It's being passed as an option to all calls together with the api key. The way options are handled has also been refactored so this is now entirely done on the Cashier class while keeping the Billable entity free from any configuration related options. This may later still be changed when a config file for Cashier is introduced: https://github.com/laravel/cashier/issues/531 Closes https://github.com/laravel/cashier/issues/621 and https://github.com/laravel/cashier/issues/642 --- src/Billable.php | 64 ++++++------------------------- src/Cashier.php | 57 +++++++++++++++++++++++++++ tests/Integration/CashierTest.php | 1 - 3 files changed, 69 insertions(+), 53 deletions(-) diff --git a/src/Billable.php b/src/Billable.php index 582672db..54159f73 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -19,13 +19,6 @@ trait Billable { - /** - * The Stripe API key. - * - * @var string - */ - protected static $stripeKey; - /** * Make a "one off" charge on the customer for the given amount. * @@ -50,7 +43,7 @@ public function charge($amount, array $options = []) throw new InvalidArgumentException('No payment source provided.'); } - return StripeCharge::create($options, ['api_key' => $this->getStripeKey()]); + return StripeCharge::create($options, Cashier::stripeOptions()); } /** @@ -65,7 +58,7 @@ public function refund($charge, array $options = []) { $options['charge'] = $charge; - return StripeRefund::create($options, ['api_key' => $this->getStripeKey()]); + return StripeRefund::create($options, Cashier::stripeOptions()); } /** @@ -100,7 +93,7 @@ public function tab($description, $amount, array $options = []) 'description' => $description, ], $options); - return StripeInvoiceItem::create($options, ['api_key' => $this->getStripeKey()]); + return StripeInvoiceItem::create($options, Cashier::stripeOptions()); } /** @@ -224,7 +217,7 @@ public function invoice(array $options = []) $parameters = array_merge($options, ['customer' => $this->stripe_id]); try { - return StripeInvoice::create($parameters, $this->getStripeKey())->pay(); + return StripeInvoice::create($parameters, Cashier::stripeOptions())->pay(); } catch (StripeErrorInvalidRequest $e) { return false; } @@ -241,9 +234,7 @@ public function invoice(array $options = []) public function upcomingInvoice() { try { - $stripeInvoice = StripeInvoice::upcoming( - ['customer' => $this->stripe_id], ['api_key' => $this->getStripeKey()] - ); + $stripeInvoice = StripeInvoice::upcoming(['customer' => $this->stripe_id], Cashier::stripeOptions()); return new Invoice($this, $stripeInvoice); } catch (StripeErrorInvalidRequest $e) { @@ -261,10 +252,10 @@ public function findInvoice($id) { try { $stripeInvoice = StripeInvoice::retrieve( - $id, $this->getStripeKey() + $id, Cashier::stripeOptions() ); - $stripeInvoice->lines = StripeInvoice::retrieve($id, $this->getStripeKey()) + $stripeInvoice->lines = StripeInvoice::retrieve($id, Cashier::stripeOptions()) ->lines ->all(['limit' => 1000]); @@ -398,7 +389,7 @@ public function updateCard($token) { $customer = $this->asStripeCustomer(); - $token = StripeToken::retrieve($token, ['api_key' => $this->getStripeKey()]); + $token = StripeToken::retrieve($token, Cashier::stripeOptions()); // If the given token already has the card as their default source, we can just // bail out of the method now. We don't need to keep adding the same card to @@ -557,7 +548,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, $this->getStripeKey() + $options, Cashier::stripeOptions() ); $this->stripe_id = $customer->id; @@ -575,11 +566,9 @@ public function createAsStripeCustomer(array $options = []) */ public function updateStripeCustomer(array $options = []) { - $customer = StripeCustomer::update( - $this->stripe_id, $options, $this->getStripeKey() + return StripeCustomer::update( + $this->stripe_id, $options, Cashier::stripeOptions() ); - - return $customer; } /** @@ -589,7 +578,7 @@ public function updateStripeCustomer(array $options = []) */ public function asStripeCustomer() { - return StripeCustomer::retrieve($this->stripe_id, $this->getStripeKey()); + return StripeCustomer::retrieve($this->stripe_id, Cashier::stripeOptions()); } /** @@ -611,33 +600,4 @@ public function taxPercentage() { return 0; } - - /** - * Get the Stripe API key. - * - * @return string - */ - public static function getStripeKey() - { - if (static::$stripeKey) { - return static::$stripeKey; - } - - if ($key = getenv('STRIPE_SECRET')) { - return $key; - } - - return config('services.stripe.secret'); - } - - /** - * Set the Stripe API key. - * - * @param string $key - * @return void - */ - public static function setStripeKey($key) - { - static::$stripeKey = $key; - } } diff --git a/src/Cashier.php b/src/Cashier.php index a0d3e795..a389d6e4 100644 --- a/src/Cashier.php +++ b/src/Cashier.php @@ -7,6 +7,20 @@ class Cashier { + /** + * The Stripe API version. + * + * @var string + */ + const STRIPE_VERSION = '2019-03-14'; + + /** + * The Stripe API key. + * + * @var string + */ + protected static $stripeKey; + /** * The current currency. * @@ -28,6 +42,49 @@ class Cashier */ protected static $formatCurrencyUsing; + /** + * Get the Stripe API key. + * + * @return string + */ + public static function stripeKey() + { + if (static::$stripeKey) { + return static::$stripeKey; + } + + if ($key = getenv('STRIPE_SECRET')) { + return $key; + } + + return config('services.stripe.secret'); + } + + /** + * Set the Stripe API key. + * + * @param string $key + * @return void + */ + public static function setStripeKey($key) + { + static::$stripeKey = $key; + } + + /** + * Get the default Stripe API options. + * + * @param array $options + * @return array + */ + public static function stripeOptions(array $options = []) + { + return array_merge([ + 'api_key' => static::stripeKey(), + 'stripe_version' => static::STRIPE_VERSION, + ], $options); + } + /** * Get the class name of the billable model. * diff --git a/tests/Integration/CashierTest.php b/tests/Integration/CashierTest.php index 1652a329..4cc9a628 100644 --- a/tests/Integration/CashierTest.php +++ b/tests/Integration/CashierTest.php @@ -57,7 +57,6 @@ class CashierTest extends TestCase public static function setUpBeforeClass() { - Stripe::setApiVersion('2019-03-14'); Stripe::setApiKey(getenv('STRIPE_SECRET')); static::setUpStripeTestData();