Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. using System;
  2. using Foundation;
  3. using Soft.ePosPayleven;
  4. using CoreLocation;
  5. using System.Linq;
  6.  
  7.  
  8. namespace Soft
  9. {
  10. public class PaylevenManeger : PLVPaymentTaskDelegate
  11. {
  12. private PLVPayleven _payleven;
  13. private PLVPaymentTask _paymentTask;
  14. private CLLocationManager _locationManager;
  15.  
  16. public PLVDevice[] DeviceList { get; set; }
  17. public PLVDevice Device { get; set; }
  18. public NSString Currency { get; set; }
  19.  
  20. public PaylevenManeger()
  21. {
  22. Currency = new NSString("EUR");
  23. _payleven = new PLVPayleven();
  24. _locationManager = new CLLocationManager();
  25. _locationManager.Delegate = new CLLocationManagerDelegate();
  26.  
  27. CLAuthorizationStatus code = CLLocationManager.Status;
  28.  
  29. _locationManager.RequestAlwaysAuthorization();
  30. _locationManager.StartUpdatingLocation();
  31. }
  32.  
  33. public void Cancel()
  34. {
  35. _paymentTask.Cancel();
  36. }
  37.  
  38. public NSString GenerateRandomString()
  39. {
  40. string alphabet = "abcdefghijklmnopqrstuvwxyz1234567890";
  41. int alphabetLength = alphabet.Length;
  42. int stringLength = 20;
  43. NSMutableString mstring = new NSMutableString(stringLength);
  44. for (nint i = 0; i < stringLength; i++)
  45. {
  46. int randomIndex = new Random().Next(1,alphabetLength);
  47. mstring.Append(new NSString(""+alphabet[randomIndex]));
  48. }
  49.  
  50. return mstring;
  51. }
  52.  
  53. public string Pay(string amount)
  54. {
  55. NSLocale locale = NSLocale.CurrentLocale;
  56. string message;
  57.  
  58. CLLocationCoordinate2D coordinate = _locationManager.Location.Coordinate;
  59. NSDecimalNumber amountDecNum = new NSDecimalNumber(amount,locale);
  60.  
  61.  
  62. if (!coordinate.IsValid())
  63. {
  64. System.Diagnostics.Debug.WriteLine("Coordinate is not valid");
  65. }
  66.  
  67. PLVPaymentRequest request = new PLVPaymentRequest(GenerateRandomString(), amountDecNum, Currency, coordinate);
  68. _paymentTask = _payleven.PaymentTaskWithRequest(request, Device, this);
  69.  
  70. if (_paymentTask == null)
  71. {
  72. message = "Could Not Create Payment, Error creating payment task. Make sure you’re logged in and the payment device is ready.";
  73. }
  74. else
  75. {
  76. _paymentTask.Start();
  77. message ="OK";
  78. }
  79. return message;
  80. }
  81.  
  82. public bool PrepareDevice()
  83. {
  84. DeviceList = (PLVDevice[]) _payleven.Devices;
  85. if(DeviceList == null)
  86. {
  87. return false;
  88. }
  89. Device = DeviceList.First();
  90.  
  91. bool isReady = false;
  92. Device.PrepareWithCompletionHandler((NSError) =>
  93. {
  94. if(Device.Ready)
  95. {
  96. isReady = true;
  97. }
  98. else
  99. {
  100. isReady = false;
  101. }
  102. });
  103.  
  104. return isReady;
  105. }
  106.  
  107. public bool LogIn(string userName, string password, string apiKey)
  108. {
  109. bool login = true;
  110. _payleven.LoginWithUsername(new NSString(userName), new NSString(password), new NSString(apiKey), (error) => {
  111. if(error != null) {
  112. login = false;
  113. }
  114. });
  115.  
  116. System.Diagnostics.Debug.WriteLine("Login State: " + _payleven.LoginState);
  117. if(_payleven.LoginState == PLVPaylevenLoginState.ingIn)
  118. {
  119. PrepareDevice();
  120. }
  121. return login;
  122. }
  123.  
  124. public bool logOut()
  125. {
  126. bool logout = true;
  127. _payleven.LogoutWithCompletionHandler((error) => {
  128. if (error != null) {
  129. logout = false;
  130. }
  131. });
  132. System.Diagnostics.Debug.WriteLine("Login State: " + _payleven.LoginState);
  133. return logout;
  134. }
  135.  
  136.  
  137. public override void PaymentTask (PLVPaymentTask paymentTask, Action<bool, CoreGraphics.CGImage> completionHandler)
  138. {
  139. }
  140.  
  141. public override void PaymentTaskDidFinish (PLVPaymentTask paymentTask)
  142. {
  143. System.Diagnostics.Debug.WriteLine("Payment complete");
  144. }
  145.  
  146. public override void PaymentTask (PLVPaymentTask paymentTask, NSError error)
  147. {
  148. System.Diagnostics.Debug.WriteLine("Payment Error, code: " + error.Code);
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement