Reputation: 111
"I'm building a website that will offer basic and premium plans, and I'm planning to use Stripe as the payment gateway. I'll be implementing the simplest integration method suggested in the documentation, which is through Checkout.
The documentation recommends listening for the 'Checkout.Session.Completed' event at the WebHook endpoint to obtain the CustomerId and immediately upgrade the user's plan to premium. However, the documentation isn't very clear about the reliability of this event triggering.
My question is:
If I only accept payments via credit card, will this event only be triggered when the payment is successful, immediately after the user enters their card details and clicks 'pay'?"
If I'm correct about the event triggering, I plan to do something similar to this:
[HttpPost("webhook")]
[AllowAnonymous]
public async Task<ActionResult> WebHoook([FromServices] IOptions<StripeKeys> stripeOptions)
{
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
try
{
var stripeEvent = EventUtility.ConstructEvent(json, Request.Headers["Stripe-Signature"], stripeOptions.Value.WebHookSecret);
switch (stripeEvent.Type)
{
case Events.CheckoutSessionCompleted:
{
// Save CustomerId and Update user's subscription to premium in the DB
// Send email to user
break;
}
case Events.InvoicePaid:
{
// Update user's subscription (update expiration date to 32 days ahead)
// Send a receipt to the customer via email.
break;
}
case Events.InvoicePaymentFailed:
{
// Revert user's subscription to the basic plan in the DB.
// Send email to user
break;
}
case Events.InvoiceUpcoming:
{
// Send "upcoming invoice" email to the user.
break;
}
case Events.CustomerSubscriptionDeleted:
{
// Revert user's subscription to the basic plan in the DB.
// Send email to user
break;
}
default:
break;
}
return Ok();
}
catch
{
return BadRequest();
}
}
Upvotes: 0
Views: 1235
Reputation: 2219
If you are using Checkout Session, then yes as soon as the payment is successful, Stripe will send you the checkout.session.completed
webhook event. And this would be a good place to handle all post-payment actions (like upgrading a Subscriptions).
Feel free to test this in test mode to make sure everything works as expected.
And if needed, you can learn more about webhook events in the documentation here.
Upvotes: 1