C# SDK C# में लिखे गए अनुप्रयोगों से Dodo Payments REST API तक सुविधाजनक पहुंच प्रदान करता है। इसमें एक async Task-आधारित API है जिसमें मजबूत टाइपिंग है और यह वर्तमान में बीटा में है।
C# SDK वर्तमान में बीटा में है। हम सुधार पर सक्रिय रूप से काम कर रहे हैं और आपकी प्रतिक्रिया का स्वागत करते हैं।
स्थापना
SDK को .NET CLI का उपयोग करके स्थापित करें:
dotnet add package DodoPayments.Client
SDK को .NET 8.0 या उच्चतर की आवश्यकता है। यह ASP.NET Core, कंसोल अनुप्रयोगों और अन्य .NET प्रोजेक्ट प्रकारों के साथ काम करता है।
त्वरित प्रारंभ
क्लाइंट को प्रारंभ करें और एक चेकआउट सत्र बनाएं:
using System ;
using DodoPayments . Client ;
using DodoPayments . Client . Models . CheckoutSessions ;
// Configured using the DODO_PAYMENTS_API_KEY and DODO_PAYMENTS_BASE_URL environment variables
DodoPaymentsClient client = new ();
CheckoutSessionCreateParams parameters = new ()
{
ProductCart =
[
new ()
{
ProductID = "product_id" ,
Quantity = 1 ,
},
],
};
var checkoutSessionResponse = await client . CheckoutSessions . Create ( parameters );
Console . WriteLine ( checkoutSessionResponse . SessionId );
हमेशा अपने API कुंजियों को सुरक्षित रूप से स्टोर करें, जैसे कि पर्यावरण चर, उपयोगकर्ता रहस्य, या Azure Key Vault का उपयोग करके। उन्हें अपने स्रोत कोड में हार्डकोड न करें या संस्करण नियंत्रण में कमिट न करें।
मुख्य विशेषताएँ
Async/Await नॉन-ब्लॉकिंग ऑपरेशनों के लिए पूर्ण async Task-आधारित API
Strong Typing नल संदर्भ प्रकारों के साथ व्यापक प्रकार की सुरक्षा
Dependency Injection .NET निर्भरता इंजेक्शन के लिए प्रथम श्रेणी का समर्थन
Configuration पर्यावरण चर या appsettings.json के माध्यम से सरल कॉन्फ़िगरेशन
कॉन्फ़िगरेशन
पर्यावरण चर
पर्यावरण चर का उपयोग करके कॉन्फ़िगर करें:
DODO_PAYMENTS_API_KEY = your_api_key_here
// Automatically reads from environment variables
DodoPaymentsClient client = new ();
मैनुअल कॉन्फ़िगरेशन
बियरर टोकन के साथ मैन्युअल रूप से कॉन्फ़िगर करें:
DodoPaymentsClient client = new () { BearerToken = "My Bearer Token" };
सामान्य संचालन
चेकआउट सत्र बनाएं
एक चेकआउट सत्र उत्पन्न करें:
var parameters = new CheckoutSessionCreateParams
{
ProductCart =
[
new ()
{
ProductID = "prod_123" ,
Quantity = 1
}
],
ReturnUrl = "https://yourdomain.com/return"
};
var session = await client . CheckoutSessions . Create ( parameters );
Console . WriteLine ( $"Checkout URL: { session . Url } " );
ग्राहकों का प्रबंधन करें
ग्राहक जानकारी बनाएं और पुनः प्राप्त करें:
// Create a customer
var createParams = new CustomerCreateParams
{
Email = "[email protected] " ,
Name = "John Doe" ,
Metadata = new Dictionary < string , string >
{
{ "user_id" , "12345" }
}
};
var customer = await client . Customers . Create ( createParams );
// Retrieve customer
var retrieved = await client . Customers . Retrieve ( "cus_123" );
Console . WriteLine ( $"Customer: { retrieved . Name } ( { retrieved . Email } )" );
सब्सक्रिप्शन को संभालें
आवर्ती सब्सक्रिप्शन बनाएं और प्रबंधित करें:
// Create a subscription
var subscriptionParams = new SubscriptionCreateParams
{
CustomerID = "cus_123" ,
ProductID = "prod_456" ,
PriceID = "price_789"
};
var subscription = await client . Subscriptions . Create ( subscriptionParams );
// Cancel subscription
await client . Subscriptions . Cancel ( subscription . Id );
ASP.NET Core एकीकरण
सेवा पंजीकरण
Program.cs में क्लाइंट को पंजीकृत करें:
using DodoPayments . Client ;
var builder = WebApplication . CreateBuilder ( args );
// Register DodoPayments client
builder . Services . AddSingleton < DodoPaymentsClient >( sp =>
{
var configuration = sp . GetRequiredService < IConfiguration >();
return new DodoPaymentsClient
{
BearerToken = configuration [ "DodoPayments:ApiKey" ]
};
});
builder . Services . AddControllers ();
var app = builder . Build ();
app . MapControllers ();
app . Run ();
कॉन्फ़िगरेशन
appsettings.json में कॉन्फ़िगरेशन जोड़ें:
{
"DodoPayments" : {
"ApiKey" : "your_api_key_here"
}
}
API नियंत्रक
निर्भरता इंजेक्शन का उपयोग करके एक नियंत्रक बनाएं:
using Microsoft . AspNetCore . Mvc ;
using DodoPayments . Client ;
using DodoPayments . Client . Models . CheckoutSessions ;
[ ApiController ]
[ Route ( "api/[controller]" )]
public class PaymentsController : ControllerBase
{
private readonly DodoPaymentsClient _client ;
private readonly ILogger < PaymentsController > _logger ;
public PaymentsController ( DodoPaymentsClient client , ILogger < PaymentsController > logger )
{
_client = client ;
_logger = logger ;
}
[ HttpPost ( "checkout" )]
public async Task < IActionResult > CreateCheckout ([ FromBody ] CheckoutRequest request )
{
try
{
var parameters = new CheckoutSessionCreateParams
{
ProductCart = request . Items ,
ReturnUrl = Url . Action ( "Return" , "Checkout" , null , Request . Scheme )
};
var session = await _client . CheckoutSessions . Create ( parameters );
return Ok ( new { checkoutUrl = session . Url });
}
catch ( Exception ex )
{
_logger . LogError ( ex , "Failed to create checkout" );
return BadRequest ( new { error = ex . Message });
}
}
}
public record CheckoutRequest ( List < ProductCartItem > Items );
सेवा परत पैटर्न
व्यापार तर्क के लिए एक सेवा बनाएं:
public interface IPaymentService
{
Task < CheckoutSessionResponse > CreateCheckoutAsync ( List < ProductCartItem > items );
Task < PaymentResponse > ProcessPaymentAsync ( decimal amount , string currency , string customerId );
}
public class PaymentService : IPaymentService
{
private readonly DodoPaymentsClient _client ;
private readonly ILogger < PaymentService > _logger ;
public PaymentService ( DodoPaymentsClient client , ILogger < PaymentService > logger )
{
_client = client ;
_logger = logger ;
}
public async Task < CheckoutSessionResponse > CreateCheckoutAsync ( List < ProductCartItem > items )
{
var parameters = new CheckoutSessionCreateParams
{
ProductCart = items ,
ReturnUrl = "https://yourdomain.com/return"
};
return await _client . CheckoutSessions . Create ( parameters );
}
public async Task < PaymentResponse > ProcessPaymentAsync (
decimal amount ,
string currency ,
string customerId )
{
try
{
var parameters = new PaymentCreateParams
{
Amount = ( long )( amount * 100 ), // Convert to cents
Currency = currency ,
CustomerID = customerId
};
return await _client . Payments . Create ( parameters );
}
catch ( Exception ex )
{
_logger . LogError ( ex , "Failed to process payment" );
throw ;
}
}
}
उपयोगकर्ता रहस्य (विकास)
विकास के लिए, API कुंजियों को स्टोर करने के लिए उपयोगकर्ता रहस्यों का उपयोग करें:
dotnet user-secrets init
dotnet user-secrets set "DodoPayments:ApiKey" "your_api_key_here"
परीक्षण
xUnit का उपयोग करके उदाहरण यूनिट परीक्षण:
using Xunit ;
using DodoPayments . Client ;
using DodoPayments . Client . Models . CheckoutSessions ;
public class PaymentServiceTests
{
private readonly DodoPaymentsClient _client ;
public PaymentServiceTests ()
{
_client = new DodoPaymentsClient
{
BearerToken = "test_key"
};
}
[ Fact ]
public async Task CreateCheckout_ShouldReturnSession ()
{
// Arrange
var parameters = new CheckoutSessionCreateParams
{
ProductCart =
[
new ()
{
ProductID = "prod_test" ,
Quantity = 1
}
]
};
// Act
var session = await _client . CheckoutSessions . Create ( parameters );
// Assert
Assert . NotNull ( session );
Assert . NotNull ( session . SessionId );
}
}
संसाधन
समर्थन
C# SDK के साथ मदद चाहिए?
योगदान
चूंकि SDK बीटा में है, आपकी प्रतिक्रिया और योगदान विशेष रूप से मूल्यवान हैं! प्रारंभ करने के लिए योगदान दिशानिर्देश की जांच करें.