✨
Custom Actions
Even if the scaffolding comes with basic controller logic to handle new subscriptions or manage existing ones, you are free to change the Billing Portal's actions in app/Actions/BillingPortal.
You just have to re-write your logic in the action methods and Cashierstream will automatically handle it for you.
1
namespace App\Actions\BillingPortal;
2
​
3
use Illuminate\Http\Request;
4
use RenokiCo\BillingPortal\BillingPortal;
5
use RenokiCo\BillingPortal\Contracts\HandleSubscriptions as HandleSubscriptionsContract;
6
use RenokiCo\CashierRegister\Plan;
7
​
8
class HandleSubscriptions implements HandleSubscriptionsContract
9
{
10
/**
11
* Mutate the checkout object before redirecting the user to subscribe to a certain plan.
12
*
13
* @param \Illuminate\Database\Eloquent\Model $billable
14
* @param \RenokiCo\CashierRegister\Plan $plan
15
* @param \Illuminate\Http\Request $request
16
* @return mixed
17
*/
18
public function checkoutOnSubscription($subscription, $billable, Plan $plan, Request $request)
19
{
20
return $subscription->checkout([
21
'success_url' => route('billing-portal.subscription.index', ['success' => "You have successfully subscribed to {$plan->getName()}!"]),
22
'cancel_url' => route('billing-portal.subscription.index', ['error' => "The subscription to {$plan->getName()} was cancelled!"]),
23
]);
24
}
25
​
26
/**
27
* Subscribe the user to a given plan.
28
*
29
* @param \Illuminate\Database\Eloquent\Model $billable
30
* @param \RenokiCo\CashierRegister\Plan $plan
31
* @param \Illuminate\Http\Request $request
32
* @return \RenokiCo\CashierRegister\Models\Stripe\Subscription
33
*/
34
public function subscribeToPlan($billable, Plan $plan, Request $request)
35
{
36
return $billable
37
->newSubscription($request->subscription, $plan->getId())
38
->create($billable->defaultPaymentMethod()->id);
39
}
40
​
41
/**
42
* Swap the current subscription plan.
43
*
44
* @param \RenokiCo\CashierRegister\Models\Stripe\Subscription $subscription
45
* @param \Illuminate\Database\Eloquent\Model $billable
46
* @param \RenokiCo\CashierRegister\Plan $plan
47
* @param \Illuminate\Http\Request $request
48
* @return \RenokiCo\CashierRegister\Models\Stripe\Subscription
49
*/
50
public function swapToPlan($subscription, $billable, Plan $plan, Request $request)
51
{
52
if (BillingPortal::proratesOnSwap()) {
53
return $subscription->swap($plan->getId());
54
}
55
​
56
return $subscription->noProrate()->swap($plan->getId());
57
}
58
​
59
/**
60
* Define the logic to be called when the user requests resuming a subscription.
61
*
62
* @param \RenokiCo\CashierRegister\Models\Stripe\Subscription $subscription
63
* @param \Illuminate\Database\Eloquent\Model $billable
64
* @param \Illuminate\Http\Request $request
65
* @return void
66
*/
67
public function resumeSubscription($subscription, $billable, Request $request)
68
{
69
$subscription->resume();
70
}
71
​
72
/**
73
* Define the subscriptioncancellation action.
74
*
75
* @param \RenokiCo\CashierRegister\Models\Stripe\Subscription $subscription
76
* @param \Illuminate\Database\Eloquent\Model $billable
77
* @param \Illuminate\Http\Request $request
78
* @return void
79
*/
80
public function cancelSubscription($subscription, $billable, Request $request)
81
{
82
$subscription->cancel();
83
}
84
}
Copied!
Copy link
Edit on GitHub