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.
| Container | Shows |
|---|---|
| Cashier.Steps | One page per section, and skips the sections the payment does not need |
| Cashier.SinglePage | Every section the payment needs on one page. The main button always pays |
| Cashier.StepPage | One 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.Outcome | Loading, 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.
| Block | What it shows | Section |
|---|---|---|
| Cashier.Header | The title bar, the back and close buttons | every page |
| Cashier.Footer | The footer under the first page | amount |
| Cashier.Amount | AmountField, CurrencyLine and AmountLimitHint together | amount |
| Cashier.AmountField | The amount the customer pays | amount |
| Cashier.CurrencyLine | The currency, and the currency sheet when the customer can change it | amount |
| Cashier.AmountLimitHint | The lowest and highest amount of the method | amount |
| Cashier.QuickAmounts | Buttons with ready amounts | amount |
| Cashier.Promotions | BonusSection or PromoCode, as the server decides, in one place | amount |
| Cashier.BonusSection | The bonuses the customer can pick | amount |
| Cashier.PromoCode | The promotion code field | amount |
| Cashier.SelectedMethod | The selected payment method, and the method picker | amount |
| Cashier.AmountChangeNote | A note when the method changes the amount | amount |
| Cashier.TotalToPay | The fee and the total | amount |
| Cashier.Terms | The terms box | amount |
| Cashier.MainButton | Continue, Pay or the Apple Pay button | every page |
| Cashier.StepTitle | The title of a later page | method details, billing |
| Cashier.MethodDetails | The details block of the selected method: card form, saved card code, wallet switch, method fields or bank picker | method details |
| Cashier.CardForm | The sealed card fields for a new card | method details |
| Cashier.SavedCardCode | The sealed CVV field for a saved card | method details |
| Cashier.WalletSwitch | The card or Apple Pay switch inside the card method | method details |
| Cashier.MethodFields | The fields of a method that is not a card | method details |
| Cashier.BankPicker | The bank list of a bank wire | method details |
| Cashier.SaveCardNote | The note that a new card is saved | method details |
| Cashier.BillingForm | The billing and contact fields | billing |
| Cashier.Outcome | Every screen after the form: loading, processing, the retry and recovery pages after a decline, results and errors | outcome |
| Cashier.Loading | The loading screen | outcome |
| Cashier.Processing | The wait while the payment runs | outcome |
| Cashier.TransactionResult | The result of the payment | outcome |
| Cashier.BankDetails | The bank details of a manual bank wire | outcome |
| Cashier.PartialApproval | The partial approval screen with its countdown | outcome |
| Cashier.ErrorScreen | An error screen | outcome |
| Cashier.NoMethods | The screen shown when no payment method is available | outcome |
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 when | Block |
|---|---|
| Always | Cashier.MainButton |
| The terms box shows | Cashier.Terms |
| The total shows | Cashier.TotalToPay |
| A new card | Cashier.CardForm (or Cashier.MethodDetails) |
| A saved card | Cashier.SavedCardCode (or Cashier.MethodDetails) |
| The card or Apple Pay switch shows | Cashier.WalletSwitch (or Cashier.MethodDetails) |
| The method has fields | Cashier.MethodFields (or Cashier.MethodDetails) |
| A bank wire | Cashier.BankPicker (or Cashier.MethodDetails) |
| The billing section | Cashier.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.
ImportantThe 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 field | Meaning |
|---|---|
| step | loading, amount, details, billing, result or error. While a payment processes it is result with no result yet |
| isBusy | The cashier is working: starting the session, paying or checking the payment status |
| canPay, canContinue | The main button submits now / continues to another section now, and is enabled |
| amount, currency | The amount the customer will pay, in major units, and its ISO 4217 code |
| currencies | The currency codes the customer can pick, A to Z |
| intent | deposit or withdrawal: the intent of the active tab |
| paymentMethod, methods | The 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, termsAccepted | The merchant requires the terms / the box is ticked |
| bonus, bonuses | The selected bonus, and the bonuses offered (id and title) |
| fee, total | The fee of the selected method and the total as the total line shows it. On a withdrawal the total is what the customer receives |
| result | The result, once the payment ended |
| Command | What 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().
Updated about 8 hours ago