xxxxxxxxxx
private readonly string WebhookSecret = "whsec_OurSigningSecret";
//Previous actions
[HttpPost]
public IActionResult ChargeChange()
{
var json = new StreamReader(HttpContext.Request.Body).ReadToEnd();
try
{
var stripeEvent = EventUtility.ConstructEvent(json,
Request.Headers["Stripe-Signature"], WebhookSecret, throwOnApiVersionMismatch: true);
Charge charge = (Charge)stripeEvent.Data.Object;
switch (charge.Status)
{
case "succeeded":
//This is an example of what to do after a charge is successful
charge.Metadata.TryGetValue("Product", out string Product);
charge.Metadata.TryGetValue("Quantity", out string Quantity);
Database.ReduceStock(Product, Quantity);
break;
case "failed":
//Code to execute on a failed charge
break;
}
}
catch (Exception e)
{
e.Ship(HttpContext);
return BadRequest();
}
return Ok();
}
xxxxxxxxxx
[HttpPost]
public ActionResult CreateCheckoutSession(string amount)
{
var options = new Stripe.Checkout.SessionCreateOptions
{
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
PriceData = new SessionLineItemPriceDataOptions
{
UnitAmount =Convert.ToInt32(amount)*100,
Currency = "inr",
ProductData = new SessionLineItemPriceDataProductDataOptions
{
Name = "T-shirt",
},
},
Quantity = 1,
},
},
Mode = "payment",
SuccessUrl = "https://localhost:44346/Home/success",
CancelUrl = "https://localhost:44346/Home/cancel",
};
var service = new Stripe.Checkout.SessionService();
Stripe.Checkout.Session session = service.Create(options);
Response.Headers.Add("Location", session.Url);
return new HttpStatusCodeResult(303);
}