PHP code example of lab404 / laravel-stripe-server
1. Go to this page and download the library: Download lab404/laravel-stripe-server 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/ */
lab404 / laravel-stripe-server example snippets
use App\Models\Order;
use Lab404\StripeServer\Facades\Stripe;
use Illuminate\Http\Request;
class OrderController
{
public function store(Request $request)
{
// Create your order
$order = new Order($request->validated());
$order->save();
// Create your checkout session
$session = Stripe::requestCreateSession()
->setCustomerEmail($request->user()->email)
->setReturnUrls($confirm_url, $cancel_url)
->setProduct('T-shirt', 5000, 'EUR', 1, $picture_url)
->call();
// Create your checkout model
$checkout = Stripe::registerCheckout($session, $order);
return Stripe::redirectSession($response->id);
}
}
use Lab404\StripeServer\Events\CheckoutSessionCompleted;
use Lab404\StripeServer\Models\StripeCheckout;
class CheckoutListener
{
public function handle(CheckoutSessionCompleted $event): void
{
/** @var StripeCheckout $checkout */
$checkout = $event->checkout;
/** Your charged model */
$chargeable = $checkout->chargeable;
/** The PaymentIntent returned by Stripe */
$payment = $event->paymentIntent;
/** Important! Mark the checkout as paid */
$checkout->markAsPaid();
}
}
$order = Order::with('checkout')->first();
if ($order->checkout->is_paid) {
echo 'Order is paid';
} else {
echo 'Order is not paid';
}
protected function schedule(Schedule $schedule)
{
$schedule->command('stripe:checkout-session-completed')->everyMinute();
}
Stripe::method();
// Scopes
Model::hasCheckouts()->get();
Model::hasPaidCheckouts()->get();
Model::hasUnpaidCheckouts()->get();
// Methods
$models->checkouts(); // returns all checkout for the model
// Eager loading
$models = Model::with('checkouts')->get();
// Scopes
Model::hasCheckout()->get();
Model::hasPaidCheckout()->get();
Model::hasUnpaidCheckout()->get();
// Methods
$models->checkout(); // returns the checkout for the model
// Eager loading
$models = Model::with('checkout')->get();