Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database called Billing that I would like populated with transaction details after the recurring charge is processed each month. I know this can be done manually but I was wondering if there is a way to do this automatically as soon as the transaction is processed by Stripe? I have an .NET MVC application in C# that I use for the user to sign up for the recurring billing using the Stripe API. If anyone can point me in the right direction I would really appreciate it.

Please don't think I'm just looking for someone to do it for me. I'm honestly just looking for advice on how I should approach this. Maybe someone wrote an article or posted a video on this?

Thanks,
Steven

What I have tried:

I have successfully setup used the Stripe API to set up recurring billing but I don't know how to have it update the database each month as soon as it processes.
Posted
Updated 22-Aug-20 21:40pm
Comments
Garth J Lancaster 22-Aug-20 23:21pm    
I took a quick look at the Stripe API - I saw 'Webhooks' but I'm not sure that will give you what you want. Could you automagically run a report at say 'midnight' each day and use that information to update your database, I'm not sure whether that's invoice data per customer or ? .. if the report data is available, it would be pretty easy to set up a service app to update your database

1 solution

@garthjlancaster was spot on his suggestion. Stripe provides webhook for very similar usecase/workflows to handle. Details: Receive event notifications with webhooks[^]

Quote:
Listen for events on your Stripe account so your integration can automatically trigger reactions.

Your reaction here once you receive the event is to trigger the database transaction population.

Now, to you project in ASP.NET, you would need to setup the receiver and then process it.
Example snippet:
C#
public class StripeController : Controller
{
    /// From: https://medium.com/c-sharp/stripe-webhooks-handling-in-asp-net-mvc-c-cff9889152b9 
    /// <summary>
    /// Stripe webhook handling
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpPost]
    [Route("webhook")]
    public IActionResult StripeWebhook()
    {
        try
        {
            var json = new StreamReader(HttpContext.Request.Body).ReadToEnd();
            
            // validate webhook called by stripe only
            var stripeEvent = EventUtility.ConstructEvent(json, Request.Headers["Stripe-Signature"], "---REPLACE STRIPE WEBHOOK SECRET---");

            switch (stripeEvent.Type)
            {
                case "customer.created":
                    var customer = stripeEvent.Data.Object as Customer;
                    // do work

                    break;

                case "customer.subscription.created":
                case "customer.subscription.updated":
                case "customer.subscription.deleted":
                case "customer.subscription.trial_will_end":
                    var subscription = stripeEvent.Data.Object as Subscription;
                    // do work

                    break;

                case "invoice.created":
                    var newinvoice = stripeEvent.Data.Object as Invoice;
                    // do work

                    break;

                case "invoice.upcoming":
                case "invoice.payment_succeeded":
                case "invoice.payment_failed":
                    var invoice = stripeEvent.Data.Object as Invoice;
                    // do work

                    break;

                case "coupon.created":
                case "coupon.updated":
                case "coupon.deleted":
                    var coupon = stripeEvent.Data.Object as Coupon;
                    // do work

                    break;

                    // DO SAME FOR OTHER EVENTS
            }

            return Ok();
        }
        catch (StripeException ex)
        {
            //_logger.LogError(ex, $"StripWebhook: {ex.Message}");
            return BadRequest();
        }
        catch (Exception ex)
        {
            //_logger.LogError(ex, $"StripWebhook: {ex.Message}");
            return BadRequest();
        }
    }
}

References of help:
Stripe WebHooks handling in ASP.NET MVC (C#)[^]
ASP.NET WebHooks receivers | Microsoft Docs[^]
Stripe Webhook endpoints[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900