卡验证
在处理您客户的卡之前验证卡的详情。
警告:该验证并不会检查客户的卡详情是否正确。验证只会检查所输入的详情格式。
开始
- 您已将
AccessCheckoutSDK
作为 Cocoapods 依赖项添加到您的项目中 - 您在 swift 文件的顶部添加了一个
import AccessCheckoutSDK
该集成的四个基本组件包括:
- 您的 UITextField 用于 PAN、过期日期和 CVC
AccessCheckoutCardValidationDelegate
旨在接收与验证有关的事件CardValidationConfig
的实例包含验证流初始化所需的所有信息,包括对要启用验证所用的视图组件的引用AccessCheckoutValidationInitialiser
负责初始化验证流
完整示例集成:您可以在
创建和引用 UI 组件
若要显示您的结账表,则必须首先使用故事板来创建您的布局。
以下是如何利用独特的识别码来引用您的 UI 组件的示例。
import AccessCheckoutSDK
class ViewController: UIViewController {
@IBOutlet weak var panTextField: UITextField!
@IBOutlet weak var expiryDateTextField: UITextField!
@IBOutlet weak var cvcTextField: UITextField!
...
import AccessCheckoutSDK class ViewController: UIViewController { @IBOutlet weak var panTextField: UITextField! @IBOutlet weak var expiryDateTextField: UITextField! @IBOutlet weak var cvcTextField: UITextField! ...
实施 AccessCheckoutCardValidationDelegate
协议
这可确保您在客户输入时收到验证事件和卡品牌更改的通知。
以下为示例。
extension ViewController: AccessCheckoutCardValidationDelegate {
// This event handler is notified when the card brand detected by the SDK has changed
func cardBrandChanged(cardBrand: CardBrand?) {
// This piece of code updates in the UI a card image displayed in a UIImageView next to the PAN
// A cardBrand contains a name and an array of images objects.
// Each image object has a URL and a type (either image/png or image/svg+xml) and represents the same brand
if let imageUrl = cardBrand?.images.filter({ $0.type == "image/png" }).first?.url,
let url = URL(string: imageUrl) {
DispatchQueue.global(qos: .userInteractive).async {
if let data = try? Data(contentsOf: url) {
DispatchQueue.main.async {
self.imageView.image = UIImage(data: data)
}
}
}
} else {
// This occurs when the SDK has not been able to identify the card brand from the PAN entered by the customer
self.imageView.image = ...
}
}
// This event handler is notified when the PAN becomes valid or invalid
func panValidChanged(isValid: Bool) {
// You might want to change the text colour
panTextField.textColor = isValid ? nil : UIColor.red
if !valid {
// You might want to disable a submit button which would normally be clicked on when all fields are valid
submitButton.isEnabled = false
}
}
// This event handler is notified when the CVC becomes valid or invalid
func cvcValidChanged(isValid: Bool) {
// You might want to change the text colour
cvcTextField.textColor = isValid ? nil : UIColor.red
if !valid {
// You might want to disable a submit button which would normally be clicked on when all fields are valid
submitButton.isEnabled = false
}
}
// This event handler is notified when the expiry date becomes valid or invalid
func expiryDateValidChanged(isValid: Bool) {
// You might want to change the text colour
expiryDateTextField.textColor = isValid ? nil : UIColor.red
if !valid {
// You might want to disable a submit button which would normally be clicked on when all fields are valid
submitButton.isEnabled = false
}
}
// This event handler is notified when the PAN, expiry date and CVC are all valid
func validationSuccess() {
// You might want to enable a submit button when all fields are valid
submitButton.isEnabled = true
}
}
extension ViewController: AccessCheckoutCardValidationDelegate { // This event handler is notified when the card brand detected by the SDK has changed func cardBrandChanged(cardBrand: CardBrand?) { // This piece of code updates in the UI a card image displayed in a UIImageView next to the PAN // A cardBrand contains a name and an array of images objects. // Each image object has a URL and a type (either image/png or image/svg+xml) and represents the same brand if let imageUrl = cardBrand?.images.filter({ $0.type == "image/png" }).first?.url, let url = URL(string: imageUrl) { DispatchQueue.global(qos: .userInteractive).async { if let data = try? Data(contentsOf: url) { DispatchQueue.main.async { self.imageView.image = UIImage(data: data) } } } } else { // This occurs when the SDK has not been able to identify the card brand from the PAN entered by the customer self.imageView.image = ... } } // This event handler is notified when the PAN becomes valid or invalid func panValidChanged(isValid: Bool) { // You might want to change the text colour panTextField.textColor = isValid ? nil : UIColor.red if !valid { // You might want to disable a submit button which would normally be clicked on when all fields are valid submitButton.isEnabled = false } } // This event handler is notified when the CVC becomes valid or invalid func cvcValidChanged(isValid: Bool) { // You might want to change the text colour cvcTextField.textColor = isValid ? nil : UIColor.red if !valid { // You might want to disable a submit button which would normally be clicked on when all fields are valid submitButton.isEnabled = false } } // This event handler is notified when the expiry date becomes valid or invalid func expiryDateValidChanged(isValid: Bool) { // You might want to change the text colour expiryDateTextField.textColor = isValid ? nil : UIColor.red if !valid { // You might want to disable a submit button which would normally be clicked on when all fields are valid submitButton.isEnabled = false } } // This event handler is notified when the PAN, expiry date and CVC are all valid func validationSuccess() { // You might want to enable a submit button when all fields are valid submitButton.isEnabled = true } }
将 CardValidationConfig
实例化并初始化验证
我们强烈建议在 UIViewController 的 viewDidLoad()
处理器中这样做。这是因为 AccessCheckoutValidationInitialiser
会异步请求一个包含验证配置的文件。
override func viewDidLoad() {
...
let validationConfig = try! CardValidationConfig.builder()
.pan(panTextField)
.expiryDate(expiryDateTextField)
.cvc(cvcTextField()
.accessBaseUrl(<ACCESS_BASE_URL>)
.validationDelegate(self)
.build()
AccessCheckoutValidationInitialiser().initialise(validationConfig)
}
override func viewDidLoad() { ... let validationConfig = try! CardValidationConfig.builder() .pan(panTextField) .expiryDate(expiryDateTextField) .cvc(cvcTextField() .accessBaseUrl(<ACCESS_BASE_URL>) .validationDelegate(self) .build() AccessCheckoutValidationInitialiser().initialise(validationConfig) }
占位符 | 描述 |
---|---|
<ACCESS_BASE_URL> |
|
卡验证规则
SDK 中的验证逻辑是基于任何卡类型的一套默认规则。具体品牌规则可从 CardTypes
JSON 文件中获取,其中包含了每个品牌的验证规则和卡品牌徽标。徽标分为 SVG
和 PNG
两种格式。
Validation rules
下表列出了我们的 SDK 用于验证您的客户卡详情的规则。
卡名称 | BIN 范围 | PAN 长度 | CVC 长度 |
---|---|---|---|
Amex | 34、37 | 15 | 4 |
Diners | 300-305、3095、36、38、39 | 14、16、19 | 3 |
Discover | 6011、644 - 649、65 | 16、19 | 3 |
JCB | 2131、1800、3088 - 3094、3096 - 3102、3112 - 3120、3158 - 3159、3337 - 3349、3528 - 3589 | 16、17、18、19 | 3 |
Maestro | 493698、500000 - 506698、506779 - 508999、56 - 59、63、67、6 | 12、13、14、15、16、17、18、19 | 3 |
MasterCard | 51 - 55、2221 - 2229、223 - 229、23 - 26、270 - 271、2720:使用 22 - 27 | 16 | 3 优化 |
Visa | 4 | 13、16、18、19 | 3 |
卡品牌限制
SDK 可让您选择性地提供所支持的卡品牌列表。也就是说,如果您不支持特定卡品牌,在识别到不支持的品牌时,SDK 会通知您无效的 PAN 代码。
默认情况下,SDK 允许任何品牌的卡。如果您不希望限制自己接受的卡,则不需要传送任何配置。
示例配置
若要限制您接受的卡品牌,只需传送在初始化 SDK 时您的确希望接受的品牌阵列即可。
以下验证配置可限制 SDK 只接受 American Express、Visa 或 Mastercard BIN 系列。
let validationConfig = try! CardValidationConfig.builder()
...
.acceptedCardBrands(["visa", "mastercard", "AMEX"])
...
.build()
let validationConfig = try! CardValidationConfig.builder() ... .acceptedCardBrands(["visa", "mastercard", "AMEX"]) ... .build()
当前支持的卡品牌
SDK 能够识别以下卡品牌:
品牌 | 代码 |
---|---|
American Express | "amex" |
Diners | "diners" |
Discover | "discover" |
JCB | "jcb" |
Maestro | "maestro" |
Mastercard | "mastercard" |
Visa | "visa" |
如果 SDK 无法识别作为上述品牌之一的 PAN,只要它符合有效 PAN 的一般标准,则也会被准许。
PAN 格式化
SDK 允许根据客户类型进行 PAN 格式设置。在默认情况下,此功能被禁用。
PAN 的格式如下所示:
卡类型 | 格式设置 |
---|---|
Visa、Mastercard、JCB、Discover、Diners、Maestro | XXXX XXXX XXXX XXXX |
Amex | XXXX XXXXXX XXXXX |
启用 PAN 格式设置
如要启用 PAN 格式设置行为,只需调用构造器中的 enablePanFormatting
方法即可。
let validationConfig = try! CardValidationConfig.builder()
...
.enablePanFormatting()
...
.build()
let validationConfig = try! CardValidationConfig.builder() ... .enablePanFormatting() ... .build()
创建会话
一旦验证就位,您就可以请求 sessions
来实施您的支付流。
后续步骤