简体   繁体   English

在 iOS 上使用 Stripe 验证信用卡

[英]Validating credit cards using Stripe with iOS

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.我开始将 Stripe 集成到我正在制作的 iOS 应用程序中,我已经到了需要验证卡信息的地步。

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.我在 StackOverflow 上检查了其他几个问题,似乎每个人都使用validateCardReturningError方法,但 iOS9 不推荐使用它。

Xcode complains and ask me to use STPCardValidator instead. Xcode 抱怨并要求我改用 STPCardValidator。

Can anyone help me with one example of card validation using STPCardValidator?任何人都可以帮助我使用 STPCardValidator 进行卡验证的一个示例吗?

I noticed I could use the following Class method as well:我注意到我也可以使用以下 Class 方法:

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

But I'm not sure how to use that inside my validateCustomerInfo method.但我不确定如何在我的 validateCustomerInfo 方法中使用它。

- (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+斯威夫特 5.0+

Don't forget to import Stripe不要忘记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. Femina 答案的 Swift 2.1 版本。 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?注意:validateCardType() 是一个正则表达式函数,如果你也想要它,你可以在这里找到它如何根据号码检测信用卡类型?

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
}

The simplest way of doing this instead of doing all the calculations.执行此操作的最简单方法,而不是进行所有计算。

You could use the validationStateForCard method of STPCardValidator which takes STPCardParams instance as an argument.您可以使用STPCardValidatorvalidationStateForCard方法,该方法将 STPCardParams 实例作为参数。

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

}

func validateCustomerInfo() -> Bool {

    // Validate card number, CVC, expMonth, expYear
    return STPCardValidator.validationStateForCard(param) == .Valid
}

For swift 3快速 3

    // Validate all params at once
    let cardSate = STPCardValidator.validationState(forCard: cardParams)
    // Validate card number, CVC, expMonth, expYear one by one
    let numberState = STPCardValidator.validationState(forNumber: cardParams.number, validatingCardBrand: true)
    let cvcState = STPCardValidator.validationState(forCVC: cardParams.cvc ?? "", cardBrand: STPCardValidator.brand(forNumber: cardParams.number ?? ""))
    let expMonthState = STPCardValidator.validationState(forExpirationMonth: String(cardParams.expMonth ?? 0))
    let expYearState = STPCardValidator.validationState(forExpirationYear: String(cardParams.expYear ?? 0), inMonth: String(cardParams.expMonth ?? 0))

It's the complete and more proper Swift answer of @iOSArchitect.com这是@iOSArchitect.com 完整且更正确的 Swift 答案

func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
    let isValid = isValidCard(cardNumber: textField.cardNumber, month: textField.expirationMonth, year: textField.expirationYear, cvv: textField.cvc)
}

func isValidCard(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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM