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
brew install cocoapods
Or as a Ruby gem
Shell
gem install cocoapods
Add the Dependency
If you haven't already added CocoaPods to your project, initialize it with:
Shell
pod init
Now add the following pod to your Podfile:
Ruby
pod 'MoyasarSdk',git:'https://github.com/moyasar/moyasar-ios-pod.git'
Make sure to add use_frameworks!
Configurations
Before using the library, make sure to set your API key:
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
let payment:PKPayment=// Payment object we got in the didAuthorizePayment eventlet service =ApplePayService()// From MoyasarSdk
service.authorizePayment(request: request, token: payment.token){result inswitch(result){case.success(let payment):handleCompletedPaymentResult(payment)break
case.error(let error):handlePaymentError(error)break@unknowndefault:// Handle any future casesbreak}}funchandleCompletedPaymentResult(_ payment:ApiPayment){// ...}funchandlePaymentError(_ error:Error){// Handle all MoyasarError enum cases}
Don't forget to import PassKit and MoyasarSdk.
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:
Swift
let paymentRequest =PaymentRequest(
amount:1000,
currency:"SAR",
description:"Flat White",
metadata:["order_id":"ios_order_3214124"],
manual:false,
saveCard:false)
SwiftUI Credit Card Payments
We can add the CreditCardView to our view as follows:
funchandlePaymentResult(result:PaymentResult){switch(result){case.completed(let payment):handleCompletedPaymentResult(payment)break
case.failed(let error):handlePaymentError(error)break
case.canceled:// Handle cancel Resultbreak@unknowndefault:// Handle any future casesbreak}}funchandleCompletedPaymentResult(_ payment:ApiPayment){// ...}funchandlePaymentError(_ error:Error){// Handle all MoyasarError enum cases}
'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:
Swift
funchandleCompletedPaymentResult(_ payment:ApiPayment){switch payment.status {case"paid":// Handle paid!break
default:// Handle other status like failed}}
Objective-C Integration
Setup a Swift file for handling payments as described in: