Skip to content

iOS

Accept card payments and Apple Pay in your iOS app with the Cardencer payment sheet.

The Cardencer iOS SDK provides a native payment sheet with cards and Apple Pay built in, and runs 3D Secure inside your app.

Requirements#

  • iOS 15.1 or later
  • Xcode with CocoaPods

1. Set up your server#

Your server creates the payment and returns sdkAuthorization, publishableKey and profileId to the app. Use the same endpoint as for Android. Never ship your secret key in the app.

2. Add the SDK#

Add the pod to your Podfile:

ruby
target 'YourApp' do
  pod 'hyperswitch-sdk-ios'
end

Install it:

bash
pod install

From now on, open your project with the .xcworkspace file. To update the SDK later, run pod install --repo-update.

3. Initialise the SDK#

Create the SDK instance with the publishable key and profile ID from your server, pointing at the Cardencer API (https://sandbox.cardencer.com for testing, https://api.cardencer.com in production):

swift
import Hyperswitch

let configuration = HyperswitchConfiguration(
    publishableKey: publishableKey,
    profileId: profileId,
    customEndpoints: .overrideEndpoints(
        OverrideEndpointConfiguration(customBackendEndpoint: "https://sandbox.cardencer.com")
    )
)
let sdk = Hyperswitch(configuration: configuration)

4. Create a payment session#

Fetch a payment from your server when the checkout screen loads, then start a session with the sdkAuthorization:

swift
let paymentSession = sdk.initPaymentSession(
    configuration: PaymentSessionConfiguration(sdkAuthorization: sdkAuthorization)
)

5. Present the payment sheet#

swift
@objc
func openPaymentSheet(_ sender: Any) {
    var configuration = PaymentSheet.Configuration()
    configuration.merchantDisplayName = "Atelier Martin"

    var appearance = PaymentSheet.Appearance()
    appearance.colors.primary = UIColor(red: 0.31, green: 0.42, blue: 0.87, alpha: 1.0)
    appearance.primaryButton.shapes.borderRadius = 12
    configuration.appearance = appearance

    paymentSession.presentPaymentSheet(viewController: self, configuration: configuration) { result in
        DispatchQueue.main.async {
            switch result {
            case .completed: self.showMessage("Payment complete")
            case .canceled: self.showMessage("Payment canceled")
            case .failed(let error): self.showMessage("Payment failed: \(error.localizedDescription)")
            }
        }
    }
}

In SwiftUI, use PaymentSheet.PaymentButton:

swift
PaymentSheet.PaymentButton(
    paymentSession: paymentSession,
    configuration: configuration,
    onCompletion: { result in self.paymentResult = result }
) {
    Text("Pay €49.00")
}

.completed means the sheet finished, not that you were paid. Confirm succeeded on your server through a webhook or GET /payments/{payment_id} before you deliver.

Apple Pay#

Apple Pay shows in the sheet on supported devices once it is enabled for your account. Contact support to enable it for your app's merchant identifier.

Test it#

Use the test cards. 4000 0038 0000 0446 shows a 3D Secure challenge.

Going live#

Switch the endpoint to https://api.cardencer.com and return your pk_prd_ key from your server. See Going live.