swift - 以编程方式填充STPPaymentCardTextField

时间:2015-09-16 13:21:27

标签: swift stripe-payments card.io

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

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

我为每个Cannot assign to the result of this expression追加发送错误paymentField。 你觉得我能怎么做?谢谢!

2 个答案:

答案 0 :(得分:0)

STPPaymentCardTextField字段是只读的,您只能" get"从那些属性。

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

答案 1 :(得分:0)

我得到了一个更简洁的答案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

您可以获得其他测试卡号,例如万事达卡,美国运通卡和发现from here from Stripe