All files / src/components ConnectOnboardingEmbed.tsx

1.78% Statements 1/56
0% Branches 0/31
0% Functions 0/7
1.81% Lines 1/55

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291                                                                    1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
/**
 * Embedded Stripe Connect Onboarding Component
 *
 * Uses Stripe's Connect embedded components to provide a seamless
 * onboarding experience without redirecting users away from the app.
 */
 
import React, { useState, useCallback } from 'react';
import {
  ConnectComponentsProvider,
  ConnectAccountOnboarding,
} from '@stripe/react-connect-js';
import { loadConnectAndInitialize } from '@stripe/connect-js';
import type { StripeConnectInstance } from '@stripe/connect-js';
import {
  CheckCircle,
  AlertCircle,
  Loader2,
  CreditCard,
  Wallet,
  Building2,
} from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { createAccountSession, refreshConnectStatus, ConnectAccountInfo } from '../api/payments';
 
interface ConnectOnboardingEmbedProps {
  connectAccount: ConnectAccountInfo | null;
  tier: string;
  onComplete?: () => void;
  onError?: (error: string) => void;
}
 
type LoadingState = 'idle' | 'loading' | 'ready' | 'error' | 'complete';
 
const ConnectOnboardingEmbed: React.FC<ConnectOnboardingEmbedProps> = ({
  connectAccount,
  tier,
  onComplete,
  onError,
}) => {
  const { t } = useTranslation();
  const [stripeConnectInstance, setStripeConnectInstance] = useState<StripeConnectInstance | null>(null);
  const [loadingState, setLoadingState] = useState<LoadingState>('idle');
  const [errorMessage, setErrorMessage] = useState<string | null>(null);
 
  const isActive = connectAccount?.status === 'active' && connectAccount?.charges_enabled;
 
  // Initialize Stripe Connect
  const initializeStripeConnect = useCallback(async () => {
    if (loadingState === 'loading' || loadingState === 'ready') return;
 
    setLoadingState('loading');
    setErrorMessage(null);
 
    try {
      // Fetch account session from our backend
      const response = await createAccountSession();
      const { client_secret, publishable_key } = response.data;
 
      // Initialize the Connect instance
      const instance = await loadConnectAndInitialize({
        publishableKey: publishable_key,
        fetchClientSecret: async () => client_secret,
        appearance: {
          overlays: 'drawer',
          variables: {
            colorPrimary: '#635BFF',
            colorBackground: '#ffffff',
            colorText: '#1a1a1a',
            colorDanger: '#df1b41',
            fontFamily: 'system-ui, -apple-system, sans-serif',
            fontSizeBase: '14px',
            spacingUnit: '12px',
            borderRadius: '8px',
          },
        },
      });
 
      setStripeConnectInstance(instance);
      setLoadingState('ready');
    } catch (err: any) {
      console.error('Failed to initialize Stripe Connect:', err);
      const message = err.response?.data?.error || err.message || t('payments.failedToInitializePayment');
      setErrorMessage(message);
      setLoadingState('error');
      onError?.(message);
    }
  }, [loadingState, onError, t]);
 
  // Handle onboarding completion
  const handleOnboardingExit = useCallback(async () => {
    // Refresh status from Stripe to sync the local database
    try {
      await refreshConnectStatus();
    } catch (err) {
      console.error('Failed to refresh Connect status:', err);
    }
    setLoadingState('complete');
    onComplete?.();
  }, [onComplete]);
 
  // Handle errors from the Connect component
  const handleLoadError = useCallback((loadError: { error: { message?: string }; elementTagName: string }) => {
    console.error('Connect component load error:', loadError);
    const message = loadError.error.message || t('payments.failedToLoadPaymentComponent');
    setErrorMessage(message);
    setLoadingState('error');
    onError?.(message);
  }, [onError, t]);
 
  // Account type display
  const getAccountTypeLabel = () => {
    switch (connectAccount?.account_type) {
      case 'standard':
        return t('payments.standardConnect');
      case 'express':
        return t('payments.expressConnect');
      case 'custom':
        return t('payments.customConnect');
      default:
        return t('payments.connect');
    }
  };
 
  // If account is already active, show status
  if (isActive) {
    return (
      <div className="space-y-6">
        <div className="bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg p-4">
          <div className="flex items-start gap-3">
            <CheckCircle className="text-green-600 dark:text-green-400 shrink-0 mt-0.5" size={20} />
            <div className="flex-1">
              <h4 className="font-medium text-green-800 dark:text-green-300">{t('payments.stripeConnected')}</h4>
              <p className="text-sm text-green-700 dark:text-green-400 mt-1">
                {t('payments.stripeConnectedDesc')}
              </p>
            </div>
          </div>
        </div>
 
        <div className="bg-gray-50 dark:bg-gray-700/50 rounded-lg p-4">
          <h4 className="font-medium text-gray-900 dark:text-white mb-3">{t('payments.accountDetails')}</h4>
          <div className="space-y-2 text-sm">
            <div className="flex justify-between">
              <span className="text-gray-600 dark:text-gray-400">{t('payments.accountType')}:</span>
              <span className="text-gray-900 dark:text-white">{getAccountTypeLabel()}</span>
            </div>
            <div className="flex justify-between">
              <span className="text-gray-600 dark:text-gray-400">{t('payments.status')}:</span>
              <span className="px-2 py-0.5 text-xs font-medium rounded-full bg-green-100 dark:bg-green-900/30 text-green-800 dark:text-green-300">
                {connectAccount.status}
              </span>
            </div>
            <div className="flex justify-between items-center">
              <span className="text-gray-600 dark:text-gray-400">{t('payments.charges')}:</span>
              <span className="flex items-center gap-1 text-green-600 dark:text-green-400">
                <CreditCard size={14} />
                {t('payments.enabled')}
              </span>
            </div>
            <div className="flex justify-between items-center">
              <span className="text-gray-600 dark:text-gray-400">{t('payments.payouts')}:</span>
              <span className="flex items-center gap-1 text-green-600 dark:text-green-400">
                <Wallet size={14} />
                {connectAccount.payouts_enabled ? t('payments.enabled') : t('payments.pending')}
              </span>
            </div>
          </div>
        </div>
      </div>
    );
  }
 
  // Completion state
  if (loadingState === 'complete') {
    return (
      <div className="bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg p-6 text-center">
        <CheckCircle className="mx-auto text-green-600 dark:text-green-400 mb-3" size={48} />
        <h4 className="font-medium text-green-800 dark:text-green-300 text-lg">{t('payments.onboardingComplete')}</h4>
        <p className="text-sm text-green-700 dark:text-green-400 mt-2">
          {t('payments.stripeSetupComplete')}
        </p>
      </div>
    );
  }
 
  // Error state
  if (loadingState === 'error') {
    return (
      <div className="space-y-4">
        <div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4">
          <div className="flex items-start gap-3">
            <AlertCircle className="text-red-600 dark:text-red-400 shrink-0 mt-0.5" size={20} />
            <div className="flex-1">
              <h4 className="font-medium text-red-800 dark:text-red-300">{t('payments.setupFailed')}</h4>
              <p className="text-sm text-red-700 dark:text-red-400 mt-1">{errorMessage}</p>
            </div>
          </div>
        </div>
        <button
          onClick={() => {
            setLoadingState('idle');
            setErrorMessage(null);
          }}
          className="w-full px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600"
        >
          {t('payments.tryAgain')}
        </button>
      </div>
    );
  }
 
  // Idle state - show start button
  if (loadingState === 'idle') {
    return (
      <div className="space-y-4">
        <div className="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-4">
          <div className="flex items-start gap-3">
            <Building2 className="text-blue-600 dark:text-blue-400 shrink-0 mt-0.5" size={20} />
            <div className="flex-1">
              <h4 className="font-medium text-blue-800 dark:text-blue-300">{t('payments.setUpPayments')}</h4>
              <p className="text-sm text-blue-700 dark:text-blue-400 mt-1">
                {t('payments.tierPaymentDescriptionWithOnboarding', { tier })}
              </p>
              <ul className="mt-3 space-y-1 text-sm text-blue-700 dark:text-blue-400">
                <li className="flex items-center gap-2">
                  <CheckCircle size={14} />
                  {t('payments.securePaymentProcessing')}
                </li>
                <li className="flex items-center gap-2">
                  <CheckCircle size={14} />
                  {t('payments.automaticPayouts')}
                </li>
                <li className="flex items-center gap-2">
                  <CheckCircle size={14} />
                  {t('payments.pciCompliance')}
                </li>
              </ul>
            </div>
          </div>
        </div>
 
        <button
          onClick={initializeStripeConnect}
          className="w-full flex items-center justify-center gap-2 px-4 py-3 text-sm font-medium text-white bg-[#635BFF] rounded-lg hover:bg-[#5851ea] transition-colors"
        >
          <CreditCard size={18} />
          {t('payments.startPaymentSetup')}
        </button>
      </div>
    );
  }
 
  // Loading state
  if (loadingState === 'loading') {
    return (
      <div className="flex flex-col items-center justify-center py-12">
        <Loader2 className="animate-spin text-[#635BFF] mb-4" size={40} />
        <p className="text-gray-600 dark:text-gray-400">{t('payments.initializingPaymentSetup')}</p>
      </div>
    );
  }
 
  // Ready state - show embedded onboarding
  if (loadingState === 'ready' && stripeConnectInstance) {
    return (
      <div className="space-y-4">
        <div className="bg-gray-50 dark:bg-gray-700/50 border border-gray-200 dark:border-gray-600 rounded-lg p-4">
          <h4 className="font-medium text-gray-900 dark:text-white mb-2">{t('payments.completeAccountSetup')}</h4>
          <p className="text-sm text-gray-600 dark:text-gray-400">
            {t('payments.fillOutInfoForPayment')}
          </p>
        </div>
 
        <div className="border border-gray-200 dark:border-gray-600 rounded-lg overflow-hidden bg-white dark:bg-gray-800 p-4">
          <ConnectComponentsProvider connectInstance={stripeConnectInstance}>
            <ConnectAccountOnboarding
              onExit={handleOnboardingExit}
              onLoadError={handleLoadError}
            />
          </ConnectComponentsProvider>
        </div>
      </div>
    );
  }
 
  return null;
};
 
export default ConnectOnboardingEmbed;