Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly Using Firebase Cloud Functions and Stripe

I am trying to use Firebase Cloud functions and Stripe, and iOS (Swift). I want to use Firebase Cloud functions to perform the card charge as is required by Stripe. I am trying to use this example: Firebase Stripe Example

I uploaded the example they gave, but I need to modify the charge function slightly. I tried setting it to the file path I have in my real time data base, but I am not sure how to modify the entire thing so that It will work off of my realtime database structure. Such as grabbing the parameters it needs. Here is what my structure looks like: Database Structure for Stripe charges. after the Payments node is the userID.

I know that the function basically looks for database updates to the specific node, but I am not sure how to make sure it grabs the correct values from the child nodes. I hope I have described this in an understandable way. If I haven't let me know and I will do my best to reword. Thank you!

like image 641
Taylor Simpson Avatar asked Dec 04 '22 21:12

Taylor Simpson


1 Answers

Alright guys I figured it out myself and I will write a mini tutorial here.

Because this question specifically deals with stripe I will only cover this specific use case. First it is important to read the Stripe documentation. It specifies the parameters it is expecting you to give to it's api. Here is the link for what is expected when you charge a card with Stripe: Stripe Charges Documentation

Second you need to model your Firebase Realtime database after those expected parameters, at least in regards to the purchases a user will make in your app. Most of the time you can use a dictionary with Key:value pairs. Make sure you multiply your amount parameter by 100 as Stripe only takes integers. This means if you you are charging 22.48 for example then you multiply it by 100 to get 2248. When you check your dashboard in stripe it will show up as 22.48.

Third Stripe needs to talk to a back end so that you can charge the card. Firebase Cloud functions are perfect for this. Remember your real time database? Well you can trigger a Cloud function when a write occurs on a node that you specify. Luckily Firebase has provided a sample on GitHub: Firebase Stripe Example

You can modify the line of code where it listens for a write to the data base. You can change it to your structure specifically as long as you return at least the token and the amount to be charged. If you are going to make the user enter their information every time, then you need to delete the customer parameter (in the index.js file) as it will expect a different token with a different prefix. (This is noted in the documentation) The rest of the example is well documented and can be followed. The outcome of the charge will be written back into your database. The following video shows you how Functions triggered on write work and some of the nomenclature used. : Youtube Video for Cloud Functions triggered on write.

I hope this helps a few of you as I know questions about stripe and firebase are fairly common.

like image 72
Taylor Simpson Avatar answered Dec 31 '22 19:12

Taylor Simpson