在 Android 中使用与我在 iOS 中创建 Stripe 客户时相同的云功能
Use the Same Cloud Function in Android as I do in iOS While Creating a Stripe Customer
当客户像在我的 iOS 应用程序上一样在我的 Android 应用程序上注册新帐户时,我正在尝试创建一个 stripeID。我的问题是 - 是否可以使用相同的 Cloud Functions 在我的 Android 应用程序中创建 Stripe Customer,或者我是否需要为 Android 创建一个全新的 Functions 文件夹?谢谢!
云函数
exports.createStripeCustomer = functions.https.onCall( async (data, context) => {
const email = data.email
const uid = context.auth.uid
console.log(uid)
if (uid === null) {
console.log('Illegal access attempt due to unauthenticated attempt.')
throw new functions.https.HttpsError('internal', 'Illegal access attempt')
}
return stripe.customers
.create({ email: email })
.then((customer) => {
return customer["id"];
})
.then((customerId) => {
admin.database().ref("customers").child(uid).update({
stripeId: customerId,
email: email,
id: uid
})
})
.catch((err) => {
console.log(err);
throw new functions.https.HttpsError(
"internal",
" Unable to create Stripe user : " + err
);
});
})
Kotlin 函数
registerViewStubSignUpButton.setOnClickListener {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(registerViewStubEmailTextView.text.toString(), registerViewStubPasswordTextView.text.toString())
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
// else if successful
uploadImageToFirebaseStorage()
uploadStripeCustomer()
}
.addOnFailureListener {
Toast.makeText(this, "Failed to create user: ${it.message}", Toast.LENGTH_SHORT).show()
}
}
private fun uploadImageToFirebaseStorage() {
if (selectedPhotoUri == null) return
val filename = UUID.randomUUID().toString()
val ref = FirebaseStorage.getInstance().getReference("/customer_profile_images/$filename")
ref.putFile(selectedPhotoUri!!)
.addOnSuccessListener {
ref.downloadUrl.addOnSuccessListener {
saveCustomerToFirebaseDatabase(it.toString())
}
}
}
private fun saveCustomerToFirebaseDatabase(profileImageUrl: String) {
val registerStub = findViewById<ViewStub>(R.id.registerStub)
val registerViewStubFullnameTextView = findViewById<TextInputEditText>(R.id.fullnameInputTextView)
val registerViewStubUsernameTextView = findViewById<TextInputEditText>(R.id.usernameTextInputView)
val registerViewStubEmailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("/customers/$uid")
val customer = Customer(uid, registerViewStubFullnameTextView.text.toString(), registerViewStubUsernameTextView.text.toString(),
registerViewStubEmailTextView.text.toString(), profileImageUrl)
ref.setValue(customer)
.addOnSuccessListener {
registerStub.alpha = 0f
finish()
}
}
private fun uploadStripeCustomer() {
}
这取决于用例。如果 Firebase Cloud Function 对 iOS 和 Android 应用程序执行相同的任务,那么只使用一个应该可以。如果 iOS 和 Android 应用程序有一些不同的用例,那么应该使用两个不同的 Cloud Functions。
在你的例子中,当客户在 iOS 和 Android 应用程序中注册新帐户时,你似乎正在创建一个 stripeID
,所以这是明智的为 iOS 和 Android 应用程序使用一个 Firebase Cloud Functions。
当客户像在我的 iOS 应用程序上一样在我的 Android 应用程序上注册新帐户时,我正在尝试创建一个 stripeID。我的问题是 - 是否可以使用相同的 Cloud Functions 在我的 Android 应用程序中创建 Stripe Customer,或者我是否需要为 Android 创建一个全新的 Functions 文件夹?谢谢!
云函数
exports.createStripeCustomer = functions.https.onCall( async (data, context) => {
const email = data.email
const uid = context.auth.uid
console.log(uid)
if (uid === null) {
console.log('Illegal access attempt due to unauthenticated attempt.')
throw new functions.https.HttpsError('internal', 'Illegal access attempt')
}
return stripe.customers
.create({ email: email })
.then((customer) => {
return customer["id"];
})
.then((customerId) => {
admin.database().ref("customers").child(uid).update({
stripeId: customerId,
email: email,
id: uid
})
})
.catch((err) => {
console.log(err);
throw new functions.https.HttpsError(
"internal",
" Unable to create Stripe user : " + err
);
});
})
Kotlin 函数
registerViewStubSignUpButton.setOnClickListener {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(registerViewStubEmailTextView.text.toString(), registerViewStubPasswordTextView.text.toString())
.addOnCompleteListener {
if (!it.isSuccessful) return@addOnCompleteListener
// else if successful
uploadImageToFirebaseStorage()
uploadStripeCustomer()
}
.addOnFailureListener {
Toast.makeText(this, "Failed to create user: ${it.message}", Toast.LENGTH_SHORT).show()
}
}
private fun uploadImageToFirebaseStorage() {
if (selectedPhotoUri == null) return
val filename = UUID.randomUUID().toString()
val ref = FirebaseStorage.getInstance().getReference("/customer_profile_images/$filename")
ref.putFile(selectedPhotoUri!!)
.addOnSuccessListener {
ref.downloadUrl.addOnSuccessListener {
saveCustomerToFirebaseDatabase(it.toString())
}
}
}
private fun saveCustomerToFirebaseDatabase(profileImageUrl: String) {
val registerStub = findViewById<ViewStub>(R.id.registerStub)
val registerViewStubFullnameTextView = findViewById<TextInputEditText>(R.id.fullnameInputTextView)
val registerViewStubUsernameTextView = findViewById<TextInputEditText>(R.id.usernameTextInputView)
val registerViewStubEmailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = FirebaseDatabase.getInstance().getReference("/customers/$uid")
val customer = Customer(uid, registerViewStubFullnameTextView.text.toString(), registerViewStubUsernameTextView.text.toString(),
registerViewStubEmailTextView.text.toString(), profileImageUrl)
ref.setValue(customer)
.addOnSuccessListener {
registerStub.alpha = 0f
finish()
}
}
private fun uploadStripeCustomer() {
}
这取决于用例。如果 Firebase Cloud Function 对 iOS 和 Android 应用程序执行相同的任务,那么只使用一个应该可以。如果 iOS 和 Android 应用程序有一些不同的用例,那么应该使用两个不同的 Cloud Functions。
在你的例子中,当客户在 iOS 和 Android 应用程序中注册新帐户时,你似乎正在创建一个 stripeID
,所以这是明智的为 iOS 和 Android 应用程序使用一个 Firebase Cloud Functions。