@yacinehamiane - So what exactly is your question? This looks like a pretty standard Cashier/Stripe implementation.
Jan 21, 2024
4
Level 1
make payment online with stripe
I worked on some project with Laravel, and this is the first time that I am faced with this kind of work. I followed the documentation but I'm a little lost.
- I installed laravel cashier
- I configured the User model and .env and config/service.php by adding STRIPE_KEY and STRIPE_SECRET . and here is my code :
<form action="{{ route('process-payment', ['bookingId' => $bookingId]) }}" method="post" class="contact-form">
@csrf
<h4 class="black p-0 mb-24">Account Holder Details</h4>
<div class="row form-group">
<input id="card-holder-name" class="form-control" type="text" placeholder="card holder name">
<input type="hidden" name="payment_method_id" id="payment_method_id" value="1">
<!-- Stripe Elements Placeholder -->
<div id="card-element" style="padding: 15px;"></div>
<button id="card-button" class="cus-btn btn-sec w-100 mt-5">
Process Payment
</button>
</div>
</form>
@section('js')
<script>
const stripe = Stripe('{{ $stripePublicKey }}');
//console.log(stripe)
const elements = stripe.elements();
const cardElement = elements.create('card');
cardElement.mount('#card-element');
const cardHolderName = document.getElementById('card-holder-name');
const cardButton = document.getElementById('card-button');
cardButton.addEventListener('click', async (e) => {
const { paymentMethod, error } = await stripe.createPaymentMethod(
'card', cardElement, {
billing_details: { name: cardHolderName.value }
}
);
if (error) {
// Affiche "error.message" à l'utilisateur en cas d'erreur...
alert("Erreur lors du traitement du paiement: " + error.message);
} else {
// La carte a été vérifiée avec succès...
document.querySelector(".contact-form").submit();
alert("Le paiement a été traité avec succès!");
}
});
</script>
@endsection
that alerts me to success, but I think he is only checking the coordinates of the card
controller :
public function processPayment(Request $request, $bookingId)
{
// Récupérer la clé secrète de votre fichier .env
//dd('wesh ya');
$stripeSecretKey = config('services.stripe.secret');
// Initialiser Stripe avec la clé secrète
Stripe::setApiKey($stripeSecretKey);
//dd($amount);
try {
// Récupérer le montant dynamique depuis la base de données ou ailleurs
$reservation = Reservation::find($bookingId);
//dd($bookingId);
$booking = BookedFlight::find($reservation->booked_flight_id);
$amount = $booking->price * 100; // Convertir le prix en cents
// Créer un PaymentIntent avec le montant dynamique
$intent = PaymentIntent::create([
'amount' => $amount,
'currency' => 'eur',
'payment_method' => $request->input('payment_method_id'),
'confirmation_method' => 'manual',
'confirm' => true,
]);
//$intent = $intent->confirm();
// Enregistrez les détails du paiement dans la table "payments"
$payment = new Payment();
$payment->reservation_id = $reservation->id; // Assurez-vous d'avoir la colonne "reservation_id" dans votre table "payments"
$payment->payment_method_id = $request->input('payment_method_id');
$payment->user_id = auth()->id(); // Convertir le montant en euros
$payment->save();
// Envoyer la réponse de confirmation au client
return response()->json(['clientSecret' => $intent->client_secret]);
} catch (\Exception $e) {
// Gérer les erreurs de manière appropriée
return response()->json(['error' => $e->getMessage()], 500);
}
}
route :
Route::post('/process-payment/{bookingId}', [BookingController::class, 'processPayment'])->name('process-payment');```
Please or to participate in this conversation.