-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
11,628 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.DotNet.InternalAbstractions; | ||
using System.Reflection; | ||
using System.Net.Http; | ||
using System.Text; | ||
|
||
namespace ChargifyNET.DotCore | ||
{ | ||
public class ChargifyConnect | ||
{ | ||
#region Constructors | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public ChargifyConnect() { } | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="url">The Chargify URL</param> | ||
/// <param name="apiKey">Your Chargify api key</param> | ||
/// <param name="password">Your Chargify api password</param> | ||
public ChargifyConnect(string url, string apiKey, string password) | ||
{ | ||
this.URL = url; | ||
this.apiKey = apiKey; | ||
this.Password = password; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="url">The Chargify URL</param> | ||
/// <param name="apiKey">Your Chargify api key</param> | ||
/// <param name="password">Your Chargify api password</param> | ||
/// <param name="sharedKey">Your Chargify hosted page shared key</param> | ||
public ChargifyConnect(string url, string apiKey, string password, string sharedKey) | ||
{ | ||
this.URL = url; | ||
this.apiKey = apiKey; | ||
this.Password = password; | ||
this.SharedKey = sharedKey; | ||
} | ||
|
||
#endregion | ||
|
||
#region Properties | ||
private static string UserAgent | ||
{ | ||
get | ||
{ | ||
if (_userAgent == null) | ||
{ | ||
_userAgent = $"Chargify.NET Client v{typeof(ChargifyConnect).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}"; | ||
} | ||
return _userAgent; | ||
} | ||
} | ||
private static string _userAgent; | ||
|
||
/// <summary> | ||
/// Get or set the API key | ||
/// </summary> | ||
public string apiKey { get; set; } | ||
|
||
/// <summary> | ||
/// Get or set the password | ||
/// </summary> | ||
public string Password { get; set; } | ||
|
||
/// <summary> | ||
/// Get or set the URL for chargify | ||
/// </summary> | ||
public string URL { get; set; } | ||
|
||
/// <summary> | ||
/// SharedKey used for url generation | ||
/// </summary> | ||
public string SharedKey { get; set; } | ||
|
||
/// <summary> | ||
/// Should Chargify.NET use JSON for output? XML by default, always XML for input. | ||
/// </summary> | ||
public bool UseJSON { get; set; } | ||
|
||
/// <summary> | ||
/// Should the library require a CVV? | ||
/// </summary> | ||
public bool CvvRequired { get { return this._cvvRequired; } set { this._cvvRequired = value; } } | ||
private bool _cvvRequired = true; | ||
|
||
|
||
/// <summary> | ||
/// The timeout (in milliseconds) for any call to Chargify. The default is 180000 | ||
/// </summary> | ||
public int Timeout | ||
{ | ||
get | ||
{ | ||
return this._timeout; | ||
} | ||
set | ||
{ | ||
this._timeout = value; | ||
} | ||
} | ||
private int _timeout = 180000; | ||
|
||
/// <summary> | ||
/// Method for determining if the properties have been set to allow this instance to connect correctly. | ||
/// </summary> | ||
public bool HasConnected | ||
{ | ||
get | ||
{ | ||
bool result = true; | ||
if (string.IsNullOrEmpty(this.apiKey)) result = false; | ||
if (string.IsNullOrEmpty(this.Password)) result = false; | ||
if (string.IsNullOrEmpty(this.URL)) result = false; | ||
return result; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Caller can plug in a delegate for logging raw Chargify requests | ||
/// </summary> | ||
public Action<HttpRequestMethod, string, string> LogRequest { get; set; } | ||
|
||
/// <summary> | ||
/// Caller can plug in a delegate for logging raw Chargify responses | ||
/// </summary> | ||
public Action<HttpStatusCode, string, string> LogResponse { get; set; } | ||
|
||
/// <summary> | ||
/// Get a reference to the last Http Response from the chargify server. This is set after every call to | ||
/// a Chargify Connect method | ||
/// </summary> | ||
public HttpResponseMessage LastResponse | ||
{ | ||
get | ||
{ | ||
return _lastResponse; | ||
} | ||
} | ||
private HttpResponseMessage _lastResponse = null; | ||
|
||
#endregion | ||
|
||
public void Test() | ||
{ | ||
Console.WriteLine(DoRequest("/subscriptions/12670109.xml", HttpRequestMethod.Get).Result); | ||
} | ||
|
||
private async Task<string> DoRequest(string methodString, HttpRequestMethod requestMethod, string postData = null) | ||
{ | ||
// make sure values are set | ||
if (string.IsNullOrEmpty(this.URL)) throw new InvalidOperationException("URL not set"); | ||
if (string.IsNullOrEmpty(this.apiKey)) throw new InvalidOperationException("apiKey not set"); | ||
if (string.IsNullOrEmpty(this.Password)) throw new InvalidOperationException("Password not set"); | ||
|
||
// create the URI | ||
string addressString = string.Format("{0}{1}", this.URL, (this.URL.EndsWith("/") ? string.Empty : "/")); | ||
|
||
var uriBuilder = new UriBuilder(addressString) | ||
{ | ||
Scheme = "https", | ||
Port = -1 // default port for scheme | ||
}; | ||
Uri address = uriBuilder.Uri; | ||
|
||
var handler = new HttpClientHandler(); | ||
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12; | ||
var client = new HttpClient(handler) | ||
{ | ||
Timeout = TimeSpan.FromMilliseconds(this._timeout), | ||
BaseAddress = address | ||
}; | ||
|
||
var byteArray = Encoding.ASCII.GetBytes($"{this.apiKey}:{this.Password}"); | ||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); | ||
client.DefaultRequestHeaders.Add("User-Agent", UserAgent); | ||
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(this.UseJSON ? "application/json" : "application/xml")); | ||
client.DefaultRequestHeaders.TransferEncodingChunked = false; | ||
|
||
try | ||
{ | ||
// Send the request for logging, if applicable. | ||
LogRequest?.Invoke(requestMethod, addressString+methodString, postData); | ||
|
||
switch (requestMethod) | ||
{ | ||
case HttpRequestMethod.Get: | ||
_lastResponse = await client.GetAsync(methodString); | ||
break; | ||
case HttpRequestMethod.Post: | ||
_lastResponse = await client.PostAsync(methodString, new StringContent(postData, Encoding.UTF8, this.UseJSON ? "application/json" : "text/xml")); | ||
break; | ||
case HttpRequestMethod.Put: | ||
_lastResponse = await client.PutAsync(methodString, new StringContent(postData, Encoding.UTF8, this.UseJSON ? "application/json" : "text/xml")); | ||
break; | ||
case HttpRequestMethod.Delete: | ||
_lastResponse = await client.DeleteAsync(methodString); | ||
break; | ||
} | ||
|
||
var content = await _lastResponse?.Content?.ReadAsStringAsync(); | ||
|
||
// Send the response for logging, if applicable. | ||
LogResponse?.Invoke(_lastResponse.StatusCode, addressString + methodString, content); | ||
|
||
return content; | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
throw; | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>c0885dae-b697-4771-af67-93d0d8569056</ProjectGuid> | ||
<RootNamespace>ChargifyNET.DotCore</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
| ||
namespace ChargifyNET | ||
{ | ||
/// <summary> | ||
/// The type of REST request | ||
/// </summary> | ||
public enum HttpRequestMethod | ||
{ | ||
/// <summary> | ||
/// Requests a representation of the specified resource | ||
/// </summary> | ||
Get, | ||
/// <summary> | ||
/// Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI | ||
/// </summary> | ||
Post, | ||
/// <summary> | ||
/// Requests that the enclosed entity be stored under the supplied URI | ||
/// </summary> | ||
Put, | ||
/// <summary> | ||
/// Deletes the specified resource | ||
/// </summary> | ||
Delete | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("ChargifyNET.DotCore")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("c0885dae-b697-4771-af67-93d0d8569056")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
|
||
"dependencies": { | ||
"Microsoft.DotNet.InternalAbstractions": "1.0.0", | ||
"NETStandard.Library": "1.6.0", | ||
"Newtonsoft.Json": "9.0.1", | ||
"System.Net.Http": "4.1.0", | ||
"System.Net.Primitives": "4.0.11", | ||
"System.Text.Encoding": "4.0.11" | ||
}, | ||
|
||
"frameworks": { | ||
"netstandard1.6": { | ||
"imports": "dnxcore50" | ||
} | ||
} | ||
} |
Oops, something went wrong.