// Revolut OAuth Integration - Frontend Implementation // This is a complete working example for your e-shop frontend import React, { useState } from 'react'; interface ConnectedAccount { type: string; tokenType: string; expiresIn: number; } const RevolutOAuthConnect = () => { const [isLoading, setIsLoading] = useState(false); const [connectedAccount, setConnectedAccount] = useState(null); // Check current connection status const checkConnectionStatus = async () => { try { const response = await fetch('/api/v1/eshop/revolut/oauth/status'); const data = await response.json(); if (data.authenticated) { setConnectedAccount({ type: data.account_type || 'revolut_pro', tokenType: data.token_type, expiresIn: data.expires_in, }); } else { setConnectedAccount(null); } } catch (error) { console.error('Failed to check connection status:', error); } }; // Start OAuth flow for specific account type const startOAuth = async (accountType: string) => { setIsLoading(true); try { const response = await fetch('/api/v1/eshop/revolut/oauth/start', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ account_type: accountType }), }); if (!response.ok) { throw new Error('Failed to start OAuth flow'); } const data = await response.json(); // Redirect user to Revolut authorization page window.location.href = data.authorization_url; } catch (error) { console.error('OAuth start error:', error); setIsLoading(false); } }; // Handle OAuth callback (called when user returns from Revolut) const handleOAuthCallback = async () => { const urlParams = new URLSearchParams(window.location.search); const code = urlParams.get('code'); const state = urlParams.get('state'); const error = urlParams.get('error'); if (error) { console.error('OAuth authorization failed'); return; } if (code && state) { try { const response = await fetch('/api/v1/eshop/revolut/oauth/callback', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ code, state }), }); const data = await response.json(); if (data.success) { setConnectedAccount({ type: data.account_type, tokenType: data.token_info.token_type, expiresIn: data.token_info.expires_in, }); // Clean up URL window.history.replaceState({}, document.title, window.location.pathname); } else { throw new Error(data.error || 'OAuth callback failed'); } } catch (error) { console.error('OAuth callback error:', error); } } setIsLoading(false); }; // Disconnect Revolut account const disconnectRevolut = async () => { try { const response = await fetch('/api/v1/eshop/revolut/oauth/disconnect', { method: 'POST', }); if (response.ok) { setConnectedAccount(null); } } catch (error) { console.error('Disconnect error:', error); } }; // Check connection status on component mount React.useEffect(() => { checkConnectionStatus(); // Handle OAuth callback if returning from Revolut if (window.location.search.includes('code=')) { handleOAuthCallback(); } }, []); return (

Revolut platby

{connectedAccount ? (
Připojen účet: {connectedAccount.type === 'business' ? 'Revolut Business' : 'Revolut Pro'}
Token typu: {connectedAccount.tokenType}
Platnost tokenu: {connectedAccount.expiresIn} sekund
) : (
Připojte svůj účet Revolut pro přijímání plateb. Podporujeme jak Revolut Pro pro živnostníky, tak Revolut Business pro firmy.
)} {!connectedAccount ? (
Vyberte typ účtu, který chcete připojit:
Revolut Pro je ideální pro živnostníky bez nutnosti registrace firmy. Revolut Business vyžaduje registrovanou společnost.
) : ( )}
Jak to funguje:
• Klikněte na tlačítko pro připojení účtu
• Budete přesměrováni na přihlášení Revolut
• Po přihlášení autorizujete přístup k platebnímu API
• Systém automaticky získá přístupový token
• Žádné API klíče nemusíte zadávat ručně!
); }; export default RevolutOAuthConnect;