Card fields
Place the SDK's card number, expiration date, CVV, and name inputs in your layout, follow their validation state, and pay with them.
PayCardFields groups the SDK's card inputs that pay together (a card number field, an expiration date field, a CVV field, and optionally a name field). Place the views anywhere in your layout, follow their state to drive your labels and your Pay button, then pass the set to pay(.card(_:)). The SDK owns what is typed into them. Your app never reads the card number, expiration date, or CVV (see PayElements). The drop-in has fields of its own.
PayCardFields and the field views run on the main actor.
let card = PayCardFields(name: PayNameField(), requiresName: true)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
number | PayCardNumberField | No | The card number input. Default: a new PayCardNumberField(). |
expiry | PayExpiryField | No | The expiration date input. Default: a new PayExpiryField(). |
cvc | PayCVCField | No | The security code input. Default: a new PayCVCField(). |
name | PayNameField? | No | The cardholder name input. Default: nil, no name field. |
requiresName | Bool | No | Whether the name must be filled. Default: false. With no name field and requiresName set to true, the set is never complete and pay throws invalidCardInput([.name]). |
Pass your own field to change its placeholder, for example PayCardNumberField(placeholder: "1234 1234 1234 1234").
Returns
A PayCardFields, with these members:
| Member | Type | Description |
|---|---|---|
number, expiry, cvc | Field views | The inputs to add to your layout. |
name | PayNameField? | The name input, when you passed one. |
requiresName | Bool | Whether the name must be filled. You can change it later. |
onChange | ((PayCardFieldsState) -> Void)? | Called on every edit of any field, even when no status changes, and when requiresName changes. |
onBinChanged | ((String?) -> Void)? | Called with the first 8 digits once they are typed, and with nil when fewer remain. Use it for BIN-based pricing or messaging. It never carries more of the number. It's the same property as number.onBinChanged, so setting one replaces the other. |
state | PayCardFieldsState | The status of every field, and the brand detected from the number. |
isComplete | Bool | true when every required field is valid. |
clear() | Empties every field. |
The CVV's expected length follows the brand of the number in the same set. A PayCVCField on its own, for a saved card's CVV, follows its brand, so set it to the card's cardBrand.
The field views
Each field view takes its own input and validates it.
| View | Default placeholder | Takes | Valid when |
|---|---|---|---|
PayCardNumberField | Card number | Up to 19 digits, shown in groups. | The length fits the brand detected from the first digits, and the number passes the Luhn check. Its brand is the PayCardBrand. |
PayExpiryField | MM / YY | 4 digits, shown as MM / YY. | The month is 01 to 12, and the date is neither past nor more than 20 years ahead. |
PayCVCField | CVC | Up to 4 digits. | It has 4 digits for American Express, 3 for other brands, and 3 or 4 while the brand is unknown. |
PayNameField | Name on card | Up to 200 characters. | It contains a letter. |
PayCardNumberField shows the card's brand at its end, matching Pay.com.js (the Visa, Mastercard, American Express, or Discover logo once detected, or a generic card icon). Set its showsBrandLogo to false to hide it.
Each view takes a placeholder in its initializer and sets the text content type AutoFill reads (card number on all versions, plus expiration date, CVV, and cardholder name starting in iOS 17). They share these members:
| Member | Type | Description |
|---|---|---|
onChange | ((PayFieldStatus) -> Void)? | Called when the field's PayFieldStatus changes. |
onFocusChange | ((Bool) -> Void)? | Called with true when the field gains focus and false when it loses it. |
status | PayFieldStatus | The current status. Read-only. |
style | PayFieldStyle | The field's look. See Style the card fields. |
placeholder | String? | The placeholder text. |
isEnabled | Bool | Whether the field accepts input. |
accessibilityLabel | String? | What VoiceOver reads for the input. The number field reads its placeholder, the others Expiry date, Security code, or Name on card. Set it to the visible label of your layout. While the number field shows a brand's logo, VoiceOver reads the brand after it, for example "Card number, Visa". |
becomeFirstResponder(), resignFirstResponder(), isFirstResponder | Move focus to or away from the field, and tell whether it has focus. | |
clear() | Empties the field. |
The views don't allow copying, cutting, sharing, or dragging their contents. You can't subclass them or read what was typed. Each is at least 44 points tall.
Errors
The fields don't throw. When they're incomplete or invalid, pay(.card(_:)) throws invalidCardInput with the invalid fields and sends nothing.
Example
import PayCheckout
import PayElements
import UIKit
final class CardViewController: UIViewController {
private let card = PayCardFields(name: PayNameField())
private let numberError = UILabel()
private let payButton = UIButton(configuration: .filled())
override func viewDidLoad() {
super.viewDidLoad()
let stack = UIStackView(arrangedSubviews: [card.number, numberError, card.expiry, card.cvc])
if let name = card.name { stack.addArrangedSubview(name) }
stack.axis = .vertical
stack.spacing = 12
view.addSubview(stack)
// Lay out the stack and the Pay button with your own constraints.
card.number.accessibilityLabel = "Card number"
payButton.isEnabled = false
card.onChange = { [weak self] state in
guard let self else { return }
payButton.isEnabled = state.isComplete
numberError.text = state.number == .invalid ? "Check the card number" : nil
}
card.onBinChanged = { [weak self] bin in
self?.updatePricing(bin: bin) // The first 8 digits, for example "42424242", or nil
}
}
}Next steps
- Style the card fields: Fonts, colors, borders, and padding.
- Pay: Pay with
.card(card). - Accept payments in your iOS app: The fields in a whole payment flow.