Payments in your app are easy with Firebase

Marie Harmsen
Nov 6, 2019 · 4 min read
Photo by : Jaime Staley-sickafoose — Dreamstime

So you are trying to make a really fantastic app, but it requires payments to be made in the app which adds a whole spectrum of complexities such as data breaches, fraudulent activities, different payment methods, refunds etc. Setting up your own servers will not only take a lot of time and money, but it will also be crucial to not compromise or mishandle any of the data or it will lead to a loss of clients or legal action against you.

However, with Firebase we are able to use existing payment processors such as PayPal or Stripe, to handle communication with the credit card APIs. Our app is not aware of payments and we don’t store the client's payment data.

Firebase has made a fantastic tutorial explaining the code and giving a demo on their website — I’ve used this code as a base to build my own app shown in this article. The links are under further reading.

In this article, we will be focusing on the capabilities and not technicalities.

Our App Concept

We are going to create a fairly simple app to demonstrate how this works. Our application is called Cooked. The app allows people to register and sell home-cooked meals. Think Uber Eats, but with home-cooked meals.

Sign in

Firebase allows for multiple methods of authentication. We are using Google authentication for our app demonstration.

Sign in with Firebase Authentication. The food background image source: Photo by Jakub Kapusnak on Unsplash

Once a user logs in through Firebase Authentication a cloud function is triggered and creates a document for the client in the Cloud Firestore database.

Firebase Authentication

This document will contain a unique customerID that can be used with the payment processor service.

Cloud Firestore database creating a document for new user

Create Stored Payment Method

To allow your customers to easily make payments we need to store basic payment details on Firebase, so they don’t have to re-enter their information every time they are hungry.

Entering credit card information that will be stored in Cloud FireStore for future use

Saving a card will trigger a cloud function that will add the credit card fingerprint to the existing client document in the Cloud Firestore database, with just the basic information stored in the file.

Making the Money

This is where we use the payment processor, in our app we are using Stripe however Firebase supports a range of payment processors. The payment methods accepted, such as debit or credit card, are completely dependent on the payment processor.

Once your customer clicks the Buy button, a cloud function is triggered to allow the Stripe SDK to process the payment. Once Stripe has completed the payment we write the result to the Cloud Firestore database so we can easily see the status of each transaction.

Buying on the application. Photo of curry by Taylor Kiser on Unsplash
Firestore Cloud charges document

The amount is shown in cents. Don't panic :)

Payment History

We write the payment result to the Firebase document, allowing for a full collection of payment history. This is extremely beneficial for the admins to see the status of the orders and the clients within the app.

Refunds

People may claim refunds, perhaps they didn’t receive the correct food or there was something wrong with their order.

The client makes a refund request which is received by the admins through Firebase. Once the admin has approved the refund a cloud function will trigger and call the Stripe SDK that will refund the client.

Further Reading

You can find out more about the code and find the Github demo version made by the Firebase team.

https://github.com/firebase/functions-samples/tree/Node-8/stripe

Conclusion

Thank you for reading and let me know in the comments below what your experiences have been with using different payment options in your apps. I would love to read about it!

DVT Software Engineering

Making an impact in Software Engineering

Marie Harmsen

Written by

iOS developer

DVT Software Engineering

Making an impact in Software Engineering

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade