2015-09-16 159 views
0

我正在開發一個應用程序並使用Stripe SDK和Card.io SDK。我想要發生的事情是用Card.io掃描的信用卡值填充STPPaymentCardTextField卡號,到期月和年份。我想:swift - 以編程方式填充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。 你認爲我可以用這個做什麼?謝謝!

+0

是嗎?我不明白爲什麼它不應該工作...什麼是你的問題,到底是什麼? – mattt

+0

我有一個錯誤'不能分配給每個'paymentField'追加的這個表達式的結果。 –

回答

0

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

就像一個魅力。

按照這個線程的潛在更新條紋SDK在未來的內置支持。

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

相關問題