Integrate Pesaswap SDK to your Swift App using Pesaswap-node
Use this guide to integrate Pesaswap SDK to your iOS app. You can use the following app as a reference with your Pesaswap credentials to test the setup. You can also check out the app on Apple TestFlight to test the payment flow.
Requirements
- iOS 13.0 and above
- CocoaPods
- npm
1. Setup the Server
Follow the Server Setup section.
2. Build Checkout Page on Your App
CocoaPods Setup (only required if not already done)
Install the latest version of CocoaPods.
To create a Podfile, run the following command:
SDK Setup
Add these lines to your Podfile:
use_frameworks!
target 'YourApp' do
pod 'Pesaswap-sdk-ios'
end
Run the following command:
Important: Moving forward, you should open your project in Xcode using the .xcworkspace file rather than the .xcodeproj file.
To update to the latest version of the SDK, run:
pod install --repo-update
2.2 Setup the SDK and Fetch a Payment
Set up the SDK using your publishable key. This is essential for initializing a PaymentSession.
import Pesaswap
paymentSession = PaymentSession(publishableKey: "YOUR_PUBLISHABLE_KEY")
Note: For Open Source Setup, initialize your custom Backend app & log URL as:paymentSession = PaymentSession(publishableKey: "YOUR_PUBLISHABLE_KEY", customBackendUrl: "YOUR_SERVER_URL", customLogUrl: "YOUR_LOG_URL")
2.3 Complete the Payment on Your App
Fetch a Payment
Request your server to fetch a payment as soon as your view is loaded. Store the client_secret returned by your server. The PaymentSession will use this secret to complete the payment process.
var paymentSession: PaymentSession?
paymentSession?.initPaymentSession(paymentIntentClientSecret: paymentIntentClientSecret)
Handle Payment Result
Handle the payment result in the completion block and display appropriate messages to your customer based on whether the payment fails with an error or succeeds.
@objc
func openPaymentSheet(_ sender: Any) { // present payment sheet
var configuration = PaymentSheet.Configuration()
configuration.merchantDisplayName = "Example, Inc."
paymentSession?.presentPaymentSheet(
viewController: self,
configuration: configuration,
completion: { result in
switch result {
case .completed:
print("Payment complete")
case .failed(let error):
print("Payment failed: \(error.localizedDescription)")
case .canceled:
print("Payment canceled.")
}
}
)
}
Please retrieve the payment status from the Pesaswap backend to get the terminal status of the payment. Do not rely solely on the status returned by the SDK, as it may not always reflect the final state of the transaction.
3. Card Element (Beta)
Create a card element view and pay button and handle the payment result in the completion block. Display appropriate messages to your customer based on whether the payment fails with an error or succeeds.
//Create a card element view and pay button.
lazy var hyperCardTextField: PaymentCardTextField = {
let cardTextField = PaymentCardTextField()
return cardTextField
}()
lazy var payButton: UIButton = {
let button = UIButton(type: .custom)
button.layer.cornerRadius = 5
button.backgroundColor = .systemBlue
button.setTitle("Pay", for: .normal)
button.addTarget(self, action: #selector(pay), for: .touchUpInside)
return button
}()
@objc
func pay() {
guard let paymentIntentClientSecret = model.paymentIntentClientSecret else {
return
}
let paymentIntentParams = PaymentIntentParams(clientSecret: paymentIntentClientSecret)
let paymentHandler = PaymentHandler.shared()
paymentHandler.confirmPayment(paymentIntentParams, with: self) { (status, paymentIntent, error) in
switch status {
case .failed:
// Handle failed payment
break
case .canceled:
// Handle canceled payment
break
case .succeeded:
// Handle successful payment
break
@unknown default:
fatalError()
}
}
}
Congratulations!
Now that you have integrated the iOS SDK, you can customize the payment sheet to blend with the rest of your app.