我目前正在通过Firebase云功能将Stripe集成到我的iOS应用程序中。我遇到一个奇怪的问题,当我尝试添加卡时,它告诉我在我的云函数中明确配置它时,我的API密钥丢失了。
我注意到的一件事是如果不包含STPPaymentConfiguration(),则在客户端,那么代码可以正常工作,并且付款来源已添加到firebase和stripe中。我在这里想念什么吗?
我认为前端方面我不太了解,因为使用
let addCardViewController = STPAddCardViewController()
我的代码可以正常工作,但是应该可以,但是现在视图控制器没有帐单地址选项。
我的前端快速代码:
@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密钥”错误。
问题似乎是您正在创建一个新的STPPaymentConfiguration实例(该实例未设置您的Stripe可发布密钥),而不是使用共享实例(您可能在代码的其他位置设置了可发布密钥)。
您需要进行以下更改:let config = STPPaymentConfiguration.shared()
实例化let addCardViewController = STPAddCardViewController()
的原因是因为初始化程序实际上将STPPaymentConfiguration.shared()
用于其配置。
我遇到了同样的错误。我正在创建STPAPIClient()的实例并设置publishableKey键。
let client = STPAPIClient()
client.publishableKey = ""
正确的方法是使用STPAPIClient()的共享实例
STPAPIClient.shared().publishableKey = ""
let cardParams = STPCardParams()
cardParams.number = cardTextField.cardNumber
cardParams.expMonth = (cardTextField.expirationMonth)
cardParams.expYear = (cardTextField.expirationYear)
cardParams.cvc = cardTextField.cvc
STPAPIClient.shared().createToken(withCard: cardParams) { (token: STPToken?, error: Error?) in
guard let token = token, error == nil else {
print(error?.localizedDescription)
}
}