Get it here https://www.nuget.org/packages/RechargeSharp/
Built to work with the api documented at https://developer.rechargepayments.com/
Please feel free to submit issues and pull requests on github
- 2021-01: The namespace for this is just
RechargeSharp. - 2021-11: The namespace for this is
RechargeSharp.v2021-11.
You can use services from both namespaces simultaneously without issues.
Use the AddRechargeSharp2021_11() to add the services to the service collection, like so:
varhostBuilder=Host.CreateDefaultBuilder().ConfigureServices(collection =>{varapiKey="<insert-your-API-key-here>";collection.AddRechargeSharp2021_11(options =>options.ApiKey=apiKey);});After this, the DI container will be able to resolve services for implemented endpoints, such as the IAddressService, ICustomerService, and so on.
Here is a tiny example of a class that uses the ICustomerService.
privatereadonlyICustomerService_customerService;publicYourServiceThatUsesRechargeSharp(ICustomerService customerService){_customerService=customerService;}protectedasyncTaskPrintCustomerEmail(){GetCustomerTypes.Response?response=await_customerService.GetCustomerAsync(1234);Console.WriteLine($"Customer email: {response!.Customer!.Email}");}If you set up your appsettings.json like this:
{ "RechargeConfiguration": {
"ApiKey": [
"1",
"2",
"3",
"4"
]
}
}
Add RechargeSharp to dependency injection. After that the services will be constructor injected.
// this example shows how you can get a list of api keys from configuration that the services will swap between automatically when encountering throttling.services.AddRechargeSharp(opts =>{opts.ApiKeyArray=_configuration.GetSection("RechargeConfiguration:ApiKey").GetChildren().Select(x =>x.Value).ToArray();opts.WebhookApiKey=_configuration["RechargeConfiguration:ApiKey:0"];})// if you only have 1 api key you wish to work with you can do the following.services.AddRechargeSharp(opts =>{opts.ApiKeyArray=new[]{"apikey"});opts.WebhookApiKey="apikey";}publicclassSubscriptionTestClass{privatereadonlySubscriptionService_subscriptionService;// constructor inject the subscription service to start working with subscriptions.publicSubscriptionTestClass(SubscriptionServicesubscriptionService){_subscriptionService=subscriptionService;}publicasyncTaskDoStuff(){// get all subscriptions with status ACTIVE and created after two months ago.varsubscriptions=awaitsubscriptionService.GetAllSubscriptionsWithParamsAsync(status:"ACTIVE",createdAtMin:DateTime.Today.AddMonths(-2));// iterate results and print subscription Id.foreach(varsubscriptioninsubscriptions){Console.WriteLine(subscription.Id);}}}publicclassCustomerTestClass{privatereadonlyCustomerService_customerService;// constructor inject the customer service to start working with customers.publicCustomerTestClass(CustomerServicecustomerService){_customerService=customerService;}publicasyncTaskDoStuff(){// get all customers created in the last two days.varcustomers=await_customerService.GetAllCustomersWithParamsAsync(createdAtMin:DateTime.Now.AddDays(-2));// iterate results and print whether the customer has a valid payment method.foreach(varcustomerincustomers){Console.WriteLine(customer.HasValidPaymentMethod);}}}