StripeTransaction 1.0.2

dotnet add package StripeTransaction --version 1.0.2
                    
NuGet\Install-Package StripeTransaction -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StripeTransaction" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StripeTransaction" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="StripeTransaction" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StripeTransaction --version 1.0.2
                    
#r "nuget: StripeTransaction, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StripeTransaction@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StripeTransaction&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=StripeTransaction&version=1.0.2
                    
Install as a Cake Tool

StripeTransaction

NuGet Version NuGet Downloads

A .NET library that provides transaction-like behavior for Stripe operations, ensuring atomicity and rollback capabilities for complex Stripe operations.

Features

  • 🔄 Transaction-like behavior for Stripe operations
  • ⚡ Support for multiple operations in a single transaction
  • 🔍 Automatic rollback on failure
  • 📝 Comprehensive logging
  • 🛡️ Type-safe operation handling

Installation

dotnet add package StripeTransaction

Requirements

  • .NET Core 3.1 or later (.NET Core 3.1, .NET 5, .NET 6, .NET 7, .NET 8)
  • Stripe.net 41.0.0 or later

Quick Start

// Initialize Stripe
StripeTransactionConfiguration.Initialize("your_stripe_api_key");

// Create a transaction
using var transaction = new StripeTransactionManager();

// Execute operations within the transaction
var customer = await transaction.ExecuteAsync(async () => {
    var customerService = new CustomerService();
    return await customerService.CreateAsync(new CustomerCreateOptions {
        Email = "customer@example.com"
    });
});

Supported Operations

Customer Operations

// Modern using declaration
using var transaction = new StripeTransaction();

// Create customer
var customer = await transaction.ExecuteAsync(async () =>
{
    var service = new CustomerService();
    return await service.CreateAsync(new CustomerCreateOptions
    {
        Email = "customer@example.com",
        Name = "John Doe"
    });
});

// Update customer
await transaction.ExecuteAsync(async () =>
{
    var service = new CustomerService();
    await service.UpdateAsync(customer.Id, new CustomerUpdateOptions
    {
        Description = "Updated customer"
    });
});

Payment Method Operations

using var transaction = new StripeTransaction();

// Create and attach payment method
var (paymentMethod, customer) = await transaction.ExecuteAsync(async () =>
{
    var paymentMethodService = new PaymentMethodService();
    var paymentMethod = await paymentMethodService.CreateAsync(new PaymentMethodCreateOptions
    {
        Type = "card",
        Card = new PaymentMethodCardOptions
        {
            Number = "4242424242424242",
            ExpMonth = 12,
            ExpYear = 2024,
            Cvc = "123"
        }
    });

    await paymentMethodService.AttachAsync(paymentMethod.Id, new PaymentMethodAttachOptions
    {
        Customer = customer.Id
    });

    return (paymentMethod, customer);
});

Subscription Operations

using var transaction = new StripeTransaction();

// Create subscription
var subscription = await transaction.ExecuteAsync(async () =>
{
    var service = new SubscriptionService();
    return await service.CreateAsync(new SubscriptionCreateOptions
    {
        Customer = customer.Id,
        Items = new List<SubscriptionItemOptions>
        {
            new SubscriptionItemOptions
            {
                Price = "price_H5ggYwtDq4fbrJ"
            }
        }
    });
});

Payment Intent Operations

using var transaction = new StripeTransaction();

// Create and confirm payment intent
var paymentIntent = await transaction.ExecuteAsync(async () =>
{
    var service = new PaymentIntentService();
    return await service.CreateAsync(new PaymentIntentCreateOptions
    {
        Amount = 2000,
        Currency = "usd",
        Customer = customer.Id,
        PaymentMethod = paymentMethod.Id,
        Confirm = true
    });
});

Webhook Operations

using var transaction = new StripeTransaction();

// Create webhook endpoint
var webhook = await transaction.ExecuteAsync(async () =>
{
    var service = new WebhookEndpointService();
    return await service.CreateAsync(new WebhookEndpointCreateOptions
    {
        Url = "https://your-domain.com/webhook",
        EnabledEvents = new List<string> { "payment_intent.succeeded" }
    });
});

Complex Transaction Example

using var transaction = new StripeTransaction();

var (customer, subscription, paymentIntent) = await transaction.ExecuteAsync(async () =>
{
    // 1. Create customer
    var customerService = new CustomerService();
    var customer = await customerService.CreateAsync(new CustomerCreateOptions
    {
        Email = "customer@example.com",
        Name = "John Doe"
    });

    // 2. Create and attach payment method
    var paymentMethodService = new PaymentMethodService();
    var paymentMethod = await paymentMethodService.CreateAsync(new PaymentMethodCreateOptions
    {
        Type = "card",
        Card = new PaymentMethodCardOptions
        {
            Number = "4242424242424242",
            ExpMonth = 12,
            ExpYear = 2024,
            Cvc = "123"
        }
    });
    await paymentMethodService.AttachAsync(paymentMethod.Id, new PaymentMethodAttachOptions
    {
        Customer = customer.Id
    });

    // 3. Create subscription
    var subscriptionService = new SubscriptionService();
    var subscription = await subscriptionService.CreateAsync(new SubscriptionCreateOptions
    {
        Customer = customer.Id,
        Items = new List<SubscriptionItemOptions>
        {
            new SubscriptionItemOptions
            {
                Price = "price_H5ggYwtDq4fbrJ"
            }
        }
    });

    // 4. Create payment intent
    var paymentIntentService = new PaymentIntentService();
    var paymentIntent = await paymentIntentService.CreateAsync(new PaymentIntentCreateOptions
    {
        Amount = 2000,
        Currency = "usd",
        Customer = customer.Id,
        PaymentMethod = paymentMethod.Id,
        Confirm = true
    });

    return (customer, subscription, paymentIntent);
});

Error Handling

try
{
    using var transaction = new StripeTransaction();
    // Your transaction code
}
catch (StripeException ex)
{
    // Handle Stripe-specific errors
}
catch (Exception ex)
{
    // Handle other errors
}

Custom Logging

public class CustomLogger : IStripeTransactionLogger
{
    public void LogDebug(string message) { /* Your implementation */ }
    public void LogInformation(string message) { /* Your implementation */ }
    public void LogWarning(string message) { /* Your implementation */ }
    public void LogError(string message, Exception? exception = null) { /* Your implementation */ }
}

// Use custom logger
using var transaction = new StripeTransaction(new CustomLogger());
// Your transaction code

Best Practices

  1. Always initialize the library with your Stripe API key at application startup
  2. Use the using declaration for cleaner code (C# 8.0+)
  3. Implement proper error handling
  4. Use custom logging for better debugging
  5. Test your rollback scenarios thoroughly
  6. Group related operations in a single ExecuteAsync call for better atomicity

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Version History

  • 1.0.2

    • Fixed namespace conflict by renaming StripeTransaction class to StripeTransactionManager
    • Improved code organization and readability
    • Updated documentation with new class name
    • Enhanced error handling and logging
  • 1.0.1

    • Added support for multiple operations in a single transaction
    • Improved error handling
    • Enhanced logging capabilities
    • Added more examples and documentation
  • 1.0.0

    • Initial release
    • Support for basic Stripe operations
    • Automatic rollback functionality
    • Custom logging support
    • Support for multiple operations in a single transaction
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 160 6/4/2025
1.0.1 150 6/4/2025
1.0.0 144 6/4/2025