11. Your own layout: blocks, containers and the session

Instead of the drop-in, you can arrange the cashier blocks yourself. Hold a CashierSession and hand your layout to CashierView, to .cashierSheet or to CashierViewController; each wraps the layout in a CashierScope for you. Inside, use the blocks of the Cashier namespace and your own views.

A payment has up to three sections: the amount, the method details and billing. The session decides which sections the payment needs; a container decides how they show.

ContainerShows
Cashier.StepsOne page per section, and skips the sections the payment does not need
Cashier.SinglePageEvery section the payment needs on one page. The main button always pays
Cashier.StepPageOne page of Steps: a scrolling content area and a bottom area that stays above the keyboard. scrolling: .wholePage scrolls the header with the content; .contentOnly keeps the header in place
Cashier.OutcomeLoading, processing, results and errors. Both containers show it unless you pass your own outcome page

Several pages

let session = CashierSession(token: token, environment: environment) { result in
    handle(result)
}

CashierView(session: session) {
    Cashier.Steps {
        Cashier.StepPage(scrolling: .wholePage) {
            Cashier.Header()
            Cashier.Amount()
            Cashier.SelectedMethod()
        } bottom: {
            Cashier.TotalToPay()
            Cashier.Terms()
            Cashier.MainButton()
        }
    } details: {
        Cashier.StepPage(scrolling: .contentOnly) {
            Cashier.Header()
            Cashier.StepTitle()
            Cashier.MethodDetails()
        } bottom: {
            Cashier.SaveCardNote()
            Cashier.MainButton()
        }
    } billing: {
        Cashier.StepPage(scrolling: .contentOnly) {
            Cashier.Header()
            Cashier.StepTitle()
            Cashier.BillingForm()
        } bottom: {
            Cashier.MainButton()
        }
    }
}

One page

CashierView(session: session) {
    Cashier.SinglePage {
        Cashier.Header()
        Cashier.Amount()
        Cashier.BonusSection()
        Cashier.SelectedMethod()
        Cashier.PromoCode()
        Cashier.MethodDetails()
        Cashier.BillingForm()
    } bottom: {
        Cashier.TotalToPay()
        Cashier.Terms()
        Cashier.MainButton()
    }
}

Cashier.Steps has exactly one page per section: you cannot put the amount and the method details together on one page of a paged layout. Use Cashier.SinglePage for that. If you place the blocks in your own stack without a container, tell the SDK how you present the sections with .cashierPresentation(.single) or .cashierPresentation(.paged); without it the SDK treats the layout as paged, opens only the amount section and sends layout_incomplete once with the reason "presentation".

The blocks

Each block is a SwiftUI view in the Cashier namespace with only an empty init(). It reads what it needs from the session in its scope and draws nothing when its data is not relevant or its section is not open.

BlockWhat it showsSection
Cashier.HeaderThe title bar, the back and close buttonsevery page
Cashier.FooterThe footer under the first pageamount
Cashier.AmountAmountField, CurrencyLine and AmountLimitHint togetheramount
Cashier.AmountFieldThe amount the customer paysamount
Cashier.CurrencyLineThe currency, and the currency sheet when the customer can change itamount
Cashier.AmountLimitHintThe lowest and highest amount of the methodamount
Cashier.QuickAmountsButtons with ready amountsamount
Cashier.PromotionsBonusSection or PromoCode, as the server decides, in one placeamount
Cashier.BonusSectionThe bonuses the customer can pickamount
Cashier.PromoCodeThe promotion code fieldamount
Cashier.SelectedMethodThe selected payment method, and the method pickeramount
Cashier.AmountChangeNoteA note when the method changes the amountamount
Cashier.TotalToPayThe fee and the totalamount
Cashier.TermsThe terms boxamount
Cashier.MainButtonContinue, Pay or the Apple Pay buttonevery page
Cashier.StepTitleThe title of a later pagemethod details, billing
Cashier.MethodDetailsThe details block of the selected method: card form, saved card code, wallet switch, method fields or bank pickermethod details
Cashier.CardFormThe sealed card fields for a new cardmethod details
Cashier.SavedCardCodeThe sealed CVV field for a saved cardmethod details
Cashier.WalletSwitchThe card or Apple Pay switch inside the card methodmethod details
Cashier.MethodFieldsThe fields of a method that is not a cardmethod details
Cashier.BankPickerThe bank list of a bank wiremethod details
Cashier.SaveCardNoteThe note that a new card is savedmethod details
Cashier.BillingFormThe billing and contact fieldsbilling
Cashier.OutcomeEvery screen after the form: loading, processing, the retry and recovery pages after a decline, results and errorsoutcome
Cashier.LoadingThe loading screenoutcome
Cashier.ProcessingThe wait while the payment runsoutcome
Cashier.TransactionResultThe result of the paymentoutcome
Cashier.BankDetailsThe bank details of a manual bank wireoutcome
Cashier.PartialApprovalThe partial approval screen with its countdownoutcome
Cashier.ErrorScreenAn error screenoutcome
Cashier.NoMethodsThe screen shown when no payment method is availableoutcome

Block rules

  • Blocks take no data. A block reads the session in its scope, so it must be inside CashierView, .cashierSheet, CashierViewController or a CashierScope.
  • Sealed blocks: CardForm, SavedCardCode, MethodFields, BillingForm and BankDetails capture or show card or personal data. You can place them, but you cannot rebuild them, read their values or put content inside them.
  • One card block at a time: two CardForm blocks, or a CardForm with a SavedCardCode, on screen at once stop the payment.

The required-block rule

Some blocks carry legal or payment meaning. When a section that needs one is open, that block must be on screen.

Needed whenBlock
AlwaysCashier.MainButton
The terms box showsCashier.Terms
The total showsCashier.TotalToPay
A new cardCashier.CardForm (or Cashier.MethodDetails)
A saved cardCashier.SavedCardCode (or Cashier.MethodDetails)
The card or Apple Pay switch showsCashier.WalletSwitch (or Cashier.MethodDetails)
The method has fieldsCashier.MethodFields (or Cashier.MethodDetails)
A bank wireCashier.BankPicker (or Cashier.MethodDetails)
The billing sectionCashier.BillingForm

A block counts only when it draws with a size above zero inside the cashier. When a needed block is missing, the main button is disabled, no payment is sent, and the SDK sends layout_incomplete with the reason "requiredBlock". Two card blocks at once give the reason "duplicateSealed". The drop-in layout has every block, so it never trips this rule.

🚧

Important

The SDK checks for common mistakes; it does not claim to catch every misuse. Test your own layout with each method your merchant account offers.

The session: state and commands

Your views inside the layout read the session from the SwiftUI Environment. Its snapshot is the read-only state your app may see; views that read it redraw when it changes. It never holds card data or personal data.

struct MyHeader: View {
    @Environment(CashierSession.self) private var cashier
    @Environment(\.cashierResolvedTheme) private var theme

    var body: some View {
        Text(cashier.snapshot.amount?.formatted() ?? "")
            .foregroundStyle(theme.color(.primaryText))
    }
}
Snapshot fieldMeaning
steploading, amount, details, billing, result or error. While a payment processes it is result with no result yet
isBusyThe cashier is working: starting the session, paying or checking the payment status
canPay, canContinueThe main button submits now / continues to another section now, and is enabled
amount, currencyThe amount the customer will pay, in major units, and its ISO 4217 code
currenciesThe currency codes the customer can pick, A to Z
intentdeposit or withdrawal: the intent of the active tab
paymentMethod, methodsThe selected method, and the methods the customer can pick for the current currency (each method in server order, then each saved card or account)
termsRequired, termsAcceptedThe merchant requires the terms / the box is ticked
bonus, bonusesThe selected bonus, and the bonuses offered (id and title)
fee, totalThe fee of the selected method and the total as the total line shows it. On a withdrawal the total is what the customer receives
resultThe result, once the payment ended
CommandWhat it does
start()Starts the session. CashierScope and CashierView call it when they first appear, so you rarely need it
close()Closes the cashier: stops all work, clears the card fields and calls onResult once with closedBy = code. canceled if the payment had not started; pending while a payment or its processing runs
next()Does what the main button does now: continues to the next section, or submits on the final section
back()Goes back one section in a paged layout
setAmount(_:)Sets the amount in major units, through the same input rules as typing. Ignored when the amount is locked (a preset session amount)
selectCurrency(_:)Picks a currency of snapshot.currencies, as a pick in the currency sheet does
selectMethod(id:)Selects a method by an id of snapshot.methods
selectBonus(id:)Selects a bonus of snapshot.bonuses; nil for none
applyPromotionCode()Applies the promotion code the customer typed
setTermsAccepted(_:)Ticks or clears the terms box; the SDK sends terms_accepted_by_code

Commands are ignored when they make no sense: while a payment runs, outside the form, or after close().


Did this page help you?