We currently have two variations of Example apps.
- A SwiftUI example app integrating the SDK using Cocoapods can be found inside the
Example
folder - A UIKit example app integrating the SDK using Swift Package Manager can be found inside in the
UIKit Example
folder
To get started with either of these:
- Open the associated project file.
- Inside
ContentView.swift
(SwiftUI Example App) orViewController.swift
(UIKit Example app), modify the Paystack object that is being built to include the public key of the integration being used.
let paystackObject = try? PaystackBuilder
.newInstance
.setKey("PUBLIC KEY GOES HERE")
.build()
- Whenever a transaction has been created on your integration, you will provide the associated access code to the application where designated in the files above.
You are now able to use the Example app to test live payments on your integration.
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
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:
- In Xcode, select “File” → “Add Packages...”
- 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
Our recommended way to use the SDK is to make use of the payment flow we provide.
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))")
}
}
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))")
}
}
There are 3 possibly results that can be returned
Completed
: The customer completed the payment process. Verify the transaction status and amount on the server before providing value. See Paystack's docsCancelled
: The customer cancelled the payment attempt.Failed
: An error occurred during the payment process. This result contains aChargeError
. TheChargeError
contains acause
property which details the actual error received and should be used for error handling and logging.