-
composer.json
Open in GitHub{ "require": { "php": "^8.0", // "stripe/stripe-php": "^7.52" }, }
-
resources/views/front/payments/partials/stripePayment.blade.php
Open in GitHub<p>Pay with Stripe:</p> <form action="/payments" method="POST"> <input type="hidden" name="amount" value="{{ session()->get('amount') }}"> @csrf <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="{{ config('settings.stripe.key') }}" data-amount="{{ session()->get('amount') }}" data-name="freek.dev ads" data-locale="auto" data-label="Pay {{ session()->get('amount') / 100 }} EUR to freek.dev" data-zip-code="true" data-currency="eur"> </script> </form> <a href="/payments" class="font-semibold text-gray-700 pb-1 border-b-2"> Change amount </a>
-
app/Http/Controllers/PaymentsController.php
Open in GitHubuse App\Mail\PaymentFailed; use App\Mail\PaymentSuccessfulMail; use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\Mail; use Stripe\Charge; use Stripe\Customer; use Stripe\Stripe; class PaymentsController { // public function setAmount(Request $request) { $request->validate([ 'amount' => 'numeric|between:1,9999', ]); session()->flash('amount', $request->amount * 100); return redirect()->action([PaymentsController::class, 'index']); } public function handlePayment(Request $request) { try { $this->performPayment($request); } catch (Exception $exception) { flash()->error('There was a problem processing your payment.'); Mail::to('freek@spatie.be')->queue(new PaymentFailed( $request->stripeEmail, $request->amount, $exception->getMessage() . $exception->getTraceAsString() )); return redirect()->action([PaymentsController::class, 'index']); } Mail::to('freek@spatie.be')->queue(new PaymentSuccessfulMail( $request->stripeEmail, $request->amount )); flash()->success('Your payment was successful! Thank you!'); return redirect()->action([PaymentsController::class, 'index']); } protected function performPayment(Request $request) { Stripe::setApiKey(config('settings.stripe.secret')); $customer = Customer::create([ 'email' => $request->stripeEmail, 'source' => $request->stripeToken, ]); Charge::create([ 'customer' => $customer->id, 'amount' => $request->amount, 'currency' => 'EUR', ]); } }
-
config/services.php
Open in GitHubreturn [ // 'stripe' => [ 'model' => App\Models\User::class, 'key' => env('STRIPE_KEY'), 'secret' => env('STRIPE_SECRET'), ], ];