Rutger Huijsmans Rutger Huijsmans -4 years ago 122
iOS Question

How to get card brand using Stripe and Swift

STPAPIClient.shared().createToken(withCard: cardParams) { (token, error) in
if error != nil {
//fail
} else if let token = token {
print(token.card?.brand) //Optional(__C.STPCardBrand)
print(token.card?.brand.hashValue) //Optional(0)
print(token.card?.brand.rawValue) //Optional(0)
}
}


Does anyone know why Stripe isn't returning the card brand? I'm using a Stripe test card and the rest of the info is getting returned.

Answer Source

So checking the API documentation I found that brand is en enum:

var brand: STPCardBrand { get }

having these values:

typedef NS_ENUM(NSInteger, STPCardBrand) {
    STPCardBrandVisa,
    STPCardBrandAmex,
    STPCardBrandMasterCard,
    STPCardBrandDiscover,
    STPCardBrandJCB,
    STPCardBrandDinersClub,
    STPCardBrandUnknown,
};

You could also consider using the static stringFromBrand function:

Returns a string representation for the provided card brand; i.e. [NSString stringFromBrand:STPCardBrandVisa] == @"Visa". Declaration

  • (nonnull NSString *)stringFromBrand:(STPCardBrand)brand;

class func string(from brand: STPCardBrand) -> String

Example:

print((STPCard.stringFromBrand(from: token.card?.brand))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download