PHP code example of rabol / laravel-simplesubscription-stripe
1. Go to this page and download the library: Download rabol/laravel-simplesubscription-stripe library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rabol / laravel-simplesubscription-stripe example snippets
return [
'stripe_key' => env('STRIPE_KEY'),
'stripe_secret' => env('STRIPE_SECRET'),
'stripe_webhook_secret' => env('STRIPE_WEBHOOK_SECRET'), // Web hook secret
'stripe_webhook_tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300) // max time diff in webhook signature
];
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Rabol\LaravelSimpleSubscriptionStripe\LaravelSimpleSubscriptionStripe;
use Auth;
class StripeController extends Controller
{
public function index()
{
return view('stripe.index')
->with('stripePrices', LaravelSimpleSubscriptionStripe::stripe()->prices->all());
}
public function gotoStripeCustomerPortal(Request $request)
{
return redirect(LaravelSimpleSubscriptionStripe::gotoStripeCustomerPortal(Auth::user(), route('stripe.index')));
}
public function checkout(Request $request)
{
$user = Auth::user();
if (!$user->stripe_id) {
$options = [
'email' => $user->email,
'name' => $user->name,
'address' => [
'city' => $user->city ?? '',
'line1' => $user->adr_line_1 ?? '',
'line2' => $user->adr_line_2 ?? '',
'postal_code' => $user->postal_code ?? '',
'country' => $user->country ?? '',
'state' => $user->state ?? '',
]];
// If the user should be Tax Exempt, and the information to the options array
LaravelSimpleSubscriptionStripe::createAsStripeCustomer($options);
}
$priceId = $request->input('priceId');
$session = LaravelSimpleSubscriptionStripe::createCheckoutSession([
'allow_promotion_codes' => true,
'success_url' => config('app.url') . '/stripe/success/{CHECKOUT_SESSION_ID}',
'cancel_url' => config('app.url') . '/stripe/canceled',
'customer' => $user->stripe_id,
'customer_update' => [
'name' => 'auto',
'address' => 'auto',
],
'payment_method_types' => [
'card'
],
'mode' => 'subscription',
'tax_id_collection' => [
'enabled' => true
],
'line_items' => [[
'price' => $priceId,
// For metered billing, do not pass quantity
'quantity' => 1,
]],
'subscription_data' => [
'metadata' => ['name' => 'Advanced'],
//'default_tax_rates' => ['txr_xxxxxxxx'] // get the tax id from the Stripe dashboard
]
]);
return redirect()->to($session->url);
}
public function cancled(Request $request)
{
return view('stripe.cancled')
->with('request', $request);
}
public function success(Request $request, string $session_id)
{
$session = LaravelSimpleSubscriptionStripe::stripe()->checkout->sessions->retrieve($session_id);
$customer = LaravelSimpleSubscriptionStripe::stripe()->customers->retrieve($session->customer);
return view('stripe.success')
->with('session', $session)
->with('customer', $customer);
}
}
bash
php artisan vendor:publish --provider="Rabol\LaravelSimpleSubscriptionStripe\LaravelSimpleSubscriptionStripeServiceProvider" --tag="laravel-simplesubscription-stripe-migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Rabol\LaravelSimpleSubscriptionStripe\LaravelSimpleSubscriptionStripeServiceProvider" --tag="laravel-simplesubscription-stripe-config"
namespace App\Http\Controllers;
use App\Models\User;
use Rabol\LaravelSimpleSubscriptionStripe\Http\Controllers\WebhookController;
class StripeWebhookController extends WebhookController
{
public function handleCustomerSubscriptionCreatedEvent($event)
{
$subscription = $event->data->object;
$user = User::where('stripe_id',$subscription->customer)->first();
// provision the subscription in the app
return Response('All ok', 200);
}
}