Event Handling and Helper Functions
The Paysafe Android SDK provides developers with methods and events for managing card form fields efficiently. These methods and events enable seamless integration of card-related functionalities into Android applications. Below are the available methods and events along with their descriptions.
Card form fields events
You can subscribe to specific events to handle customer interactions on card number, expiry date & CVV fields. By implementing event handlers, you can customize the behavior of the card form fields based on customer actions, ensuring a smooth and intuitive checkout experience.
The supported events are listed below:
Event | Description |
---|---|
INVALID | Triggered when invalid input is detected in a card form field. |
VALID | Triggered when valid input is detected in a card form field. |
FOCUS | Triggered when the customer focuses on the card form field. |
FIELD_VALUE_CHANGE | Triggered when the card form field value changes. |
INVALID_CHARACTER | Triggered when an invalid character is entered. |
An example is shown below:
cardNumberField.onEvent = { event ->
when (event) {
PSCardFieldInputEvent.FOCUS -> print("User focused on the field")
PSCardFieldInputEvent.VALID -> print("Valid value entered")
PSCardFieldInputEvent.INVALID -> print("Invalid value entered")
PSCardFieldInputEvent.FIELD_VALUE_CHANGE -> print("Field value changed event")
PSCardFieldInputEvent.INVALID_CHARACTER -> print("Invalid character entered in the field")
else -> {}
}
}
The supported events are described by the following enum:
/// PSCardFieldInputEvent
enum class PSCardFieldInputEvent {
FOCUS,
VALID,
INVALID,
FIELD_VALUE_CHANGE,
INVALID_CHARACTER
}
Card form field helper methods
All card fields define several common methods that you can use to interact with these components. These methods facilitate functionalities such as validation, resetting, and obtaining placeholder information, enabling seamless interaction with card-related input fields within the payment processing workflow.
Below are the method signatures along with their descriptions:
isEmpty() -> Boolean
This method checks whether the card field is empty or not.
Returns true if the field is empty.
let isFieldEmpty = cardNumberField.isEmpty() // Check if card number field is empty or not
isValid() -> Boolean
This method verifies whether the entered card information is valid or not.
Returns true if the field contains valid values.
let isFieldValid = cardNumberField.isValid() //Check if card number field is valid or not
reset() -> Void
This method resets the content of the card field, clearing any entered information.
cardField.reset()
placeholderString: String
This attribute retrieves the placeholder text for the card field, if available.
val placeholder = binding.cardField.placeholderString
Card form helper methods
PsCardFormController defines several common methods that you can use to interact with all the field components. These methods facilitate functionalities such as validation and resetting, plus obtaining and recognizing card brand information.
areAllFieldsValid() -> Boolean
This method returns a Boolean value indicating whether all fields in the card form controller are valid.
Returns true if all the fields are valid.
val isFormValid = cardFormController.areAllFieldsValid()
getCardBrand() -> PSCardBrand
This method retrieves the card brand from the card form.
Returns PSCardBrand enum containing card brand information.
val cardBrand = cardFormController.getCardBrand()
onCardBrandRecognition: ((PSCreditCardType) -> Unit)
This event is triggered when the Paysafe Android SDK algorithm identifies a card brand in the card form.
cardForm.onCardBrandRecognition = { print("Card brand recognition => \(it)")}
NOTE: The PSCreditCardType enum defines the following cases:
- .VISA: Visa card
- .MASTERCARD: Mastercard card
- .AMEX: American Express card
- .DISCOVER: Discover card
- .UNKNOWN: Unknown card, but it does not mean incorrect. Valid (by Luhn algorithm) credit card numbers will be accepted, but the trademark icon will not be displayed in the card number field of the user interface.
resetCardDetails()
This function provides the flexibility to clear previously inserted card data in card form fields. Use this when the customer is switching between paying with а new or saved card.
cardForm.resetCardDetails()
You can use these methods and events to enhance the functionality of your Android applications by effectively managing card form fields, validating input, and responding to card brand recognition events triggered by the SDK algorithm.