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

[10.x] Update Stripe SDK to v7 #784

Merged
merged 4 commits into from
Sep 26, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
phpunit.xml
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"illuminate/view": "~5.8.0|^6.0|^7.0",
"moneyphp/money": "^3.2",
"nesbot/carbon": "^2.0",
"stripe/stripe-php": "^6.40",
"stripe/stripe-php": "^7.0",
"symfony/http-kernel": "^4.3",
"symfony/intl": "^4.3"
},
Expand Down
49 changes: 26 additions & 23 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
use Stripe\BankAccount as StripeBankAccount;
use Stripe\Card as StripeCard;
use Stripe\Customer as StripeCustomer;
use Stripe\Error\Card as StripeCardException;
use Stripe\Error\InvalidRequest as StripeErrorInvalidRequest;
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 Stripe\PaymentMethod as StripePaymentMethod;
use Stripe\Refund as StripeRefund;
use Stripe\SetupIntent as StripeSetupIntent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -63,7 +64,10 @@ public function refund($paymentIntent, array $options = [])
{
$intent = StripePaymentIntent::retrieve($paymentIntent, $this->stripeOptions());

return $intent->charges->data[0]->refund($options);
return StripeRefund::create(
['charge' => $intent->charges->data[0]->id] + $options,
$this->stripeOptions()
);
}

/**
Expand Down Expand Up @@ -231,7 +235,7 @@ public function invoice(array $options = [])
$stripeInvoice = $stripeInvoice->pay();

return new Invoice($this, $stripeInvoice);
} catch (StripeErrorInvalidRequest $e) {
} catch (StripeInvalidRequestException $exception) {
return false;
} catch (StripeCardException $exception) {
$payment = new Payment(
Expand All @@ -258,7 +262,7 @@ public function upcomingInvoice()
$stripeInvoice = StripeInvoice::upcoming(['customer' => $this->stripe_id], $this->stripeOptions());

return new Invoice($this, $stripeInvoice);
} catch (StripeErrorInvalidRequest $e) {
} catch (StripeInvalidRequestException $exception) {
//
}
}
Expand All @@ -276,12 +280,8 @@ public function findInvoice($id)
$id, $this->stripeOptions()
);

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

return new Invoice($this, $stripeInvoice);
} catch (Exception $e) {
} catch (Exception $exception) {
//
}
}
Expand Down Expand Up @@ -334,7 +334,10 @@ public function invoices($includePending = false, $parameters = [])

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

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

// Here we will loop through the Stripe invoices and create our own custom Invoice
// instances that have more helper methods and are generally more convenient to
Expand Down Expand Up @@ -440,22 +443,22 @@ public function removePaymentMethod($paymentMethod)

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

if ($stripePaymentMethod->customer === $this->stripe_id) {
$stripePaymentMethod->detach(null, $this->stripeOptions());
if ($stripePaymentMethod->customer !== $this->stripe_id) {
return;
}

$customer = $this->asStripeCustomer();
$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];
$defaultPaymentMethod = $customer->invoice_settings->default_payment_method;

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

$this->forceFill([
'card_brand' => null,
'card_last_four' => null,
])->save();
}
// If the payment method was the default payment method, we'll remove it manually...
if ($stripePaymentMethod->id === $defaultPaymentMethod) {
$this->forceFill([
'card_brand' => null,
'card_last_four' => null,
])->save();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/VerifyWebhookSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Foundation\Application;
use Stripe\Error\SignatureVerification;
use Stripe\Exception\SignatureVerificationException;
use Stripe\WebhookSignature;

class VerifyWebhookSignature
Expand Down Expand Up @@ -53,7 +53,7 @@ public function handle($request, Closure $next)
$this->config->get('cashier.webhook.secret'),
$this->config->get('cashier.webhook.tolerance')
);
} catch (SignatureVerification $exception) {
} catch (SignatureVerificationException $exception) {
$this->app->abort(403);
}

Expand Down
25 changes: 16 additions & 9 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Carbon\Carbon;
use Dompdf\Dompdf;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
use Stripe\Invoice as StripeInvoice;
use Stripe\InvoiceLineItem as StripeInvoiceLineItem;
use Symfony\Component\HttpFoundation\Response;

class Invoice
Expand All @@ -24,6 +26,13 @@ class Invoice
*/
protected $invoice;

/**
* The Stripe invoice items.
*
* @var \Stripe\Collection|\Stripe\InvoiceLineItem[]
*/
protected $items;

/**
* Create a new invoice instance.
*
Expand Down Expand Up @@ -220,17 +229,15 @@ public function subscriptions()
*/
public function invoiceItemsByType($type)
{
$lineItems = [];

if (isset($this->lines->data)) {
foreach ($this->lines->data as $line) {
if ($line->type == $type) {
$lineItems[] = new InvoiceItem($this->owner, $line);
}
}
if (is_null($this->items)) {
$this->items = new Collection($this->lines->autoPagingIterator());
}

return $lineItems;
return $this->items->filter(function (StripeInvoiceLineItem $item) use ($type) {
return $item->type === $type;
})->map(function (StripeInvoiceLineItem $item) {
return new InvoiceItem($this->owner, $item);
})->all();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Laravel\Cashier\Tests\Fixtures\User;
use Laravel\Cashier\Tests\TestCase;
use Stripe\ApiResource;
use Stripe\Error\InvalidRequest;
use Stripe\Exception\InvalidRequestException;
use Stripe\Stripe;

abstract class IntegrationTestCase extends TestCase
Expand Down Expand Up @@ -38,7 +38,7 @@ protected static function deleteStripeResource(ApiResource $resource)
{
try {
$resource->delete();
} catch (InvalidRequest $e) {
} catch (InvalidRequestException $e) {
//
}
}
Expand Down