[FIXED] Validierung von Kreditkarten mit Stripe mit iOS

Ausgabe

Ich fange an, Stripe in eine von mir erstellte iOS-App zu integrieren, und ich bin an einem Punkt angelangt, an dem ich die Karteninformationen validieren muss.

Ich habe es versucht:

#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;

}

Ich habe hier auf StackOverflow ein paar andere Fragen überprüft, und jeder scheint die validateCardReturningErrorMethode zu verwenden, und sie ist für iOS9 veraltet.

Xcode beschwert sich und bittet mich, stattdessen STPCardValidator zu verwenden.

Kann mir jemand mit einem Beispiel für die Kartenvalidierung mit STPCardValidator helfen?

Mir ist aufgefallen, dass ich auch die folgende Klassenmethode verwenden könnte:

[STPCardValidator validationStateForNumber:self.txtFieldCardNumber.text validatingCardBrand:true];

Aber ich bin mir nicht sicher, wie ich das in meiner Methode validateCustomerInfo verwenden soll.

Lösung

- (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
                                              }
                                          }];
}


Beantwortet von –
Femina Brahmbhatt


Antwort geprüft von –
Dawn Plyler (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like