4
votes

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!

1
using the key ending with Bb2 you can get snapshot of all he details under it and use your payment array from snapshot - iOS Geek
or what do you basically want to fetch and what value do you have to make a query to FirebaseDB - iOS Geek
@iOSGeek Stripe requires, the amount, the currency type, and the token they send you. stripe.com/docs/charges So I guess I need to grab those items, but I haven't used javascript before so I am not sure what it all means. I could read the documentation, but I still don't know they syntax etc.. - Taylor Simpson
so to access Stripe you need the value stored under paymentID ? amount and token value - iOS Geek
yes, they need the amount and the token, because that is the key they send you and then you use your back end to send it back so the charge can be processed along with the parameters needed. I am just not sure if I have to name my nodes specifically etc.. - Taylor Simpson

1 Answers

16
votes

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.