Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Replaced getBillableFromRequest with resolveBillable
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed May 11, 2021
1 parent bf91616 commit 1794790
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/BillingPortal.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function setBillableOnRequest(Closure $callback)
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public static function getBillableFromRequest(Request $request)
public static function resolveBillable(Request $request)
{
$closure = static::$billableOnRequest;

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Inertia/BillingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BillingController extends Controller
public function portal(Request $request)
{
return $this->getBillingPortalRedirect(
BillingPortal::getBillableFromRequest($request)
BillingPortal::resolveBillable($request)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Inertia/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InvoiceController extends Controller
*/
public function index(Request $request)
{
$invoices = BillingPortal::getBillableFromRequest($request)->invoices()->map(function ($invoice) {
$invoices = BillingPortal::resolveBillable($request)->invoices()->map(function ($invoice) {
return [
'description' => $invoice->lines->data[0]->description,
'created' => $invoice->created,
Expand Down
52 changes: 27 additions & 25 deletions src/Http/Controllers/Inertia/PaymentMethodController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PaymentMethodController extends Controller
public function __construct(Request $request)
{
$this->middleware(function (Request $request, Closure $next) {
BillingPortal::getBillableFromRequest($request)->createOrGetStripeCustomer();
BillingPortal::resolveBillable($request)->createOrGetStripeCustomer();

return $next($request);
});
Expand All @@ -35,24 +35,24 @@ public function __construct(Request $request)
*/
public function index(Request $request)
{
BillingPortal::getBillableFromRequest($request)->updateDefaultPaymentMethodFromStripe();

$defaultPaymentMethod = BillingPortal::getBillableFromRequest($request)->defaultPaymentMethod();

$methods = BillingPortal::getBillableFromRequest($request)
->paymentMethods()
->filter(function ($method) {
return $method->type === 'card';
})->map(function ($method) use ($defaultPaymentMethod) {
return [
'default' => $method->id === optional($defaultPaymentMethod)->id,
'id' => $method->id,
'brand' => $method->card->brand,
'last_four' => $method->card->last4,
'month' => $method->card->exp_month,
'year' => $method->card->exp_year,
];
});
$billable = BillingPortal::resolveBillable($request);

$billable->updateDefaultPaymentMethodFromStripe();

$defaultPaymentMethod = $billable->defaultPaymentMethod();

$methods = $billable->paymentMethods()->filter(function ($method) {
return $method->type === 'card';
})->map(function ($method) use ($defaultPaymentMethod) {
return [
'default' => $method->id === optional($defaultPaymentMethod)->id,
'id' => $method->id,
'brand' => $method->card->brand,
'last_four' => $method->card->last4,
'month' => $method->card->exp_month,
'year' => $method->card->exp_year,
];
});

return Inertia::render('BillingPortal/PaymentMethod/Index', [
'methods' => $methods,
Expand All @@ -68,7 +68,7 @@ public function index(Request $request)
public function create(Request $request)
{
return Inertia::render('BillingPortal/PaymentMethod/Create', [
'intent' => BillingPortal::getBillableFromRequest($request)->createSetupIntent(),
'intent' => BillingPortal::resolveBillable($request)->createSetupIntent(),
'stripe_key' => config('cashier.key'),
]);
}
Expand All @@ -85,10 +85,12 @@ public function store(Request $request)
'token' => ['required', 'string'],
]);

BillingPortal::getBillableFromRequest($request)->addPaymentMethod($request->token);
$billable = BillingPortal::resolveBillable($request);

$billable->addPaymentMethod($request->token);

if (! BillingPortal::getBillableFromRequest($request)->hasDefaultPaymentMethod()) {
BillingPortal::getBillableFromRequest($request)->updateDefaultPaymentMethod($request->token);
if (! $billable->hasDefaultPaymentMethod()) {
$billable->updateDefaultPaymentMethod($request->token);
}

return Redirect::route('billing-portal.payment-method.index')
Expand All @@ -105,7 +107,7 @@ public function store(Request $request)
public function destroy(Request $request, string $paymentMethod)
{
try {
$paymentMethod = BillingPortal::getBillableFromRequest($request)->findPaymentMethod($paymentMethod);
$paymentMethod = BillingPortal::resolveBillable($request)->findPaymentMethod($paymentMethod);
} catch (Exception $e) {
return Redirect::route('billing-portal.payment-method.index')
->with('success', 'The payment method got removed!');
Expand All @@ -129,7 +131,7 @@ public function destroy(Request $request, string $paymentMethod)
public function setDefault(Request $request, string $paymentMethod)
{
try {
BillingPortal::getBillableFromRequest($request)->updateDefaultPaymentMethod($paymentMethod);
BillingPortal::resolveBillable($request)->updateDefaultPaymentMethod($paymentMethod);
} catch (Exception $e) {
return Redirect::route('billing-portal.payment-method.index')
->with('success', 'The default payment method got updated!');
Expand Down
26 changes: 13 additions & 13 deletions src/Http/Controllers/Inertia/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(Request $request)
*/
public function index(Request $request)
{
$user = BillingPortal::getBillableFromRequest($request);
$user = BillingPortal::resolveBillable($request);

$subscription = $this->getCurrentSubscription($user, $request->subscription);

Expand All @@ -58,7 +58,7 @@ public function index(Request $request)
*/
public function subscribeToPlan(Request $request, string $planId)
{
$user = BillingPortal::getBillableFromRequest($request);
$user = BillingPortal::resolveBillable($request);

$plan = Saas::getPlan($planId);

Expand Down Expand Up @@ -89,23 +89,23 @@ public function swapPlan(Request $request, string $newPlanId)
{
$plan = Saas::getPlan($newPlanId);

$user = BillingPortal::getBillableFromRequest($request);
$billable = BillingPortal::resolveBillable($request);

$subscription = $this->getCurrentSubscription($user, $request->subscription);
$subscription = $this->getCurrentSubscription($billable, $request->subscription);

if ($plan->getPrice() > 0.00 && ! $user->defaultPaymentMethod()) {
if ($plan->getPrice() > 0.00 && ! $billable->defaultPaymentMethod()) {
return $this->subscribeToPlan($request, $newPlanId);
}

if (! $user->subscribed($subscription->name, $plan->getId())) {
if (! $billable->subscribed($subscription->name, $plan->getId())) {
$hasValidSubscription = $subscription && $subscription->valid();

$subscription = $hasValidSubscription
? $subscription->swap($newPlanId)
: $user->newSubscription($request->subscription, $newPlanId)->create(optional($user->defaultPaymentMethod())->id);
: $billable->newSubscription($request->subscription, $newPlanId)->create(optional($billable->defaultPaymentMethod())->id);
}

BillingPortal::syncQuotas(BillingPortal::getBillableFromRequest($request), $subscription);
BillingPortal::syncQuotas($billable, $subscription);

return Redirect::route('billing-portal.subscription.index')
->with('success', "The plan got successfully changed to {$plan->getName()}!");
Expand All @@ -119,15 +119,15 @@ public function swapPlan(Request $request, string $newPlanId)
*/
public function resumeSubscription(Request $request)
{
$user = BillingPortal::getBillableFromRequest($request);
$billable = BillingPortal::resolveBillable($request);

$subscription = $this->getCurrentSubscription($user, $request->subscription);
$subscription = $this->getCurrentSubscription($billable, $request->subscription);

if ($subscription->onGracePeriod()) {
$subscription->resume();
}

BillingPortal::syncQuotas(BillingPortal::getBillableFromRequest($request), $subscription);
BillingPortal::syncQuotas($billable, $subscription);

return Redirect::route('billing-portal.subscription.index')
->with('success', 'The subscription has been resumed.');
Expand All @@ -141,15 +141,15 @@ public function resumeSubscription(Request $request)
*/
public function cancelSubscription(Request $request)
{
$user = BillingPortal::getBillableFromRequest($request);
$billable = BillingPortal::resolveBillable($request);

$subscription = $this->getCurrentSubscription($user, $request->subscription);

if ($subscription->recurring()) {
$subscription->cancel();
}

BillingPortal::syncQuotas(BillingPortal::getBillableFromRequest($request), $subscription);
BillingPortal::syncQuotas($billable, $subscription);

return Redirect::route('billing-portal.subscription.index')
->with('success', 'The current subscription got cancelled!');
Expand Down

0 comments on commit 1794790

Please sign in to comment.