forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePaymentCheckout.ts
More file actions
64 lines (51 loc) · 1.44 KB
/
Copy pathusePaymentCheckout.ts
File metadata and controls
64 lines (51 loc) · 1.44 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
import { useState } from "react";
type CheckoutParams = {
amount: number;
splitId: string;
};
export const usePaymentCheckout = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [lastSubmittedId, setLastSubmittedId] = useState<string | null>(null);
const isWalletConnected = () => {
// replace with real wallet hook
return Boolean(window?.ethereum?.selectedAddress);
};
const isCorrectNetwork = () => {
// replace with real chain check
return true;
};
const submitPayment = async (params: CheckoutParams) => {
const { splitId } = params;
// 🚫 prevent duplicate submits
if (loading || lastSubmittedId === splitId) return;
setError(null);
if (!isWalletConnected()) {
setError("Wallet not connected");
return;
}
if (!isCorrectNetwork()) {
setError("Wrong network");
return;
}
try {
setLoading(true);
setLastSubmittedId(splitId);
// simulate payment request
await new Promise((res) => setTimeout(res, 1000));
// replace with real API call
console.log("Payment successful for split:", splitId);
} catch (e: any) {
setError(e.message || "Payment failed");
} finally {
setLoading(false);
}
};
return {
submitPayment,
loading,
error,
isWalletConnected: isWalletConnected(),
isCorrectNetwork: isCorrectNetwork(),
};
};