CaptainPayment 1.0.0.4
dotnet add package CaptainPayment --version 1.0.0.4
NuGet\Install-Package CaptainPayment -Version 1.0.0.4
<PackageReference Include="CaptainPayment" Version="1.0.0.4" />
<PackageVersion Include="CaptainPayment" Version="1.0.0.4" />
<PackageReference Include="CaptainPayment" />
paket add CaptainPayment --version 1.0.0.4
#r "nuget: CaptainPayment, 1.0.0.4"
#:package CaptainPayment@1.0.0.4
#addin nuget:?package=CaptainPayment&version=1.0.0.4
#tool nuget:?package=CaptainPayment&version=1.0.0.4
Captain Payment
A comprehensive .NET payment processing library that provides seamless integration with Stripe for subscription management, customer handling, and payment processing.
🚀 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
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
📞 Support
- Documentation: GitHub Wiki
- Issues: GitHub Issues
- Email: general@tobikareem.com
🔄 Changelog
v1.0.0
- Initial release
- Stripe integration for subscriptions, customers, and payments
- Comprehensive error handling
- Full async support
Product | Versions 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. |
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.6)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.6)
- Stripe.net (>= 48.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release of Captain Payment library featuring Stripe integration for subscriptions, customer management, and payment processing with full async support and comprehensive error handling.