0
votes

I am trying to use Stripe with Firebase and have followed along the linked Firestripe example project. When a user signs up for the app, the following function is triggered in Firebase which creates a Customer object in Stripe.

exports.createStripeCustomer = functions.auth.user().onCreate(event => {
  const data = event.data;
  return stripe.customers.create({
  }).then(customer => {
    return admin.database().ref(`/stripe_customers/${data.uid}/customer_id`).set(customer.id);
  });
});

The last line in the function above is supposed to write the Stripe customer ID as a child of the Firebase UID in the database which it does successfully.

Now on the client side (iOS - Swift) when trying to write to the same authenticated user by obtaining their UID using the following:

let userID = Auth.auth().currentUser!.uid
Database.database().reference(fromURL: "https://REDACTED.firebaseio.com/").child("stripe_customers").child(uid).child("sources").setValue([
                    "token": token.tokenId,
                    ])

it creates a whole different child under /stripe_customers/ meaning the UIDs don't match... How do I go on obtaining the real UID of the user from the client side? Or am I doing something wrong? Any help or insight would be appreciated.

1

1 Answers

0
votes

What you're doing in the first code sample is storing data under Stripe customer ID while in your second code sample, you're attempting to retrieve data based on your Firebase UID.

What you want to do is store user objects named as your Firebase UID, then store your Stripe ID in a child object. Finally, query for your Firebase UID object not its Stripe ID child.

Furthermore, I can't see you using your let userID = Auth.auth().currentUser!.uid anywhere in your query. If you declare a constant that you're not using for your example, it's better to leave it out.

Let me know if you need more help.

Also, have a look at Firebase's new Cloud Firestore. It's still in beta, but it's extremely powerful compared to Firebase Realtime Database.