Skip to content

Commit

Permalink
Set Stripe API Version
Browse files Browse the repository at this point in the history
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: #531

Closes #621 and #642
  • Loading branch information
driesvints committed Apr 16, 2019
1 parent 0a1b1ac commit 38e0c92
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 53 deletions.
64 changes: 12 additions & 52 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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());
}

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

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

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

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

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

/**
Expand All @@ -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;
}
}
57 changes: 57 additions & 0 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
1 change: 0 additions & 1 deletion tests/Integration/CashierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class CashierTest extends TestCase

public static function setUpBeforeClass()
{
Stripe::setApiVersion('2019-03-14');
Stripe::setApiKey(getenv('STRIPE_SECRET'));

static::setUpStripeTestData();
Expand Down

0 comments on commit 38e0c92

Please sign in to comment.