AsthaIT.Stripe
1.0.1
dotnet add package AsthaIT.Stripe --version 1.0.1
NuGet\Install-Package AsthaIT.Stripe -Version 1.0.1
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="AsthaIT.Stripe" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AsthaIT.Stripe" Version="1.0.1" />
<PackageReference Include="AsthaIT.Stripe" />
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 AsthaIT.Stripe --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AsthaIT.Stripe, 1.0.1"
#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 AsthaIT.Stripe@1.0.1
#: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=AsthaIT.Stripe&version=1.0.1
#tool nuget:?package=AsthaIT.Stripe&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AsthaIT.Stripe
A lightweight .NET library for integrating Stripe payment gateway functionality into your applications. This package provides a simple and secure way to process payments, manage customers, handle subscriptions, and validate transactions.
Features
- ✅ Customer Management - Create, update, retrieve, and delete customers
- ✅ One-Time Payments - Process single payments with Stripe Checkout
- ✅ Subscription Payments - Handle recurring payments and subscriptions
- ✅ Plan Management - Create and manage subscription plans
- ✅ Transaction Validation - Verify payment status and retrieve transaction details
- ✅ Error Handling - Comprehensive error handling with detailed response objects
- ✅ Async/Await Support - Full asynchronous operation support
- ✅ Type Safety - Strongly typed records and responses
Installation
dotnet add package AsthaIT.Stripe
Quick Start
1. Initialize the Client
using AsthaIT.Stripe;
// Initialize with your Stripe API key
var client = new AITStripeClient("sk_test_your_api_key_here");
2. Create a One-Time Payment
var paymentRecord = new OneTimePaymentCreationRecord
{
ProductName = "Premium Software License",
SuccessUrl = "https://yourapp.com/success?session_id={CHECKOUT_SESSION_ID}",
CancelUrl = "https://yourapp.com/cancel",
Currency = "USD",
Amount = 99.99M,
ExpiresAt = DateTime.UtcNow.AddHours(1),
MetaData = new Dictionary<string, string>
{
{ "UserId", "12345" },
{ "ProductId", "premium_license" }
}
};
var response = await client.CreateOneTimePaymentRequestAsync(paymentRecord);
if (response.IsSuccess)
{
Console.WriteLine($"Payment URL: {response.Data?.PaymentUrl}");
Console.WriteLine($"Session ID: {response.Data?.SessionId}");
}
else
{
Console.WriteLine($"Error: {response.ErrorMessage}");
}
Customer Management
Create a Customer
var customerRecord = new CustomerCreationRecord
{
Name = "John Doe",
Email = "john.doe@example.com",
Description = "Premium customer",
Address = new CustomerAddress
{
Line1 = "123 Main St",
City = "New York",
State = "NY",
PostalCode = "10001",
Country = "US"
},
Metadata = new Dictionary<string, string>
{
{ "Source", "Website" },
{ "Plan", "Premium" }
}
};
var response = await client.CreateCustomerAsync(customerRecord);
if (response.IsSuccess)
{
Console.WriteLine($"Customer created: {response.Data?.CustomerId}");
Console.WriteLine($"Email: {response.Data?.Email}");
}
Update a Customer
var updateRecord = new CustomerUpdateRecord
{
CustomerId = "cus_1234567890",
Name = "John Smith",
Email = "john.smith@example.com",
Description = "Updated customer information"
};
var response = await client.UpdateCustomerAsync(updateRecord);
Get All Customers
// Get first 10 customers
var response = await client.GetAllCustomersAsync(limit: 10);
// Get customers with email filter
var response = await client.GetAllCustomersAsync(
limit: 20,
customerEmail: "john@example.com"
);
// Pagination example
var response = await client.GetAllCustomersAsync(
limit: 10,
startingAfter: "cus_previous_customer_id"
);
Get Specific Customer
var response = await client.GetCustomerAsync("cus_1234567890");
Delete Customer
var response = await client.DeleteCustomerAsync("cus_1234567890");
Payment Processing
One-Time Payment
var paymentRecord = new OneTimePaymentCreationRecord
{
ProductName = "Software License",
SuccessUrl = "https://yourapp.com/success",
CancelUrl = "https://yourapp.com/cancel",
Currency = "USD",
Amount = 49.99M,
ExpiresAt = DateTime.UtcNow.AddMinutes(30),
MetaData = new Dictionary<string, string>
{
{ "OrderId", "ORD-12345" },
{ "UserId", "user_123" }
}
};
var response = await client.CreateOneTimePaymentRequestAsync(paymentRecord);
if (response.IsSuccess)
{
// Redirect customer to payment URL
var paymentUrl = response.Data?.PaymentUrl;
var sessionId = response.Data?.SessionId;
}
Subscription Payment
var subscriptionRecord = new SubscriptionPaymentCreationRecord
{
PlanId = "price_1RiYUMIX6b1hkTaF4Qlsu6rK",
CustomerId = "cus_1234567890",
SuccessUrl = "https://yourapp.com/success",
CancelUrl = "https://yourapp.com/cancel",
ExpiresAt = DateTime.UtcNow.AddHours(1),
MetaData = new Dictionary<string, string>
{
{ "SubscriptionType", "Monthly" },
{ "UserId", "user_123" }
}
};
var response = await client.CreateSubscriptionPaymentRequestAsync(subscriptionRecord);
Transaction Validation
Get Transaction by Session ID
var response = await client.GetPaymentTransactionBySessionIdAsync("cs_test_session_id");
if (response.IsSuccess)
{
var transaction = response.Data;
Console.WriteLine($"Payment Status: {transaction?.PaymentStatus}");
Console.WriteLine($"Amount: {transaction?.Amount}");
Console.WriteLine($"Currency: {transaction?.Currency}");
}
Get Transaction by Payment Intent ID
var response = await client.GetPaymentTransactionByPaymentIntentIdAsync("pi_3Ri8eGIX6b1hkTaF2m5XvQVV");
Plan Management
Create a Plan
var planRecord = new PlanCreationRecord
{
PlanName = "Monthly Premium",
PlanDescription = "Premium features with monthly billing",
Nickname = "Premium Monthly",
Amount = 29.99M,
Currency = "USD",
Interval = RcurringPaymentIntervalType.Monthly,
IntervalCount = 1,
Metadata = new Dictionary<string, string>
{
{ "CreatedBy", "Admin" },
{ "PlanType", "Premium" }
}
};
var response = await client.CreatePlanAsync(planRecord);
Get All Plans
// Get all plans
var response = await client.GetAllPlansAsync();
// Get plans with filter
var response = await client.GetAllPlansAsync(
planName: "Premium",
limit: 20
);
Get Specific Plan
var response = await client.GetPlanAsync("price_1RiYUMIX6b1hkTaF4Qlsu6rK");
Enable/Disable Plans
// Enable a plan
var response = await client.EnablePlanAsync("price_1RiYUMIX6b1hkTaF4Qlsu6rK");
// Disable a plan
var response = await client.DisablePlanAsync("price_1RiYUMIX6b1hkTaF4Qlsu6rK");
Subscription Management
Get All Subscriptions
// Get all active subscriptions
var response = await client.GetAllSubscriptionsAsync();
// Get subscriptions for specific customer
var response = await client.GetAllSubscriptionsAsync(
customerId: "cus_1234567890",
status: SubscriptionStatus.Active
);
// Get all subscriptions (active, canceled, etc.)
var response = await client.GetAllSubscriptionsAsync(
status: SubscriptionStatus.All,
limit: 50
);
Get Specific Subscription
var response = await client.GetSubscriptionAsync("sub_1RirDQIX6b1hkTaF9bB2Odau");
Cancel Subscription
var response = await client.CancelSubscriptionAsync("sub_1RirDQIX6b1hkTaF9bB2Odau");
Response Handling
All methods return a PaymentResponse<T>
object with the following structure:
public class PaymentResponse<T>
{
public bool IsSuccess { get; set; }
public T? Data { get; set; }
public string? ErrorMessage { get; set; }
}
Example Response Handling
var response = await client.CreateCustomerAsync(customerRecord);
if (response.IsSuccess)
{
// Handle success
var customer = response.Data;
Console.WriteLine($"Customer created: {customer?.CustomerId}");
}
else
{
// Handle error
Console.WriteLine($"Error: {response.ErrorMessage}");
}
Configuration
API Keys
- Test Mode: Use keys starting with
sk_test_
- Live Mode: Use keys starting with
sk_live_
Environment Variables
// Recommended: Use environment variables for API keys
var apiKey = Environment.GetEnvironmentVariable("STRIPE_API_KEY");
var client = new AITStripeClient(apiKey);
Error Handling
The library provides comprehensive error handling:
try
{
var response = await client.CreateOneTimePaymentRequestAsync(paymentRecord);
if (!response.IsSuccess)
{
// Handle specific error cases
switch (response.ErrorMessage)
{
case var error when error.Contains("invalid_request_error"):
Console.WriteLine("Invalid payment request");
break;
case var error when error.Contains("authentication_error"):
Console.WriteLine("Authentication failed");
break;
default:
Console.WriteLine($"Payment error: {response.ErrorMessage}");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
Best Practices
1. Always Check Response Status
var response = await client.CreateCustomerAsync(customerRecord);
if (!response.IsSuccess)
{
// Handle error appropriately
return;
}
2. Use Proper Error Handling
try
{
var response = await client.CreateOneTimePaymentRequestAsync(paymentRecord);
// Handle response
}
catch (Exception ex)
{
// Log and handle unexpected errors
_logger.LogError(ex, "Payment creation failed");
}
3. Store Session IDs
var response = await client.CreateOneTimePaymentRequestAsync(paymentRecord);
if (response.IsSuccess)
{
// Store session ID for later verification
var sessionId = response.Data?.SessionId;
// Save to database or session
}
4. Validate Payment Status
// After customer completes payment, verify the transaction
var transaction = await client.GetPaymentTransactionBySessionIdAsync(sessionId);
if (transaction.IsSuccess && transaction.Data?.PaymentStatus == "paid")
{
// Process successful payment
}
Dependencies
- Stripe.net: Official Stripe .NET library
License
MIT License
Author
Developed and maintained by AIT.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Stripe.net (>= 48.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.