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.
Firebase allows for multiple methods of authentication. We are using Google authentication for our app demonstration.
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.
This document will contain a unique customerID that can be used with the payment processor service.
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.
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.
The amount is shown in cents. Don't panic :)
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.
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.
You can find out more about the code and find the Github demo version made by the Firebase team.
Go serverless: Manage Payments in your Apps with Cloud Functions for Firebase
Note: If you prefer this content in video form, you can see the talk written by Susan Goldblatt and me here.
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!