iOS SDK


Overview

The iOS SDK is a small framework that allows you to quickly and safely integrate Moyasar payments within your SwiftUI or UIKit apps.

On this page


Installing CocoaPods

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

brew install cocoapods

Or as a Ruby gem

gem install cocoapods

Add the Dependency

If you haven’t alrady added CocoaPods to you project, initialize it with:

pod init

Now add the following pod to your Podfile:

pod 'MoyasarSdk', git: 'https://github.com/moyasar/moyasar-ios-pod.git'

Note: make sure to add use_frameworks!.

Configurations

Before using the library, make sure to set your API key:

import MoyasarSdk

try! Moyasar.setApiKey("pk_live_1234567")

Note: an error will be raised when the key format is incorrect.

Credit Card Payments

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:

let paymentRequest = PaymentRequest(
    amount: 1000,
    currency: "SAR",
    description: "Flat White",
    metadata: ["order_id": "ios_order_3214124"]
)

Then we can add it to our view as follows:

struct ContentView: View {
    func handlePaymentResult(result: PaymentResult) {
        // ...
    }
    
    func handlePaymentResult(_ payment: ApiPayment) {
        // ...
    }
    
    func handlePaymentError(_ error: Error) {
        // Handle all MoyasarError enum cases
    }

    var body: some View {
        CreditCardView(request: paymentRequest, callback: handlePaymentResult)
    }
}

English credit card view in a light theme Arabic credit card view in a dark theme

Handling Result

Now, we can handle the result as follows:

func handlePaymentResult(result: PaymentResult) {
    switch (result) {
    case .completed(let payment):
        handlePaymentResult(payment)
        break
    case .failed(let error):
        handlePaymentError(error)
        break
    case .canceled:
        // Handle Cancel Result
        break
    @unknown default:
        fatalError()
    }
}

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

func handlePaymentResult(_ payment: ApiPayment) {
    switch payment.status {
    case "paid":
        // Handle Paid!
        break
    default:
        // Handle other status like failed
    }
}

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 need to pass the token to ApplePayService found within PKPayment object. Here is an example:

let payment: PKPayment = // Payment object we got in the didAuthorizePayment event

let service = ApplePayService()
service.authorizePayment(request: request, token: payment.token) {result in
    // Handle result as in (Handling Result)
}

Last Modified : Nov 2021