Fixed payment namespacing

This commit is contained in:
2025-06-20 17:42:49 -07:00
parent 3d4c9d8cd9
commit 91e58fa267
4 changed files with 24 additions and 104 deletions
@@ -1,12 +1,7 @@
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using MistoxWebsite.Server.Controllers.Payment;
using MistoxWebsite.Server.Services.DatabaseService;
using MistoxWebsite.Shared;
using Newtonsoft.Json;
using Stripe;
using Stripe.Climate;
using Stripe.Tax;
using MistoxWebsite.Server.Entities;
namespace MistoxWebsite.Server.Controllers {
[ApiController]
@@ -24,7 +19,7 @@ namespace MistoxWebsite.Server.Controllers {
public async Task<string> GetPaymentKey( [FromQuery] string userID ) {
string OrderNumber = Guid.NewGuid().ToString().Substring(0,10);
Shared.Account? acc = await _databaseService.GetAccount(userID);
Account? acc = await _databaseService.GetAccount(userID);
if (acc != null) {
List<Cart> cart = await _databaseService.GetCart(acc);
@@ -51,11 +46,11 @@ namespace MistoxWebsite.Server.Controllers {
try {
const string endpointSecret = "whsec_HCO7uv2BPIPmUPOiSg9tfwLZul8usCGG";
string body = await new StreamReader(Request.Body).ReadToEndAsync();
Event e = EventUtility.ConstructEvent( body, Request.Headers["Stripe-Signature"], endpointSecret );
Stripe.Event e = Stripe.EventUtility.ConstructEvent( body, Request.Headers["Stripe-Signature"], endpointSecret );
if( e.Type == "payment_intent.succeeded" ) {
// Extract Data from payment confirm
PaymentIntent intent = (PaymentIntent)e.Data.Object;
Stripe.PaymentIntent intent = (Stripe.PaymentIntent)e.Data.Object;
string orderNumber = "";
int userID = 0;
List<int> productIDs = new List<int>();
@@ -84,7 +79,7 @@ namespace MistoxWebsite.Server.Controllers {
}
// Clear the cart
Shared.Account account = new Shared.Account{
Account account = new() {
ID = userID
};
await _databaseService.ClearCart( account );
@@ -1,4 +1,4 @@
using MistoxWebsite.Shared;
using MistoxWebsite.Server.Entities;
namespace MistoxWebsite.Server.Controllers.Payment {
@@ -1,11 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MistoxWebsite.Server.Controllers.Payment;
using MistoxWebsite.Server.Services.DatabaseService;
using MistoxWebsite.Shared;
using Stripe;
using Stripe.Tax;
using MistoxWebsite.Server.Entities;
namespace MistoxWebsite.Server.Controllers {
@@ -17,16 +12,16 @@ namespace MistoxWebsite.Server.Controllers {
_databaseService = databaseService;
}
public async Task<(bool, string)> Purchase(string OrderNumber, Shared.Account user, List<Cart> cart) {
public async Task<(bool, string)> Purchase(string OrderNumber, Account user, List<Cart> cart) {
try {
// build Recipt and calculate Tax
var options = new CalculationCreateOptions {
var options = new Stripe.Tax.CalculationCreateOptions {
Currency = "usd",
CustomerDetails = new CalculationCustomerDetailsOptions {
CustomerDetails = new Stripe.Tax.CalculationCustomerDetailsOptions {
AddressSource = "billing",
},
Expand = new List<string>() { "line_items" },
LineItems = new List<CalculationLineItemOptions>()
LineItems = new List<Stripe.Tax.CalculationLineItemOptions>()
};
List<int> prods = new List<int>();
@@ -34,12 +29,12 @@ namespace MistoxWebsite.Server.Controllers {
// Add items to receipt
int subtotal = 0;
foreach (Cart items in cart) {
Shared.Product? product = await _databaseService.GetProduct(items.ProductID);
Product? product = await _databaseService.GetProduct(items.ProductID);
if (product != null) {
prods.Add(product.ID);
if (product != null) {
subtotal += product.Cost;
options.LineItems.Add(new CalculationLineItemOptions {
options.LineItems.Add(new Stripe.Tax.CalculationLineItemOptions {
Amount = product.Cost,
TaxCode = "txcd_10201000", // Tax code for downloadable digital games
Quantity = 1,
@@ -51,8 +46,8 @@ namespace MistoxWebsite.Server.Controllers {
}
var service = new CalculationService();
Calculation result = service.Create(options);
var service = new Stripe.Tax.CalculationService();
Stripe.Tax.Calculation result = service.Create(options);
string csv = "";
foreach (int cur in prods) {
@@ -60,7 +55,7 @@ namespace MistoxWebsite.Server.Controllers {
}
// Crate Payment Intent
PaymentIntentCreateOptions paymentIntent = new PaymentIntentCreateOptions() {
Stripe.PaymentIntentCreateOptions paymentIntent = new Stripe.PaymentIntentCreateOptions() {
Amount = result.AmountTotal,
Currency = "usd",
Metadata = new Dictionary<string, string> {
@@ -73,8 +68,8 @@ namespace MistoxWebsite.Server.Controllers {
StatementDescriptor = "Mistox.Net #" + OrderNumber
};
PaymentIntentService intentService = new PaymentIntentService();
PaymentIntent x = await intentService.CreateAsync(paymentIntent);
Stripe.PaymentIntentService intentService = new Stripe.PaymentIntentService();
Stripe.PaymentIntent x = await intentService.CreateAsync(paymentIntent);
return (true, x.ClientSecret);
} catch(Exception e) {
@@ -1,9 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using MistoxWebsite.Server.Services.DatabaseService;
using MistoxWebsite.Shared;
using Newtonsoft.Json;
using MistoxWebsite.Server.Entities;
using System.Security.Claims;
using System.Text;
namespace MistoxWebsite.Server.Controllers {
[ApiController]
@@ -128,74 +126,6 @@ namespace MistoxWebsite.Server.Controllers {
}
}
DirObj RecursiveBuild( DirObj DirObj, string workingPath, List<ReceiptProduct> purchased ) {
string[] files = Directory.GetFiles(workingPath);
string[] directories = Directory.GetDirectories(workingPath);
List<DirObj> building = new List<DirObj>();
// Get File Names
Parallel.For( 0, files.Length, ( i ) => {
string fileName = files[i].Substring(workingPath.Length, files[i].Length - (workingPath.Length));
building.Add( new DirObj {
Type = FileType.File,
Path = fileName
});
} );
// Get Path Names
Parallel.For( 0, directories.Length, ( i ) => {
foreach( ReceiptProduct cur in purchased ) {
string dirName = directories[i].Substring(workingPath.Length, directories[i].Length - (workingPath.Length));
if( contains( dirName, cur.product.URL ) ) {
DirObj dir = new DirObj {
Type = FileType.Directory,
Path = dirName,
};
building.Add( dir );
RecursiveBuild( dir, directories [i], purchased );
}
}
} );
DirObj.Children = building.ToArray();
return DirObj;
}
string _FolderRoot = "/home/downloads/";
[Route( "api/product/showdownloads" )]
[HttpPost]
public async Task<IActionResult> ShowDownloads() {
try {
if( User.Identity != null && User.Identity.IsAuthenticated ) {
List<Claim> userClaims = User.Claims.ToList();
int UserID = -1;
foreach( Claim claim in userClaims ) {
if( claim.Type == "ID" ) {
UserID = Convert.ToInt32( claim.Value );
break;
}
}
List<ReceiptProduct> purchased = await _databaseService.GetAllReceiptsJoinedToProduct( new Account{ ID = UserID } );
byte[] datapacket = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(RecursiveBuild(new DirObj {
Path = @"\",
Type = FileType.Directory,
}, _FolderRoot, purchased)));
return new FileContentResult( datapacket, "text/html" );
}
return Unauthorized();
} catch {
return NotFound();
}
}
bool contains( string outer, string inner ) {
if ( outer.Length >= inner.Length ) {
for ( int i=0; i<outer.Length-inner.Length; i++ ) {
@@ -221,10 +151,10 @@ namespace MistoxWebsite.Server.Controllers {
if ( contains( Product, product.URL ) ) {
Receipt? receipt = await _databaseService.GetReceipt(user, product);
if( receipt != null ) {
FileStream fileStream = new FileStream(_FolderRoot + Product, FileMode.Open, FileAccess.Read);
return new FileStreamResult( fileStream, "application/octet-stream" ) {
FileDownloadName = fileStream.Name
};
//FileStream fileStream = new FileStream(_FolderRoot + Product, FileMode.Open, FileAccess.Read);
//return new FileStreamResult( fileStream, "application/octet-stream" ) {
// FileDownloadName = fileStream.Name
//};
}
break;
}