PaystackHQ / paystack-sdk-ios

MIT License
1 stars 0 forks source link

Paystack iOS SDK

Getting started with the Example app:

We currently have two variations of Example apps.

To get started with either of these:

  let paystackObject = try? PaystackBuilder
        .newInstance
        .setKey("PUBLIC KEY GOES HERE")
        .build()

You are now able to use the Example app to test live payments on your integration.

Integrating the SDK into your app:

We have two modules: PaystackCore and PaystackUI We recommend those who want to make use of our Payment flows to use both. However, if you are just looking for just the basic APIs, you only need to import PaystackCore

Swift Package Manager:

To install the SDK using Swift Package Manager you can follow the tutorial published by Apple using the URL for the Paystack SDK repo with the current version:

  1. In Xcode, select “File” → “Add Packages...”
  2. Enter https://github.com/PaystackHQ/paystack-sdk-ios.git

After installing with Swift package Manager into your project import it using

import PaystackCore
import PaystackUI

Using the PaymentUI payment flow

Our recommended way to use the SDK is to make use of the payment flow we provide.

SwiftUI

For SwiftUI, we provide a customizable button that inserts directly into your UI.

paystackObject?.chargeUIButton(accessCode: "transaction access code",
                onComplete: paymentDone) {
    // Stylize your button 
    Text("Pay")
}

func paymentDone(_ result: TransactionResult) {
    switch (result){
    case .completed(let details):
        print("Transaction completed with reference: \(details.reference)")
    case .cancelled:
        print("Transaction was cancelled")
    case .error(error: let error, reference: let reference):
        print("An error occured: \(error.message) with reference: \(String(describing: reference))")
    }
}

UIKit

For UIKit, you would create your own button to trigger the flow and provide a reference to the view controller being displayed.

@IBAction func payButtonTapped(_ sender: Any) {
        paystack?.presentChargeUI(on: self,
                                  accessCode: "transaction access code",
                                  onComplete: paymentDone)
}

func paymentDone(_ result: TransactionResult) {
    switch (result){
    case .completed(let details):
        print("Transaction completed with reference: \(details.reference)")
    case .cancelled:
        print("Transaction was cancelled")
    case .error(error: let error, reference: let reference):
        print("An error occured: \(error.message) with reference: \(String(describing: reference))")
    }
}

Handling the Transaction Result

There are 3 possibly results that can be returned