> ## Documentation Index
> Fetch the complete documentation index at: https://primer.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PrimerDelegate

## Overview

Set the `PrimerDelegate` in order to receive different callbacks from Primer SDK.

## primerDidCompleteCheckoutWithData

```swift SWIFT theme={"dark"}
primerDidCompleteCheckoutWithData(_:additionalInfo:)
```

This method will be called with the when the payment has been completed, returning the payment's data.\
\

⚠️ This method won't be called when you are using manual payment handling, or the vault manager.

### Parameters

**Parameters**

**additionalInfo** `PrimerCheckoutAdditionalInfo`

**direct subclasses**

**XenditCheckoutVoucherAdditionalInfo**

**Parameters**

**expiresAt** `String` *required*

**couponCode** `String` *required*

**retailerName** `String` *required*

**PromptPayCheckoutAdditionalInfo**

**Parameters**

**expiresAt** `String` *required*

**qrCodeUrl** `String`

**qrCodeBase64** `String` *required*

**MultibancoCheckoutAdditionalInfo**

**Parameters**

**expiresAt** `String`

**entity** `String`

**reference** `String`

**ACHAdditionalInfo**

**Subclasses**

**ACHBankAccountCollectorAdditionalInfo** `ACHAdditionalInfo`

**Properties**

**collectorViewController** `UIViewController`

Provides the bank account collector for ACH bank account
selection.

**ACHMandateAdditionalInfo** `ACHAdditionalInfo`

Provides an empty `ACHMandateAdditionalInfo` object to
prompt the merchant to display the mandate information.

### Example

```swift SWIFT theme={"dark"}
// 👇 Add this
import PrimerSDK

// 👇 Conform to PrimerDelegate
class MyViewController: UIViewController, PrimerDelegate {

	// ...

    // 👇 Add this
	func primerDidCompleteCheckoutWithData(_ data: PrimerCheckoutData) {
        // Do something with the payment's data
    }
}
```

## primerDidTokenizePaymentMethod

```swift SWIFT theme={"dark"}
func primerDidTokenizePaymentMethod(_:decisionHandler:)
```

This method will be called with the when the payment method has been tokenized, returning the payment method's tokenization data. Use the `token` to create a payment on your backend. Make sure that you call the `decisionHandler` once your operation has been completed.\
\

⚠️ This method will be called only on the manual flow, or when vaulting.

### Parameters

**Parameters**

**paymentMethodTokenData** `PrimerPaymentMethodTokenData`

**Parameters**

**analyticsId** `String`

**id** `String`

**paymentInstrumentData** `PaymentInstrumentData`

**Parameters**

**paymentMethodConfigId** `String`

**paymentMethodType** `String`

**sessionInfo** `SessionInfo`

**first6Digits** `String`

**firstDigits** `String`

**last4Digits** `String`

**accountNumberLast4Digits** `String`

**expirationMonth** `String`

**expirationYear** `String`

**cardholderName** `String`

**network** `String`

**isNetworkTokenized** `String`

**binData** `BinData`

**threeDSecureAuthentication** `ThreeDS.AuthenticationDetails`

**paypalBillingAgreementId** `String`

**externalPayerInfo** `ExternalPayerInfo`

Additional PayPal data.

**Properties**

**email** `String`

The payer's email address.

**externalPayerId** `String?`

The payer's unique ID.

**firstName** `String?`

The payer's given name.

**lastName** `String?`

The payer's given surname.

**shippingAddress** `ShippingAddress`

**klarnaCustomerToken** `String`

**sessionData** `SessionData`

**bankName** `String`

**paymentInstrumentType** `PaymentInstrumentType`

**Cases**

**paymentCard**

**offSession**

**cardOffSession**

**payPalOrder**

**payPalBillingAgreement**

**applePay**

**googlePay**

**klarna**

**klarnaPaymentSession**

**klarnaCustomerToken**

**apayaToken**

**hoolah**

**unknown**

**threeDSecureAuthentication** `ThreeDS.AuthenticationDetails`

**token** `String` *required*

**tokenType** `TokenType`

**Cases**

**multiUse**

**singleUse**

**vaultData** `VaultData`

**decisionHandler** `enum PrimerResumeDecision` *required*

**Cases**

**succeed()**

Call `.succeed` when you want to show the success screen, or finalize the flow successfully in case you have hidden the success screen.

**.fail(withErrorMessage:)**

Call `.fail` when you want to show the error screen, or finalize the flow with error in case you have hidden the error screen.

**.continueWithNewClientToken(\_:)**

Call `.continueWithNewClientToken` when the payment has entered in a PENDING state and a required action client token has been provided.

### Example

```swift SWIFT theme={"dark"}
// 👇 Add this
import PrimerSDK

// 👇 Conform to PrimerDelegate
class MyViewController: UIViewController, PrimerDelegate {

	// ...

    // 👇 Add this
	func primerDidTokenizePaymentMethod(_ paymentMethodTokenData: PrimerPaymentMethodTokenData, decisionHandler: @escaping (PrimerResumeDecision) -> Void) {
        // Create your payment
        createPayment(with: paymentMethodTokenData.token) { res, err in
            if let err = err {
                // 👇 Call the decisionHandler to show the error screen with your custom error message
                decisionHandler(.fail(withErrorMessage: "Oh no, something went wrong creating the payment..."))

            } else if let res = res {
                if let newClientToken = res.requiredAction?.clientToken {
                    // 👇 Call the decisionHandler with the new client token to continue the flow
                    decisionHandler(.continueWithNewClientToken(requiredAction.clientToken))
                } else {
                    // 👇 Call the decisionHandler to show the success screen
                    decisionHandler(.succeed())
                }
            }
        }
    }
}
```

## primerDidResumeWith

```swift SWIFT theme={"dark"}
func primerDidResumeWith(_:decisionHandler:)
```

This method will be called providing a `resumeToken` so you can resume the payment.\
\

⚠️ This method will be called only on the manual flow when the payment is in a `.PENDING` state..

### Parameters

**Parameters**

**resumeToken** `String` *required*

The token used to resume the payment.

**decisionHandler** `enum PrimerResumeDecision` *required*

**Cases**

**succeed()**

Call `.succeed` when you want to show the success screen, or finalize the flow successfully in case you have hidden the success screen.

**.fail(withErrorMessage:)**

Call `.fail` when you want to show the error screen, or finalize the flow with error in case you have hidden the error screen.

**.continueWithNewClientToken(\_:)**

Call `.continueWithNewClientToken` when the payment has entered in a PENDING state and a required action client token has been provided.

### Example

```swift SWIFT theme={"dark"}
// 👇 Add this
import PrimerSDK

// 👇 Conform to PrimerDelegate
class MyViewController: UIViewController, PrimerDelegate {

	// ...

    // 👇 Add this
	func primerDidResumeWith(_ resumeToken: String, decisionHandler: @escaping (PrimerResumeDecision) -> Void)
        // Resume your payment
        resumePayment(with: resumeToken) { res, err in
            if let err = err {
                // 👇 Call the decisionHandler to show the error screen with your custom error message
                decisionHandler(.fail(withErrorMessage: "Oh no, something went wrong creating the payment..."))

            } else if let res = res {
                if let newClientToken = res.requiredAction?.clientToken {
                    // 👇 Call the decisionHandler with the new client token to continue the flow
                    decisionHandler(.continueWithNewClientToken(requiredAction.clientToken))
                } else {
                    // 👇 Call the decisionHandler to show the success screen
                    decisionHandler(.succeed())
                }
            }
        }
    }
}
```

## primerClientSessionWillUpdate

This method will be called to notify you that the client session will be updated, e.g.
a surcharge needs to be applied when a payment method has been chosen.

```swift SWIFT theme={"dark"}
func primerClientSessionWillUpdate()
```

## primerClientSessionDidUpdate

```swift SWIFT theme={"dark"}
func primerClientSessionDidUpdate(_ clientSession: PrimerClientSession)
```

This method will be called to notify you that the client session has been updated, e.g.
surcharge has been applied when a payment method has been chosen.

### Parameters

**Parameters**

**clientSession** `PrimerClientSession` *required*

## primerWillCreatePaymentWithData

This method will be called with just before the payment gets created, and lets you decide whether you want to proceed
with the payment creation.

⚠️ This method will be called only when checking out using the `auto` [flow](https://primer.io/docs/sdk/ios/v2.x.x/common-objects/PrimerSettings.md).

```swift SWIFT theme={"dark"}
func primerWillCreatePaymentWithData(_ data: PrimerCheckoutPaymentMethodData, decisionHandler: @escaping (PrimerPaymentCreationDecision) -> Void)
```

### Parameters

**Parameters**

**data** `PrimerCheckoutPaymentMethodData`

**Parameters**

**paymentMethodType** `PrimerCheckoutPaymentMethodType`

**Parameters**

**type** `String`

**decisionHandler** `enum PrimerPaymentCreationDecision` *required*

**Cases**

**.continuePaymentCreation(withIdempotencyKey:)**

Call `.continuePaymentCreation` when you want to continue with the payment creation.
Optionally provide a `withIdempotencyKey` to prevent duplicate payments. The SDK attaches it as `X-Idempotency-Key` header on payment creation and resume requests. The same key is reused for the entire payment lifecycle and automatically cleared on completion or failure.

**.abortPaymentCreation(withErrorMessage:)**

Call `.abortPaymentCreation` when you want to abort the payment. Optionally you can provide an error message to get presented on the error screen.

### Example

```swift SWIFT theme={"dark"}
// 👇 Add this
import PrimerSDK

// 👇 Conform to PrimerDelegate
class MyViewController: UIViewController, PrimerDelegate {

	// ...

    // 👇 Add this
	func primerWillCreatePaymentWithData(_ data: PrimerCheckoutPaymentMethodData, decisionHandler: @escaping (PrimerPaymentCreationDecision) -> Void)

    }
}
```

### Example with Idempotency Key

```swift SWIFT theme={"dark"}
class MyViewController: UIViewController, PrimerDelegate {
    func primerWillCreatePaymentWithData(
        _ data: PrimerCheckoutPaymentMethodData,
        decisionHandler: @escaping (PrimerPaymentCreationDecision) -> Void
    ) {
        let idempotencyKey = UUID().uuidString
        decisionHandler(.continuePaymentCreation(withIdempotencyKey: idempotencyKey))
    }
}
```

## primerDidEnterResumePendingWithPaymentAdditionalInfo

```swift SWIFT theme={"dark"}
func primerDidEnterResumePendingWithPaymentAdditionalInfo(_ additionalInfo: PrimerCheckoutAdditionalInfo?)
```

This method will be called when the payment cannot get settled at this time, and will get settled in the future (e.g. a voucher)

**Parameters**

**additionalInfo** `PrimerCheckoutAdditionalInfo`

**direct subclasses**

**XenditCheckoutVoucherAdditionalInfo**

**Parameters**

**expiresAt** `String` *required*

**couponCode** `String` *required*

**retailerName** `String` *required*

**PromptPayCheckoutAdditionalInfo**

**Parameters**

**expiresAt** `String` *required*

**qrCodeUrl** `String`

**qrCodeBase64** `String` *required*

**MultibancoCheckoutAdditionalInfo**

**Parameters**

**expiresAt** `String`

**entity** `String`

**reference** `String`

**ACHAdditionalInfo**

**Subclasses**

**ACHBankAccountCollectorAdditionalInfo** `ACHAdditionalInfo`

**Properties**

**collectorViewController** `UIViewController`

Provides the bank account collector for ACH bank account
selection.

**ACHMandateAdditionalInfo** `ACHAdditionalInfo`

Provides an empty `ACHMandateAdditionalInfo` object to
prompt the merchant to display the mandate information.

## primerDidFailWithError

```swift SWIFT theme={"dark"}
func primerDidFailWithError(_ error: Error, data: PrimerCheckoutData?, decisionHandler: @escaping ((PrimerErrorDecision) -> Void))
```

This method will be called when an error occurs. It may return `PrimerCheckoutData` if the occurs after the payment creation.
Make sure you call the `decisionHandler` to finalize the flow.

### Parameters

**Parameters**

**error** `Error` *required*

The error that occurred during the operation.

**data** `PrimerCheckoutData`

**Parameters**

**additionalInfo** `PrimerCheckoutAdditionalInfo`

**direct subclasses**

**XenditCheckoutVoucherAdditionalInfo**

**Parameters**

**expiresAt** `String` *required*

**couponCode** `String` *required*

**retailerName** `String` *required*

**PromptPayCheckoutAdditionalInfo**

**Parameters**

**expiresAt** `String` *required*

**qrCodeUrl** `String`

**qrCodeBase64** `String` *required*

**MultibancoCheckoutAdditionalInfo**

**Parameters**

**expiresAt** `String`

**entity** `String`

**reference** `String`

**ACHAdditionalInfo**

**Subclasses**

**ACHBankAccountCollectorAdditionalInfo** `ACHAdditionalInfo`

**Properties**

**collectorViewController** `UIViewController`

Provides the bank account collector for ACH bank account
selection.

**ACHMandateAdditionalInfo** `ACHAdditionalInfo`

Provides an empty `ACHMandateAdditionalInfo` object to
prompt the merchant to display the mandate information.

**decisionHandler** `PrimerErrorDecision` *required*

**enum cases**

**fail(withErrorMessage:)** `void`

Call with your custom error message.

### Example

```swift SWIFT theme={"dark"}
// 👇 Add this
import PrimerSDK

// 👇 Conform to PrimerDelegate
class MyViewController: UIViewController, PrimerDelegate {

	// ...

    // 👇 Add this
	func primerDidFailWithError(_ error: Error, data: PrimerCheckoutData?, decisionHandler: @escaping ((PrimerErrorDecision) -> Void)) {

    }
}
```

## primerDidDismiss

This method will be called to notify you that the Primer SDK has been dismissed.

```swift SWIFT theme={"dark"}
func primerDidDismiss()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.