This guide will walk you through a straightforward solution to accept payments within your iOS application. The iOS SDK is a small framework built with SwiftUI that allows you to quickly and safely integrate Moyasar payments within your SwiftUI or UIKit apps.
Before you can add the library to your project, you need to install CocoaPods on your macOS using the following command:
Or as a Ruby gem
If you haven't already added CocoaPods to your project, initialize it with:
Now add the following pod to your Podfile:
Make sure to add use_frameworks!
Before using the library, make sure to set your API key:
An error will be raised when the key format is incorrect.
You can follow Offering Apple Pay in Your App to implement Apple Pay within your app.
When the user authorizes the payment using Face ID or Touch ID on their iOS device, the didAuthorizePayment event will be dispatched. In this step, you must pass the token to ApplePayService found within the PKPayment object. Here is an example:
Don't forget to import PassKit and MoyasarSdk.
The SDK provides a SwiftUI view called CreditCardView that allows you to easily create a credit card form, here is an example. But first, we need to prepare a PaymentRequest object:
We can add the CreditCardView to our view as follows:
If you are using UIKit you will need to create a wrapper to host the SwiftUI CreditCardView view:
Don't forget to import SwiftUI.
Now, we can handle the result as follows:
'Completed' payment doesn't necessarily mean that the payment is successful. It means that the payment process has been completed successfully. You need to check the payment status as follows to make sure that the payment is successful.
Make sure to handle the screen navigation after getting the result.
The payment status could be paid or failed, we need to handle this:
Setup a Swift file for handling payments as described in:
After that, you can initialize the Swift payments class when processing payments.
Learn more about integrating Swift files in Objective-C apps: https://developer.apple.com/documentation/swift/importing-swift-into-objective-c