Skip to content
Snippets Groups Projects
Commit 2ecc4c17 authored by paula_rodriguezslash's avatar paula_rodriguezslash
Browse files

almost done (just seat error left)

parent 50e5e0f8
No related branches found
No related tags found
No related merge requests found
...@@ -142,6 +142,7 @@ namespace BookingMicroservice.Controllers ...@@ -142,6 +142,7 @@ namespace BookingMicroservice.Controllers
} }
} }
[Authorize]
[HttpPost("create-payment-intent")] [HttpPost("create-payment-intent")]
public async Task<IActionResult> CreatePaymentIntent([FromBody] PaymentIntentCreateRequest request) public async Task<IActionResult> CreatePaymentIntent([FromBody] PaymentIntentCreateRequest request)
{ {
......
...@@ -24,7 +24,6 @@ function BookingCard({ flight }: IBookingCard) { ...@@ -24,7 +24,6 @@ function BookingCard({ flight }: IBookingCard) {
price: isEconomy ? flight.economyPrice : flight.businessPrice price: isEconomy ? flight.economyPrice : flight.businessPrice
}; };
try { try {
//await bookFlight(bookingInfo);
alert("Flight booked successfully, navigating to payments page..."); alert("Flight booked successfully, navigating to payments page...");
navigate('/booking/payment-form', { state: { bookingInfo } }); navigate('/booking/payment-form', { state: { bookingInfo } });
} catch (error) { } catch (error) {
......
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom'; import { useLocation, useNavigate } from "react-router-dom";
import Api from "../../helpers/Api"; import Api from "../../helpers/Api";
import { CardElement, useStripe, useElements, Elements } from '@stripe/react-stripe-js'; import { CardElement, useStripe, useElements, Elements } from '@stripe/react-stripe-js';
import { loadStripe } from '@stripe/stripe-js'; import { loadStripe } from '@stripe/stripe-js';
import axios from 'axios'; import {bookFlight} from "../../services/BookingList/BookingOrder";
import './PaymentForm.scss'; import './PaymentForm.scss';
const stripePromise = loadStripe('pk_test_51P5UhOExQclpActcEGkHdut1X1k6uQnEXQ2cKKTD5l9FS9a1TyB2ap1lRSQXZt35Dpd7mh8gHOwFyVb4TiqpZfJr00bDRrD4vF'); const stripePromise = loadStripe('pk_test_51P5UhOExQclpActcEGkHdut1X1k6uQnEXQ2cKKTD5l9FS9a1TyB2ap1lRSQXZt35Dpd7mh8gHOwFyVb4TiqpZfJr00bDRrD4vF');
...@@ -12,6 +12,7 @@ const PaymentForm = () => { ...@@ -12,6 +12,7 @@ const PaymentForm = () => {
const stripe = useStripe(); const stripe = useStripe();
const elements = useElements(); const elements = useElements();
const location = useLocation(); const location = useLocation();
const navigate = useNavigate();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [bookingInfo, setBookingInfo] = useState(null); const [bookingInfo, setBookingInfo] = useState(null);
const [message, setMessage] = useState(""); const [message, setMessage] = useState("");
...@@ -24,38 +25,62 @@ const PaymentForm = () => { ...@@ -24,38 +25,62 @@ const PaymentForm = () => {
}, [location]); }, [location]);
useEffect(() => { useEffect(() => {
if (!bookingInfo || clientSecret) return; if (!stripe || !bookingInfo) return;
const fetchClientSecret = async () => { const fetchClientSecret = async () => {
try { try {
let price = bookingInfo.price; const form = { items: [{ price: bookingInfo.price * 100 }] };
let priceInCents = price * 100; const response = await Api.post("Booking/create-payment-intent", form, { withCredentials: true });
const form = { items: [{ price: priceInCents }] };
const response = await Api.post("Booking/create-payment-intent", form);
setClientSecret(response.data.clientSecret); setClientSecret(response.data.clientSecret);
setLoading(false);
} catch (error) { } catch (error) {
console.error('Failed to retrieve client secret:', error); console.error('Failed to retrieve client secret:', error);
setLoading(false); setMessage("Failed to retrieve Stripe secret key");
} }
}; };
fetchClientSecret(); fetchClientSecret();
}, [bookingInfo, clientSecret]); }, [stripe, bookingInfo]);
const handleSubmit = async (event) => { const handleSubmit = async (event) => {
event.preventDefault(); event.preventDefault();
if (!stripe || !elements || !clientSecret) { if (!stripe || !elements || !clientSecret) {
console.log('Stripe.js has not yet loaded or client secret not available.'); console.log('Stripe.js has not yet loaded or client secret not available.');
return; return;
} }
setLoading(true);
try {
const { error, paymentIntent } = await stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: elements.getElement(CardElement),
},
});
if (error) {
setMessage(`Payment failed: ${error.message}`);
} else if (paymentIntent && paymentIntent.status === 'succeeded') {
setMessage("Payment succeeded!");
const updatedBookingInfo = {
...bookingInfo,
paymentIntentId: paymentIntent.id
};
setBookingInfo(updatedBookingInfo);
await bookFlight(updatedBookingInfo);
alert("Flight booked successfully!");
navigate('/customer/bookings');
setLoading(true); } else {
setMessage(`Payment processed but check status: ${paymentIntent.status}`);
}
} catch (error) {
setLoading(false); console.error('Payment confirmation failed:', error);
setMessage("An error occurred while confirming the payment.");
} finally {
setLoading(false);
}
}; };
return ( return (
...@@ -63,7 +88,10 @@ const PaymentForm = () => { ...@@ -63,7 +88,10 @@ const PaymentForm = () => {
<form onSubmit={handleSubmit} className="payment-form"> <form onSubmit={handleSubmit} className="payment-form">
<h3 id="form-heading">Last step!</h3> <h3 id="form-heading">Last step!</h3>
<p id="form-description">Enter your payment info below.</p> <p id="form-description">Enter your payment info below.</p>
<CardElement className="StripeElement" /> <CardElement className="StripeElement" onChange={event => {
setDisabled(event.empty);
setMessage(event.error ? event.error.message : "");
}} />
<button type="submit" className="button" disabled={!stripe || loading}> <button type="submit" className="button" disabled={!stripe || loading}>
{loading ? 'Processing...' : 'Pay'} {loading ? 'Processing...' : 'Pay'}
</button> </button>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment