Skip to content

Commit

Permalink
Merge pull request #43 from balanced/error_message
Browse files Browse the repository at this point in the history
Edit APIException to accept string for additional field
  • Loading branch information
rserna2010 committed Sep 24, 2014
2 parents 8edb1e0 + aa8dfc0 commit 233887f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Balanced/Exceptions/APIException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Balanced.Exceptions
{
public class APIException : HTTPException
{
public Dictionary<string, string> additional { get; set; }
public string additional { get; set; }
public string category_code { get; set; }
public string category_type { get; set; }
public string description { get; set; }
Expand All @@ -27,13 +27,12 @@ public APIException(
HttpWebResponse response,
Dictionary<string, object>responsePayload)
{
if (responsePayload.ContainsKey("additional") && responsePayload["additional"] != null)
additional = ((JObject)responsePayload["additional"]).ToObject<Dictionary<string, string>>();
additional = responsePayload.ContainsKey("additional") ? (string)responsePayload["additional"] : null;
category_code = responsePayload.ContainsKey("category_code") ? (string)responsePayload["category_code"] : null;
category_type = responsePayload.ContainsKey("category_type") ? (string)responsePayload["category_type"] : null;
description = responsePayload.ContainsKey("description") ? (string)responsePayload["description"] : null;
if (responsePayload.ContainsKey("extras") && responsePayload["extras"] != null)
additional = ((JObject)responsePayload["extras"]).ToObject<Dictionary<string, string>>();
extras = ((JObject)responsePayload["extras"]).ToObject<Dictionary<string, string>>();
request_id = responsePayload.ContainsKey("request_id") ? (string)responsePayload["request_id"] : null;
status = responsePayload.ContainsKey("status") ? (string)responsePayload["status"] : null;
status_code = Convert.ToInt16(responsePayload["status_code"]);
Expand Down
18 changes: 18 additions & 0 deletions BalancedTests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ protected Card createCard()
return card;
}

protected Card createPorcessorErrorCard()
{
Dictionary<string, string> address = new Dictionary<string, string>();
address.Add("line1", "123 Fake Street");
address.Add("city", "Jollywood");
address.Add("postal_code", "90210");

Card card = new Card();
card.name = "Homer Jay";
card.number = "4444444444444448";
card.cvv = "123";
card.expiration_month = 12;
card.expiration_year = 2016;
card.address = address;
card.Save();
return card;
}

protected Card createCreditableCard()
{
Card card = new Card();
Expand Down
29 changes: 29 additions & 0 deletions BalancedTests/DebitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Balanced;
using System.Collections.Generic;
using Balanced.Exceptions;

namespace BalancedTests
{
Expand Down Expand Up @@ -29,6 +30,34 @@ public void TestDebitCreate()
Assert.AreEqual("A simple debit", debit.description);
}

[TestMethod]
public void TestFailedDebitAPIException()
{
Customer customer = createPersonCustomer();
Card card = createPorcessorErrorCard();
card.AssociateToCustomer(customer);

Dictionary<string, object> meta = new Dictionary<string, object>();
meta.Add("invoice_id", "12141");

Dictionary<string, object> payload = new Dictionary<string, object>();
payload.Add("amount", 0);
payload.Add("description", "A simple debit");
payload.Add("meta", meta);

try
{
Debit debit = card.Debit(payload);
}
catch(Balanced.Exceptions.APIException ex)
{
Assert.IsTrue(string.IsNullOrEmpty(ex.additional));
Assert.IsInstanceOfType(ex.extras, typeof(Dictionary<String,String>));
}


}

[TestMethod]
public void TestDebitCreateNoCustomer()
{
Expand Down

0 comments on commit 233887f

Please sign in to comment.