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)| Hook | What it does |
|---|---|
| onEvent | Gets each CashierEvent in the order things happen |
| onAmountChange | Gets 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 |
| methodFilter | Gets 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 |
| fieldRules | Adds a check to a field: a pattern, a minimum length or a required mark, optionally per method and with a message key from CashierTexts |
| skippedFieldChecks | Turns off the checks of named fields per method |
| fieldAdjustments | Hides a field, makes it required, or gives it a start value: .hide(field:method:), .require(field:method:), .defaultValue(_:field:method:) |
ImportantWhen 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 value | Swift name | When |
|---|---|---|
| initSession | .initSession | The session was read |
| initApp | .initApp | The cashier was set up |
| cashier_loaded | .cashierLoaded | The cashier is ready. Always carries the base currency, with the preset amount in it when there is one |
| payment_method_selected | .paymentMethodSelected | A method was selected |
| createPaymentPage | .createPaymentPage | The form of the selected method is ready |
| hosted_payment_fields_loaded | .hostedPaymentFieldsLoaded | The card fields showed |
| hpf_form_valid | .hostedFieldsValid | The card fields became valid |
| hpf_form_invalid | .hostedFieldsInvalid | The card fields became invalid |
| apm_fields_loaded | .methodFieldsLoaded | The method fields showed |
| apm_form_valid | .methodFieldsValid | The method fields became valid |
| apm_form_invalid | .methodFieldsInvalid | The method fields became invalid |
| cashier_submit | .submit | The payment was handed off |
| transaction_attempted | .transactionAttempted | The payment answer arrived and is OK |
| redirection | .redirection | A provider page opened |
| transaction | .transaction | A transaction status arrived |
| thank_you_page_loaded | .thankYouPageLoaded | The final result showed |
| createResultPage | .createResultPage | The result page was made |
| transaction_failed | .transactionFailed | The payment ended on an error |
| cashier_loading_failed | .loadingFailed | The cashier could not load, or ended on an error screen |
| layout_incomplete | .layoutIncomplete | Your layout leaves out a block the payment needs (section 11) |
| configuration_value_ignored | .configurationValueIgnored | A value you set in code was ignored (a hex colour that is not valid, an unknown text key) |
| terms_accepted_by_code | .termsAcceptedByCode | Your app ticked the terms box |
| cashier_back_button | .backButton | The 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
| Field | Meaning |
|---|---|
| amount | The amount the customer adds, in the base currency of the account |
| totalCharge | The amount to charge in the payment currency; nil before it is known |
| displayAmount | The 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) |
| fee | The fee in the payment currency; nil while no method is selected |
| total | The total to pay. On a withdrawal: what the customer receives (an included fee taken out) |
| currency | The ISO 4217 code of the payment currency |
Methods in the filter
| Field | Meaning |
|---|---|
| id | A plain method: the method key. A saved card or account: an opaque id saved-<n>, never a label or card digits |
| name | A plain method: the server title. A saved row: an SDK text with no digits and no label |
| type | The method type, for example "cc", when the server sent one |
| logoURL | The server logo of the method |
| isSaved | The row is a saved card or a saved account |
| isInMaintenance | The method is in maintenance and cannot pay now |
Updated about 8 hours ago
Did this page help you?