Accept payments in your iOS app
Take a first payment in an iOS app, with Pay.com's drop-in or your own screen.
To take a payment in the sandbox, your server creates a payment session, the app takes the payment, and your server confirms the result. Show the drop-in, which takes the least code, or build your own screen. The iOS SDK reference describes the classes used here.
| Drop-in (recommended) | Your own UI | |
|---|---|---|
| What you build | A button that presents Pay.com's sheet | A screen, from the payment methods and the card fields |
| Payment methods | Cards, redirect methods, and Apple Pay | The ones you show |
| Errors and retries | The sheet shows them | You show them |
| Look | The system's, or your colors and shapes | Yours |
Both show the customer's saved methods at checkout. The drop-in shows them in a carousel, and your own screen shows them as in Pay with a saved payment method.
- 1
Create a payment session on your server
Call Create a payment session with your sandbox key.
amount,currency, andcustomer_reference_id(your own identifier for the customer) are required. Amounts are in minor units. For example,1000is $10.00.curl -X POST https://api.sandbox.pay.com/v1/sessions/payment \ -u ':sk-pay-com-sandbox-…' \ -H "Content-Type: application/json" \ -d '{ "amount": 1000, "currency": "usd", "customer_reference_id": "cust-001" }'Send only the
client_secretfrom the response to the app. Never put your API key in the app. - 2
Add the SDK
Add the
pay-com-iospackage with Swift Package Manager and link the products for the UI you choose. See Install.- The drop-in:
PayCheckout,PayCore, andPayDropIn. - Your own UI:
PayCheckout,PayCore, andPayElements.
- The drop-in:
- 3
Start a checkout
Create a
PayCheckoutwith theclient_secret. The initializer checks the secret and loads the session, so it'sasyncand throws aPayErrorwhen it fails.import PayCheckout import PayCore // PayResult, PayError, PaymentIDs: PayCheckout doesn't re-export them let checkout = try await PayCheckout( clientSecret: clientSecretFromYourServer, configuration: .init(returnURL: URL(string: "myshop://pay")!) ) // The session's payment methods, in the order the API returns them. let methods = checkout.paymentMethods() // [PayPaymentMethod(type: "card", kind: .card), …]returnURLis the URL that redirect payment methods return the customer to. It's a custom URL scheme your app handles, or a universal link. Live and sandbox sessions use the same configuration. - 4
Take the payment
Present the drop-in, or build your own card form and call
pay.Present the sheet from your view controller when the customer taps your checkout button. It shows the session's methods, the card form, and any failure. It also runs 3DS and closes when the payment is done.
import PayDropIn let dropIn = PayDropIn(checkout: checkout, configuration: .init(merchantDisplayName: "My Shop")) switch await dropIn.present(from: self) { case .succeeded(let ids), .processing(let ids): showConfirmation(ids) // A hint: your server verifies before fulfilling case .canceled: break // The customer closed the sheet case .failed(let error): show(error) // The session can't be paid any more }To show Apple Pay in the sheet, set the configuration's
applePay. Present the drop-in has the options and what the customer sees, and you can change its look and text. - 5
Pass return URLs to the SDK
In your scene delegate or SwiftUI
onOpenURL, pass every URL your app is opened with:.onOpenURL { url in PayCheckout.handleReturn(url) // false when the URL isn't a Pay.com return }It's safe to call with any URL. It returns
trueonly when the URL belongs to a Pay.com flow in progress. - 6
Confirm the payment on your server
PayResultreflects only client-side state. Before you fulfill the order, retrieve the session with Retrieve a payment session or wait for the signedpayment_session.completedwebhook. Use the session ID your server got when it created the session.