Skip to content

Commit 80c9a6e

Browse files
committed
Merge pull request AuthorizeNet#52 from ramittal/master
Merge handling of schema validation errors
2 parents b635689 + 5affebc commit 80c9a6e

File tree

6 files changed

+333
-48
lines changed

6 files changed

+333
-48
lines changed

Authorize.NET/Util/HttpUtility.cs

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AuthorizeNet.Util
22
{
33
using System;
4+
using System.IO;
45
using System.Net;
56
using System.Text;
67
using System.Xml;
@@ -46,49 +47,73 @@ public static ANetApiResponse PostData<TQ, TS>(AuthorizeNet.Environment env, TQ
4647
webRequest.Proxy = SetProxyIfRequested(webRequest.Proxy);
4748

4849
var requestType = typeof (TQ);
49-
5050
var serializer = new XmlSerializer(requestType);
51-
52-
using (var writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8))
51+
using (var writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8))
5352
{
5453
serializer.Serialize(writer, request);
5554
}
56-
// Get the response
57-
using (var webResponse = webRequest.GetResponse())
55+
56+
// Get the response
57+
String responseAsString = null;
58+
Logger.debug(string.Format("Retreiving Response from Url: '{0}'", postUrl));
59+
using (var webResponse = webRequest.GetResponse())
5860
{
59-
Logger.debug(string.Format("Received Response: '{0}'", webResponse));
60-
61-
var responseType = typeof (TS);
62-
var deSerializer = new XmlSerializer(responseType);
63-
using (var stream = webResponse.GetResponseStream())
64-
{
65-
Logger.debug(string.Format("Deserializing Response from Stream: '{0}'", stream));
66-
67-
if (null != stream)
68-
{
69-
var deSerializedObject = deSerializer.Deserialize(stream);
70-
//if error response
71-
if (deSerializedObject is ErrorResponse)
61+
Logger.debug(string.Format("Received Response: '{0}'", webResponse));
62+
63+
using (var responseStream = webResponse.GetResponseStream())
64+
{
65+
if (null != responseStream)
66+
{
67+
using (var reader = new StreamReader(responseStream))
7268
{
73-
response = deSerializedObject as ErrorResponse;
69+
responseAsString = reader.ReadToEnd();
7470
}
75-
else
71+
Logger.debug(string.Format("Response from Stream: '{0}'", responseAsString));
72+
}
73+
}
74+
}
75+
if (null != responseAsString)
76+
{
77+
using (var memoryStreamForResponseAsString = new MemoryStream(Encoding.UTF8.GetBytes(responseAsString)))
78+
{
79+
var responseType = typeof (TS);
80+
var deSerializer = new XmlSerializer(responseType);
81+
82+
Object deSerializedObject;
83+
try
84+
{
85+
// try deserializing to the expected response type
86+
deSerializedObject = deSerializer.Deserialize(memoryStreamForResponseAsString);
87+
}
88+
catch (Exception)
89+
{
90+
// probably a bad response, try if this is an error response
91+
memoryStreamForResponseAsString.Seek(0, SeekOrigin.Begin); //start from beginning of stream
92+
var genericDeserializer = new XmlSerializer(typeof (ANetApiResponse));
93+
deSerializedObject = genericDeserializer.Deserialize(memoryStreamForResponseAsString);
94+
}
95+
96+
//if error response
97+
if (deSerializedObject is ErrorResponse)
98+
{
99+
response = deSerializedObject as ErrorResponse;
100+
}
101+
else
102+
{
103+
//actual response of type expected
104+
if (deSerializedObject is TS)
76105
{
77-
//actual response of type expected
78-
if (deSerializedObject is TS)
79-
{
80-
response = deSerializedObject as TS;
81-
}
82-
else if (deSerializedObject is ANetApiResponse) //generic response
83-
{
84-
response = deSerializedObject as ANetApiResponse;
85-
}
106+
response = deSerializedObject as TS;
107+
}
108+
else if (deSerializedObject is ANetApiResponse) //generic response
109+
{
110+
response = deSerializedObject as ANetApiResponse;
86111
}
87112
}
88-
}
89-
}
113+
}
114+
}
90115

91-
return response;
116+
return response;
92117
}
93118

94119
public static IWebProxy SetProxyIfRequested(IWebProxy proxy)
@@ -107,7 +132,7 @@ public static IWebProxy SetProxyIfRequested(IWebProxy proxy)
107132
{
108133
newProxy = new WebProxy(proxyUri);
109134
}
110-
if (null != newProxy)
135+
//if (null != newProxy)
111136
{
112137
newProxy.UseDefaultCredentials = true;
113138
newProxy.BypassProxyOnLocal = true;

AuthorizeNETtest/Api/Controllers/SampleTest/ArbSubscriptionSampleTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ArbSubscriptionSampleTest : ApiCoreTestBase {
3333
base.TearDown();
3434
}
3535

36-
[Test, Ignore]
36+
[Test]
3737
public void SampleCodeGetSubscriptionList()
3838
{
3939
LogHelper.info(Logger, "Sample GetSubscriptionList");
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
namespace AuthorizeNet.Api.Controllers.SampleTest
2+
{
3+
using System.Globalization;
4+
using AuthorizeNet.Api.Contracts.V1;
5+
using AuthorizeNet.Api.Controllers;
6+
using AuthorizeNet.Api.Controllers.Bases;
7+
using AuthorizeNet.Api.Controllers.Test;
8+
using AuthorizeNet.Util;
9+
using NUnit.Framework;
10+
11+
[TestFixture]
12+
public class CreateCustomerProfileFromTransactionSampleTest : ApiCoreTestBase
13+
{
14+
15+
[TestFixtureSetUp]
16+
public new static void SetUpBeforeClass()
17+
{
18+
ApiCoreTestBase.SetUpBeforeClass();
19+
}
20+
21+
[TestFixtureTearDown]
22+
public new static void TearDownAfterClass()
23+
{
24+
ApiCoreTestBase.TearDownAfterClass();
25+
}
26+
27+
[SetUp]
28+
public new void SetUp()
29+
{
30+
base.SetUp();
31+
}
32+
33+
[TearDown]
34+
public new void TearDown()
35+
{
36+
base.TearDown();
37+
}
38+
39+
[Test]
40+
public void SampleCodeCreateCustomerProfileFromTransaction()
41+
{
42+
LogHelper.info(Logger, "Sample createCustomerProfileFromTransaction");
43+
44+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
45+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;
46+
47+
//setup some transaction to use
48+
var transactionId = GetTransactionId();
49+
var createRequest = new createCustomerProfileFromTransactionRequest
50+
{
51+
refId = RefId,
52+
transId = transactionId.ToString(CultureInfo.InvariantCulture),
53+
};
54+
//create
55+
var createController = new createCustomerProfileFromTransactionController(createRequest);
56+
var createResponse = createController.ExecuteWithApiResponse();
57+
58+
//validate
59+
Assert.NotNull(createResponse);
60+
Assert.NotNull(createResponse.messages);
61+
Assert.AreEqual(messageTypeEnum.Ok, createResponse.messages.resultCode);
62+
Assert.NotNull(createResponse.customerProfileId);
63+
Assert.NotNull(createResponse.customerPaymentProfileIdList);
64+
Assert.AreNotEqual(0, createResponse.customerPaymentProfileIdList.Length);
65+
66+
long customerProfileId;
67+
long.TryParse(createResponse.customerProfileId, out customerProfileId);
68+
Assert.AreNotEqual(0, customerProfileId);
69+
70+
long customerPaymentProfileId;
71+
long.TryParse(createResponse.customerPaymentProfileIdList[0], out customerPaymentProfileId);
72+
Assert.AreNotEqual(0, customerPaymentProfileId);
73+
//if shipping profile is added, shipping profile id will be retrieved too
74+
}
75+
76+
private long GetTransactionId()
77+
{
78+
//Common code to set for all requests
79+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
80+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;
81+
82+
//set up data based on transaction
83+
var transactionAmount = SetValidTransactionAmount(Counter);
84+
var creditCard = new creditCardType { cardNumber = "4111111111111111", expirationDate = "0622" };
85+
var aCustomer = new customerDataType { email = string.Format( "{0}@b.bla", Counter)};
86+
87+
//standard api call to retrieve response
88+
var paymentType = new paymentType { Item = creditCard };
89+
var transactionRequest = new transactionRequestType
90+
{
91+
transactionType = transactionTypeEnum.authOnlyTransaction.ToString(),
92+
payment = paymentType,
93+
amount = transactionAmount,
94+
customer = aCustomer,
95+
};
96+
var request = new createTransactionRequest { transactionRequest = transactionRequest };
97+
var controller = new createTransactionController(request);
98+
controller.Execute();
99+
var response = controller.GetApiResponse();
100+
101+
//validate
102+
Assert.NotNull(response);
103+
Assert.NotNull(response.messages);
104+
Assert.NotNull(response.transactionResponse);
105+
Assert.AreEqual(messageTypeEnum.Ok, response.messages.resultCode);
106+
Assert.False(string.IsNullOrEmpty(response.transactionResponse.transId));
107+
long transactionId;
108+
long.TryParse(response.transactionResponse.transId, out transactionId);
109+
Assert.AreNotEqual(0, transactionId);
110+
111+
return transactionId;
112+
}
113+
}
114+
}

AuthorizeNETtest/Api/Controllers/SampleTest/CreateTransactionSampleTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ public class CreateTransactionSampleTest : ApiCoreTestBase
3636
base.TearDown();
3737
}
3838

39-
[Test, Ignore]
40-
public void SampleCodecreateTransaction()
39+
[Test]
40+
public void SampleCodeCreateTransaction()
4141
{
4242
LogHelper.info(Logger, "Sample createTransaction");
4343

4444
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
4545
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;
4646

4747
//create a transaction
48-
var transactionRequestType = new transactionRequestType()
48+
var transactionRequestType = new transactionRequestType
4949
{
5050
transactionType = transactionTypeEnum.authCaptureTransaction.ToString(),
5151
//amount = SetValidTransactionAmount(Counter),
@@ -88,8 +88,8 @@ public void SampleCodecreateTransaction()
8888
}
8989
}
9090

91-
[Test]//, Ignore]
92-
public void CreateTransactionWithCreditCard()
91+
[Test]
92+
public void SampleCodeCreateTransactionWithCreditCard()
9393
{
9494
//Common code to set for all requests
9595
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
@@ -120,8 +120,8 @@ public void CreateTransactionWithCreditCard()
120120
Assert.False( string.IsNullOrEmpty(response.transactionResponse.transId));
121121
}
122122

123-
[Test]//, Ignore]
124-
public void CreateTransactionWithApplePay()
123+
[Test]
124+
public void SampleCodeCreateTransactionWithApplePay()
125125
{
126126
//Common code to set for all requests
127127
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
@@ -157,8 +157,8 @@ public void CreateTransactionWithApplePay()
157157
}
158158

159159

160-
[Test]//, Ignore]
161-
public void CreateTransactionWithPayPal()
160+
[Test]
161+
public void SampleCodeCreateTransactionWithPayPal()
162162
{
163163
//Common code to set for all requests
164164
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;

AuthorizeNETtest/Api/Controllers/Test/ArbSubscriptionTest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
namespace AuthorizeNet.Api.Controllers.Test
22
{
33
using System;
4-
using System.Collections.Generic;
54
using AuthorizeNet.Api.Contracts.V1;
65
using AuthorizeNet.Api.Controllers;
7-
using AuthorizeNet.Api.Controllers.Bases;
86
using AuthorizeNet.Util;
97
using NUnit.Framework;
108

@@ -33,7 +31,7 @@ public class ArbSubscriptionTest : ApiCoreTestBase {
3331
base.TearDown();
3432
}
3533

36-
[Test, Ignore]
34+
[Test]
3735
public void TestGetSubscriptionList()
3836
{
3937

0 commit comments

Comments
 (0)