Skip to main content

Overview

The ManageBilling component is a comprehensive billing management interface for users. It displays the user’s subscription status, payment cards, and billing information. The component handles loading states and dynamically updates the displayed information based on the user’s subscription details.

Import

import ManageBilling from './path/to/ManageBilling';

Usage

import React from 'react';
import ManageBilling from './components/ManageBilling';

const BillingPage = () => {
    return (
        <div>
            <h1>Billing Management</h1>
            <ManageBilling />
        </div>
    );
};

export default BillingPage;

Component Code

import React, { useEffect, useState } from 'react';
import PlanStatus from '../modules/PlanStatus';
import PaymentCards from '../modules/PaymentCards';
import BillingInformation from '../modules/BillingInformation';
import { useBillingContext } from '@/billing-ui/context/BillingContext';
import { BillingInfo, PaymentCard } from '../../types';
import useSubscription from '@/billing-ui/hooks/useSubscription';
import LoadingSpinner from '../ui/LoadingSpinner';

const ManageBilling: React.FC = () => {
    const { userToken } = useBillingContext();
    const { subscription } = useSubscription(String(userToken)) as any;

    const [paymentCards, setPaymentCards] = useState<PaymentCard[]>([]);
    const [billingInfo, setBillingInfo] = useState<BillingInfo | null>(null);

    useEffect(() => {
        if (subscription) {
            const customer = subscription.customer;
            const address = subscription.paymentMethods ? subscription.paymentMethods[0]?.billing_details?.address || {} : {};
            const billingData: BillingInfo = {
                name: customer?.name || '',
                email: customer?.email || '',
                address: address.line1 || '',
                city: address.city || '',
                country: address.country || '',
                postalCode: address.postal_code || '',
            };
            if (billingData.email !== '' || billingData.name !== '') {
                setBillingInfo(billingData);
            }

            const cards = subscription.paymentMethods?.map((method: any) => ({
                brand: method.card?.brand || '',
                last4: method.card?.last4 || '',
                expiry: `${method.card?.exp_month}/${method.card?.exp_year}`,
                default: method.default || false,
            })) || [];
            setPaymentCards(cards);
        }
    }, [subscription]);

    return (
    <>
        {(subscription) ? (
        <div className="flex h-screen">
            <aside className="bg-black text-white w-1/3 p-8 flex flex-col">
                <h2 className="text-2xl font-semibold mb-4">Manage your Specter billing settings</h2>
                <a href="/chat" className="text-primary mt-4 block">Return to Home</a>
                <div className="mt-auto">
                    <p className="text-gray-400 text-sm mt-1">Learn more about Stripe Billing</p>
                    <p className="text-gray-400 text-sm mt-1">Privacy</p>
                </div>
            </aside>
            <main className="bg-white w-2/3 p-8 flex flex-col">
                <PlanStatus subscription={subscription} />
                <PaymentCards paymentCards={paymentCards} setPaymentCards={setPaymentCards} />
                {billingInfo && <BillingInformation billingInfo={billingInfo} />}
            </main>
        </div> ) : (
            <div className="w-full min-h-screen flex items-center justify-center bg-gray-50 p-8">
                <LoadingSpinner />
            </div>
        )}
    </>
    );
};

export default ManageBilling;

Customization

The ManageBilling component can be customized in various ways to fit different requirements:
  1. Styles and Classes: Modify the Tailwind CSS classes to change the appearance of the layout, sections, and text.
  2. Additional Features: Add more sections or features as needed, such as a section for managing invoices or a form for updating billing details.
  3. Translations: Implement translations for text to support multiple languages.
  4. API Integration: Modify the hooks or context to fetch additional data from other APIs if needed.

Example Customization

const CustomizedManageBilling = () => {
    // Customized logic here

    return (
        <div className="custom-container">
            {/* Customized JSX here */}
        </div>
    );
};

export default CustomizedManageBilling;

Usage Guide

  • Import and Use: Import the ManageBilling component into your page or parent component and include it in the JSX.
  • Handle Loading and Errors: Ensure to handle loading states and errors appropriately to provide a good user experience.
  • Customization: Leverage the customization options to fit the component into your application’s design and functionality requirements.

Placeholder for Images

ManageBilling Screenshot
I