using Controllers.DataBase; namespace Controllers.Payment { public class PaymentTestor : IPayment { static List ImpodentKeys = new List(); DbDriver? _DBdriver = null; // Inject the DB driver public PaymentTestor() { _DBdriver = new DbDriver(); } public (bool, string) CreatePayment(string user) { string guid = Guid.NewGuid().ToString(); ImpodentKeys.Add(new ImpodentKey{ Key = guid, User = user }); return (true, guid); } public (bool, string) TryPayment(string ImpodentKey, float Price) { try { // Find the impotency key ImpodentKey? found = null; foreach( ImpodentKey key in ImpodentKeys) { if (key.Key == ImpodentKey) { found = key; break; } } if (found == null) { return (false, "Payment session closed or never existed"); } // Make sure the database is injected correctly if (_DBdriver == null) { found.Processed = true; return (false, "The Database cannot be loaded"); } // Get the users money string dbPrefix = $"[{found.User.ToLower()}]:"; bool passed = float.TryParse(_DBdriver.Get( dbPrefix + "money" ), out float result); if (!passed) { found.Processed = true; return (false, "Unable to parse money from the database"); } // Make sure the user has enough money if (result < Price) { found.Processed = true; return (false, "You dont have enough money to purchase this item"); } // Remove the money and save // _DBdriver.Set( dbPrefix + "money", (result - Price).ToString()); -> Dont save in here as its done externally for thread safty found.PaymentSuccess = true; found.Processed = true; return (true, ""); }catch(Exception e) { return (false, e.ToString()); } } public (bool, string) TrySell(string ImpodentKey, float Price) { // Find the impotency key ImpodentKey? found = null; foreach( ImpodentKey key in ImpodentKeys) { if (key.Key == ImpodentKey) { found = key; break; } } if (found == null) { return (false, "Payment session closed or never existed"); } // Make sure the database is injected correctly if (_DBdriver == null) { found.Processed = true; return (false, "The Database cannot be loaded"); } // Get the users money string dbPrefix = $"[{found.User.ToLower()}]:"; bool passed = float.TryParse(_DBdriver.Get( dbPrefix + "money" ), out float result); if (!passed) { found.Processed = true; return (false, "Unable to parse money from the database"); } // Add the money and save // _DBdriver.Set( dbPrefix + "money", (result + Price).ToString()); -> Dont save in here as its done externally for thread safty found.PaymentSuccess = true; found.Processed = true; return (true, ""); } } class ImpodentKey { public string Key { get; set; } = ""; public string User { get; set; } = ""; public bool PaymentSuccess { get; set; } = false; public bool Processed { get; set; } = false; } }