Skip to Content
Docs

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 buildA button that presents Pay.com's sheetA screen, from the payment methods and the card fields
Payment methodsCards, redirect methods, and Apple PayThe ones you show
Errors and retriesThe sheet shows themYou show them
LookThe system's, or your colors and shapesYours

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. 1

    Create a payment session on your server

    Call Create a payment session with your sandbox key. amount, currency, and customer_reference_id (your own identifier for the customer) are required. Amounts are in minor units. For example, 1000 is $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_secret from the response to the app. Never put your API key in the app.

  2. 2

    Add the SDK

    Add the pay-com-ios package with Swift Package Manager and link the products for the UI you choose. See Install.

    • The drop-in: PayCheckout, PayCore, and PayDropIn.
    • Your own UI: PayCheckout, PayCore, and PayElements.
  3. 3

    Start a checkout

    Create a PayCheckout with the client_secret. The initializer checks the secret and loads the session, so it's async and throws a PayError when 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), …]

    returnURL is 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. 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. 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 true only when the URL belongs to a Pay.com flow in progress.

  6. 6

    Confirm the payment on your server

    PayResult reflects only client-side state. Before you fulfill the order, retrieve the session with Retrieve a payment session or wait for the signed payment_session.completed webhook. Use the session ID your server got when it created the session.

If you offer payment methods that redirect, your server must create the session with return_url set to the same URL as returnURL.

Was this page helpful?