1
votes

I have deployed a cloud function to my Firebase In order to use Firebase as my backend server to handle Stripe payment. The link of the sample Firebase cloud functions i have used: https://github.com/firebase/functions-samples/tree/master/stripe

Here is the function I should trigger when I charge the user in my app

exports.createStripeCharge = functions.database.ref('/stripe_customers/{userId}/charges/{id}').onWrite((event) => {
  const val = event.data.val();


  if (val === null || val.id || val.error) return null;

  return admin.database().ref(`/stripe_customers/${event.params.userId}/customer_id`).once('value').then((snapshot) => {
    return snapshot.val();
  }).then((customer) => {

    const amount = val.amount;
    const idempotency_key = event.params.id;
    let charge = {amount, currency, customer};
    if (val.source !== null) charge.source = val.source;
    return stripe.charges.create(charge, {idempotency_key});
  }).then((response) => {

    return event.data.adminRef.set(response);
  }).catch((error) => {


    return event.data.adminRef.child('error').set(userFacingMessage(error));
  }).then(() => {
    return reportError(error, {user: event.params.userId});
  });
});

I know that the above function will be triggered when my database changed. My question is, what is the proper way to pass the Stripe payment detail to my Firebase Database? I am not sure what should I pass to my firebase database after reading the stripe document.

Could anyone help me with this question? Thanks!!

P.S. My developing environment: Objective C, IOS application.

2

2 Answers

0
votes

The easiest way is to have your app make an HTTP POST request to an HTTP Firebase function. Your app should already be doing this in order to create the Stripe ephemeral key and you can handle payments in a similar way. After the charge is successful you can save the resulting info to your database.

The function:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const bodyParser = require('body-parser');

const stripeApp = express();

stripeApp.use(bodyParser.json());

stripeApp.post('/order', (req, res) => {
  const { source } = req.body; // This is the token provided by your app

  return stripe.charges.create({...}) // the actual payment
});

exports.stripeAPI = functions.https.onRequest(stripeApp);

Have a took at Stripe's demo iOS app - its swift but it should still make sense in Obj-C world. https://github.com/stripe/stripe-connect-rocketrides/tree/master/ios/RocketRides.

Particularly https://github.com/stripe/stripe-connect-rocketrides/blob/master/ios/RocketRides/MainAPIClient.swift#L39

0
votes

So the process that works for me is -

1) iOS provides card details to Stripe using native SDK

2) Stripe provides a token which you send it to your Firebase backend

you could store it in stripeTokens/userId/yourToken

3) Firebase cloud function then triggers a function and uses this token to create Stripe customer (See saving for later and Customer)

you could store it in stripe_customers/userId/stripeCustomerId like your example

4) Remember to remove yourToken because it's only valid once

5) finally you can use this stripeCustomerId to make payments

Important concept here is to create a customer and store in your backend for future payments.

Hope this helps.