1

I'm using the STPPaymentCardTextField to collect card information from the user.

I can use the delegate method of the STPPaymentCardTextFieldDelegate to get information like the 'cardNumber, expirationMonth etc:

func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
   print("NUMBER", textField.cardNumber)
   print("EXP MONTH", textField.expirationMonth)
   print("EXP YEAR", textField.expirationYear)
   print("CVC", textField.cvc)   
}

However, this textField seems to also display the card type, based on the info the user types in: enter image description here

How can I programmatically access this card type? I use this in another UIView to generate a preview of the card, set its background color and logo based on the card type etc.

Alk
  • 5,215
  • 8
  • 47
  • 116

3 Answers3

0

You can get the brand image directly: https://stripe.dev/stripe-ios/docs/Classes/STPPaymentCardTextField.html#/c:objc(cs)STPPaymentCardTextField(py)brandImage

In your case it'll be textField.brandImage.

Paul Asjes
  • 5,361
  • 1
  • 18
  • 20
0
// number = credit card number

let card = CreditCardValidator()
let imageName = card.type(from: number)
        
print(imageName!.name)
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes – Boken Dec 09 '20 at 09:21
0

You can use STPCardValidator , Stripe Docs for Card Validator.

if you want to print the type in above function , here is the code

func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
   print("NUMBER", textField.cardNumber)
   print("EXP MONTH", textField.expirationMonth)
   print("EXP YEAR", textField.expirationYear)
   print("CVC", textField.cvc)   
   print("Brand Type", STPCardValidator.brand(forNumber: textField.cardNumber))
}
Kashif
  • 1
  • https://stripe.dev/stripe-android/stripe/com.stripe.android/-card-utils/index.html if you want to look for android as well. where you can use getPossibleCardBrand – Kashif Dec 23 '20 at 12:48