-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSoccerBall.tsx
More file actions
109 lines (93 loc) · 3.07 KB
/
SoccerBall.tsx
File metadata and controls
109 lines (93 loc) · 3.07 KB
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
import React, { useContext, useState, useCallback } from "react";
import { PayPalSDKContext } from "../context/sdkContext";
import PayPalButton from "../components/PayPalButton";
import VenmoButton from "../components/VenmoButton";
import { PaymentSessionOptions, OnApproveData } from "../types/paypal";
import ProductDisplay from "../components/ProductDisplay";
import PaymentModal from "../components/PaymentModal";
import soccerBallImage from "../static/images/world-cup.jpg";
import { captureOrder } from "../utils";
import "../static/styles/SoccerBall.css";
import "../static/styles/Modal.css";
// Types
type ModalType = "success" | "cancel" | "error" | null;
interface ModalContent {
title: string;
message: string;
}
// Constants
const PRODUCT = {
name: "World Cup Ball",
icon: "⚽️",
price: 100.0,
imageSrc: soccerBallImage,
imageAlt: "Official World Cup Soccer Ball",
} as const;
const SoccerBall: React.FC = () => {
const { sdkInstance, eligiblePaymentMethods } = useContext(PayPalSDKContext);
const [modalState, setModalState] = useState<ModalType>(null);
// Payment handlers
const handlePaymentCallbacks: PaymentSessionOptions = {
onApprove: async (data: OnApproveData) => {
console.log("Payment approved:", data);
const captureResult = await captureOrder({ orderId: data.orderId });
console.log("Payment capture result:", captureResult);
setModalState("success");
},
onCancel: () => {
console.log("Payment cancelled");
setModalState("cancel");
},
onError: (error: Error) => {
console.error("Payment error:", error);
setModalState("error");
},
};
const getModalContent = useCallback(
(state: ModalType): ModalContent | null => {
switch (state) {
case "success":
return {
title: "Payment Successful! 🎉",
message: "Thank you for your purchase!",
};
case "cancel":
return {
title: "Payment Cancelled",
message: "Your payment was cancelled.",
};
case "error":
return {
title: "Payment Error",
message:
"There was an error processing your payment. Please try again.",
};
default:
return null;
}
},
[],
);
// Check payment method eligibility
const isPayPalEligible =
sdkInstance && eligiblePaymentMethods?.isEligible("paypal");
const isVenmoEligible =
sdkInstance && eligiblePaymentMethods?.isEligible("venmo");
const modalContent = getModalContent(modalState);
return (
<div className="soccer-ball-container" data-testid="soccer-ball-container">
{modalContent && (
<PaymentModal
content={modalContent}
onClose={() => setModalState(null)}
/>
)}
<ProductDisplay product={PRODUCT} />
<div className="payment-options">
{isPayPalEligible && <PayPalButton {...handlePaymentCallbacks} />}
{isVenmoEligible && <VenmoButton {...handlePaymentCallbacks} />}
</div>
</div>
);
};
export default SoccerBall;