iOS SDK
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!
We need to prepare a PaymentRequest object:
Don't forget to import MoyasarSdk.
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.
An error will be printed if the API key format is incorrect.
The SDK provides a SwiftUI view called CreditCardView that allows you to easily create a credit card form.
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 Credit Card payment result as follows:
If the payment failed during the 3DS authentication process, the PaymentResult will be .failed with the MoyasarError enum case beggining with webview.... You should fetch the payment as per this documentation and check it's status as it might be paid.
Make sure to dismiss the webview screen after getting the result.
The payment status could be paid, failed or other statuses, we need to handle this:
'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 to make sure that the payment is successful.
You can find payment statuses here: Payment Status Reference
Use the create method in the PaymentService class like this:
Make sure to add the 'sdk' field with the value of 'ios' in ApiPaymentRequest metadata dictionary field. (Only when creating a custom UI)
Now when the payment is initiated successfully you need to initialize the 3DS web view as follows:
You can view the full example here: https://github.com/moyasar/moyasar-ios-sdk/tree/master/SwiftUiDemo/SwiftUiDemo/Custom%20View
You can find payment statuses here: Payment Status Reference
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
Moyasar provides a sandbox environment for testing credit card payments without charging any real money. This allows you to test your integration and ensure that everything is working correctly before going live with actual payments. Learn more about our testing cards here
Testing using a simulator will not work! Learn more about Apple Pay testing here.
This upgrade changes the following:
- Also, now you can handle the errors based on MoyasarError enum.