【问题标题】:How to use two stripe keys in app delegate in swift ios如何在swift ios的应用程序委托中使用两个条带键
【发布时间】:2023-03-22 03:28:01
【问题描述】:

我想在 Swift iOS 中的 App 委托字段中添加两个条带键。我有两个条纹帐户,根据条件我想做信用卡/借记卡付款。提前致谢。

我所做的不是在 App 委托中声明字符串键,而是在类本身中声明键,在该类中我为支付创建令牌,如下所示。我使用每个键的每个班级都有两个班级。

@IBAction func submitCard(_ sender: AnyObject) {

    let cardParams = paymentTextField.cardParams
    Stripe.setDefaultPublishableKey("pk_test_1234567899875gh")// example key

    // It takes the card details and creates the token here.

    STPAPIClient.shared().createToken(withCard: cardParams) { token, error in
        guard token != nil else {

           print("Error creating token: %@", error!.localizedDescription);
           return
        }
        print(token)
    }
}

这是写的吗?还是我需要更改代码?

注意:这与环境无关,而是两个不同的条带帐户。

【问题讨论】:

  • STPPaymentConfiguration.shared().publishableKey 仅接受单个密钥。如果您想要有条件的付款,最好在网上处理付款。您只需将付款页面加载到 WKWebView。

标签: ios swift stripe-payments


【解决方案1】:

您可以使用不同的密钥简单地创建多个 STPAPIClients,然后根据需要使用它们,而不是使用共享实例。

https://stripe.dev/stripe-ios/docs/Classes/STPAPIClient.html#/c:objc(cs)STPAPIClient(im)initWithPublishableKey:

let clientA = STPAPIClient(publishableKey: "pk_123")
let clientB = STPAPIClient(publishableKey: "pk_456")

if usingClientA {
    clientA.createToken(withCard: cardParams) { ... }
} else if usingClientB {
    clientB.createToken(withCard: cardParams) { ... }
}

另外,一般来说,您可能希望从后端服务器上的端点返回公钥,而不是硬编码到应用程序中,因为如果您需要更改所使用的 Stripe 帐户而无需通过,这样会更容易应用审查。

【讨论】:

    【解决方案2】:

    理想情况下,您应该只使用一个 Stripe 帐户。您在 AppDelegate 中设置 publishableKey 的方式取决于您当前的环境(即开发/生产),如下所示:

    let key = isProduction ? "pk_live_TYooMQauvdEDq54NiTphI7jx" : "pk_test_TYooMQauvdEDq54NiTphI7jx"
    Stripe.setDefaultPublishableKey(key)
    

    如果您需要根据您提到的“条件”设置新密钥,稍后只需使用函数setDefaultPublishableKey设置即可。

    【讨论】:

      【解决方案3】:

      你可以在AppDelegate中使用这个条件:

      #if DEBUG
          Stripe.setDefaultPublishableKey(testKey)
      #else
          Stripe.setDefaultPublishableKey(liveKey)
      #endif
      

      结果:如果您将使用调试环境,那么 Stripe 将获得测试密钥。例如,在归档和部署到 TestFlight 的情况下,Stripe 将获得实时密钥。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多