Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 9x 9x 9x 9x 9x 9x 9x 10x 2x 2x 7x 10x 1x 1x 6x 6x 6x 6x 6x 6x 10x 1x 1x 5x 10x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 1x 1x 1x 1x 4x 4x 10x 1x 1x 10x 6x 6x 6x 6x 10x 10x 10x 35x 35x 35x 9x 9x 26x 26x 26x 35x 6x 6x 6x 6x 6x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x | "use client";
import React, { useState } from "react";
import {
CardElement,
useStripe,
useElements } from "@stripe/react-stripe-js";
import ErrorMessage from "@/components/ui/ErrorMessage";
import LoadingSpinner from "@/components/ui/LoadingSpinner";
import toast from "react-hot-toast";
import { clientLogger } from "@/lib/logging/clientLogger";
import { Button } from "@/components/ui";
import { formatCurrency } from "@/lib/core";
interface StripePaymentFormProps {
orderId: number;
amount: number;
clientSecret: string;
onSuccess: () => void;
onError: (error: string) => void;
}
export default function StripePaymentForm({
orderId,
amount,
clientSecret,
onSuccess,
onError}: StripePaymentFormProps) {
const stripe = useStripe();
const elements = useElements();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!stripe || !elements) {
const message = "Stripe not loaded";
setError(message);
onError(message);
return;
}
setLoading(true);
setError(null);
try {
// Get CardElement from elements
const cardElement = elements.getElement(CardElement);
if (!cardElement) {
throw new Error("Card element not found");
}
// Create payment method
const { error: createError, paymentMethod } =
await stripe.createPaymentMethod({
type: "card",
card: cardElement});
if (createError) {
throw new Error(createError.message);
}
if (!paymentMethod) {
throw new Error("Failed to create payment method");
}
// Confirm payment
const { error: confirmError, paymentIntent } =
await stripe.confirmCardPayment(clientSecret, {
payment_method: paymentMethod.id});
if (confirmError) {
throw new Error(confirmError.message);
}
if (paymentIntent?.status === "succeeded") {
toast.success("Payment successful!");
// Update order status via API
try {
const response = await fetch("/api/payments", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
orderId,
amount,
paymentMethod: "credit_card",
stripePaymentIntentId: paymentIntent.id})});
if (!response.ok) {
throw new Error("Failed to confirm payment on server");
}
} catch (apiError) {
clientLogger.error("Server-side payment confirmation failed", apiError instanceof Error ? apiError : new Error(String(apiError)));
// Even if server confirmation fails, Stripe payment succeeded
// You may want to handle this case separately
}
onSuccess();
} else {
throw new Error("Payment not completed. Please try again.");
}
} catch (err) {
const error = err instanceof Error ? err : new Error("Payment failed");
setError(error.message);
onError(error.message);
toast.error(error.message);
} finally {
setLoading(false);
}
};
if (loading) {
return <LoadingSpinner message="Processing payment..." />;
}
return (
<form onSubmit={handleSubmit} className="space-y-4">
{error && (
<ErrorMessage
title="Payment Error"
message={error}
onRetry={() => setError(null)}
/>
)}
<div className="space-y-3">
<label className="block font-medium text-dark">Card Details</label>
<div className="border border-gray-3 rounded-md p-4 bg-gray-1">
<CardElement
options={{
style: {
base: {
fontSize: "16px",
color: "#424770",
"::placeholder": {
color: "#aab7c4"}},
invalid: {
color: "#9e2146"}}}}
/>
</div>
</div>
<div className="bg-gray-1 border border-gray-3 rounded-md p-4">
<div className="flex justify-between mb-2">
<span className="text-gray-5">Subtotal</span>
<span className="font-medium">{formatCurrency(amount)}</span>
</div>
<div className="flex justify-between mb-3 pb-3 border-b border-gray-3">
<span className="text-gray-5">Shipping</span>
<span className="font-medium">Free</span>
</div>
<div className="flex justify-between">
<span className="font-bold text-dark">Total</span>
<span className="font-bold text-blue text-lg">
{formatCurrency(amount)}
</span>
</div>
</div>
<Button
type="submit"
disabled={loading || !stripe}
variant="primary"
size="md"
fullWidth
className="py-3"
>
{loading ? "Processing..." : `Pay ${formatCurrency(amount)}`}
</Button>
<p className="text-xs text-gray-5 text-center">
Your payment information is encrypted and secure
</p>
</form>
);
}
|