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.
Show the phase
phase shows only the charges and prepare() calls that this instance runs.
Disable controls while busy
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. You can read isSupported from all threads.
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.
What prepare does
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,phasechanges to.connecting. If the reader installs an update,phasechanges to.updatingReader(progress:). Show the progress, as you do during a charge. prepare()uses the same lock as a charge. Thus, achargethat starts beforeprepare()is complete throws.busy. Disable the charge button whileisBusyistrue.cancel()stopsprepare(), andprepare()throws.cancelled.prepare()throws the same errors as a charge, for example.unsupportedDevice,.termsNotAccepted,.passcodeRequired, and.network. Refer to Handle errors.
Without prepare
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.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:
readiness.supportedCountries is a list of two-letter country codes. Tap to Pay on iPhone is available in these countries. Refer to Onboard accounts.
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.
charge then throws .outcomeUnknown, examine the payment before you charge again. Refer to Handle errors.
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:NSLocationWhenInUseUsageDescription. If it does not, this prompt does not show.
Next steps
Full SwiftUI example
A complete payment screen that uses these items.
Handle errors
The message to show for each error code.

