ios - iOS 中的 Stripe 集成 - 您没有提供 API 密钥?
问题描述
我目前正在通过 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()
用于其配置。
推荐阅读
- javascript - 从 Electron Packager 构建脚本设置应用程序变量
- julia - Julia 将 SymEngine 表达式写入文件
- java - 是否可以在 Spring Boot 中创建通用 CRUD
- javascript - React Router v4.2 风格
- dart - 如何使按钮的 onPressed [在单独的类中] 将值返回给父类?
- php - 页脚上的 Nofollow 链接
- php - 可以使用include将其他标签放在html标签之外吗?
- redis - 错误:与 127.0.0.1:6379 的 Redis 连接失败 - 连接 ECONNREFUSED 127.0.0。1:6379
- android - 如何获取选中的 radioButton 的值,以便我能够在下一个活动中以复选框的形式显示它们?
- amazon-redshift - 验证 Redshift 中的目标查询组