Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace EasyHttp with RestSharp #17

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions SpreedlyCoreSharp.Test/CoreServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public void GetTransactions_Deserialization_Purchase()
Assert.AreEqual(4, transaction.TransactionPaymentMethod.Month);
Assert.AreEqual("credit_card", transaction.TransactionPaymentMethod.PaymentMethodType);
Assert.AreEqual("XXXX-XXXX-XXXX-1111", transaction.TransactionPaymentMethod.Number);

Assert.AreEqual(null, transaction.TransactionResponse.FraudReview);
}

[Test]
Expand Down
11 changes: 3 additions & 8 deletions SpreedlyCoreSharp.Test/SpreedlyCoreSharp.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EasyHttp, Version=1.6.52.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EasyHttp.1.6.52.0\lib\net40\EasyHttp.dll</HintPath>
</Reference>
<Reference Include="JsonFx, Version=2.0.1209.2802, Culture=neutral, PublicKeyToken=315052dd637f8a52, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\JsonFx.2.0.1209.2802\lib\net40\JsonFx.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
Expand Down Expand Up @@ -120,6 +112,9 @@
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
1 change: 1 addition & 0 deletions SpreedlyCoreSharp.Test/Xml/Transaction_Purchase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<cvv_message nil="true"/>
<error_code/>
<error_detail nil="true"/>
<fraud_review nil="true"/>
<created_at type="datetime">2012-11-26T21:34:19Z</created_at>
<updated_at type="datetime">2012-11-26T21:34:19Z</updated_at>
</response>
Expand Down
2 changes: 0 additions & 2 deletions SpreedlyCoreSharp.Test/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EasyHttp" version="1.6.52.0" targetFramework="net45" />
<package id="JsonFx" version="2.0.1209.2802" targetFramework="net45" />
<package id="NUnit" version="2.6.2" targetFramework="net45" />
</packages>
6 changes: 3 additions & 3 deletions SpreedlyCoreSharp.WebSample/Modules/PaymentModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PaymentModule(ICoreService service)
{
ApiEnvironment = service.APIEnvironment,
RedirectUrl = ConfigurationManager.AppSettings["PublicWebUrl"] + "/payment/redirect-back",
Country = "GB" // Default it to help your customers, it's a long list!
Country = "USA" // Default it to help your customers, it's a long list!
}];
};

Expand All @@ -28,8 +28,8 @@ public PaymentModule(ICoreService service)

var transaction = service.ProcessPayment(new ProcessPaymentRequest
{
AmountInDecimal = 1.0m, // Same as saying Amount = 100
CurrencyCode = CurrencyCode.GBP,
Amount = 100, // 1.00 GBP
CurrencyCode = CurrencyCode.USD,
PaymentMethodToken = Request.Query.token
});

Expand Down
4 changes: 2 additions & 2 deletions SpreedlyCoreSharp.WebSample/Views/Gateways/Gateways.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}

<div class="span12">
<a href="/gateways/add-test-gateway" class="btn" onclick="return confirm('Are you sure you want to add a test gateway?')">Add Test Gateway</a>
<a href="@Url.Content("~/gateways/add-test-gateway")" class="btn" onclick="return confirm('Are you sure you want to add a test gateway?')">Add Test Gateway</a>

<br />

Expand Down Expand Up @@ -36,7 +36,7 @@
<td>
@if (!gateway.Redacted)
{
<a href="/gateways/redact/@gateway.Token" class="btn btn-danger" onclick="return confirm('Are you sure, redacting a gateway is permenant.')">Redact</a>
<a href="@Url.Content("~/gateways/redact/" + gateway.Token)" class="btn btn-danger" onclick="return confirm('Are you sure, redacting a gateway is permenant.')">Redact</a>
}
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
<td>@trans.CurrencyCode</td>
<td>@trans.TransactionType</td>
<td>
<a href="/transactions/?since=@trans.Token">View From</a><br />
<a href="/transactions/@trans.Token">View Details</a><br />
<a href="/transactions/@trans.Token/transcript">View Transcript</a>
<a href="@Url.Content("~/transactions/?since=" + trans.Token)">View From</a><br />
<a href="@Url.Content("~/transactions/" + trans.Token)">View Details</a><br />
<a href="@Url.Content("~/transactions/" + trans.Token + "/transcript")">View Transcript</a>
</td>
</tr>
}
Expand All @@ -43,5 +43,5 @@

<p>Click a transaction token to use that as the start of paging (it is limited to 20 records at a time)</p>

<a href="/transactions">All Transactions</a>
<a href="@Url.Content("~/transactions")">All Transactions</a>
</div>
12 changes: 6 additions & 6 deletions SpreedlyCoreSharp.WebSample/Views/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<head>
<title>SpreedlyCoreSharp</title>

<link type="text/css" rel="stylesheet" href="/Content/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="@Url.Content("~/Content/bootstrap.min.css")" />
<style type="text/css">
body {
padding-top: 60px;
Expand All @@ -36,19 +36,19 @@
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></a><a class="brand" href="#">SpreedlyCoreSharp</a><div class="nav-collapse collapse">
<ul class="nav">
<li class="@IsActive("Home")">
<a href="/">Home</a>
<a href="@Url.Content("~/")">Home</a>
</li>
<li class="@IsActive("Payment")">
<a href="/payment">Payment Form</a>
<a href="@Url.Content("~/payment")">Payment Form</a>
</li>
<li class="@IsActive("3DPayment")">
<a href="/3d-secure">3D Payment Form</a>
<a href="@Url.Content("~/3d-secure")">3D Payment Form</a>
</li>
<li class="@IsActive("Transaction")">
<a href="/transactions">Transactions</a>
<a href="@Url.Content("~/transactions")">Transactions</a>
</li>
<li class="@IsActive("Gateway")">
<a href="/gateways">Gateways</a>
<a href="@Url.Content("~/gateways")">Gateways</a>
</li>
</ul>

Expand Down
111 changes: 63 additions & 48 deletions SpreedlyCoreSharp/CoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using EasyHttp.Http;
using SpreedlyCoreSharp.Domain;
using SpreedlyCoreSharp.Request;
using SpreedlyCoreSharp.Response;
using RestSharp;
using RestSharp.Authenticators;

namespace SpreedlyCoreSharp
{
Expand All @@ -25,8 +26,9 @@ public class CoreService : ICoreService
private const string PaymentMethodUrl = "payment_methods/{0}.xml";
private const string PaymentMethodRetainUrl = "payment_methods/{0}/retain.xml";
private const string TransactionTranscriptUrl = "transactions/{0}/transcript";
private const string TransactionCreditUrl = "transactions/{0}/credit.xml";

private readonly HttpClient _client;
private readonly RestClient _client;

private readonly string _apiEnvironment;
private readonly string _apiSecret;
Expand All @@ -45,10 +47,10 @@ public CoreService(string apiEnvironment, string apiSecret, string apiSigningSec
_apiSigningSecret = apiSigningSecret;
_gatewayToken = gatewayToken;

_client = new HttpClient();
_client.Request.SetBasicAuthentication(_apiEnvironment, _apiSecret);
_client.Request.Accept = HttpContentTypes.ApplicationXml;
_client.Request.ForceBasicAuth = true;
_client = new RestClient(BaseUrl);
_client.Authenticator = new HttpBasicAuthenticator(_apiEnvironment, _apiSecret);
_client.AddDefaultHeader("accept", "application/xml");
_client.AddDefaultHeader("content-type", "application/xml");
}

/// <summary>
Expand Down Expand Up @@ -87,7 +89,16 @@ public T Deserialize<T>(string xml)

var serializer = new XmlSerializer(typeof(T));

var obj = (T)serializer.Deserialize(stream);
T obj;

try
{
obj = (T)serializer.Deserialize(stream);
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException("Error deserializing XML: " + Environment.NewLine + xml, ex);
}

if (typeof(T) == typeof(Transaction))
{
Expand Down Expand Up @@ -123,21 +134,24 @@ public string Serialize<T>(object item)
/// </summary>
/// <param name="gatewayRequest">gateway request object</param>
/// <returns></returns>
public Gateway AddGateway(object gatewayRequest)
public Gateway AddGateway(BaseGatewayRequest gatewayRequest)
{
var response = _client.Post(BaseUrl + GatewaysUrl, gatewayRequest, "application/xml");
var request = new RequestWrapper(GatewaysUrl);
request.AddBody(gatewayRequest);

var response = _client.Post(request);

return Deserialize<Gateway>(response.RawText);
return Deserialize<Gateway>(response.Content);
}

/// <summary>
/// Redacts a gateway, this is permanent.
/// </summary>
/// <param name="gatewayToken">token of gateway</param>
public void RedactGateway(string gatewayToken)
{
{
// TODO: do something with response?
_client.Put(BaseUrl + string.Format(RedactGatewayUrl, gatewayToken), "", "application/xml");
_client.Put(new RequestWrapper(string.Format(RedactGatewayUrl, gatewayToken)));
}

/// <summary>
Expand All @@ -146,9 +160,9 @@ public void RedactGateway(string gatewayToken)
/// <returns></returns>
public List<Gateway> GetGateways()
{
var response = _client.Get(BaseUrl + GatewaysUrl);
var response = _client.Get(new RequestWrapper(GatewaysUrl));

var gateways = Deserialize<GetGatewaysResponse>(response.RawText);
var gateways = Deserialize<GetGatewaysResponse>(response.Content);

return gateways.Gateways;
}
Expand All @@ -160,11 +174,9 @@ public List<Gateway> GetGateways()
/// <returns></returns>
public Transaction GetTransaction(string token)
{
string url = BaseUrl + string.Format(TransactionUrl, token);

var response = _client.Get(url);
var response = _client.Get(new RequestWrapper(string.Format(TransactionUrl, token)));

return Deserialize<Transaction>(response.RawText);
return Deserialize<Transaction>(response.Content);
}

/// <summary>
Expand All @@ -174,20 +186,16 @@ public Transaction GetTransaction(string token)
/// <returns></returns>
public List<Transaction> GetTransactions(string sinceToken = "")
{
string url;
var request = new RequestWrapper(TransactionsUrl);

if (!string.IsNullOrWhiteSpace(sinceToken))
{
url = string.Format("{0}{1}?since_token={2}", BaseUrl, TransactionsUrl, sinceToken);
}
else
{
url = string.Format("{0}{1}", BaseUrl, TransactionsUrl);
request.AddParameter("since_token", sinceToken);
}

var response = _client.Get(url);
var response = _client.Get(request);

var transactions = Deserialize<GetTransactionsResponse>(response.RawText);
var transactions = Deserialize<GetTransactionsResponse>(response.Content);

return transactions.Transactions;
}
Expand All @@ -199,9 +207,9 @@ public List<Transaction> GetTransactions(string sinceToken = "")
/// <returns>transaction's payment method</returns>
public Transaction.PaymentMethod GetPaymentMethod(string token)
{
var response = _client.Get(BaseUrl + string.Format(PaymentMethodUrl, token));
var response = _client.Get(new RequestWrapper(string.Format(PaymentMethodUrl, token)));

return Deserialize<Transaction.PaymentMethod>(response.RawText);
return Deserialize<Transaction.PaymentMethod>(response.Content);
}

/// <summary>
Expand All @@ -211,20 +219,16 @@ public Transaction.PaymentMethod GetPaymentMethod(string token)
/// <returns></returns>
public List<Transaction.PaymentMethod> GetPaymentMethods(string sinceToken = "")
{
string url;
var request = new RequestWrapper(PaymentMethodsUrl);

if (!string.IsNullOrWhiteSpace(sinceToken))
{
url = string.Format("{0}{1}?since_token={2}", BaseUrl, PaymentMethodsUrl, sinceToken);
}
else
{
url = string.Format("{0}{1}", BaseUrl, PaymentMethodsUrl);
request.AddParameter("since_token", sinceToken);
}

var response = _client.Get(url);
var response = _client.Get(request);

var transactions = Deserialize<GetPaymentMethodsResponse>(response.RawText);
var transactions = Deserialize<GetPaymentMethodsResponse>(response.Content);

return transactions.PaymentMethods;
}
Expand All @@ -237,39 +241,40 @@ public Transaction.PaymentMethod GetPaymentMethod(string token)
/// <returns></returns>
public string GetTransactionTranscript(string token)
{
string url = BaseUrl + string.Format(TransactionTranscriptUrl, token);

var response = _client.Get(url);
var response = _client.Get(new RequestWrapper(string.Format(TransactionTranscriptUrl, token)));

return response.RawText;
return response.Content;
}

/// <summary>
/// Sends a purchase request to the active gateway
/// </summary>
/// <param name="request">purchase request</param>
/// <returns></returns>
public Transaction ProcessPayment(ProcessPaymentRequest request)
public Transaction ProcessPayment(ProcessPaymentRequest requestBody)
{
var response = _client.Post(BaseUrl + string.Format(ProcessPaymentUrl, _gatewayToken), request, "application/xml");

if (request.Attempt3DSecure && string.IsNullOrWhiteSpace(request.CallbackUrl))
if (requestBody.Attempt3DSecure && string.IsNullOrWhiteSpace(requestBody.CallbackUrl))
{
throw new ArgumentException("Callback URL cannot be empty.");
}

if (request.Attempt3DSecure && string.IsNullOrWhiteSpace(request.RedirectUrl))
if (requestBody.Attempt3DSecure && string.IsNullOrWhiteSpace(requestBody.RedirectUrl))
{
throw new ArgumentException("Redirect URL cannot be empty.");
}

byte[] byteArray = Encoding.ASCII.GetBytes(response.RawText);
var request = new RequestWrapper(string.Format(ProcessPaymentUrl, _gatewayToken));
request.AddBody(requestBody);

var response = _client.Post(request);

byte[] byteArray = Encoding.ASCII.GetBytes(response.Content);

var stream = new MemoryStream(byteArray);

// Seems if you send absolutely nothing it decides to return <errors> rather than full <transaction> doc...
// Not sure how to append this to a Transaction document.
if (response.RawText.StartsWith("<errors>"))
if (response.Content.StartsWith("<errors>"))
{
var errors = (TransactionErrors)new XmlSerializer(typeof(TransactionErrors)).Deserialize(stream);

Expand All @@ -282,7 +287,17 @@ public Transaction ProcessPayment(ProcessPaymentRequest request)
};
}

return Deserialize<Transaction>(response.RawText);
return Deserialize<Transaction>(response.Content);
}

public Transaction RefundPayment(string transactionToken, RefundPaymentRequest requestBody)
{
var request = new RequestWrapper(string.Format(TransactionCreditUrl, transactionToken));
request.AddBody(requestBody);

var response = _client.Post(request);

return Deserialize<Transaction>(response.Content);
}

/// <summary>
Expand Down
Loading