@page "/Checkout"
@using Stripe.Checkout
@using BrioCharge.Data
@using Microsoft.Extensions.Configuration;
@inject NavigationManager NavigationManager
@inject IJSRuntime jsRuntime
<p>
Assessed cost for data processing is @Fields.priceFmt
</p>
<button class="btn btn-primary mx-3" @onclick="@(e => CheckoutHandler(Fields.prcid))">Accept Charge</button>
@code
{
// [Parameter] // I no longer need to pass the price as a parameter
//public string PrcCode { get; set; }
public static string PrcCode = Fields.helper; // contains Stripe client id
private string str;
private static string price;
public string msg;
public Fields localField = new Fields(); // Don't need this
public Checkout()
{
PrcCode = Fields.prcid;
}
public IConfiguration Configuration { get; }
public Checkout(IConfiguration configuration)
{
Configuration = configuration;
var auxstr = Configuration.GetSection("ConfigMessage");
msg = auxstr.ToString();
// public string from = (string) auxstr;
}
private async Task CheckoutHandler(string prcid)
{
string sessionId = await CreateCheckoutSessionAsync(prcid); // PrcCode); // Fields.prcid);
await jsRuntime.InvokeVoidAsync("redirectToCheckout", sessionId);
}
public async Task<string> CreateCheckoutSessionAsync(string PrcCode, string customerId = null) //PrcCode, string customerId = null)
{
var options = new SessionCreateOptions
{
Customer = customerId,
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
Price = PrcCode,
Quantity = 1
},
},
Mode = "payment",
SuccessUrl = $"{NavigationManager.BaseUri}success?session_id={{CHECKOUT_SESSION_ID}}",
CancelUrl = $"{NavigationManager.BaseUri}checkout",
//SuccessUrl = $"https://localhost:44367/success?session_id={{CHECKOUT_SESSION_ID}}",
//CancelUrl = $"https://localhost:5001/checkout",
};
return (await new SessionService().CreateAsync(options)).Id;
}
protected override void OnInitialized()
{
// PrcCode = localField.helper;
}
}