gpt4 book ai didi

swift - 以编程方式填充 STPPaymentCardTextField

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:20 26 4
gpt4 key购买 nike

我正在开发一个应用程序并使用 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。你觉得我能用这个做什么?谢谢!

最佳答案

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)
}

像魅力一样工作。

关注此线程以了解 future 对 Stripe SDK 的内置支持的潜在更新。

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

关于swift - 以编程方式填充 STPPaymentCardTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32609843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com