Present the drop-in
Show a ready-made payment sheet over a checkout, with the session's methods, the card form, and the Pay button, and get the result when it closes.
PayDropIn is a ready-made payment sheet over a checkout, and present(from:) shows it and returns the result when it closes. Use it instead of building your own screen from the payment methods, the card fields, and pay. Link the PayDropIn product, which depends on PayCheckout, PayCore, and PayElements.
The sheet is built from system parts. For what it shows, from top to bottom and in each state, see What the customer sees. A returning customer sees their saved payment methods first. See A returning customer.
The customer selects a method, then pays. 3DS challenges and redirects open over the sheet. After a decline, the sheet shows a message and the customer can try again. When the session can't be paid anymore, the sheet disables everything but closing.
PayDropIn runs on the main actor.
let dropIn = PayDropIn(checkout: checkout, configuration: .init(merchantDisplayName: "My Shop"))
let result = await dropIn.present(from: self)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
checkout | PayCheckout | Yes | The checkout to pay with. |
configuration | PayDropIn.Configuration | No | What the sheet shows. Default: Configuration(). It's also dropIn.configuration, which you can change before you present. |
configuration.merchantDisplayName | String? | No | Your name, above the total and in the save-card label, for example "Save card for future use at My Shop". Default: nil, for the total alone and "Save card for future use". |
configuration.cardholderName | PayDropIn.CardholderName | No | Controls the cardholder name field (.hidden, .optional, or .required). Default: .optional. |
configuration.saveCard | PayDropIn.SaveCard | No | .automatic shows the save-card switch unless the session saves the card anyway (its setup_future_usage is on_session, or it's a recurring session) or only saves it (a setup session). .hidden never shows it. Default: .automatic. |
configuration.paymentMethods | [String]? | No | Show only these types, for example ["card", "paypal"], ignoring case and in the session's order. It filters the saved methods by their type too, and Apple Pay shows only if the list includes applepay. Default: nil, every method. |
configuration.allowsRemovingSavedPaymentMethods | Bool | No | Shows Edit over the saved methods, so the customer can remove them after confirming. Default: false. |
configuration.applePay | PayApplePayConfiguration? | No | Shows the Apple Pay button above the other methods when the session offers Apple Pay and canPayWithApplePay is true. Its buttonType sets the button. Default: nil, no Apple Pay. |
configuration.appearance | PayDropInAppearance | No | Colors, fonts, shapes, and icons. Default: the system look. See Style the drop-in. |
configuration.strings | PayDropInStrings | No | Every text the sheet shows, in English by default. See Change the drop-in's text. |
configuration.beforePay | (@MainActor (PayPaymentMethod) async -> Bool)? | No | Runs immediately before the SDK submits the payment, with the method the customer chose, for example to update the session's amount on your server. Return false to stop. The sheet stays open, with no message. For Apple Pay, it runs before the Apple Pay sheet opens, and that sheet shows the new total. For the other methods, a tap that changes the amount isn't charged. The customer taps again, and beforePay runs again, so set the amount rather than adding to it. See When the amount changes. Default: nil. |
presenter | UIViewController | Yes | The view controller to present the sheet over (passed to present(from:)). If it's already presenting another controller, the sheet opens over that. |
Returns
present(from:) returns a PayResult once the sheet has closed:
| Result | When |
|---|---|
.succeeded, .processing | The customer paid, the payment is pending, or in a setup session their payment method was saved or chosen. The sheet closes by itself. |
.canceled | The customer closed the sheet. |
.failed(PayError) | The session can't be paid anymore and the customer closed the sheet. Create a new session to try again. Or the sheet wasn't shown. See Errors. |
When the customer cancels a 3DS challenge or a redirect, the sheet stays open so they can try again.
Errors
present(from:) doesn't throw. It returns these immediately, without showing the sheet:
| Error | When |
|---|---|
unsupported | The drop-in has no available payment methods (no method passes paymentMethods, the session offers only Google Pay, or Apple Pay is its only method and isn't configured or available). |
paymentInProgress | This drop-in is already showing. |
These errors end the sheet's payments. It shows a message and disables everything but closing, and present returns the error when the customer closes it.
| Error | When |
|---|---|
declined, with maxAttemptsReached | The session's last attempt was declined. |
sessionExpired | The session expired. |
sessionAlreadyCompleted, paymentEnded | The session was already paid, or the payment ended without being paid. |
Other errors, such as a decline with attempts left or a network failure, show a message in the sheet, and the customer can try again.
What the customer sees
The drop-in is a system page sheet with a grabber and a navigation bar. On iOS 16 and later, it opens at the height of its content. On iOS 15, it opens at half height, or at full height when the session takes cards. It grows to full height while the customer types. On every version, a new customer's sheet at checkout opens at full height when the session takes cards, and its content scrolls above the pinned button.
From top to bottom
The sheet stacks these parts, and shows each one only when it applies.
- The top. At checkout, Close on the left, then your
merchantDisplayNameand the total. In a setup or payout session, the title, "Payment" or "Payout method", centered, with Close on the left, and Edit on the right when the customer can remove saved methods. - Apple Pay, when you set it up, the session offers it, and the device can pay, with "or" under it when other methods follow.
- Saved payment methods, in setup and payout sessions. Each row has the method's logo and name, a card also its "Expires MM/YY", and the customer's default reads "Default". At checkout, a returning customer sees theirs in a carousel instead, and the methods below under Other ways to pay.
- Pay with (or Get paid with in a payout session) lists Card followed by the redirect methods in the session's order, each row with its logo. A checkmark marks the selected row. There's no list when Card is the only choice.
- Card information appears while Card is selected, presenting one grouped list with the card number, the expiration date and CVV side by side, and the name field (optional by default). Each input is labeled by its placeholder, and the number shows the card's brand at its end.
- The save-card switch, under the card and off by default ("Save card for future use at My Shop"). It isn't shown in a setup or recurring session, or when the session's
setup_future_usageison_session. - The failure message, after a failed attempt.
- One button, pinned above the keyboard. There's none when Apple Pay is the only method. Under it, the card brands the session accepts, when it lists them.
In a payout session:
A returning customer
At checkout, a customer with saved payment methods sees them first, and pays with the one in front.
- Close, on the left, then your
merchantDisplayNameand the total. - Apple Pay, when you set it up, the session offers it, and the device can pay, with "or" under it.
- The carousel of saved methods shows the chosen method in front with neighboring methods behind it. A card shows its card art when the card network sends it, and a drawn card otherwise. Swiping or tapping a card behind brings it to the front. The carousel holds up to five methods, starting with the default followed by the others in the order Pay.com lists them. Picking another method on Payment methods puts it in front.
- The chosen method's name, for example "Visa •••• 4242", and Change payment method.
- A CVV field, when the session checks CVVs and Pay.com doesn't hold one for the card.
- One button labeled Pay €24.90.
Change payment method opens Payment methods, listing every unexpired saved method as tiles in two columns with a checkmark on the chosen one. Tapping a tile goes back with that method in front, and Back goes back without a change.
When the customer can remove saved methods, Edit is on the right of Payment methods. It puts a remove control on each tile.
The last row, Other ways to pay, opens the card form and the session's other payment methods, with their own Pay button and, under it, the card brands the session accepts.
States
The sheet changes as the customer selects methods and pays.
| State | What the customer sees |
|---|---|
| The sheet opens | At checkout, a returning customer's default saved method, in front of the carousel. Otherwise their default saved method selected, or else the first row. |
| Another method is selected | The card form and its switch collapse, and come back with Card. |
| Card is the only method | The form, without a list. |
| Apple Pay is the only method | Its button alone. |
| The card details are wrong | "Card number is invalid", "Expiry date is invalid", or "CVV must be 3 or 4 digits" under the input immediately. After a tap on Pay, incomplete fields also display their validation messages, empty ones read "Required", and the first gets focus. |
| Paying | A spinner in the Pay button, or the Apple Pay sheet's own progress. Everything is disabled, and the sheet can't be closed. |
| 3DS or a redirect | Its screen opens over the sheet. A cancel returns to the sheet, with no message. |
| Declined | "Your payment method has been declined." The customer can try again. |
| The total changed | "The total has changed. Check the new total, then pay again." The button shows the new total, and nothing was charged. |
| Another failure | "Check your connection and try again." or "Something went wrong. Please try again." |
| The session can't be paid anymore | "You have reached the maximum number of payment attempts.", "The session has expired and payment can no longer be made.", or "This payment can no longer be completed." Everything but Close is disabled. |
| A setup or payout session | Saved methods first. Using one makes it the customer's default. |
| Removing a saved method | Edit shows a red minus on each saved row. The customer confirms "Remove Visa •••• 4242?" with Remove, or taps Cancel. Once nothing is left to select, the sheet closes as .canceled. |
| Removing a saved method at checkout | On Payment methods, Edit shows a remove control on each tile. The customer confirms "Remove Visa •••• 4242?" with Remove, or taps Cancel, and a spinner shows on the tile while it's removed. If the chosen method is removed, the default, or else the first, takes its place. When the customer removes their last saved method, the sheet switches to the new-customer view. |
| Large text | At accessibility text sizes, a list headed "Saved" replaces the carousel and method name, showing every saved method with a checkmark on the chosen one and Change payment method underneath. Payment methods shows its tiles in one column. |
| Success | The sheet closes automatically and returns the PayResult so your app can display confirmation. |
The button
The button's label depends on the session and the selected method.
| Session and selection | The button reads |
|---|---|
| A payment | Pay €10.00, or Pay when the session has no amount or currency |
| A setup session, with a new method | Add your payment method |
| A setup session, with a saved method | Use payment method |
| A payout session, with any method | Use payment method |
Logos
The sheet shows the same logos as Pay.com.js. They're bundled in PayElements, so nothing is downloaded.
| Where | What it shows |
|---|---|
| A method's row | The method's logo, as Pay.com.js shows it. A method without a logo of its own shows its monogram, as Pay.com.js does (up to two initials of its name on a white card, for example BD for Bacs Direct Debit). |
| The Card row | A card. |
| A saved method's row | The card's brand, or the method's logo. Without either, a monogram. |
| A saved method at checkout | A card's art, when the card network sends it, or else a drawn card in the brand's color, with the issuer or brand, the last 4 digits, the expiration date, and the brand's mark. PayPal and bank accounts show their logo on a white card in light mode, and a dark gray one in dark mode. |
| The card number input | The detected card brand at the end of the field (Visa, Mastercard, American Express, or Discover), or a generic card icon. VoiceOver reads the brand after the input's label. |
| Under the button | When the session takes cards and lists accepted brands, the logos for Visa, Mastercard, Discover, and American Express appear under the button. Other brands aren't shown there. VoiceOver reads them as "Accepted cards: Visa, Mastercard". |
Your methodIcons replace the logo or monogram on every row of their type. An icon for card replaces the Card row's and the saved cards'. A redirect method's row reads the method's name, as Pay.com.js shows it, unless you set one in strings.methodNames. Its monogram takes its initials from the name the row shows.
When the amount changes
The drop-in checks the session's total when the sheet opens and again before it charges.
- When the sheet opens, it reads the session's total again, for up to 5 seconds. If it can't, it opens with the total it has, and still checks before it charges.
- When
beforePayor your server changes the total, nothing is charged at the old one. The Pay button shows the new total, under "The total has changed. Check the new total, then pay again.", and the customer taps again. beforePayruns again on that tap, so set the amount rather than adding to it.- Apple Pay opens with the new total. If the total changes while the Apple Pay sheet is open, that sheet closes and the drop-in shows the same message.
present doesn't return amountChanged. The customer pays again or closes the sheet. See When the amount changes for the check itself.
Example
import PayCheckout
import PayCore
import PayDropIn
import UIKit
extension CartViewController {
// clientSecret comes from your server, which created the session.
func checkoutTapped(clientSecret: String) async {
do throws(PayError) {
let checkout = try await PayCheckout(
clientSecret: clientSecret,
configuration: .init(returnURL: URL(string: "myshop://pay")!)
)
var configuration = PayDropIn.Configuration(merchantDisplayName: "My Shop")
configuration.applePay = PayApplePayConfiguration(merchantIdentifier: "merchant.com.myshop")
configuration.beforePay = { [weak self] _ in
await self?.reserveStock() ?? false // Return false to stop without a message
}
switch await PayDropIn(checkout: checkout, configuration: configuration).present(from: self) {
case .succeeded(let ids), .processing(let ids):
showConfirmation(ids) // A hint: your server confirms before fulfilling
case .canceled:
break // The customer closed the sheet
case .failed(let error):
showMessage(error.localizedDescription) // Create a new session to try again
}
} catch {
showMessage(error.localizedDescription) // The checkout couldn't start
}
}
}Next steps
- Style the drop-in: Colors, fonts, shapes, and icons.
- Change the drop-in's text: Replace or translate every text.
- Handle the return URL: Finish redirects that come back to your app.
- Pay with Apple Pay: The Apple Pay requirements.