From d0f84d118d734a871724be7c253ad1b19fd5a10e Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 8 Aug 2014 23:08:19 -0700 Subject: [PATCH 1/2] Edit APIException to accept string for additional field --- Balanced/Exceptions/APIException.cs | 7 +++---- BalancedTests/DebitTest.cs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Balanced/Exceptions/APIException.cs b/Balanced/Exceptions/APIException.cs index a934bdc..dab0839 100644 --- a/Balanced/Exceptions/APIException.cs +++ b/Balanced/Exceptions/APIException.cs @@ -12,7 +12,7 @@ namespace Balanced.Exceptions { public class APIException : HTTPException { - public Dictionary 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; } @@ -27,13 +27,12 @@ public APIException( HttpWebResponse response, DictionaryresponsePayload) { - if (responsePayload.ContainsKey("additional") && responsePayload["additional"] != null) - additional = ((JObject)responsePayload["additional"]).ToObject>(); + 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>(); + extras = ((JObject)responsePayload["extras"]).ToObject>(); 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"]); diff --git a/BalancedTests/DebitTest.cs b/BalancedTests/DebitTest.cs index 84d5336..def9359 100644 --- a/BalancedTests/DebitTest.cs +++ b/BalancedTests/DebitTest.cs @@ -2,6 +2,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Balanced; using System.Collections.Generic; +using Balanced.Exceptions; namespace BalancedTests { @@ -29,6 +30,33 @@ public void TestDebitCreate() Assert.AreEqual("A simple debit", debit.description); } + [TestMethod] + public void TestFailedDebitAPIException() + { + Customer customer = createPersonCustomer(); + Card card = createPorcessorErrorCard(); + card.AssociateToCustomer(customer); + + Dictionary meta = new Dictionary(); + meta.Add("invoice_id", "12141"); + + Dictionary payload = new Dictionary(); + payload.Add("amount", 10000); + payload.Add("description", "A simple debit"); + payload.Add("meta", meta); + + try + { + Debit debit = card.Debit(payload); + } + catch(Balanced.Exceptions.APIException ex) + { + Assert.IsInstanceOfType(ex.additional, typeof(string)); + } + + + } + [TestMethod] public void TestDebitCreateNoCustomer() { From aa8dfc0341f6e1f342a403be5ddac477c6323eb5 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 8 Aug 2014 23:26:43 -0700 Subject: [PATCH 2/2] Add test for extras --- BalancedTests/BaseTest.cs | 18 ++++++++++++++++++ BalancedTests/DebitTest.cs | 5 +++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/BalancedTests/BaseTest.cs b/BalancedTests/BaseTest.cs index 4cc3335..e8b83d9 100644 --- a/BalancedTests/BaseTest.cs +++ b/BalancedTests/BaseTest.cs @@ -38,6 +38,24 @@ protected Card createCard() return card; } + protected Card createPorcessorErrorCard() + { + Dictionary address = new Dictionary(); + 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(); diff --git a/BalancedTests/DebitTest.cs b/BalancedTests/DebitTest.cs index def9359..8922b4a 100644 --- a/BalancedTests/DebitTest.cs +++ b/BalancedTests/DebitTest.cs @@ -41,7 +41,7 @@ public void TestFailedDebitAPIException() meta.Add("invoice_id", "12141"); Dictionary payload = new Dictionary(); - payload.Add("amount", 10000); + payload.Add("amount", 0); payload.Add("description", "A simple debit"); payload.Add("meta", meta); @@ -51,7 +51,8 @@ public void TestFailedDebitAPIException() } catch(Balanced.Exceptions.APIException ex) { - Assert.IsInstanceOfType(ex.additional, typeof(string)); + Assert.IsTrue(string.IsNullOrEmpty(ex.additional)); + Assert.IsInstanceOfType(ex.extras, typeof(Dictionary)); }