Skip to content

Commit

Permalink
fix: fix stripe pages stability (#5161)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored May 3, 2021
1 parent e275f31 commit 53977cc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 52 deletions.
30 changes: 25 additions & 5 deletions app/Http/Controllers/Settings/SubscriptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Settings;

use Illuminate\View\View;
use App\Traits\StripeCall;
use App\Helpers\DateHelper;
use Illuminate\Http\Request;
use Laravel\Cashier\Cashier;
Expand All @@ -21,6 +22,8 @@

class SubscriptionsController extends Controller
{
use StripeCall;

/**
* Display a listing of the resource.
*
Expand Down Expand Up @@ -53,8 +56,14 @@ public function index()
$invoices = $account->invoices();
}

$planInformation = InstanceHelper::getPlanInformationFromConfig($subscription->name);

if ($planInformation === null) {
abort(404);
}

return view('settings.subscriptions.account', [
'planInformation' => InstanceHelper::getPlanInformationFromConfig($subscription->name),
'planInformation' => $planInformation,
'nextBillingDate' => $nextBillingDate,
'subscription' => $subscription,
'hasInvoices' => $hasInvoices,
Expand All @@ -80,9 +89,14 @@ public function upgrade(Request $request)
}

$plan = $request->query('plan');
$planInformation = InstanceHelper::getPlanInformationFromConfig($plan);

if ($planInformation === null) {
abort(404);
}

return view('settings.subscriptions.upgrade', [
'planInformation' => InstanceHelper::getPlanInformationFromConfig($plan),
'planInformation' => $planInformation,
'nextTheoriticalBillingDate' => DateHelper::getFullDate(DateHelper::getNextTheoriticalBillingDate($plan)),
'intent' => auth()->user()->account->createSetupIntent(),
]);
Expand All @@ -96,10 +110,16 @@ public function upgrade(Request $request)
*/
public function confirmPayment($id)
{
try {
$payment = $this->stripeCall(function () use ($id) {
return StripePaymentIntent::retrieve($id, Cashier::stripeOptions());
});
} catch (StripeException $e) {
return back()->withErrors($e->getMessage());
}

return view('settings.subscriptions.confirm', [
'payment' => new Payment(
StripePaymentIntent::retrieve($id, Cashier::stripeOptions())
),
'payment' => new Payment($payment),
'redirect' => request('redirect'),
]);
}
Expand Down
53 changes: 53 additions & 0 deletions app/Traits/StripeCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Traits;

use App\Exceptions\StripeException;
use Illuminate\Support\Facades\Log;

trait StripeCall
{
/**
* Call stripe.
*
* @param callable $callback
* @return mixed
*/
private function stripeCall($callback)
{
try {
return $callback();
} catch (\Stripe\Exception\CardException $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
$errorMessage = trans('settings.stripe_error_card', ['message' => $err['message']]);
Log::error('Stripe card decline error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\RateLimitException $e) {
// Too many requests made to the API too quickly
$errorMessage = trans('settings.stripe_error_rate_limit');
Log::error('Stripe RateLimit error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\InvalidRequestException $e) {
// Invalid parameters were supplied to Stripe's API
$errorMessage = trans('settings.stripe_error_invalid_request');
Log::error('Stripe InvalidRequest error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\AuthenticationException $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
$errorMessage = trans('settings.stripe_error_authentication');
Log::error('Stripe Authentication error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\ApiConnectionException $e) {
// Network communication with Stripe failed
$errorMessage = trans('settings.stripe_error_api_connection_error');
Log::error('Stripe ApiConnection error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\ApiErrorException $e) {
$errorMessage = $e->getMessage();
Log::error('Stripe error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
Log::error('Stripe error: '.(string) $e);
}

throw new StripeException($errorMessage);
}
}
51 changes: 4 additions & 47 deletions app/Traits/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use App\Helpers\DateHelper;
use Laravel\Cashier\Billable;
use App\Helpers\InstanceHelper;
use App\Exceptions\StripeException;
use Illuminate\Support\Facades\Log;

trait Subscription
{
use Billable;
use Billable, StripeCall;

/**
* Process the upgrade payment.
Expand All @@ -22,6 +20,9 @@ trait Subscription
public function subscribe(string $payment_method, string $planName)
{
$plan = InstanceHelper::getPlanInformationFromConfig($planName);
if ($plan === null) {
abort(404);
}

return $this->stripeCall(function () use ($payment_method, $plan) {
$this->newSubscription($plan['name'], $plan['id'])
Expand Down Expand Up @@ -141,48 +142,4 @@ public function getNextBillingDate()
return DateHelper::getFullDate($timestamp);
});
}

/**
* Call stripe.
*
* @param callable $callback
* @return mixed
*/
private function stripeCall($callback)
{
try {
return $callback();
} catch (\Stripe\Exception\CardException $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
$errorMessage = trans('settings.stripe_error_card', ['message' => $err['message']]);
Log::error('Stripe card decline error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\RateLimitException $e) {
// Too many requests made to the API too quickly
$errorMessage = trans('settings.stripe_error_rate_limit');
Log::error('Stripe RateLimit error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\InvalidRequestException $e) {
// Invalid parameters were supplied to Stripe's API
$errorMessage = trans('settings.stripe_error_invalid_request');
Log::error('Stripe InvalidRequest error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\AuthenticationException $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
$errorMessage = trans('settings.stripe_error_authentication');
Log::error('Stripe Authentication error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\ApiConnectionException $e) {
// Network communication with Stripe failed
$errorMessage = trans('settings.stripe_error_api_connection_error');
Log::error('Stripe ApiConnection error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Stripe\Exception\ApiErrorException $e) {
$errorMessage = $e->getMessage();
Log::error('Stripe error: '.(string) $e, $e->getJsonBody() ?: []);
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
Log::error('Stripe error: '.(string) $e);
}

throw new StripeException($errorMessage);
}
}

0 comments on commit 53977cc

Please sign in to comment.