Impliment soft-payments

This commit is contained in:
2026-03-08 19:28:10 -07:00
parent 54ea7c3027
commit 78bdf250ff
7 changed files with 114 additions and 12 deletions
+60 -6
View File
@@ -1,4 +1,5 @@
@page "/" @page "/"
@using Controllers.Payment
@rendermode InteractiveServer @rendermode InteractiveServer
<PageTitle>Home</PageTitle> <PageTitle>Home</PageTitle>
@@ -53,7 +54,10 @@
<button disabled>@trainButtonText</button> <button disabled>@trainButtonText</button>
<button disabled>@predictButtonText</button> <button disabled>@predictButtonText</button>
} }
<span>@PredictError</span> <button @onclick="buyStock">Buy Stock</button>
<input placeholder="Stock Symbol [NVDA]" @bind="buyStockSymbol" />
<input placeholder="1.0" @bind="buyStockQuantity" />
<span>@resultError</span>
</div> </div>
</div> </div>
} }
@@ -103,6 +107,15 @@
new stockPredictionPair(){ Symbol = "FUN" } new stockPredictionPair(){ Symbol = "FUN" }
}; };
string PaymentKey = "";
protected override async Task OnInitializedAsync(){
(bool, string) result = PaymentProcessor.CreatePayment();
if (!result.Item1){
resultError = result.Item2;
}
PaymentKey = result.Item2;
}
async Task LoginSession(){ async Task LoginSession(){
string dbPrefix = $"[{userName.ToLower()}]:"; // Set the DB prefix for the get and set string dbPrefix = $"[{userName.ToLower()}]:"; // Set the DB prefix for the get and set
string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash string passwordhash = dbDriver.Get( dbPrefix + "password" ); // Pull the password hash
@@ -136,11 +149,11 @@
string pullButtonText = "Pull Data"; string pullButtonText = "Pull Data";
string trainButtonText = "Train AI"; string trainButtonText = "Train AI";
string predictButtonText = "Predict AI"; string predictButtonText = "Predict AI";
string PredictError = ""; string resultError = "";
bool Debounce = true; bool Debounce = true;
async Task pull(){ async Task pull(){
PredictError = ""; resultError = "";
if (Debounce){ if (Debounce){
Debounce = false; Debounce = false;
pullButtonText = "Do not refresh the page. The data is pulling."; pullButtonText = "Do not refresh the page. The data is pulling.";
@@ -159,7 +172,7 @@
} }
async Task train(){ async Task train(){
PredictError = ""; resultError = "";
if (Debounce){ if (Debounce){
Debounce = false; Debounce = false;
trainButtonText = "Do not refresh the page. The AI is training."; trainButtonText = "Do not refresh the page. The AI is training.";
@@ -178,7 +191,7 @@
} }
async Task predict(){ async Task predict(){
PredictError = ""; resultError = "";
if (Debounce){ if (Debounce){
Debounce = false; Debounce = false;
predictButtonText = "Do not refresh the page. The AI is predicting"; predictButtonText = "Do not refresh the page. The AI is predicting";
@@ -190,7 +203,7 @@
if (string.IsNullOrEmpty(Result.Item1)){ if (string.IsNullOrEmpty(Result.Item1)){
cur.Movement = Result.Item2; cur.Movement = Result.Item2;
}else{ }else{
PredictError = Result.Item1; resultError = Result.Item1;
} }
Console.WriteLine("Received Signal [" + cur.Symbol + "] : " + cur.Movement); Console.WriteLine("Received Signal [" + cur.Symbol + "] : " + cur.Movement);
}); });
@@ -205,6 +218,47 @@
} }
} }
// Stock Manipulation
string buyStockSymbol = "";
string buyStockQuantity = "";
float StockPrice = 0;
void buyStock(){
if (Session != null){
// Try Parse the quantitiy input
bool success = float.TryParse(buyStockQuantity, out float QuantityResult);
if (!success){
resultError = "Quantity field is not a number";
return;
}
// Try Pay for the stock
(bool, string) result = PaymentProcessor.TryPayment(PaymentKey, QuantityResult * StockPrice);
if (!result.Item1){
resultError = result.Item2;
return;
}
// Add the item
Session.TrackedStocks.Add( new PurchasedStock(){
Symbol = buyStockSymbol.ToLower(),
PurchasePrice = StockPrice,
Quantity = QuantityResult,
PurchaseDate = DateTime.Now
} );
// Reset the Impodent Key
result = PaymentProcessor.CreatePayment();
if (!result.Item1){
resultError = "[Payment suceeded but new payment session failed] : " + result.Item2;
return;
}
PaymentKey = result.Item2;
}
}
// Data Types // Data Types
class stockPredictionPair { class stockPredictionPair {
+4 -2
View File
@@ -1,13 +1,14 @@
@using System.Net.Http @using System.Net.Http
@using System.Net.Http.Json @using System.Net.Http.Json
@using DataBase @using Controllers.Payment
@using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode @using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization @using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop @using Microsoft.JSInterop
@using PythonInterop @using Controllers.PythonInterop
@using Controllers.DataBase
@using WebServer @using WebServer
@using WebServer.Components @using WebServer.Components
@using BCrypt.Net; @using BCrypt.Net;
@@ -15,3 +16,4 @@
@inject DbDriver dbDriver @inject DbDriver dbDriver
@inject AIModule aiModule @inject AIModule aiModule
@inject IPayment PaymentProcessor
+1 -1
View File
@@ -1,6 +1,6 @@
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
namespace DataBase { namespace Controllers.DataBase {
public class DbDriver { public class DbDriver {
@@ -0,0 +1,13 @@
namespace Controllers.Payment {
public interface IPayment {
// [ Success, ErrorMessage | ImpodentKey ]
public (bool, string) CreatePayment();
// [ Success, ErrorMessage ]
public (bool, string) TryPayment(string ImpodentKey, float Price);
}
}
@@ -0,0 +1,27 @@
namespace Controllers.Payment {
public class PaymentTestor : IPayment {
public static List<string> ImpodentKeys = new List<string>();
public (bool, string) CreatePayment() {
string guid = Guid.NewGuid().ToString();
ImpodentKeys.Add(guid);
return (true, guid);
}
public (bool, string) TryPayment(string ImpodentKey, float Price) {
try {
if (ImpodentKeys.Contains(ImpodentKey)) {
ImpodentKeys.Remove(ImpodentKey);
return (true, "");
} else {
return (false, "Payment session closed or never existed");
}
}catch(Exception e) {
return (false, e.ToString());
}
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
using Python.Runtime; using Python.Runtime;
namespace PythonInterop { namespace Controllers.PythonInterop {
public class AIModule { public class AIModule {
+8 -2
View File
@@ -1,6 +1,7 @@
using WebServer.Components; using WebServer.Components;
using PythonInterop; using Controllers.PythonInterop;
using DataBase; using Controllers.DataBase;
using Controllers.Payment;
// Load the module in globally and use correct path for local or docker runners // Load the module in globally and use correct path for local or docker runners
#if DEBUG #if DEBUG
@@ -21,6 +22,11 @@ if (args.Contains("Pull-Stock-Data")) {
// Insert the DB driver for Dependency Injection // Insert the DB driver for Dependency Injection
builder.Services.AddScoped<DbDriver>(); builder.Services.AddScoped<DbDriver>();
// Insert the payment Processor
builder.Services.AddSingleton<IPayment>(
new PaymentTestor() // Of type Payment Testor -> Change this with stripe or square in the future
);
// Insert the python modlue for Dependency Injection // Insert the python modlue for Dependency Injection
builder.Services.AddSingleton(interopModule); builder.Services.AddSingleton(interopModule);