Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/api/payment-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { API_BASE_URL, apiCall } from "./utils";

export const getPaymentLink = async (orderId: string): Promise<string> => {
const jwtToken = localStorage.getItem("token");

if (!jwtToken) {
throw new Error("Authorization token not found. Please log in.");
}

try {
const response = await apiCall(`${API_BASE_URL}/api/payment/order/${orderId}`, {
method: "GET",
headers: {
Authorization: `Bearer ${jwtToken}`,
"Content-Type": "application/json",
},
});

if (!response.ok) {
throw new Error(`Failed to get payment link: ${response.status} ${response.statusText}`);
}

const data = await response.json();
return data.paymentLink;
} catch (error) {
console.error("API Error getting payment link:", error);
throw error;
}
};

export const processPayment = async (paymentLink: string): Promise<void> => {
const jwtToken = localStorage.getItem("token");

if (!jwtToken) {
throw new Error("Authorization token not found. Please log in.");
}

try {
const response = await apiCall(`${API_BASE_URL}/api/payment/${paymentLink}`, {
method: "POST",
headers: {
Authorization: `Bearer ${jwtToken}`,
"Content-Type": "application/json",
},
});

if (!response.ok) {
throw new Error(`Failed to process payment: ${response.status} ${response.statusText}`);
}
} catch (error) {
console.error("API Error processing payment:", error);
throw error;
}
};
3 changes: 3 additions & 0 deletions src/pages/PaymentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PaymentSummary from "./payment/PaymentSummary.tsx";
import CreditCardIcon from "@mui/icons-material/CreditCard";
import { Box, Typography, Container, Grid, Alert, Card, Button } from "@mui/material";
import { useNavigate, useParams } from "react-router-dom";
import { getPaymentLink, processPayment } from "../api/payment-service.ts";

const PaymentPage: React.FC = () => {
const navigate = useNavigate();
Expand All @@ -33,6 +34,8 @@ const PaymentPage: React.FC = () => {
await clearFullCart();
// Redirect to order confirmation page with security token
if (orderId) {
const paymentLink = await getPaymentLink(orderId);
await processPayment(paymentLink);
const confirmationToken = crypto.randomUUID();
navigate(`/order-confirmation/${orderId}/${confirmationToken}`);
} else {
Expand Down
Loading