Mobile SDKs

iOS SDK

27min

Moyasar iOS SDK

Overview

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.

Installing CocoaPods

Before you can add the library to your project, you need to install CocoaPods on your macOS using the following command:

Shell


Or as a Ruby gem

Shell


Add the Dependency

If you haven't already added CocoaPods to your project, initialize it with:

Shell


Now add the following pod to your Podfile:

Ruby


Make sure to add use_frameworks!

Configuring a Payment Request

We need to prepare a PaymentRequest object:

Swift


Don't forget to import MoyasarSdk.

Apple Pay Payments

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:

Swift


Don't forget to import PassKit.

An error will be printed if the API key format is incorrect.

SwiftUI Credit Card Payments

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:

Swift


UIKit Credit Card Payments

If you are using UIKit you will need to create a wrapper to host the SwiftUI CreditCardView view:

Swift


Don't forget to import SwiftUI.



iOS SDK Dark Arabic
iOS SDK Dark Arabic

iOS SDK Light English
iOS SDK Light English


Handling Credit Card Payment Result

Now, we can handle the Credit Card payment result as follows:

Swift


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.

Handling Completed Payment Result

The payment status could be paid, failed or other statuses, we need to handle this:

Swift


'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

Customizing Credit Card View

Use the create method in the PaymentService class like this:

Swift


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:

Swift


You can find payment statuses here: Payment Status Reference

Objective-C Integration

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

Testing

Credit Cards

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

Apple Pay

Testing using a simulator will not work! Learn more about Apple Pay testing here.

Migration Guide

From 0.4 to 1.0

This upgrade changes the following:

Setting the API key

Text


Handling Payment Statuses

Text

  • Also, now you can handle the errors based on MoyasarError enum.

APIs Documentation

Demo Examples



Updated 03 Sep 2024
Did this page help you?