Skip to content

Commit

Permalink
Starting #33
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed Aug 16, 2016
1 parent afb8be0 commit fed294f
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Source/Chargify.NET/Chargify.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@
<Compile Include="HttpRequestMethod.cs" />
<Compile Include="Interfaces\IMetadata.cs" />
<Compile Include="Interfaces\IMetadataResult.cs" />
<Compile Include="Interfaces\INote.cs" />
<Compile Include="Interfaces\IPublicSignupPage.cs" />
<Compile Include="Interfaces\IPayment.cs" />
<Compile Include="Interfaces\IRenewalDetails.cs" />
<Compile Include="Interfaces\ISubscriptionCreateOptions.cs" />
<Compile Include="Interfaces\ISubscriptionOverride.cs" />
<Compile Include="Metadata.cs" />
<Compile Include="MetadataResult.cs" />
<Compile Include="Note.cs" />
<Compile Include="Payment.cs" />
<Compile Include="PaymentProfileBase.cs" />
<Compile Include="PaymentProfileView.cs" />
Expand Down
59 changes: 52 additions & 7 deletions Source/Chargify.NET/ChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ public ISubscription CreateSubscription(string ProductHandle, int ChargifyID, IC
if (CreditCardAttributes == null) throw new ArgumentNullException("CreditCardAttributes");
if (ChargifyID == int.MinValue) throw new ArgumentException("Invalid Customer ID detected", "ChargifyID");

return CreateSubscription(new SubscriptionCreateOptions() { ProductHandle = ProductHandle, CustomerID = ChargifyID, CreditCardAttributes = (CreditCardAttributes)CreditCardAttributes, NextBillingAt = NextBillingAt });
return CreateSubscription(new SubscriptionCreateOptions() { ProductHandle = ProductHandle, CustomerID = ChargifyID, CreditCardAttributes = (CreditCardAttributes) CreditCardAttributes, NextBillingAt = NextBillingAt });
}

/// <summary>
Expand Down Expand Up @@ -4984,7 +4984,7 @@ private IAdjustment CreateAdjustment(int SubscriptionID, decimal amount, int amo
if (amount == decimal.MinValue) value_in_cents = amount_in_cents;
if (amount_in_cents == int.MinValue) value_in_cents = Convert.ToInt32(amount * 100);
if (value_in_cents == int.MinValue) value_in_cents = 0;
decimal value = Convert.ToDecimal((double)(value_in_cents) / 100.0);
decimal value = Convert.ToDecimal((double) (value_in_cents) / 100.0);

// make sure data is valid
if (string.IsNullOrEmpty(memo)) throw new ArgumentNullException("Memo");
Expand Down Expand Up @@ -5179,7 +5179,7 @@ public IPaymentProfileView UpdatePaymentProfile(PaymentProfileView PaymentProfil
if (!string.IsNullOrWhiteSpace(PaymentProfile.BillingCity)) xml.AppendFormat("<billing_city>{0}</billing_city>", PaymentProfile.BillingCity);
if (!string.IsNullOrWhiteSpace(PaymentProfile.BillingCountry)) xml.AppendFormat("<billing_country>{0}</billing_country>", PaymentProfile.BillingCountry);
if (!string.IsNullOrWhiteSpace(PaymentProfile.BillingState)) xml.AppendFormat("<billing_state>{0}</billing_state>", PaymentProfile.BillingState);
if (!string.IsNullOrWhiteSpace(PaymentProfile.BillingZip)) xml.AppendFormat("<billing_zip>{0}</billing_zip>", PaymentProfile.BillingZip);
if (!string.IsNullOrWhiteSpace(PaymentProfile.BillingZip)) xml.AppendFormat("<billing_zip>{0}</billing_zip>", PaymentProfile.BillingZip);
if (PaymentProfile.CustomerID != int.MinValue) xml.AppendFormat("<customer_id>{0}</customer_id>", PaymentProfile.CustomerID);
if (!string.IsNullOrWhiteSpace(PaymentProfile.FirstName)) xml.AppendFormat("<first_name>{0}</first_name>", PaymentProfile.FirstName);
if (!string.IsNullOrWhiteSpace(PaymentProfile.LastName)) xml.AppendFormat("<last_name>{0}</last_name>", PaymentProfile.LastName);
Expand Down Expand Up @@ -5227,6 +5227,51 @@ public IRenewalDetails PreviewRenewal(int subscriptionID)
}
#endregion

#region Notes
public INote CreateNote(INote note)
{
return CreateNote(note.SubscriptionID, note.Body, note.Sticky);
}

private INote CreateNote(int subscriptionId, string body, bool sticky = false)
{
// make sure data is valid
if (subscriptionId > 0) throw new ArgumentNullException(nameof(subscriptionId));
// make sure that the system ID is unique
if (this.LoadSubscription(subscriptionId) != null) throw new ArgumentException("Not valid", "subscriptionId");
// create XML for creation of customer
var NoteXML = new StringBuilder(GetXMLStringIfApplicable());
NoteXML.Append("<note>");
NoteXML.AppendFormat("<body>{0}</body>", body);
NoteXML.AppendFormat("<sticky>{0}</sticky>", sticky);
NoteXML.Append("</note>");
// now make the request
string response = this.DoRequest(string.Format("subscriptions/{0}/notes.{1}", subscriptionId, GetMethodExtension()), HttpRequestMethod.Post, NoteXML.ToString());
// change the response to the object
return response.ConvertResponseTo<Note>("note");
}

public IDictionary<int, INote> GetNotesForSubscription(int SubscriptionID)
{
throw new NotImplementedException();
}

public INote LoadNote(int SubscriptionID, int NoteID)
{
throw new NotImplementedException();
}

public bool DeleteNote(int SubscriptionID, int NoteID)
{
throw new NotImplementedException();
}

public INote UpdateNote(int SubscriptionID, INote UpdatedNote)
{
throw new NotImplementedException();
}
#endregion

#region Utility Methods
private Dictionary<int, T> GetListedJSONResponse<T>(string key, string response)
where T : class, IChargifyEntity
Expand All @@ -5241,7 +5286,7 @@ private Dictionary<int, T> GetListedJSONResponse<T>(string key, string response)
if ((array.Items[i] as JsonObject).ContainsKey("statement"))
{
JsonObject jsonObj = (array.Items[i] as JsonObject)["statement"] as JsonObject;
T value = (T)Activator.CreateInstance(typeof(T), jsonObj);
T value = (T) Activator.CreateInstance(typeof(T), jsonObj);
if (!retValue.ContainsKey(value.ID))
{
retValue.Add(value.ID, value);
Expand Down Expand Up @@ -5275,7 +5320,7 @@ private Dictionary<int, T> GetListedXmlResponse<T>(string key, string response)
{
if (childNode.Name == key)
{
T value = (T)Activator.CreateInstance(typeof(T), childNode);
T value = (T) Activator.CreateInstance(typeof(T), childNode);
if (!retValue.ContainsKey(value.ID))
{
retValue.Add(value.ID, value);
Expand Down Expand Up @@ -5448,7 +5493,7 @@ private byte[] DoFileRequest(string methodString, HttpRequestMethod requestMetho
// build exception and set last response
if (wex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
using (HttpWebResponse errorResponse = (HttpWebResponse) wex.Response)
{
newException = new ChargifyException(errorResponse, wex, postData);
_lastResponse = errorResponse;
Expand Down Expand Up @@ -5591,7 +5636,7 @@ private string DoRequest(string methodString, HttpRequestMethod requestMethod, s
// build exception and set last response
if (wex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
using (HttpWebResponse errorResponse = (HttpWebResponse) wex.Response)
{
newException = new ChargifyException(errorResponse, wex, postData);
_lastResponse = errorResponse;
Expand Down
8 changes: 8 additions & 0 deletions Source/Chargify.NET/Interfaces/IChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ public interface IChargifyConnect
IRenewalDetails PreviewRenewal(int subscriptionID);
#endregion

#region Notes
INote CreateNote(INote NewNote);
IDictionary<int, INote> GetNotesForSubscription(int SubscriptionID);
INote LoadNote(int SubscriptionID, int NoteID);
bool DeleteNote(int SubscriptionID, int NoteID);
INote UpdateNote(int SubscriptionID, INote UpdatedNote);
#endregion

/// <summary>
/// Method for adding a metered component usage to the subscription
/// </summary>
Expand Down
46 changes: 46 additions & 0 deletions Source/Chargify.NET/Interfaces/INote.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#region License, Terms and Conditions
//
// INote.cs
//
// Authors: Kori Francis <twitter.com/djbyter>, David Ball
// Copyright (C) 2011 Clinical Support Systems, Inc. All rights reserved.
//
// THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#endregion

namespace ChargifyNET
{
#region Imports
using System;
#endregion

public interface INote : IComparable<INote>
{
int ID { get; }
string Body { get; }
int SubscriptionID { get; }
DateTime CreatedAt { get; }
DateTime UpdatedAt { get; }
bool Sticky { get; }
}
}
Loading

0 comments on commit fed294f

Please sign in to comment.