使用Stripe with iOS验证信用卡

时间:2015-10-15 00:28:57

标签: ios objective-c stripe-payments

我开始将Stripe集成到我正在制作的iOS应用中,我已经到了需要验证卡信息的地步。

我试过了:

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

}

我在StackOverflow上检查了其他几个问题,每个人似乎都使用validateCardReturningError方法,并且不推荐使用iOS9。

Xcode抱怨并要求我使用STPCardValidator。

任何人都可以帮助我使用STPCardValidator进行卡片验证的一个例子吗?

我注意到我也可以使用以下Class方法:

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

但我不确定如何在validateCustomerInfo方法中使用它。

5 个答案:

答案 0 :(得分:3)

Swift 5.0 +

别忘了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
    }
}

答案 1 :(得分:1)

Swift 2.1版本的Femina的回答。注意:validateCardType()是一个正则表达式函数,如果你也想要,你可以在这里找到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
}

答案 2 :(得分:1)

执行此操作的最简单方法,而不是执行所有计算。

您可以使用validationStateForCard STPCardValidator方法将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
}

答案 3 :(得分:0)

对于swift 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))

答案 4 :(得分:0)

这是@ 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
    }
}
相关问题