Stripe - STPPaymentCardTextField - 如何删除或隐藏CVC代码字段?

时间:2016-08-23 09:59:54

标签: ios swift stripe-payments

Stripe的iOS SDK“STPPaymentCardTextField”有4个字段用于生成卡片令牌:1。信用卡号码2.到期月份3.到期年份4. CVC字段

我们只保存信用卡号码和到期月/年。我们不需要从用户那里获得CVC号码。那么如何从STPPaymentCardTextField中隐藏或删除CVC字段。

请给我一些处理这个过程的想法。

2 个答案:

答案 0 :(得分:1)

可能但我们需要仔细执行此操作,确定打开STPPaymentCardTextField.m文件并获得CVC字段名称的属性为STPFormTextField

@property(nonatomic, readwrite, weak)STPFormTextField *cvcField; 

最后隐藏与cvcField相关的内容,只要它在该页面上占据该页面中的 21个地方,例如

STPFormTextField *cvcField = [self buildTextField];
cvcField.tag = STPCardFieldTypeCVC;
cvcField.alpha = 0;
self.cvcField = cvcField;
self.cvcPlaceholder = @"CVC";
[self.fieldsView addSubview:cvcField];

- (void)setCvcPlaceholder:(NSString * __nullable)cvcPlaceholder {
_cvcPlaceholder = [cvcPlaceholder copy];
self.cvcField.placeholder = _cvcPlaceholder;
}

否则隐藏

[self.fieldsView addSubview:cvcField];

答案 1 :(得分:0)

第一种方式:修改框架源

如果你可以修改框架源,你可以更容易地做到这一点:

STPPaymentCardTextField.m更改

self.allFields = @[numberField,
                   expirationField,
                   cvcField,
                   postalCodeField];

self.allFields = @[numberField,
                   expirationField,
                   postalCodeField];

并在STPPaymentCardTextFieldViewModel.m更改

- (BOOL)isValid {
    return ([self validationStateForField:STPCardFieldTypeNumber] == STPCardValidationStateValid
            && [self validationStateForField:STPCardFieldTypeExpiration] == STPCardValidationStateValid
            && [self validationStateForField:STPCardFieldTypeCVC] == STPCardValidationStateValid
            && (!self.postalCodeRequired
                || [self validationStateForField:STPCardFieldTypePostalCode] == STPCardValidationStateValid));
}

- (BOOL)isValid {
    return ([self validationStateForField:STPCardFieldTypeNumber] == STPCardValidationStateValid
            && [self validationStateForField:STPCardFieldTypeExpiration] == STPCardValidationStateValid
            && (!self.postalCodeRequired
                || [self validationStateForField:STPCardFieldTypePostalCode] == STPCardValidationStateValid));
}

第二种方式:对STPPaymentCardTextField进行子类化

我不喜欢编辑框架,因为我想更新,所以我通过继承STPPaymentCardTextField(Swift中的例子)来做到这一点:

class STPPaymentCardTextFieldNoCVC: STPPaymentCardTextField {

  func removeCVC() {
    if let fieldsView = subviews.first {
      for view in fieldsView.subviews where view.tag == 2 {
        // set a default cvc for the validator and remove the field
        (view as? UITextField)?.text = "123"
        view.removeFromSuperview()
      }
    }
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    removeCVC()
  }

  override init(frame: CGRect) {
    super.init(frame: frame)
    removeCVC()
  }
}