64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using Stripe;
|
|
|
|
namespace VCart.Controllers
|
|
{
|
|
public class PlansController : Controller
|
|
{
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult Create(PaymentIntentCreateRequest request)
|
|
{
|
|
var paymentIntentService = new PaymentIntentService();
|
|
var paymentIntent = paymentIntentService.Create(new PaymentIntentCreateOptions
|
|
{
|
|
Amount = CalculateOrderAmount(request.Items),
|
|
Currency = "usd",
|
|
// In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default.
|
|
AutomaticPaymentMethods = new PaymentIntentAutomaticPaymentMethodsOptions
|
|
{
|
|
Enabled = true,
|
|
},
|
|
});
|
|
|
|
return Json(new
|
|
{
|
|
clientSecret = paymentIntent.ClientSecret
|
|
// [DEV]: For demo purposes only, you should avoid exposing the PaymentIntent ID in the client-side code.
|
|
//dpmCheckerLink = $"https://dashboard.stripe.com/settings/payment_methods/review?transaction_id={paymentIntent.ID}"
|
|
});
|
|
}
|
|
|
|
private long CalculateOrderAmount(Item[] items)
|
|
{
|
|
// Calculate the order total on the server to prevent
|
|
// people from directly manipulating the amount on the client
|
|
long total = 0;
|
|
foreach (Item item in items)
|
|
{
|
|
total += item.Amount;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
public class Item
|
|
{
|
|
[JsonProperty("id")]
|
|
public string Id { get; set; }
|
|
[JsonProperty("Amount")]
|
|
public long Amount { get; set; }
|
|
}
|
|
|
|
public class PaymentIntentCreateRequest
|
|
{
|
|
[JsonProperty("items")]
|
|
public Item[] Items { get; set; }
|
|
}
|
|
}
|
|
}
|