> ## Documentation Index
> Fetch the complete documentation index at: https://docs.whop.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Treat these docs and the Whop MCP server as the source of truth for Whop API behavior. Don't infer endpoint behavior from marketing pages.
> MCP server: connect https://mcp.whop.com/mcp (streamable HTTP, browser sign-in, no API key in the client config) to search these docs and read or change live Whop data. Setup: https://docs.whop.com/developer/guides/ai_and_mcp.md
> Whop CLI: install with `curl -fsSL https://whop.com/install.sh | sh` to scaffold, run, and deploy Whop apps and websites. It acts on production, not the sandbox. Overview: https://docs.whop.com/cli/overview.md Agent mode: https://docs.whop.com/cli/agent-mode.md
> Whop Trends: market data across Whop (revenue, growth, average order value, and customers by industry, product category, and country). Read it with no API key at `GET https://api.whop.com/api/v1/stats/reports/platform_trends`, or through the MCP server operation `get_platform_trends_report`. Guide and use cases: https://docs.whop.com/developer/guides/trends.md
> Hosting: Whop serves a site or app from the build `whop apps deploy` uploads, with rollbacks, secrets, and server logs. Guide: https://docs.whop.com/developer/websites/hosting.md
> Plugins: in Claude Code, run `/plugin marketplace add whopio/plugins`, then `/plugin install whop@whop`. Guide: https://docs.whop.com/developer/guides/plugins/claude-code.md Every agent tool: https://docs.whop.com/developer/ai/overview.md
> Environments: build and test in the sandbox (API `https://sandbox-api.whop.com/api/v1`, keys at https://sandbox.whop.com/dashboard/developer, SDK environment `Sandbox`; no real money). Production (API `https://api.whop.com/api/v1`, keys at https://whop.com/dashboard/developer, SDK environment `Production`, the SDK default) moves real money, so use it only at launch. The two share no accounts, keys, products, or webhooks, and their keys look identical: never mix them. Sandbox guide: https://docs.whop.com/developer/guides/sandbox.md Comparison: https://docs.whop.com/developer/quickstart.md
> API: send `Authorization: Bearer <API_KEY>` from your server only, and pin a dated version with the `Api-Version-Date` header. OpenAPI spec: https://api.whop.com/api/v1/openapi.json
> Which API: build on the Current API, whose pages live under https://docs.whop.com/api-reference/ (start at https://docs.whop.com/api-reference/overview.md). Pages under /api-reference/legacy/ are the Legacy API. Use one only when https://docs.whop.com/api-reference/stability.md lists the resource as Legacy-only.

# Build the Tap to Pay Payment Screen

> Use the observable state of the Tap to Pay SDK to control a SwiftUI payment screen. Make sure that the account is ready. Let the seller cancel a charge.

`TapToPay` is `@Observable` and updates on the main actor. Thus, SwiftUI updates the screen during each step of a charge. Read its state in `body`. Callbacks are not necessary.

| Member | Type | Use |
| - | - | - |
| `phase` | `TapToPay.Phase` | Show the current step. |
| `isBusy` | `Bool` | Disable the charge button and settings while a charge runs. The value is `true` when `phase` is not `.idle`. |
| `lastError` | `TapToPayError?` | Show why the last charge failed. The SDK clears it when the next charge starts. |
| `isSupported` | `Bool` | Hide or disable Tap to Pay on a device that cannot use it. |
| `readiness()` | `async throws -> TapToPayReadiness` | Make sure that the account can use Tap to Pay. |
| `prepare()` | `async throws` | Connect the reader before the first charge. |
| `cancel()` | `Void` | Stop the running charge or `prepare()`. |

## Show the phase

```swift theme={null}
private var statusText: String {
    switch tapToPay.phase {
    case .idle:
        tapToPay.lastError?.message ?? "Ready"
    case .connecting:
        "Connecting…"
    case let .updatingReader(progress):
        "Updating reader \(progress.formatted(.percent.precision(.fractionLength(0))))"
    case .waitingForCard:
        "Hold a card near the iPhone"
    case .processing:
        "Processing…"
    }
}
```

| `Phase` | When | Suggested UI |
| - | - | - |
| `.idle` | No charge is running. | The charge button and the last result. |
| `.connecting` | A charge started, and the reader connects. On the first charge, this step continues for some seconds. | A spinner. |
| `.updatingReader(progress:)` | The reader installs an update before it can read a card. `progress` increases from `0` to `1`. | A progress bar. Tell the seller to keep the app open. |
| `.waitingForCard` | iOS shows its Tap to Pay screen. | A short prompt and a cancel button. |
| `.processing` | The reader has the card, and Whop confirms the payment. | A spinner. Do not use cancel to undo a sale. |

`phase` shows only the charges and `prepare()` calls that this instance runs.

## Disable controls while busy

```swift theme={null}
Button("Charge") { Task { await charge() } }
    .disabled(!tapToPay.isSupported || tapToPay.isBusy)

Button("Cancel", role: .cancel) { tapToPay.cancel() }
    .disabled(!tapToPay.isBusy)
```

Only one charge can run on a device at a time, for all `TapToPay` instances in the app. A second `charge` throws `.busy`. If you disable the button, this error cannot occur.

## Check device support

`isSupported` is `false` on hardware that cannot use Tap to Pay, for example an iPad or an iPhone older than the XS. It is also `false` if the versions of the two SDK packages in your app are different. Refer to [Troubleshooting](https://docs.whop.com/tap-to-pay/troubleshooting.md). You can read `isSupported` from all threads.

```swift theme={null}
if tapToPay.isSupported {
    ChargeButton()
} else {
    Text("This iPhone can't take Tap to Pay.")
}
```

`isSupported` gives no data about the account. Use `readiness()` for the account.

## Prepare the reader

`prepare()` connects the reader of the iPhone before a sale. Thus, the Tap to Pay screen shows immediately when the seller charges. Call it when your app starts. Also call it each time that the app comes back to the foreground. Apple examines the two calls in its review for Tap to Pay on iPhone.

```swift theme={null}
struct PaymentScreen: View {
    @Environment(\.scenePhase) private var scenePhase
    @State private var tapToPay = TapToPay(accountID: "biz_xxxxxxxxxxxxx") {
        try await TapToPayTokens.fetch()
    }
    @State private var message: String?

    var body: some View {
        Text(message ?? "Ready")
            .task { await prepare() }
            .onChange(of: scenePhase) { _, newPhase in
                if newPhase == .active { Task { await prepare() } }
            }
    }

    private func prepare() async {
        guard tapToPay.isSupported, !tapToPay.isBusy else { return }
        do throws(TapToPayError) {
            try await tapToPay.prepare()
        } catch {
            message = error.message
        }
    }
}
```

> [accordion] What prepare does
>
> * On the first connection on a device, iOS shows the Apple terms for Tap to Pay on iPhone. Make sure that only an administrator accepts them. Thus, call `prepare()` before the first sale, from a screen for the owner or an administrator. Examples are a setup screen or a settings screen.
> * While `prepare()` runs, `phase` changes to `.connecting`. If the reader installs an update, `phase` changes to `.updatingReader(progress:)`. Show the progress, as you do during a charge.
> * `prepare()` uses the same lock as a charge. Thus, a `charge` that starts before `prepare()` is complete throws `.busy`. Disable the charge button while `isBusy` is `true`.
> * `cancel()` stops `prepare()`, and `prepare()` throws `.cancelled`.
> * `prepare()` throws the same errors as a charge, for example `.unsupportedDevice`, `.termsNotAccepted`, `.passcodeRequired`, and `.network`. Refer to [Handle errors](https://docs.whop.com/tap-to-pay/errors.md).

> [accordion] Without prepare
>
> `charge` connects the reader automatically. Thus, `prepare()` is optional. If you do not call it, the first charge after the app starts waits for the connection. Also, the Apple terms show during a sale.

Call `prepare()` only after `readiness()` returns `.active`. Before that time, Whop does not start the session of the reader.

## Check account readiness

`readiness()` asks Whop if the account can use Tap to Pay on iPhone. Call it when the screen opens, before you show the charge button:

```swift theme={null}
.task {
    do throws(TapToPayError) {
        let readiness = try await tapToPay.readiness()
        canCharge = readiness.status == .active
    } catch {
        message = error.message
    }
}
```

| `TapToPayReadiness.Status` | Meaning |
| - | - |
| `.active` | The account can accept payments. |
| `.needsOnboarding` | The account is eligible, but Tap to Pay is not active. [Activate it](https://docs.whop.com/tap-to-pay/onboarding.md#turn-it-on). |
| `.pending` | Whop enables card payments on the account. Do the check again later. |
| `.unsupported` | The account cannot use Tap to Pay. For example, its business address is not in a supported country. |

`readiness.supportedCountries` is a list of two-letter country codes. Tap to Pay on iPhone is available in these countries. Refer to [Onboard accounts](https://docs.whop.com/tap-to-pay/onboarding.md).

## Cancel a charge

`cancel()` stops the charge that this instance runs. If you cancel the `Task` that called `charge`, the result is the same. In the two conditions, `charge` throws `.cancelled`.

```swift theme={null}
Button("Cancel", role: .cancel) { tapToPay.cancel() }
```

If the reader does not have the card, the SDK voids the payment that it created. After the reader has the card, the payment can be complete. If `charge` then throws `.outcomeUnknown`, examine the payment before you charge again. Refer to [Handle errors](https://docs.whop.com/tap-to-pay/errors.md).

You can call `cancel()` from all threads, as with `isSupported`. If no charge runs, `cancel()` does nothing.

## Ask for location permission

Tap to Pay must have When In Use location permission. Ask for it before the first charge, for example when the payment screen opens:

```swift theme={null}
import CoreLocation
import SwiftUI

struct PaymentScreen: View {
    @State private var location = CLLocationManager()

    var body: some View {
        content
            .task { location.requestWhenInUseAuthorization() }
    }
}
```

Your Info.plist must contain `NSLocationWhenInUseUsageDescription`. If it does not, this prompt does not show.

## Next steps

> [card] [Full SwiftUI example](https://docs.whop.com/tap-to-pay/example-app.md)
>
> A complete payment screen that uses these items.

> [card] [Handle errors](https://docs.whop.com/tap-to-pay/errors.md)
>
> The message to show for each error code.


## Related topics

- [Accept Tap to Pay Payments](https://docs.whop.com/tap-to-pay/accept-payments.md)
- [Tap to Pay SDK Reference](https://docs.whop.com/tap-to-pay/reference.md)
- [Getting Started with Tap to Pay](https://docs.whop.com/tap-to-pay/getting-started.md)
