CaptainPayment 1.0.0.4

dotnet add package CaptainPayment --version 1.0.0.4
                    
NuGet\Install-Package CaptainPayment -Version 1.0.0.4
                    
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="CaptainPayment" Version="1.0.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CaptainPayment" Version="1.0.0.4" />
                    
Directory.Packages.props
<PackageReference Include="CaptainPayment" />
                    
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 CaptainPayment --version 1.0.0.4
                    
#r "nuget: CaptainPayment, 1.0.0.4"
                    
#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 CaptainPayment@1.0.0.4
                    
#: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=CaptainPayment&version=1.0.0.4
                    
Install as a Cake Addin
#tool nuget:?package=CaptainPayment&version=1.0.0.4
                    
Install as a Cake Tool

Captain Payment

A comprehensive .NET payment processing library that provides seamless integration with Stripe for subscription management, customer handling, and payment processing.

NuGet Version License Build Status

🚀 Features

  • Subscription Management: Create, update, cancel, and retrieve subscription information
  • Customer Management: Full CRUD operations for customer data
  • Payment Processing: Handle payment intents and transactions
  • Error Handling: Comprehensive exception handling with custom payment exceptions
  • Async Support: Full async/await support for all operations
  • Logging Integration: Built-in logging support using Microsoft.Extensions.Logging
  • Configuration: Easy setup using .NET dependency injection and configuration patterns

📦 Installation

Install the package via NuGet Package Manager:

dotnet add package CaptainPayment

Or via Package Manager Console:

Install-Package CaptainPayment

⚙️ Configuration

1. Configure Stripe Settings

Add your Stripe configuration to appsettings.json:

{
  "StripeSettings": {
    "SecretKey": "sk_test_your_stripe_secret_key",
    "PublishableKey": "pk_test_your_stripe_publishable_key",
    "ProviderName": "Stripe",
    "WebhookSecret": "whsec_your_webhook_secret"
  }
}

2. Register Services

In your Program.cs or Startup.cs:

using CaptainPayment.Core.Config;
using CaptainPayment.Stripe.Services.Subscriptions;
using CaptainPayment.Stripe.Services.Customers;
using CaptainPayment.Stripe.Services.Payments;

var builder = WebApplication.CreateBuilder(args);

// Configure Stripe settings
builder.Services.Configure<StripeSettings>(
    builder.Configuration.GetSection("StripeSettings"));

// Register Captain Payment services
builder.Services.AddScoped<ISubscriptionService, StripeSubscriptionService>();
builder.Services.AddScoped<ICustomerService, StripeCustomerService>();
builder.Services.AddScoped<IPaymentService, StripePaymentService>();
builder.Services.AddScoped<IPaymentProvider, StripeSubscriptionService>();

// Example Usage:
services.AddStripePayments(options =>
        {
            options.SecretKey = configuration.Payment.Stripe.SandboxClientSecret; // This is usually the secret key for test mode
            options.PublishableKey = configuration.Payment.Stripe.SandboxClientId; // This is usually the publishable key
            options.WebhookSecret = configuration.Payment.Stripe.WebhookSecret;
            options.ProviderName = configuration.Payment.Stripe.ProviderName;
            options.PaymentOptions = configuration.Payment.Stripe.PaymentOptions;
            options.SubscriptionDefaults = configuration.Payment.Stripe.SubscriptionDefaults;
        });

var app = builder.Build();

🔧 Usage Examples

Subscription Management

Creating a Subscription
public class SubscriptionController : ControllerBase
{
    private readonly ISubscriptionService _subscriptionService;

    public SubscriptionController(ISubscriptionService subscriptionService)
    {
        _subscriptionService = subscriptionService;
    }

    [HttpPost("create")]
    public async Task<IActionResult> CreateSubscription([FromBody] CreateSubscriptionRequest request)
    {
        try
        {
            var result = await _subscriptionService.CreateSubscriptionAsync(request);
            return Ok(result);
        }
        catch (PaymentException ex)
        {
            return BadRequest(new { Error = ex.Message, Code = ex.ErrorCode });
        }
    }
}
Retrieving a Subscription
[HttpGet("{subscriptionId}")]
public async Task<IActionResult> GetSubscription(string subscriptionId)
{
    try
    {
        var subscription = await _subscriptionService.GetSubscriptionAsync(subscriptionId);
        return Ok(subscription);
    }
    catch (PaymentException ex)
    {
        return NotFound(new { Error = ex.Message });
    }
}
Canceling a Subscription
[HttpDelete("{subscriptionId}")]
public async Task<IActionResult> CancelSubscription(string subscriptionId)
{
    try
    {
        var result = await _subscriptionService.CancelSubscriptionAsync(subscriptionId);
        return Ok(result);
    }
    catch (PaymentException ex)
    {
        return BadRequest(new { Error = ex.Message });
    }
}

Customer Management

Creating a Customer
public class CustomerController : ControllerBase
{
    private readonly ICustomerService _customerService;

    public CustomerController(ICustomerService customerService)
    {
        _customerService = customerService;
    }

    [HttpPost]
    public async Task<IActionResult> CreateCustomer([FromBody] CreateCustomerRequest request)
    {
        try
        {
            var customer = await _customerService.CreateCustomerAsync(request);
            return Ok(customer);
        }
        catch (PaymentException ex)
        {
            return BadRequest(new { Error = ex.Message });
        }
    }
}
Searching Customers
[HttpGet("search")]
public async Task<IActionResult> SearchCustomers(string query)
{
    try
    {
        var customer = await _customerService.SearchCustomersAsync(query);
        return Ok(customer);
    }
    catch (PaymentException ex)
    {
        return BadRequest(new { Error = ex.Message });
    }
}

Payment Processing

Creating a Payment Intent
public class PaymentController : ControllerBase
{
    private readonly IPaymentService _paymentService;

    public PaymentController(IPaymentService paymentService)
    {
        _paymentService = paymentService;
    }

    [HttpPost("intent")]
    public async Task<IActionResult> CreatePaymentIntent([FromBody] PaymentIntentRequest request)
    {
        try
        {
            var intent = await _paymentService.CreatePaymentIntentAsync(
                request.Amount, 
                request.Currency, 
                request.CustomerId);
            
            return Ok(new { ClientSecret = intent.ClientSecret });
        }
        catch (Exception ex)
        {
            return BadRequest(new { Error = ex.Message });
        }
    }
}

📋 API Reference

ISubscriptionService

Method Description Parameters Returns
CreateSubscriptionAsync Creates a new subscription CreateSubscriptionRequest CreateSubscriptionResult
GetSubscriptionAsync Retrieves subscription details string subscriptionId SubscriptionDetails
CancelSubscriptionAsync Cancels a subscription string subscriptionId CancelSubscriptionResult
UpdateSubscriptionAsync Updates subscription details string subscriptionId, UpdateSubscriptionRequest UpdateSubscriptionResult
ValidateSubscriptionAsync Validates subscription status string subscriptionId bool

ICustomerService

Method Description Parameters Returns
CreateCustomerAsync Creates a new customer CreateCustomerRequest CustomerResult
GetCustomerAsync Retrieves customer details string customerId CustomerResult
UpdateCustomerAsync Updates customer information string customerId, UpdateCustomerRequest CustomerResult
DeleteCustomerAsync Deletes a customer string customerId bool
ListCustomersAsync Lists customers with pagination CustomerListRequest PagedResult<CustomerResult>
SearchCustomersAsync Searches customers by query string query CustomerResult

IPaymentService

Method Description Parameters Returns
CreatePaymentIntentAsync Creates a payment intent decimal amount, string currency, string customerId PaymentIntent

🚨 Error Handling

Captain Payment uses custom exception handling with the PaymentException class:

try
{
    var result = await _subscriptionService.CreateSubscriptionAsync(request);
    // Handle success
}
catch (PaymentException ex)
{
    // Handle payment-specific errors
    Console.WriteLine($"Payment Error: {ex.Message}");
    Console.WriteLine($"Error Code: {ex.ErrorCode}");
    Console.WriteLine($"Provider: {ex.Provider}");
}
catch (Exception ex)
{
    // Handle general errors
    Console.WriteLine($"General Error: {ex.Message}");
}

🔗 Data Models

CreateSubscriptionRequest

public class CreateSubscriptionRequest
{
    public string Email { get; set; }
    public string FullName { get; set; }
    public string CustomerId { get; set; }
    public string PaymentMethodId { get; set; }
    public string PriceId { get; set; }
    public int? TrialPeriodDays { get; set; }
    public Dictionary<string, string> Metadata { get; set; }
}

CreateCustomerRequest

public class CreateCustomerRequest
{
    public string Email { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Description { get; set; }
    public CustomerAddress Address { get; set; }
    public Dictionary<string, string> Metadata { get; set; }
}

🔐 Security Considerations

  • API Keys: Store Stripe API keys securely using environment variables or Azure Key Vault
  • Webhooks: Validate webhook signatures to ensure requests are from Stripe
  • PCI Compliance: Never store sensitive card data in your application
  • Logging: Avoid logging sensitive payment information

📈 Monitoring and Logging

Captain Payment integrates with Microsoft.Extensions.Logging. Configure logging in your application:

builder.Services.AddLogging(config =>
{
    config.AddConsole();
    config.AddDebug();
    config.SetMinimumLevel(LogLevel.Information);
});

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License

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

📞 Support

🔄 Changelog

v1.0.0

  • Initial release
  • Stripe integration for subscriptions, customers, and payments
  • Comprehensive error handling
  • Full async support

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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.0.4 117 7/17/2025
1.0.0.3 125 7/4/2025
1.0.0.2 141 7/3/2025
1.0.0.1 139 7/3/2025
1.0.0 144 7/3/2025

Initial release of Captain Payment library featuring Stripe integration for subscriptions, customer management, and payment processing with full async support and comprehensive error handling.