# SDK Swift for iOS

The KKiaPay SDK for Swift is designed to make the integration of our plugin into your mobile application as easy as possible. To set it up you will need to [create an account](/v1/en-1.0.0/compte/creation-dun-compte.md) on [kkiapay.me](/v1/en-1.0.0/accueil/kkiapay.md) platform and follow the process here describes :&#x20;

* **Add the KKiaPay Swift SDK to your application**

You can add the KKiaPay Swift SDK to your project using the Swift package manager.

{% hint style="info" %}
Practical guide for installing a package with Swift : [Installing a package](https://github.com/apple/swift-package-manager/tree/main/Documentation).
{% endhint %}

* **Import the package KKiaPay**

```swift
 import KKiaPaySDK;
```

* **Create a view model to use later**

```swift
@ObservedObject var viewModel = KKiaPayViewModel()
```

* **Initialise the KKiaPay Instance**

```swift
 private var kkiaPay: KKiaPay{
    KKiaPay(amount: "3000",
            phone: "22997000000",
            data: "Hello world",
            publicAPIKey: "xxxxxxxxxxxxxxxxxxx",
            sandbox: true,//set this to false in production
            theme: "#4E6BFC",
            name: "John Doe",
            email:"user@email.com",
            callback: "https://redirect.kkiapay.com",
            viewModel:viewModel
    )
}
```

ParamètresTypeDescriptionLe montant à payer en FCFAStringUn numéro mobile money valideLa donnée ou information liée à une transaction et interprétable par votre systèmeStringLa [clé publique](https://docs.kkiapay.me/v1/compte/untitled#recuperation-des-cles-de-lapi-kkiapay) liée à votre [compte KKiaPay](https://docs.kkiapay.me/v1/compte/creation-dun-compte)​StringLes nom et prénoms de l’initiateur du paiementStringL’adresse mail de l’initiateur du paiementVous permet de basculer de l'environnement de test à l'environnement de production. La valeur ***true*** de cet attribut signifie que vous êtes en [environnement de test](https://docs.kkiapay.me/v1/compte/kkiapay-sandbox-guide-de-test)​La fonction de callback appelée uniquement lorsque le paiement est effectué avec succès

<table><thead><tr><th width="210.7072125074535">Attribute</th><th width="150">Type</th><th>Description</th></tr></thead><tbody><tr><td>amount</td><td>Numeric</td><td>The amount to be paid (in XOF)</td></tr><tr><td>phone</td><td>String</td><td>A valid money mobile number (MTN or MOOV)<br>Ex: 22997000000<br>Ex: 22995000000</td></tr><tr><td>data</td><td>String</td><td>The data or information related to a transaction and interpretable by your system</td></tr><tr><td>publicAPIKey</td><td>String</td><td>The <a href="/pages/-LZJbJj9MGLVjir8f2Fy#api-keys">public key</a> linked to your <a href="/pages/-LZAiPPO6niiRYglOZtO">KKiaPay account</a></td></tr><tr><td>name</td><td>String</td><td>The first and last name of the initiator of the payment</td></tr><tr><td>email</td><td>String</td><td>The email address of the initiator of the payment</td></tr><tr><td>sandbox</td><td>Boolean</td><td>Allows you to switch from the <a href="/pages/-LgC0LzgcIQmxMNj-Suu">test environment</a> to the production environment. The value <em><strong>true</strong></em> for this attribute means that you are in a test environment</td></tr><tr><td>callback</td><td>Function</td><td>The callback function called only when the payment was successful</td></tr></tbody></table>

{% hint style="danger" %}
**Important !**&#x20;

To avoid fraud, perform the server-side verification of the transaction operation. Learn more about the [Admin SDKs (Server-Side)](/v1/en-1.0.0/plugin-et-sdk/admin-sdks-server-side.md).
{% endhint %}

You can subscribe to payment transaction events with the **onReceive** function. To be notified of the successful status of a payment transaction, you can proceed as follows :

```swift
kkiaPay.onReceive(self.viewModel.paymentData.receive(on: RunLoop.main)){paymentData in
    
    if(paymentData.isSuccessful){
        print("The amount of the transaction is " + paymentData.amount+" with id "+paymentData.transactionId)
        showWebView = false
    }else{
        print("The payment was not successful")
    }
}
```

### Example

```swift
import SwiftUI
import KKiaPaySDK

struct ContentView: View {
        //Create a view model instance to use later
        @ObservedObject var viewModel = KKiaPayViewModel()
        @State private var showWebView = false
        
        //Initialise the Kkiapay Instance
        private var kkiaPay: KKiaPay{
            KKiaPay(amount: "3000",
                    phone: "97000000",
                    data: "Hello world",
                    publicAPIKey: "xxxxxxxxxxxxxxxxxxx",
                    sandbox: true,//set this to false in production
                    theme: "#4E6BFC",
                    name: "John Doe",
                    email:"user@email.com",
                    callback: "https://redirect.kkiapay.com",
                    viewModel:viewModel
            )
        }
        
        var body: some View {
            Button {
                showWebView.toggle()
            } label: {
                Text("Pay")
            }
            .sheet(isPresented: $showWebView) {
                
                //Get the transaction data back 
                kkiaPay.onReceive(self.viewModel.paymentData.receive(on: RunLoop.main)){paymentData in
                    
                    if(paymentData.isSuccessful){
                        print("The amount of the transaction is " + paymentData.amount+" with id "+paymentData.transactionId)
                        showWebView = false
                    }else{
                        print("The payment was not successful")
                    }
                }
            }
        }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
```

{% hint style="info" %}
For more informations on the SDK please follow the link below : [KKiaPay Swift SDK.](https://github.com/kkiapay/kkiapay-ios-sdk)
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kkiapay.me/v1/en-1.0.0/plugin-et-sdk/sdk-swift-for-ios.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
