ios - iOS 中的 Stripe 集成 - 您没有提供 API key ?

标签 ios swift google-cloud-functions stripe-payments

我目前正致力于通过 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/

相关文章:

ios - 简单的 iOS NavigationController + PDFViewer 在每次初始化时都会泄漏 1.2 到 2 Mb 的内存。 (内存泄漏)( swift )

c - 如何在swift中导入c枚举

swift - AWS 认知 : Trying to Alert User of Error

ios - 在 UNNotification 我得到的 userInfo 值与 UNNotificationRequest Swift 中的设置值不同

javascript - 在 Firebase 函数中计算后删除传入的写入事件

javascript - 如何使用 typescript 中的函数从firestore获取文档?

ios - 如何动态为按钮分配方法

iphone - 推送通知的 APNS 端口号和主机名

ios - 如何指示用户滑动的方向,而不是 Swift 中的下一个和上一个 View Controller ?

javascript - 无需等待响应即可调用云函数