-
Notifications
You must be signed in to change notification settings - Fork 49
Getting Started
Kori Francis edited this page Jun 27, 2016
·
3 revisions
To install the latest from NuGet, search for chargify
and install or run Install-Package chargify
from the NuGet Package Manager Console.
To interact with the Chargify API, simply construct the ChargifyConnect client object.
using ChargifyNET;
var client = new ChargifyConnect(url: "https://<subdomain>.chargify.com", apiKey: "<api key>", password: "X") {
ProtocolType = SecurityProtocolType.Tls12
};
var customers = client.GetCustomerList();
var customer = customers.Values.FirstOrDefault();
var customerSubscriptions = client.GetSubscriptionListForCustomer(customer.ChargifyID);
Exceptions from Chargify may contain useful information specific to the call being made, so wrap your call in a try/catch and specifically catch the ChargifyException
:
try {
var newSubscription = client.CreateSubscription("missing_product_handle", customer.ChargifyID, paymentInfo);
} catch (ChargifyException ex) {
Console.WriteLine(ex.Message);
if (ex.ErrorMessages != null) Console.WriteLine(string.Join(", ", ex.ErrorMessages.Select(m => m.Message));
}
If you are going to use the API wrapper in any big useful capacity, you'll likely want one of the following strategies so that you don't continually need to construct the object. Pick one and prosper.
// C#6 Expression-bodied Member
public ChargifyConnect Chargify => _chargify ?? (_chargify = new ChargifyConnect
{
apiKey = "<api key>",
Password = "X",
URL = "https://<subdomain>.chargify.com/",
ProtocolType = SecurityProtocolType.Tls12
});
private ChargifyConnect _chargify;
OR
// Useful cached client useful in ASP.NET, uses the Chargify configuration section added by NuGet.
private ChargifyConnect Chargify
{
get
{
if (HttpContext.Cache["Chargify"] == null)
{
ChargifyAccountRetrieverSection config = ConfigurationManager.GetSection("chargify") as ChargifyAccountRetrieverSection;
ChargifyAccountElement accountInfo = config.GetDefaultOrFirst();
ChargifyConnect chargify = new ChargifyConnect();
chargify.apiKey = accountInfo.ApiKey;
chargify.Password = accountInfo.ApiPassword;
chargify.URL = accountInfo.Site;
chargify.SharedKey = accountInfo.SharedKey;
chargify.UseJSON = config.UseJSON;
HttpContext.Cache.Add("Chargify", chargify, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
}
return HttpContext.Cache["Chargify"] as ChargifyConnect;
}
}