【发布时间】:2019-06-29 13:15:29
【问题描述】:
我目前正致力于通过 firebase 云功能将 Stripe 集成到我的 iOS 应用程序中。我遇到了一个奇怪的问题,当我尝试添加卡时,它告诉我我的 API 密钥丢失了,而我确实在我的云函数中配置了它。
我注意到的一件事是在客户端,如果我不包含 STPPaymentConfiguration(),那么代码可以正常工作,并且支付源会添加到 firebase 和 stripe。我在这里遗漏了什么吗?
我认为这是我不太了解的前端方面的东西,因为
let addCardViewController = STPAddCardViewController()
我的代码工作正常,但现在视图控制器没有帐单地址选项。
我的前端 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 密钥”错误。
【问题讨论】:
-
如果将
let config = STPPaymentConfiguration()更改为let config = STPPaymentConfiguration().shared()会发生什么? -
静态成员“共享”不能用于“STPPaymentConfiguration”类型的实例是我得到的
-
应该是
let config = STPPaymentConfiguration.shared(),shared()是类方法,不是实例方法。 -
使用以下方法得到相同的错误。 STPAPIClient.shared().createToken(withCard: cardParams) { (token: STPToken?, error: Error?) in.}
标签: ios swift google-cloud-functions stripe-payments