Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Foundation;
- using Soft.ePosPayleven;
- using CoreLocation;
- using System.Linq;
- namespace Soft
- {
- public class PaylevenManeger : PLVPaymentTaskDelegate
- {
- private PLVPayleven _payleven;
- private PLVPaymentTask _paymentTask;
- private CLLocationManager _locationManager;
- public PLVDevice[] DeviceList { get; set; }
- public PLVDevice Device { get; set; }
- public NSString Currency { get; set; }
- public PaylevenManeger()
- {
- Currency = new NSString("EUR");
- _payleven = new PLVPayleven();
- _locationManager = new CLLocationManager();
- _locationManager.Delegate = new CLLocationManagerDelegate();
- CLAuthorizationStatus code = CLLocationManager.Status;
- _locationManager.RequestAlwaysAuthorization();
- _locationManager.StartUpdatingLocation();
- }
- public void Cancel()
- {
- _paymentTask.Cancel();
- }
- public NSString GenerateRandomString()
- {
- string alphabet = "abcdefghijklmnopqrstuvwxyz1234567890";
- int alphabetLength = alphabet.Length;
- int stringLength = 20;
- NSMutableString mstring = new NSMutableString(stringLength);
- for (nint i = 0; i < stringLength; i++)
- {
- int randomIndex = new Random().Next(1,alphabetLength);
- mstring.Append(new NSString(""+alphabet[randomIndex]));
- }
- return mstring;
- }
- public string Pay(string amount)
- {
- NSLocale locale = NSLocale.CurrentLocale;
- string message;
- CLLocationCoordinate2D coordinate = _locationManager.Location.Coordinate;
- NSDecimalNumber amountDecNum = new NSDecimalNumber(amount,locale);
- if (!coordinate.IsValid())
- {
- System.Diagnostics.Debug.WriteLine("Coordinate is not valid");
- }
- PLVPaymentRequest request = new PLVPaymentRequest(GenerateRandomString(), amountDecNum, Currency, coordinate);
- _paymentTask = _payleven.PaymentTaskWithRequest(request, Device, this);
- if (_paymentTask == null)
- {
- message = "Could Not Create Payment, Error creating payment task. Make sure you’re logged in and the payment device is ready.";
- }
- else
- {
- _paymentTask.Start();
- message ="OK";
- }
- return message;
- }
- public bool PrepareDevice()
- {
- DeviceList = (PLVDevice[]) _payleven.Devices;
- if(DeviceList == null)
- {
- return false;
- }
- Device = DeviceList.First();
- bool isReady = false;
- Device.PrepareWithCompletionHandler((NSError) =>
- {
- if(Device.Ready)
- {
- isReady = true;
- }
- else
- {
- isReady = false;
- }
- });
- return isReady;
- }
- public bool LogIn(string userName, string password, string apiKey)
- {
- bool login = true;
- _payleven.LoginWithUsername(new NSString(userName), new NSString(password), new NSString(apiKey), (error) => {
- if(error != null) {
- login = false;
- }
- });
- System.Diagnostics.Debug.WriteLine("Login State: " + _payleven.LoginState);
- if(_payleven.LoginState == PLVPaylevenLoginState.ingIn)
- {
- PrepareDevice();
- }
- return login;
- }
- public bool logOut()
- {
- bool logout = true;
- _payleven.LogoutWithCompletionHandler((error) => {
- if (error != null) {
- logout = false;
- }
- });
- System.Diagnostics.Debug.WriteLine("Login State: " + _payleven.LoginState);
- return logout;
- }
- public override void PaymentTask (PLVPaymentTask paymentTask, Action<bool, CoreGraphics.CGImage> completionHandler)
- {
- }
- public override void PaymentTaskDidFinish (PLVPaymentTask paymentTask)
- {
- System.Diagnostics.Debug.WriteLine("Payment complete");
- }
- public override void PaymentTask (PLVPaymentTask paymentTask, NSError error)
- {
- System.Diagnostics.Debug.WriteLine("Payment Error, code: " + error.Code);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement