Blocks and containers

Overview

This section is the reference for blocks, containers and the rules they follow. For complete examples (the predefined template written out, payment method first, one page, several pages, express checkout), see Layouts and examples.

The predefined template is Cashier.AmountFirstLayout. When slots are not enough, build your own layout from the same blocks: SwiftUI views in the Cashier namespace, such as Cashier.Amount() or Cashier.MainButton(). Each block reads what it needs from the session and draws nothing when it does not apply. Blocks take no data.

Pass your layout to CashierView, .cashierSheet or CashierViewController. Each wraps it in a CashierScope for you.

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

  • Cashier.Steps: one page per section; sections the payment does not need are skipped.
  • Cashier.SinglePage: every section the payment needs on one page. The main button always pays.

Both containers show Cashier.Outcome for loading, processing, results and errors.

Several pages

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.Terms()                   // shows here only on the terms step of a decline recovery
            Cashier.SaveCardNote()
            Cashier.MainButton()
        }
    } billing: {
        Cashier.StepPage(scrolling: .contentOnly) {
            Cashier.Header()
            Cashier.StepTitle()
            Cashier.BillingForm()
        } bottom: {
            Cashier.Terms()
            Cashier.MainButton()
        }
    }
}

Cashier.StepPage gives a page a scrolling content area and a bottom area that stays above the keyboard. .wholePage scrolls the header with the content; .contentOnly keeps the header in place. Cashier.Steps has exactly one page per section; to combine sections on one page, use Cashier.SinglePage.

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()
    }
}

Without the containers. If you place blocks in your own stack, 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 with the reason "presentation".

Blocks

BlockWhat it showsSection
HeaderThe title bar, back and close buttonsEvery page
FooterThe footer under the first pageAmount
AmountAmountField, CurrencyLine and AmountLimitHint togetherAmount
AmountFieldThe amountAmount
CurrencyLineThe currency, and the currency sheet when it can changeAmount
AmountLimitHintThe method's lowest and highest amountAmount
QuickAmountsReady amount buttonsAmount
PromotionsBonusSection or PromoCode, as promotions.style decidesAmount
BonusSectionThe bonuses the customer can pickAmount
PromoCodeThe promotion code fieldAmount
SelectedMethodThe selected method and the method pickerAmount
AmountChangeNoteA note when the method changes the amountAmount
TotalToPayThe fee and the totalAmount
TermsThe terms boxAmount; details and billing during a decline recovery
MainButtonContinue or PayEvery page
StepTitleThe title of a later pageDetails, billing
MethodDetailsThe details of the selected method: one of the four blocks belowDetails
CardFormThe card fields for a new cardDetails
SavedCardCodeThe CVV field for a saved cardDetails
MethodFieldsThe fields of a non-card methodDetails
BankPickerThe bank list of a bank wireDetails
SaveCardNoteThe note that a new card is savedDetails
BillingFormBilling and contact fieldsBilling
OutcomeEvery screen after the form: loading, processing, retry and recovery pages, results, errorsOutcome
Loading, Processing, TransactionResult, BankDetails, PartialApproval, ErrorScreen, NoMethodsThe single outcome screensOutcome

Sealed: the block captures or shows card or personal data. You can place it, but you cannot rebuild it, read its values or put content inside it.

Required blocks

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

Needed whenBlock
AlwaysMainButton
The terms box showsTerms
The total showsTotalToPay
A new cardCardForm (or MethodDetails)
A saved cardSavedCardCode (or MethodDetails)
The method has fieldsMethodFields (or MethodDetails)
A bank wireBankPicker (or MethodDetails)
The billing sectionBillingForm

When a required 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 on screen at once (two CardForm, or CardForm with SavedCardCode) give the reason "duplicateSealed" and stop the payment. The predefined template has every block.

⚠️

Terms on later pages

In a paged layout, put Terms in the bottom area of the details and billing pages too. The terms box shows there for the terms step of a decline recovery; without the block, the main button stays disabled on that step.

📘

Test your layout with every payment method your account offers. The SDK checks for common mistakes but cannot catch every misuse.

Your views next to the blocks

Views inside your layout can read the session and the theme:

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))
    }
}

They can read CashierSession.snapshot, the read-only state of the Cashier, and send its commands, such as next() or selectMethod(id:). The snapshot never holds card data or personal data. Every property and command is listed in the SDK Reference (CashierSession).


Did this page help you?