-
+const Checkout = ({ socketId, transactionSuccess }) => {
+ const [products, setProducts] = useState([]);
+ const [itemIds, setItemIds] = useState(null);
+ const [token, setToken] = useState(null);
+
+ const [qrisData, setQrisData] = useState(null);
+ const [value, setValue] = useState(null);
+ const [loadingPay, setLoadingPay] = useState(false);
+
+ const [redirect_uri, setRedirect_Uri] = useState('');
+ const [redirect_failed, setRedirect_Failed] = useState('');
+
+ useEffect(() => {
+ const urlParams = new URLSearchParams(window.location.search);
+ const tokenParam = urlParams.get('token');
+ const itemsIdString = urlParams.get('itemsId');
+ setRedirect_Uri(urlParams.get('redirect_uri'));
+ setRedirect_Failed(urlParams.get('redirect_failed'));
+
+ setToken(tokenParam);
+
+ if (!itemsIdString) {
+ window.location.href = redirect_failed;
+ return;
+ }
+
+ try {
+ const parsedIds = JSON.parse(itemsIdString);
+ if (!Array.isArray(parsedIds) || parsedIds.length === 0) {
+ window.location.href = redirect_failed;
+ return;
+ }
+ setItemIds(parsedIds);
+ } catch (e) {
+ console.error('Invalid itemsId format', e);
+ window.location.href = redirect_failed;
+ }
+ }, []);
+
+ // Fetch products
+ useEffect(() => {
+ if (itemIds && Array.isArray(itemIds) && itemIds.length > 0) {
+ fetch('https://bot.kediritechnopark.com/webhook/store-dev/products', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({ itemsId: itemIds }),
+ })
+ .then((res) => res.json())
+ .then((data) => {
+ setProducts(data);
+ })
+ .catch((err) => {
+ console.error('Error fetching products:', err);
+ });
+ }
+ }, [itemIds]);
+
+ const handleRemove = (id) => {
+ const updatedItemIds = itemIds.filter((itemId) => itemId !== id);
+ const updatedProducts = products.filter((product) => product.id !== id);
+
+ if (updatedItemIds.length === 0) {
+ window.location.href = redirect_failed;
+ return;
+ }
+
+ setItemIds(updatedItemIds);
+ setProducts(updatedProducts);
+
+ const url = new URL(window.location);
+ url.searchParams.set('itemsId', JSON.stringify(updatedItemIds));
+ window.history.replaceState(null, '', url.toString());
+ };
+
+ const handlePay = async () => {
+ if (!itemIds || !token) {
+ alert('Token atau itemsId tidak ditemukan.');
+ return;
+ }
+
+ setLoadingPay(true);
+
+ try {
+ const params = new URLSearchParams();
+ itemIds.forEach((id) => params.append('itemsId', id));
+ params.append('socketId', socketId);
+ // Jika butuh socketId bisa tambahkan di sini, misal: params.append('socketId', socketId);
+
+ const response = await fetch('https://bot.kediritechnopark.com/webhook/store-dev/pay', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ Authorization: `Bearer ${token}`,
+ },
+ body: params.toString(),
+ });
+
+ const result = await response.json();
+
+ if (response.ok && result?.qris_dynamic) {
+ setQrisData(result.qris_dynamic);
+ setValue(result.total_price);
+ } else {
+ alert(`Gagal mendapatkan QRIS: ${result?.error || 'Unknown error'}`);
+ }
+ } catch (error) {
+ console.error('Network error:', error);
+ alert('Terjadi kesalahan jaringan.');
+ } finally {
+ setLoadingPay(false);
+ }
+ };
+
+ useEffect(() => {
+ if (transactionSuccess) {
+ const timer = setTimeout(() => {
+ window.location.href = redirect_uri;
+ }, 10000); // 10 detik = 10000 ms
+
+ // Bersihkan timer kalau komponen unmount atau transactionSuccess berubah
+ return () => clearTimeout(timer);
+ }
+ }, [transactionSuccess]);
+
+
+ return (
+
+
+ {/* Product List */}
+
+ {!qrisData &&
+ <>
+
Your Cart
+
+ {products.map((item, index) => (
+ -
+
+

+
+
{item.name}
+
Rp{item.price?.toLocaleString('id-ID')}
+ {item.duration?.hours && (
+
+ Durasi: {item.duration.hours} jam
+
+ )}
+
+
+
+
+ ))}
+
+ >
+ }
+
+ {qrisData && (
+ <>
+
Silahkan scan QRIS ini
+
+
+
+
+ {transactionSuccess && (
+
+ )}
+
+ {!transactionSuccess && (
+ <>
+
Rp{value?.toLocaleString('id-ID')}
+ >
+ )}
+
+ >
+ )}
+
+
+
+
+ {/* Checkout form */}
+
+
+
+ Rp{qrisData ? value : products.reduce((acc, item) => acc + (item.price || 0), 0).toLocaleString('id-ID')}
+
+
+
+
+
+
+
+ {/* Footer */}
+
+ Powered by KEDIRITECHNOPARK •{' '}
+
+
-
-
-
- {/* Footer */}
-