Ant*_*han 3 ios stripe-payments swift google-cloud-functions
我目前正在通过Firebase云功能将Stripe集成到我的iOS应用程序中。我遇到一个奇怪的问题,当我尝试添加卡时,它告诉我在云功能中明确配置它时API密钥丢失了。
我注意到的一件事是,如果我不包括STPPaymentConfiguration(),则在客户端,则代码可以正常工作,并且付款来源已添加到firebase和stripe中。我在这里想念什么吗?
我认为我不太了解前端方面的内容,因为
let addCardViewController = STPAddCardViewController()
Run Code Online (Sandbox Code Playgroud)
我的代码可以正常工作,但是应该可以,但是现在视图控制器没有帐单地址选项。
我的前端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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的云功能代码:
'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});
});
});
Run Code Online (Sandbox Code Playgroud)
向我添加配置STPAddCardViewController时,它给我一个“您未提供API密钥”错误。
问题似乎是您正在创建一个新的STPPaymentConfiguration实例(未为其设置Stripe可发布密钥),而不是使用共享实例(您可能在代码中的其他位置设置了可发布密钥)。
您需要进行以下更改:
let config = STPPaymentConfiguration.shared()
实例化let addCardViewController = STPAddCardViewController()工作的原因是因为初始化程序实际上STPPaymentConfiguration.shared()用于其配置。
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |