I am starting to integrate Stripe to an iOS app I am making and I have got to a point where I need to validate the card information.
I tried:
#pragma mark - My Actions
- (IBAction)buyButtonTapped:(id)sender {
//Populate STPCard property
self.stripeCard = [[STPCard alloc]init];
self.stripeCard.name = self.txtFieldCardHolderName.text;
self.stripeCard.number = self.txtFieldCardNumber.text;
self.stripeCard.cvc = self.txtFieldCardCvc.text;
self.stripeCard.expMonth = [self.selectedMonth integerValue];
self.stripeCard.expYear = [self.selectedYear integerValue];
//Validate Customer info
if ([self validateCustomerInfo]) {
[self performStripeOperation];
}
}
-(BOOL)validateCustomerInfo{
//Create Alert
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please, try again!"
message:@"Please, enter all required information."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
//Add some action
}];
//Add its action
[alert addAction:action];
//Validate text fields
if (self.txtFieldCardHolderName.text == 0 || self.txtFieldCardNumber.text == 0 || self.txtFieldCardExpDate.text == 0 || self.txtFieldCardCvc.text == 0) {
[self presentViewController:alert animated:true completion:nil];
return NO;
}
//Validate card number, CVC, expMonth, expYear
NSError *error = nil;
[self.stripeCard validateCardReturningError:&error];
//3
if (error) {
alert.message = [error localizedDescription];
[self presentViewController:alert animated:true completion:nil];
return NO;
}
return YES;
}
I checked a couple of other questions here on StackOverflow and everyone seems to use validateCardReturningError
method, and it is deprecated for iOS9.
Xcode complains and ask me to use STPCardValidator instead.
Can anyone help me with one example of card validation using STPCardValidator?
I noticed I could use the following Class method as well:
[STPCardValidator validationStateForNumber:self.txtFieldCardNumber.text validatingCardBrand:true];
But I'm not sure how to use that inside my validateCustomerInfo method.