【问题标题】:swift - Populate STPPaymentCardTextField Programmaticallyswift - 以编程方式填充 STPPaymentCardTextField
【发布时间】:2015-12-13 02:43:27
【问题描述】:

我正在开发一个应用程序并使用 Stripe SDK 和 Card.io SDK。我想要发生的是用Card.io 扫描的信用卡值填充STPPaymentCardTextField 卡号、到期月份和年份。我试过了:

var paymentField = STPPaymentCardTextField()

func userDidProvideCreditCardInfo(cardInfo: CardIOCreditCardInfo!, inPaymentViewController paymentViewController: CardIOPaymentViewController!) {
    var scanViewController: CardIOPaymentViewController = CardIOPaymentViewController(paymentDelegate: self)
    paymentField.cardNumber = cardInfo.cardNumber
    paymentField.expirationMonth = cardInfo.expiryMonth
    paymentField.expirationYear = cardInfo.expiryYear

    paymentViewController.dismissViewControllerAnimated(true, completion: nil)
}

对于每个 paymentField 附加,我都有一个错误 Cannot assign to the result of this expression。 你觉得我能用这个做什么?谢谢!

【问题讨论】:

  • 是吗?我看不出它不应该工作的任何原因......你到底有什么问题?
  • 对于每个 paymentField 追加,我都有一个错误 Cannot assign to the result of this expression。

标签: swift stripe-payments card.io


【解决方案1】:

STPPaymentCardTextField 字段是只读的,您只能从这些属性中“获取”。

STPPaymentCardTextField 用于收集信用卡详细信息。在您的情况下,您已经在使用 CardIOCreditCardInfo 执行此操作。获得信用卡详细信息后,您可以将数据组装到 STPCardParams 对象中。

收集卡号、有效期和 CVC 后,将它们打包到 STPCardParams 对象中并调用 STPAPIClient 类的 createTokenWithCard: 方法。

现在你的方法可能看起来像这样......

func userDidProvideCreditCardInfo(cardInfo: CardIOCreditCardInfo!, inPaymentViewController paymentViewController: CardIOPaymentViewController!) {

        let card: STPCardParams = STPCardParams()
        card.number = info.cardNumber
        card.expMonth = info.expiryMonth
        card.expYear = info.expiryYear
        card.cvc = info.cvv

        STPAPIClient.sharedClient().createTokenWithCard(card, completion: {(result, error) -> Void in
            if error == nil {
                // Either save the card token with your customer data or charge them right away
                //createBackendChargeWithToken(token)
            }
            else {
                //handleError(error)
            }
        })
    }
    paymentViewController?.dismissViewControllerAnimated(true, completion: nil)
}

更新可能是您问题的正确答案。

import Stripe
class PaymentCardEditorField: STPPaymentCardTextField {
    func setExistingCard(card: STPCardParams) {
        replaceField("numberField", withValue: card.number!)
        replaceField("expirationField", withValue: String(format: "%02d/%02d", card.expMonth, (card.expYear % 100)))
        replaceField("cvcField", withValue: card.cvc!)
    }

    func replaceField(memberName: String, withValue value: String) {
        let field = self.valueForKey(memberName) as! UITextField
        let delegate = self as! UITextFieldDelegate
        let len = field.text?.characters.count
        if delegate.textField?(field, shouldChangeCharactersInRange: NSMakeRange(0, len!), replacementString: value) ?? false {
        field.text = value
        }
    }
}

然后调用setExistingCard

func userDidProvideCreditCardInfo(cardInfo: CardIOCreditCardInfo!, inPaymentViewController paymentViewController: CardIOPaymentViewController!) {

    let card: STPCardParams = STPCardParams()
    card.number = info.cardNumber
    card.expMonth = info.expiryMonth
    card.expYear = info.expiryYear
    card.cvc = info.cvv
    paymentTextField.setExistingCard(card)
}

像魅力一样工作。

关注此线程以获取 Stripe SDK 的潜在更新,以便在未来获得内置支持。

https://github.com/stripe/stripe-ios/issues/127

【讨论】:

    【解决方案2】:

    我得到了一个更清晰的答案from here

    let cardParams = STPCardParams()
    cardParams.number = "4242424242424242"
    cardParams.expMonth = 07 // this data type is UInt and *not* Int
    cardParams.expYear = 19 // this data type is UInt and *not* Int
    cardParams.cvc = "123"
    
    let paymentField = STPPaymentCardTextField()
    paymentField.cardParams = cardParams //the paymentTextField will now show the cc #, exp, and cvc from above
    

    您可以获取其他测试卡号,例如 Mastercard、Amex 和 Discover from here from Stripe

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多