static bool DoesCallExist(Guid guid, webServiceEntities dataContext)
		{
			var dc = dataContext ?? new webServiceEntities();

			var call = from t in dc.paypalwebservicecalls
					   where t.guid == guid
					   select t;

			return call.Count() > 0;
		}
		static void UpdateWebServiceCall(Guid guid, string data, webServiceEntities dataContext)
		{
			try
			{
				var dc = dataContext ?? new webServiceEntities();

				var row = dc.paypalwebservicecalls.Single(g => g.guid == guid);
				row.data = data;
				dc.SaveChanges();
			}
			catch (Exception exception)
			{
				Logger.TraceErr(exception);
				Logger.TraceErr("Transaction data: " + data);
			}
		}
		/// <summary>
		/// Inserts a paypal webservice call row and returns the unique id for the row.
		/// </summary>
		/// <exception cref="PayPalCallAlreadyMade"></exception>
		static void CreateWebServiceCall(Guid guid, webServiceEntities dataContext)
		{
			var dc = dataContext ?? new webServiceEntities();

			if (DoesCallExist(guid, dc))
				throw new PayPalCallAlreadyMade(guid);

			dc.AddTopaypalwebservicecalls(new paypalwebservicecall { guid = guid, created = DateTime.Now });
			dc.SaveChanges();
		}
		static void InsertPayPalTransaction(string data, webServiceEntities dataContext)
		{
			try
			{
				var dc = dataContext ?? new webServiceEntities();
				var datas = data.Split(',');
				var row = new paypaltransaction
							{
								TransactionId = datas[1],
								Amount = datas[2],
								AVSCode = datas[3],
								CVV2Code = datas[4],
								FMFDetails = data
							};
				dc.AddTopaypaltransactions(row);
				dc.SaveChanges();
			}
			catch (Exception exception)
			{
				Logger.TraceErr(exception);
				Logger.TraceErr("Transaction data: " + data);
			}
		}
		public byte[] DoPayment(byte[] paymentInfo)
		{
			byte[] ret;
			var response = MakeErrorString(0, "Error", "Unknown server error.");
			CreditCardInformation creditCardInformation = null;
			webServiceEntities dc = null;

			try
			{
				dc = new webServiceEntities();
				using (var stream = new MemoryStream())
				{
					Crypto.SetKey(AssemblyDex.Pigs);
					Crypto.ResetIV();

					var decryptedPaymentBytes = Crypto.DecryptBytes(paymentInfo);
					stream.Write(decryptedPaymentBytes, 0, decryptedPaymentBytes.Length);
					stream.Seek(0, SeekOrigin.Begin);
					var formatter = new BinaryFormatter();
					creditCardInformation = (CreditCardInformation)formatter.Deserialize(stream);
				}

				if (creditCardInformation.Id == Guid.Empty)
					response = MakeErrorString(1, "Error",
						"Invalid credit card information id.");
				else
				{
					// check if this is a duplicate transaction
					CreateWebServiceCall(creditCardInformation.Id, dc);
					IPAddress address;
					if (IPAddress.TryParse(creditCardInformation.IPAddress, out address))
					{
						if (PayPalUtils.DoPayment(creditCardInformation, address, out response))
							InsertPayPalTransaction(response, dc);
					}
					else
						response = MakeErrorString(2, "Error", "Invalid IP address.");
				}
			}
			catch (Exception exception)
			{
				Logger.TraceErr(exception);
				response = MakeErrorString(3, "Exception", exception.Message);
			}
			finally
			{
				Crypto.SetKey(AssemblyDex.Fly);
				Crypto.ResetIV();
				ret = Crypto.EncryptBytes((Crypto.AsciiToBytes(response)));
				if (creditCardInformation != null)
					UpdateWebServiceCall(creditCardInformation.Id, response, dc);
			}
			return ret;
		}
		public byte[] DeleteInformationKey(byte[] infoKey)
		{
			byte[] ret;
			var data = string.Empty;

			try
			{
				Crypto.SetKey(AssemblyDex.Pigs);
				Crypto.ResetIV();
				var infoKeyBytes = Crypto.DecryptBytes(infoKey);
				var dc = new webServiceEntities();
				var decryptedInfoKey = Crypto.BytesToAscii(infoKeyBytes);
				var dd = (from info in dc.information
						  where info.InfoKey == decryptedInfoKey
						  select info).FirstOrDefault();
				if (dd != null)
				{
					dc.DeleteObject(dd);
					dc.SaveChanges();
				}
				else
				{
					data = "@@@NotFound";
				}
			}
			catch (Exception exception)
			{
				Logger.TraceErr(exception);
				data = "@@@Exception: " + exception.Message;
			}
			finally
			{
				Crypto.SetKey(AssemblyDex.Fly);
				Crypto.ResetIV();
				ret = Crypto.Encrypt(data);
			}
			return ret;
		}
		public byte[] GetInformationKeys(byte[] key)
		{
			byte[] ret;
			var data = string.Empty;

			try
			{
				Crypto.SetKey(AssemblyDex.Pigs);
				Crypto.ResetIV();
				var receivedInformationKey = Crypto.DecryptBytes(key);
				var guid = new Guid(receivedInformationKey);
				if (guid != new Guid(Properties.Settings.Default.InformationKey))
				{
					throw new InvalidOperationException("Invalid key received.");
				}
				var dc = new webServiceEntities();
				var informations = (from info in dc.information
									select info.InfoKey).ToList();
				data = string.Join(Environment.NewLine, informations);
			}
			catch (Exception exception)
			{
				Logger.TraceErr(exception);
				data = "@@@Exception: " + exception.Message;
			}
			finally
			{
				Crypto.SetKey(AssemblyDex.Fly);
				Crypto.ResetIV();
				ret = Crypto.Encrypt(data);
			}
			return ret;
		}