Validating credit cards using Stripe with iOS
- (IBAction)buyButtonTapped:(id)sender{
param = [[STPCardParams alloc]init];
param.number = txt_cc_no.text;
param.cvc = txt_cc_cvvno.text;
param.expMonth =[self.selectedMonth integerValue];
param.expYear = [self.selectedYear integerValue];
if ([self validateCustomerInfo]) {
[self performStripeOperation];
}
}
- (BOOL)validateCustomerInfo {
//2. Validate card number, CVC, expMonth, expYear
[STPCardValidator validationStateForExpirationMonth:[self.selectedMonth stringValue]];
[STPCardValidator validationStateForExpirationYear:[self.selectedYear stringValue] inMonth:[self.selectedMonth stringValue]];
if ([txt_cc_type.text isEqualToString:@"visa"]) {
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandVisa];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandVisa];
}
else if ([txt_cc_type.text isEqualToString:@"MasterCard"]){
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandMasterCard];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandMasterCard];
}
else if ([txt_cc_type.text isEqualToString:@"American Express"]){
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandAmex];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandAmex];
}
else if ([txt_cc_type.text isEqualToString:@"Discover"]){
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandDiscover];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandDiscover];
}
else if ([txt_cc_type.text isEqualToString:@"Diners Club"]){
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandDinersClub];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandDinersClub];
}
else if ([txt_cc_type.text isEqualToString:@"JCB"]){
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandJCB];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandJCB];
}
else if ([txt_cc_type.text isEqualToString:@"Unknown"]){
[STPCardValidator validationStateForCVC:txt_cc_cvvno.text cardBrand:STPCardBrandUnknown];
[STPCardValidator validationStateForNumber:txt_cc_no.text validatingCardBrand:STPCardBrandUnknown];
}
return YES;
}
- (void)performStripeOperation {
[[STPAPIClient sharedClient] createTokenWithCard:param
completion:^(STPToken *token, NSError *error) {
if (error) {
// [self handleError:error];
NSLog(@"ERRRRR = %@",error);
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Please try again"
message:[NSString stringWithFormat:@"%@",error.localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else {
//when credit card details is correct code here
}
}];
}
Swift 5.0+
Don't forget to import Stripe
func isCardDetailValid(cardNumber : String, month : UInt, year : UInt, cvv : String) -> Bool {
let params = STPCardParams()
params.number = cardNumber
params.expMonth = month
params.expYear = year
params.cvc = cvv
if STPCardValidator.validationState(forCard: params) == .valid {
return true
} else {
return false
}
}
Swift 2.1 Version of Femina's answer. Note: validateCardType() is a regex function, if you want that too you can find it here How do you detect Credit card type based on number?
func validateCustomerInfo() -> Bool {
// Validate card number, CVC, expMonth, expYear
STPCardValidator.validationStateForExpirationMonth(self.expMonth.text!)
STPCardValidator.validationStateForExpirationYear(self.expYear.text!, inMonth: self.expMonth.text!)
if (self.validateCardType(creditCardField.text!) == "Visa") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.Visa)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
else if (self.validateCardType(creditCardField.text!) == "MasterCard") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.MasterCard)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
else if (self.validateCardType(creditCardField.text!) == "American Express") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.Amex)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
else if (self.validateCardType(creditCardField.text!) == "Discover") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.Discover)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
else if (self.validateCardType(creditCardField.text!) == "Diners Club") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.DinersClub)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
else if (self.validateCardType(creditCardField.text!) == "JCB") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.JCB)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
else if (self.validateCardType(creditCardField.text!) == "Unknown") {
STPCardValidator.validationStateForCVC(self.securityField.text!, cardBrand: STPCardBrand.Unknown)
STPCardValidator.validationStateForNumber(self.creditCardField.text!, validatingCardBrand: true)
}
return true
}