init commit over here
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MistoxWebsite.Server.Services.DatabaseService;
|
||||
using MistoxWebsite.Shared;
|
||||
using Newtonsoft.Json;
|
||||
using Stripe;
|
||||
using Stripe.Climate;
|
||||
using Stripe.Tax;
|
||||
|
||||
namespace MistoxWebsite.Server.Controllers {
|
||||
[ApiController]
|
||||
public class PaymentController : ControllerBase {
|
||||
|
||||
DatabaseService _databaseService;
|
||||
|
||||
public PaymentController( DatabaseService databaseService ) {
|
||||
_databaseService = databaseService;
|
||||
}
|
||||
|
||||
// Charges
|
||||
[Route( "api/getCheckoutToken" )]
|
||||
[HttpPost]
|
||||
public async Task<string> GetPaymentKey( [FromQuery] string userID ) {
|
||||
|
||||
// Stored Variables
|
||||
string OrderNumber = Guid.NewGuid().ToString().Substring(0,10);
|
||||
int subtotal = 0;
|
||||
|
||||
// Get the user purchasing the items
|
||||
string UserID = "";
|
||||
Shared.Account? acc = await _databaseService.GetAccount(userID);
|
||||
if( acc != null ) {
|
||||
UserID = acc.ID.ToString();
|
||||
List<Cart> cart = await _databaseService.GetCart( acc );
|
||||
|
||||
// build Recipt and calculate Tax
|
||||
var options = new CalculationCreateOptions{
|
||||
Currency = "usd",
|
||||
CustomerDetails = new CalculationCustomerDetailsOptions{
|
||||
AddressSource = "billing",
|
||||
},
|
||||
Expand = new List<string>(){ "line_items" },
|
||||
LineItems = new List<CalculationLineItemOptions>()
|
||||
};
|
||||
|
||||
List<int> prods = new List<int>();
|
||||
|
||||
// Add items to receipt
|
||||
foreach( Cart items in cart ) {
|
||||
Shared.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 {
|
||||
Amount = product.Cost,
|
||||
TaxCode = "txcd_10201000", // Tax code for downloadable digital games
|
||||
Quantity = 1,
|
||||
Reference = product.Name,
|
||||
TaxBehavior = "exclusive"
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var service = new CalculationService();
|
||||
Calculation result = service.Create( options );
|
||||
|
||||
string csv = "";
|
||||
foreach (int cur in prods ) {
|
||||
csv = csv + cur + ",";
|
||||
}
|
||||
|
||||
// Crate Payment Intent
|
||||
PaymentIntentCreateOptions paymentIntent = new PaymentIntentCreateOptions(){
|
||||
Amount = result.AmountTotal,
|
||||
Currency = "usd",
|
||||
Metadata = new Dictionary<string, string> {
|
||||
{ "ordernumber", OrderNumber },
|
||||
{ "user", UserID },
|
||||
{ "products", csv },
|
||||
{ "subtotal", subtotal.ToString() },
|
||||
{ "total", result.AmountTotal.ToString() }
|
||||
},
|
||||
StatementDescriptor = "Mistox.Net #" + OrderNumber
|
||||
};
|
||||
|
||||
PaymentIntentService intentService = new PaymentIntentService();
|
||||
PaymentIntent x = await intentService.CreateAsync( paymentIntent );
|
||||
return x.ClientSecret;
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
|
||||
[Route( "/api/payment/response" )]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> paymentWebhook() {
|
||||
try {
|
||||
const string endpointSecret = "whsec_HCO7uv2BPIPmUPOiSg9tfwLZul8usCGG";
|
||||
string body = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||
Event e = 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;
|
||||
string orderNumber = "";
|
||||
int userID = 0;
|
||||
List<int> productIDs = new List<int>();
|
||||
int subtotal = 0;
|
||||
int total = 0;
|
||||
|
||||
KeyValuePair<string, string>[] y = intent.Metadata.ToArray();
|
||||
foreach( KeyValuePair<string, string> cur in y ) {
|
||||
string val = cur.Key;
|
||||
if( val == "ordernumber" ) {
|
||||
orderNumber = cur.Value;
|
||||
} else if( val == "user" ) {
|
||||
userID = int.Parse( cur.Value );
|
||||
} else if( val == "products" ) {
|
||||
string[] products = cur.Value.Split(',');
|
||||
foreach( string product in products ) {
|
||||
if ( !string.IsNullOrEmpty(product) ) {
|
||||
productIDs.Add( Convert.ToInt32( product ) );
|
||||
}
|
||||
}
|
||||
} else if( val == "subtotal" ) {
|
||||
subtotal = int.Parse( cur.Value );
|
||||
} else if( val == "total" ) {
|
||||
total = int.Parse( cur.Value );
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the cart
|
||||
Shared.Account account = new Shared.Account{
|
||||
ID = userID
|
||||
};
|
||||
await _databaseService.ClearCart( account );
|
||||
|
||||
// Add data to misox receipt
|
||||
for( int i = 0; i < productIDs.Count; i++ ) {
|
||||
int product = productIDs[i];
|
||||
await _databaseService.NewReceipt( new Receipt {
|
||||
AccountID = userID,
|
||||
ProductID = product,
|
||||
ReceiptID = orderNumber,
|
||||
Time = DateTime.Now,
|
||||
TaxAmount = total - subtotal,
|
||||
TotalCost = total,
|
||||
LineItem = i
|
||||
} );
|
||||
}
|
||||
} else {
|
||||
Console.WriteLine( "Unhandled event type: {0}", e.Type );
|
||||
}
|
||||
return Ok();
|
||||
} catch( Exception ex ) {
|
||||
return Content(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user