我目前正致力于通过 firebase 云功能将 Stripe 集成到我的 iOS 应用程序中。我遇到了一个奇怪的问题,当我尝试添加一张卡时,它告诉我我的 API key 丢失了,而我确实在我的云函数中配置了它。
我注意到的一件事是在客户端,如果我不包含 STPPaymentConfiguration(),那么代码会正常工作,并且支付源会添加到 firebase 和 stripe。我在这里遗漏了什么吗?
我认为这是我不太了解的前端方面的东西,因为
let addCardViewController = STPAddCardViewController()
我的代码工作正常,但现在 View Controller 没有账单地址选项。
我的前端swift代码:
@objc func addPaymentPressed(_ sender:UIButton) {
// Setup add card view controller
let config = STPPaymentConfiguration()
config.requiredBillingAddressFields = .full
let addCardViewController = STPAddCardViewController(configuration: config, theme: theme.stpTheme)
//Creating VC without configuration and theme works just fine
//let addCardViewController = STPAddCardViewController()
addCardViewController.delegate = self
let navigationController = UINavigationController(rootViewController: addCardViewController)
navigationController.navigationBar.stp_theme = theme.stpTheme
present(navigationController, animated: true, completion: nil)
}
func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) {
// Dismiss add card view controller
dismiss(animated: true)
}
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: @escaping STPErrorBlock) {
dismiss(animated: true)
let cardObject = token.allResponseFields["card"]
print("Printing Strip Token:\(token.tokenId)")
CustomerServices.instance.addPaymentToDB(uid: currentUserId, payment_token: token.tokenId, stripe_id: token.stripeID, cardInfo: cardObject as Any) { (success) in
if success {
print("successfully added card info to subcollection!")
} else {
print("TODO: add error message handler")
}
}
}
我的云功能代码:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const stripe = require('stripe')(functions.config().stripe.token);
const currency = functions.config().stripe.currency || 'USD';
// Add a payment source (card) for a user by writing a stripe payment source token to database
exports.addPaymentSource = functions.firestore
.document('Customers/{userId}/paymentSources/{paymentId}')
.onWrite((change, context) => {
let newPaymentSource = change.after.data();
let token = newPaymentSource.payment_token;
return admin.firestore().collection("Customers").doc(`${context.params.userId}`).get()
.then((doc) => {
return doc.data().customer_id;
}).then((customer) => {
return stripe.customers.createSource(customer, {"source" : token});
});
});
向我添加配置时 STPAddCardViewController 它给我一个“你没有提供 API key ”的错误。
最佳答案
问题看起来是您正在创建一个新的 STPPaymentConfiguration 实例(它没有设置您的 Stripe 可发布 key ),而不是使用共享实例(您可能在代码的其他地方设置了您的可发布 key ) .
您需要进行以下更改:
let config = STPPaymentConfiguration.shared()
仅仅实例化 let addCardViewController = STPAddCardViewController()
的原因是因为初始化程序实际上使用 STPPaymentConfiguration.shared()
进行配置。
关于ios - iOS 中的 Stripe 集成 - 您没有提供 API key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539161/