Source: HostForLIFE Blog

HostForLIFE Blog European ASP.NET Core 9.0 Hosting - HostForLIFE :: Processing Invoices and Payments with Stripe in.NET

Stripe is one of the online and in-person payment processing systems. Its not open source. In below sample, we are using test version. We can do operations like create a customer, charge, invoices, refund, webook, etc. In the below sample, I have used the Stripe.net package for Stripe payment processing in dotnet and Stripe REST API's.The Card Tokenization with Angular 15 and various payment-related functionalities using tokens generated by Card tokenization will be covered in the upcoming article.In this article, we will look at an operation. Stripe Account Setup Creation of Customer Payment method and payment intent Invoices and invoice line items Charge Refund Stripe Account SetupTo begin using Stripe for payment integration, the first step is to create a Stripe account. Step 1. Navigate to Stripe register. Step 2. Fill out the required fields, such as email, full name, and password, country. Step 3. After creating your account, Stripe will ask you for some business details (or personal details if you're using it as an individual) to complete the setup. Stripe operates in both test mode and live mode. In test mode, you can simulate transactions without actually charging cards. Once everything is working as expected, you can switch to live mode to begin processing real payments.Note. Stripe uses test mode by default. You can toggle to live mode from the dashboard once you're ready.CustomerInstall a stripe.net package from nuget manager to our project. Create CustomerThe customer is the main object which will hold multiple payment methods, invoices, and billing info. we can create customer for individual or business organization.Code Sample// Configures the Stripe SDK with the secret key for API communicationprivate void ConfigureStripe(){    // Retrieve the secret key from the configuration settings    string? stripeSecretKey = _configuration.GetValue<string>("SecretKey");    // Check if the secret key is null or empty; throw an exception if validation fails    if (string.IsNullOrEmpty(stripeSecretKey))    {        throw new Exception("Stripe secret key is null or empty");    }    // Set the Stripe secret key to the SDK's global configuration    StripeConfiguration.ApiKey = stripeSecretKey;}// Creates a new customer in the Stripe system using provided customer detailspublic Customer CreateCustomer(StripeCustomerRequest stripeCustomerRequest){    try    {        // Ensure Stripe is properly configured with the secret key        ConfigureStripe();        // Define the options for creating a Stripe customer        var options = new CustomerCreateOptions        {            Email = stripeCustomerRequest.Email,        // Customer's email address            Name = stripeCustomerRequest.Name,          // Customer's full name            Phone = stripeCustomerRequest.Phone,        // Customer's phone number            Description = stripeCustomerRequest.Description // Description for the customer (e.g., reason for account creation)        };        // Instantiate the CustomerService to interact with Stripe's Customer API        var service = new CustomerService();        // Create and return the new customer by making an API call to Stripe        return service.Create(options);    }    catch (StripeException ex)    {        // Log the error message for debugging purposes        Console.WriteLine($"Stripe Exception: {ex.StripeError.Message}");        // Throw a new exception with a custom message and the original exception as the inner exception        throw new Exception("Failed to create customer", ex);    }}Create customers with tokenizationCreates a new customer in the Stripe system, associating a payment source; basically, it will create a customer after getting card info from users.Code Sample// Creates a new customer in the Stripe system, associating a payment source (e.g., tokenized card)public Customer CreateCustomerWithToken(StripeCustomerRequest stripeCustomerRequest){    try    {        // Ensure Stripe is properly configured with the secret key        ConfigureStripe();        // Define the options for creating a Stripe customer with a payment source        var options = new CustomerCreateOptions        {            Email = stripeCustomerRequest.Email,                 // Customer's email address            Name = stripeCustomerRequest.Name,                   // Customer's full name            Phone = stripeCustomerRequest.Phone,                 // Customer's phone number            Description = stripeCustomerRequest.Description,     // Description for the customer            Source = stripeCustomerRequest.SourceID              // Token representing a payment source (e.g., card details)        };        // Instantiate the CustomerService to interact with Stripe's Customer API        var service = new CustomerService();        // Create and return the new customer by making an API call to Stripe        return service.Create(options);    }    catch (StripeException ex)    {        // Log the error message for debugging purposes        Console.WriteLine($"Stripe Exception: {ex.StripeError.Message}");        // Throw a new exception with a custom message and the original exception as the inner exception        throw new Exception("Failed to create customer with token", ex);    }}Update CustomerWe can update existing customers using customer ID. We can update the info like email, phone, description, source ID, etc.Code Sample// Updates an existing customer in the Stripe system with the provided customer details.public Customer UpdateCustomer(StripeCustomerRequest stripeCustomerRequest){    try    {        // Ensure Stripe is properly configured with the secret key        ConfigureStripe();        // Create an instance of CustomerUpdateOptions to define the fields to update        var options = new CustomerUpdateOptions        {            Email = stripeCustomerRequest.Email,        // Update customer's email address            Name = stripeCustomerRequest.Name,          // Update customer's full name            Phone = stripeCustomerRequest.Phone,        // Update customer's phone number            Description = stripeCustomerRequest.Description // Update description (e.g., reason for the update)        };        // If SourceID is provided, add it to the options to update the payment method (e.g., a new token)        if (!string.IsNullOrEmpty(stripeCustomerRequest.SourceID))        {            options.Source = stripeCustomerRequest.SourceID; // Update the payment source for the customer        }        // Instantiate the CustomerService to interact with Stripe's Customer API        var service = new CustomerService();        // Update the customer by calling the Update method with the customer ID and the update options        return service.Update(stripeCustomerRequest.CustomerID, options

Read full article »
Est. Annual Revenue
$25-100M
Est. Employees
250-500
CEO Avatar

CEO

Anthony Johnson

CEO Approval Rating

70/100