7. Events and hooks

Put your code in CashierHooks and pass it in the configuration. Every hook runs on the main actor.

let hooks = CashierHooks(
    onEvent: { event in analytics.track(event.name.rawValue, traceID: event.traceID) },
    onAmountChange: { update in cartView.show(update.total ?? update.amount, update.currency) },
    methodFilter: { methods in methods.filter { !$0.isInMaintenance }.map(\.id) },
    fieldRules: [CashierFieldRule(field: "zip", minimumLength: 4)]
)
let configuration = CashierConfiguration(hooks: hooks)
HookWhat it does
onEventGets each CashierEvent in the order things happen
onAmountChangeGets a CashierAmountUpdate while the amount section shows with an amount: first when it shows, then each time the amount, the charge, the fee, the total or the currency changes
methodFilterGets the methods the picker would offer and returns the ids to keep, in the order to show. It can remove and reorder methods; it cannot add one. Unknown ids are ignored
fieldRulesAdds a check to a field: a pattern, a minimum length or a required mark, optionally per method and with a message key from CashierTexts
skippedFieldChecksTurns off the checks of named fields per method
fieldAdjustmentsHides a field, makes it required, or gives it a start value: .hide(field:method:), .require(field:method:), .defaultValue(_:field:method:)
🚧

Important

When your method filter keeps no method, or fails, the cashier shows its error screen and ends with configuration_error.

Events

The names are the web Payment Page names, so your analytics can treat the app and the web the same way.

Raw valueSwift nameWhen
initSession.initSessionThe session was read
initApp.initAppThe cashier was set up
cashier_loaded.cashierLoadedThe cashier is ready. Always carries the base currency, with the preset amount in it when there is one
payment_method_selected.paymentMethodSelectedA method was selected
createPaymentPage.createPaymentPageThe form of the selected method is ready
hosted_payment_fields_loaded.hostedPaymentFieldsLoadedThe card fields showed
hpf_form_valid.hostedFieldsValidThe card fields became valid
hpf_form_invalid.hostedFieldsInvalidThe card fields became invalid
apm_fields_loaded.methodFieldsLoadedThe method fields showed
apm_form_valid.methodFieldsValidThe method fields became valid
apm_form_invalid.methodFieldsInvalidThe method fields became invalid
cashier_submit.submitThe payment was handed off
transaction_attempted.transactionAttemptedThe payment answer arrived and is OK
redirection.redirectionA provider page opened
transaction.transactionA transaction status arrived
thank_you_page_loaded.thankYouPageLoadedThe final result showed
createResultPage.createResultPageThe result page was made
transaction_failed.transactionFailedThe payment ended on an error
cashier_loading_failed.loadingFailedThe cashier could not load, or ended on an error screen
layout_incomplete.layoutIncompleteYour layout leaves out a block the payment needs (section 11)
configuration_value_ignored.configurationValueIgnoredA value you set in code was ignored (a hex colour that is not valid, an unknown text key)
terms_accepted_by_code.termsAcceptedByCodeYour app ticked the terms box
cashier_back_button.backButtonThe customer tapped back on the first section. The cashier stays open

What an event carries

Only these fields, and a field is nil when the event does not use it: the method type (paymentMethod), the action type, the status, the error code, the card brand name, a validity flag, the amount and currency, the trace ID, the name of an ignored value (ignoredName), the reason of layout_incomplete (reason), the intent of the tab (intent) and the session's other intents (secondaryIntents).

  • methodKeys is empty on cashier_loaded and nil on every other event.
  • There is no event for a step change: read the snapshot's step instead. There are no click events and no events per field.
  • intent is deposit or withdrawal, so analytics can track withdrawals apart from deposits. A card authorization session reports a deposit. initSession, initApp, createPaymentPage, createResultPage and the SDK's own events carry no intent.
  • secondaryIntents is set on cashier_loaded and cashier_loading_failed in a session that offers more than one intent (for example a Deposit and a Withdraw tab).
  • A tab switch and a cancelled pending withdrawal send no event.

Live amount

FieldMeaning
amountThe amount the customer adds, in the base currency of the account
totalChargeThe amount to charge in the payment currency; nil before it is known
displayAmountThe amount in the payment currency with an included fee taken out; nil while no method is selected. On a withdrawal: what leaves the balance (a fee on top added)
feeThe fee in the payment currency; nil while no method is selected
totalThe total to pay. On a withdrawal: what the customer receives (an included fee taken out)
currencyThe ISO 4217 code of the payment currency

Methods in the filter

FieldMeaning
idA plain method: the method key. A saved card or account: an opaque id saved-<n>, never a label or card digits
nameA plain method: the server title. A saved row: an SDK text with no digits and no label
typeThe method type, for example "cc", when the server sent one
logoURLThe server logo of the method
isSavedThe row is a saved card or a saved account
isInMaintenanceThe method is in maintenance and cannot pay now
📘

Note

Events and hooks never carry card numbers, CVVs, card tokens, customer data (name, email, phone, address), field values, server messages or the session token. For this reason the SDK offers no custom check that reads field values and no hook with the customer object.


Did this page help you?