DEV Community

Datravous Odds
Datravous Odds

Posted on

Implementing Firebase Payment Methods Loading

This weekend, I implemented the payment methods loading functionality for the payment information tab in my e-commerce website. The main task was to create an asynchronous function that loads payment method information, including bank accounts and credit card accounts, from Firebase.

The Implementation

The core of this implementation is the loadPaymentMethods function that retrieves payment information from Firebase. Here's a detailed breakdown of how it works:

async function loadPaymentMethods(userData) {
  if (!userData) return null;
  try {
    // user profile reference
    const userProfileRef = doc(db, "userProfiles", userData.email);

    // references to bank and credit card documents
    const bankAccountsRef = collection(userProfileRef, "bankAccounts");
    const creditCardsRef = collection(userProfileRef, "creditCards");

    // fetch bank and credit card information at the same time
    const [bankAccountSnapshot, creditCardsSnapshot] = await Promise.all([
      getDocs(bankAccountsRef),
      getDocs(creditCardsRef)
    ]);

    // map bank information to an object
    const bankAccounts = bankAccountSnapshot.docs.map((doc) => ({
      id: doc.id,
      ...doc.data()
    }));

    // map credit card information to an object
    const creditCardAccounts = creditCardsSnapshot.docs.map((doc) => ({
      id: doc.id,
      ...doc.data()
    }));

    return { bankAccounts, creditCardAccounts };
  } catch (error) {
    console.error("Error happened when fetching payment information");
    throw error;
  }
}
Enter fullscreen mode Exit fullscreen mode

Key Components Explained

User Data Validation
The function starts with a simple but important check:

if (!userData) return null;
Enter fullscreen mode Exit fullscreen mode

This ensures we have valid user data before proceeding. In a production environment, we could enhance this by returning a UI component to inform users when no data is available.

Firebase Document References

The function creates necessary references to access the Firebase documents:

User Profile Reference:

const userProfileRef = doc(db, "userProfiles", userData.email);
Enter fullscreen mode Exit fullscreen mode

This creates a reference to the user's profile document using their email as the identifier.

Payment Method References:

const bankAccountsRef = collection(userProfileRef, "bankAccounts");
const creditCardsRef = collection(userProfileRef, "creditCards");
Enter fullscreen mode Exit fullscreen mode

These references point to the subcollections containing bank and credit card information.

Parallel Data Fetching
One of the key optimizations is the use of Promise.all() to fetch both bank and credit card data simultaneously:

const [bankAccountSnapshot, creditCardsSnapshot] = await Promise.all([
  getDocs(bankAccountsRef),
  getDocs(creditCardsRef)
]);
Enter fullscreen mode Exit fullscreen mode

This approach is more efficient than sequential fetching as both requests are processed in parallel.

Data Mapping
The retrieved snapshots are then mapped to more usable objects:

const bankAccounts = bankAccountSnapshot.docs.map((doc) => ({
  id: doc.id,
  ...doc.data()
}));
Enter fullscreen mode Exit fullscreen mode

This transformation makes the data easier to work with in the rest of the application, combining the document ID with its data.

Error Handling

The entire operation is wrapped in a try-catch block to handle potential errors gracefully:

try {
  // ... implementation
} catch (error) {
  console.error("Error happened when fetching payment information");
  throw error;
}
Enter fullscreen mode Exit fullscreen mode

Next Steps

In upcoming implementations, I'll be adding functionality to:

  • Handle credit card and bank account transactions

  • Update the UI to display payment method information

  • Implement error states and loading indicators

Stay tuned for the next post where I'll cover the transaction implementation details!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay