skip to Main Content

I’m switching data from Firebase Realtime Database to Firestore because I need more querying capabilities. However, I’m having trouble with saving my customer’s stripeID to their collection document. My Cloud Function is hitting perfectly because Stripe is creating the customer correctly, but it’s not assigning to the collection reference. What do I need to fix so the collection reference could recognize the stripeID as well? Thank you!

What I’m Seeing

enter image description here

enter image description here

Customer Model

struct Customer {
    
    let uid: String
    let stripeId: String
    let fullname: String
    let email: String
    let username: String
    let profileImageUrl: String
    
    init(dictionary: [String : Any]) {
        self.uid = dictionary["uid"] as? String ?? ""
        self.stripeId = dictionary["stripeId"] as? String ?? ""
        self.fullname = dictionary["fullname"] as? String ?? ""
        self.email = dictionary["email"] as? String ?? ""
        self.username = dictionary["username"] as? String ?? ""
        self.profileImageUrl = dictionary["profileImageUrl"] as? String ?? ""
    }
    
}

AuthServices

struct CustomerCredentials {
    let email: String
    let password: String
    let fullname: String
    let username: String
    let profileImage: UIImage
}

static func createCustomer(credentials: CustomerCredentials, completion: CollectionCompletion) {

    Auth.auth().createUser(withEmail: credentials.email, password: credentials.password) { result, error in
                if let error = error {
                    debugPrint(error.localizedDescription)
                    return
                }
                    
                guard let uid = result?.user.uid else { return }
                    
                let values = ["uid" : uid,
                              "email" : credentials.email,
                              "fullname" : credentials.fullname,
                              "username" : credentials.username,
                              "profileImageUrl" : profileImageUrl]
                    
                REF_CUSTOMERS.document(uid).setData(values, completion: completion)
            }
    }

}

RegistrationController

@objc func handleCreateAccount() {

    let credentials = CustomerCredentials(email: email, password: password, fullname: fullname,
                                          username: username, profileImage: profileImage)
        
    AuthService.createCustomer(credentials: credentials) { error in
        if let error = error {
            Auth.auth().handleFireAuthError(error: error, vc: self)
            self.showLoader(false)
            return
        }
            
        Functions.functions().httpsCallable("createStripeCustomer").call(["email" : email]) { result, error in
            if let error = error {
                debugPrint(error.localizedDescription)
                return
            }
        }

    }

}

2

Answers


  1. Chosen as BEST ANSWER

    In my RegistrationController, I had to add an updateData value inside my Cloud Function code snippet in order to add it to my customer reference.

    RegistrationController

    Functions.functions().httpsCallable("createStripeCustomer").call(["email" : email]) { result, error in
          if let error = error {
              debugPrint(error.localizedDescription)
              return
          }
                    
          let value = ["stripeId" : result?.data]
                    
          guard let uid = Auth.auth().currentUser?.uid else { return }
                    
          REF_CUSTOMERS.document(uid).updateData(value as [AnyHashable : Any])
    }
    

  2. Assuming that REF_CUSTOMERS points to the "customers" collection, then when you’re using the following line of code:

    REF_CUSTOMERS.document(uid).setData(values, completion: completion)
    

    For writing the following values:

    let values = ["uid" : uid,
                  "email" : credentials.email,
                  "fullname" : credentials.fullname,
                  "username" : credentials.username,
                  "profileImageUrl" : profileImageUrl]
    

    The document that will be added will always contain only the data that exists in the values object. Since there is no stripeId present, the document that will be written will not contain the stripeId. If you need to have the stripeId as a field in the user document, then you have to add it like this:

    let values = ["uid" : uid,
                  "email" : credentials.email,
                  "fullname" : credentials.fullname,
                  "username" : credentials.username,
                  "profileImageUrl" : profileImageUrl
                  "stripeId" : stripeId] //👈
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search