Current generation of business apps take advantage of unique external devices which expand mobile system capabilities. Sometimes you want to use less popular solutions which don’t have full support for Xamarin. In those situations you will need to use a third-party libraries and binding projects.
To learn more about binding projects be sure to read through general topic connected with Binding Objective-C Libraries from Advances Topics in Xamarin. Full article is avaiable here: Binding Objective-C Libraries
Today we will show you how to begin with Payleven SDK in Xamarin iOS. Payleven offers you a card payments the way you want. Their devices are contactless and are using Bluetooth technology. So far Payleven doesn’t provide support for Xamarin, so we must use their iOS Framework. Fortunatelly for us we got one of theirs devices and we can test them for you.
Payleven documentation is available here: Payleven mPOS SDK Reference
Just for your convenience we split this article into two parts:
1. Creating sample iOS project & binding library
2. Making your first mobile payment with Payleven Framework
Creating sample iOS project & binding library
Create a new solution and merge two projects:
- PaylevenSample as Xamarin.iOS Empty Project
- PaylevenLib as iOS Binding Project (Unified API)
Step 1.0 – Payleven Sample iOS Project & Solution
Step 1.1 – Payleven new Binding Project
Step 1.2 – Payleven complete solution
After creating new solution with two projects inside add PaylevenLib as reference in PaylevenSample.
Step 1.3 – Adding reference
Prepare Payleven SDK files
Payleven SDK for iOS is avaiable. Save it to your hard drive.
Step 2.0 Github SDK
Then extract files from Framework/directory that are neccesary in our project:
- AdyenToolkit with all header files from AdyenToolkit.framework
- PaylevenSDK with all header files from PaylevenSDK.framework/Versions/A/
Before we will add those files inside our project we need to rename two files:
- AdyenToolkit file to libAdyenToolkit.a
- PaylevenSDK to libPaylevenSDK.a
Files should look like on image below:
Step 2.1 All necessary files
Put Payleven SDK / AdyenToolkit into binding project
Click on PaylevenLib project (main directory) and add existing folder with your files. Then select all files without AdyenToolkit.bundle and OS system data.
Step 3.0 – Add files to project
Step 3.1 – Adding files dialog
AdyenToolkit.bundle should be inside Resources folder, so select Resources folder and then click add existing folder and select correct one. After this your PaylevenLib project should looks like:
Step 3.2 – All files inside project
so check if:
- All headers inside Header folder
- Two files – libAdyenToolkit.a and libPaylevenSDK.a are present
- In your resources folder there is AdyenToolkit.bundle
Set LinkWith attribute for binary files
After you added libAdyenToolkit.a and libPaylevenSDK.a your IDE created two files with extension .linkwith.cs.
Step 4.0 – Link with files
Default file looks like:
using System; using ObjCRuntime; [assembly: LinkWith ("libdefault.a", SmartLink = true, ForceLoad = true)]
Those files contain informations for linker that you need update with link targets, frameworks and flags. They are specified in Payleven documentation:
CoreData.framework CoreLocation.framework ExternalAccessory.framework SystemConfiguration.framework libsqlite3.0.dylib
So, your updated files should looks like:
[assembly: LinkWith ("libAdyenToolkit.a", LinkTarget.Simulator | LinkTarget.Simulator64 | LinkTarget.ArmV7 | LinkTarget.Arm64, SmartLink = true, ForceLoad = true, Frameworks = "CoreFoundation CoreText UIKit Foundation CoreLocation ExternalAccessory SystemConfiguration CoreGraphics", LinkerFlags = "-lz -lsqlite3")]
[assembly: LinkWith ("libPaylevenSDK.a", LinkTarget.Simulator | LinkTarget.Simulator64 | LinkTarget.ArmV7 | LinkTarget.Arm64, SmartLink = true, ForceLoad = true, Frameworks = "CoreFoundation CoreText UIKit Foundation CoreData CoreLocation ExternalAccessory SystemConfiguration", LinkerFlags = "-lz -lsqlite3")]
As you see there are targets that I will use in our project and some frameworks that are required for assemlies to work correctly.
Update ApiDefinition.cs & StructsAndEnums.cs
The last step is to simply setup new definitions of interfaces that are definied in our assemblies.
Step 5.0 – API definitions
This step is most time-consuming step. This is the list of interfaces that we need to declare to make payment in our sample app:
Some of those interfaces require additional enumeration types which we declare in StructsAndEnums file. For example PLVPaymentResult interface contains method State which return one those types like Approved, Declined or Cancelled. What we need is a missing delcaration of enumeration PLVPaymentResultState in our project.
Step 5.1 – Interface declaration
Step 5.2 – Enum declaration
Now check just to be sure if you are able to compile project without any errors and we can move on. You can check out the full source code for the Payleven sample application shown above on my GitHub.