From 9679ef58050d7eeea2be22e78500e2935e9982c6 Mon Sep 17 00:00:00 2001 From: Ariel Michalik Date: Sun, 11 Jan 2026 11:46:19 +0100 Subject: [PATCH] added support for payment links --- src/api/payment-service.ts | 54 ++++++++++++++++++++++++++++++++++++++ src/pages/PaymentPage.tsx | 3 +++ 2 files changed, 57 insertions(+) create mode 100644 src/api/payment-service.ts diff --git a/src/api/payment-service.ts b/src/api/payment-service.ts new file mode 100644 index 0000000..dc14e28 --- /dev/null +++ b/src/api/payment-service.ts @@ -0,0 +1,54 @@ +import { API_BASE_URL, apiCall } from "./utils"; + +export const getPaymentLink = async (orderId: string): Promise => { + 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 => { + 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; + } +}; diff --git a/src/pages/PaymentPage.tsx b/src/pages/PaymentPage.tsx index 894a9b0..ec0b01c 100644 --- a/src/pages/PaymentPage.tsx +++ b/src/pages/PaymentPage.tsx @@ -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(); @@ -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 {