This my index.js -
const functions = require('firebase-functions');
const admin = require('firebase-admin')
const stripe = require('stripe') ('sk_test_51HmAsMFzRM1fSBZ2CkBBjW5a3sEpxEQLGT7OkGxa8BZMMfgzT1Jeqs7dq63AlPRLIJmzHFC0HHAmKHDazcpriuh9001IWIVmw1');
admin.initializeApp()
const db = admin.firestore()
const storage = admin.storage().bucket()
const auth = admin.auth()
exports.createStripeUser = functions.https.onCall( async (data, context) => {
const email = data.email
const uid = context.auth.uid
if (uid === null) {
throw new functions.https.HttpsError('internal', 'Illegal access attempt')
}
return stripe.customers.create({
email: email,
})
.then(customer => {
return customer["id"]
})
.then( customerId => {
admin.firestore().collection('users').doc(uid).set(
{
stripeId: customerId,
email: email,
id: uid
}
)
})
.catch( err => {
throw new functions.https.HttpsError('internal', 'Unable to create Stripe customer')
})
})
exports.createEphemeralKey = functions.https.onCall(async (data, context) => {
const customerId = data.customer_id;
const stripeVersion = data.stripe_version;
const uid = context.auth.uid;
if (uid === null) {
console.log('Illegal access attempt due to unauthenticated attempt.')
throw new functions.https.HttpsError('internal', 'Illegal access attempt');
}
return stripe.ephemeralKeys.create(
{ customer: customerId },
{ stripe_version: stripeVersion }
).then((key) => {
return key
}).catch( (err) => {
functions.logger.log('Error creating ephemeral key', err)
throw new functions.https.HttpsError('internal', 'Unable to create ephemeral key: ' + err)
})
})
exports.createPaymentIntent = functions.https.onCall( async (data, context) => {
const total = data.total
const idempotency = data.idempotency
const customer = context.auth.uid
const uid = context.auth.uid;
if (uid === null) {
console.log('Illegal access attempt due to unauthenticated attempt.')
throw new functions.https.HttpsError('internal', 'Illegal access attempt');
}
return stripe.paymentIntents.create(
{
amount: total,
currency: 'usd',
customer: customer,
payment_method_types: ['card', 'ach_debit']
},
{
idempotencyKey: idempotency
}
).then( intent => {
return intent.client_secret
}).catch( err => {
functions.logger.log('Unable to create Stripe Payment Intent.')
return null
})
})
exports.fetchBankSources = functions.https.onCall(async (data, context) => {
const customerId = data.customer_id
const bankAccounts = await stripe.customers.listSources(
customerId,
{ object: 'bank_account'}
)
return { banks: bankAccounts.data }
})
exports.deleteSource = functions.https.onCall(async (data, context) => {
const customerId = data.customer_id
const source = data.source
return stripe.customers.deleteSource(customerId, source)
.then(res => {
return {
status: 200
}
})
.catch(err => {
functions.logger.log(err)
throw new functions.https.HttpsError('internal', 'Unable to delete bank source: ' + err)
})
})
// PLAID
const plaid = require('plaid');
exports.createPlaidLinkToken = functions.https.onCall(async (data, context) => {
const customerId = context.auth.uid
const env = functions.config().app.env
let plaidEnv = plaid.environments.sandbox
if (env === 'production') {
plaidEnv = plaid.environments.development
} else {
plaidEnv = plaid.environments.sandbox
}
const plaidClient = new plaid.Client({
clientID: functions.config().plaid.client_id,
secret: functions.config().plaid.secret,
env: plaidEnv,
options: {
version: '2019-05-29'
}
})
return plaidClient.createLinkToken({
user: {
client_user_id: customerId
},
client_name: 'Bon Voyage',
products: ['auth'],
country_codes: ['US'],
language: 'en',
redirect_uri: 'https://bon-voyage.jonnybcodes.com'
})
.then((apiResponse) => {
const linkToken = apiResponse.link_token
return linkToken
})
.catch((err) => {
functions.logger.log(err)
throw new functions.https.HttpsError('internal', 'Unable to create plaid link token: ' + err)
})
})
exports.createPlaidBankAccount = functions.https.onCall(async (data, context) => {
const token = data.publicToken
const custId = data.stripeId
const bankAccountId = data.accountId
const env = functions.config().app.env
let plaidEnv = plaid.environments.sandbox
if (env === 'production') {
plaidEnv = plaid.environments.development
} else {
plaidEnv = plaid.environments.sandbox
}
const plaidClient = new plaid.Client({
clientID: functions.config().plaid.client_id,
secret: functions.config().plaid.secret,
env: plaidEnv,
options: {
version: '2019-05-29'
}
})
return plaidClient.exchangePublicToken(token)
.then(res => {
const accessToken = res.access_token
return plaidClient.createStripeToken(accessToken, bankAccountId)
})
.then(resp => {
const bankAccountToken = resp.stripe_bank_account_token
return stripe.customers.createSource(custId, {
source: bankAccountToken
}).then(bank => {
return bank
})
})
.catch((err) => {
functions.logger.log(err)
throw new functions.https.HttpsError('internal', 'Unable to create plaid link token: ' + err)
})
})
The functions show deployed in the console as shown below:-
This is my viewcontroller-
import UIKit
import FirebaseFirestore
import FirebaseAuth
import Stripe
import FirebaseFunctions
class SignUpViewController: UIViewController {
var paymentContext = STPPaymentContext()
@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var passwordConfirm: UITextField!
@IBAction func signUpAction(_ sender: Any) {
if password.text != passwordConfirm.text {let alertController = UIAlertController(title: "Password Incorrect", message: "Please re-type password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}else{
Auth.auth().createUser(withEmail: email.text!, password: password.text!){ (user, error) in if error == nil {
self.performSegue(withIdentifier: "signupToHome", sender: self)
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
Functions.functions().httpsCallable("createStripeUser").call(["email": email.text ?? ""]) { (result, error) in
if let error = error {
debugPrint(error.localizedDescription)
return
}
self.dismiss(animated: true)
}
}
}
It shows on the Firebase auth console:-
BUT, it shows nothing on the Stripe Dashboard:-
what am I doing wrong? As far as I know, everything is correct. As the functions are deployed and it creates user in the firebase console, why is no customer created in the Stripe Dashboard?
Hey the code do not have an issue I face the same issue I fixed it by Log out or quit the simulator and run the application on new simulator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.