We currently have two variations of Example apps.
Example
folderUIKit Example
folderTo get started with either of these:
ContentView.swift
(SwiftUI Example App) or ViewController.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()
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:
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 a ChargeError
. The ChargeError
contains a cause
property which details the actual error received and should be used for error handling and logging.