Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support upgrading of user's plan through Stripe
- Loading branch information
1 parent
94db5ac
commit 416443e
Showing
14 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use App\Exceptions\UserNotFoundException; | ||
use App\Exceptions\UserStripelessException; | ||
use App\Models\User; | ||
use Stripe\StripeClient; | ||
|
||
class CreateStripeCheckoutAction | ||
{ | ||
public function execute(int $userId): string | ||
{ | ||
$user = User::find($userId); | ||
|
||
if (!$user) { | ||
throw new UserNotFoundException(); | ||
} | ||
|
||
if (!$user->stripe_customer_id) { | ||
throw new UserStripelessException(); | ||
} | ||
|
||
$stripe = new StripeClient(config('stripe.secret')); | ||
|
||
$stripeCheckout = $stripe->checkout->sessions->create([ | ||
'success_url' => route('settings.billing'), | ||
'cancel_url' => route('settings.billing'), | ||
'mode' => 'subscription', | ||
'payment_method_types' => ['card'], | ||
'customer' => $user->stripe_customer_id, | ||
'line_items' => [ | ||
[ | ||
'price' => config('stripe.premium_plan_price_id'), | ||
'quantity' => 1 | ||
] | ||
] | ||
]); | ||
|
||
return $stripeCheckout->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use App\Exceptions\UserNotFoundException; | ||
use App\Models\User; | ||
use Stripe\StripeClient; | ||
|
||
class CreateStripeCustomerAction | ||
{ | ||
public function execute(int $userId): void | ||
{ | ||
$user = User::find($userId); | ||
|
||
if (!$user) { | ||
throw new UserNotFoundException(); | ||
} | ||
|
||
$stripe = new StripeClient(config('stripe.secret')); | ||
|
||
$customer = $stripe->customers->create([ | ||
'name' => $user->name, | ||
'email' => $user->email | ||
]); | ||
|
||
$user->update([ | ||
'stripe_customer_id' => $customer->id | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class UserStripelessException extends Exception | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
return [ | ||
'key' => env('STRIPE_KEY'), | ||
'secret' => env('STRIPE_SECRET'), | ||
'premium_plan_price_id' => env('STRIPE_PREMIUM_PLAN_PRICE_ID') | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<script src="https://js.stripe.com/v3"></script> | ||
<script> | ||
var stripe = Stripe("{{ config('stripe.key') }}"); | ||
stripe.redirectToCheckout({ | ||
sessionId: '{{ $stripeCheckoutId }}' | ||
}).then(function (result) { | ||
// If `redirectToCheckout` fails due to a browser or network | ||
// error, display the localized error message to your customer | ||
// using `result.error.message`. | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters