Last active
August 29, 2015 14:07
-
-
Save bzbetty/00d0afe73afab39a5887 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EWayOffsitePaymentSettings : IPaymentSettings | |
{ | |
public string PaymentType | |
{ | |
get { return "Sale"; } | |
set { } | |
} | |
public string[] ProcessorCreditCards | |
{ | |
get { return new string[] { }; } | |
set { } | |
} | |
} | |
public partial class paymentresult : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
EWayOffsiteGateway.ConfirmPayment(Request.Params["AccessPaymentCode"]); | |
} | |
} | |
public class EWayOffsitePaymentProvider : PaymentProcessorProviderBase, IOffsitePaymentProcessorProvider, IPaymentProcessorProvider | |
{ | |
public IPaymentResponse SubmitTransaction(IPaymentRequest data) | |
{ | |
return Sale(data); | |
} | |
public IPaymentResponse Sale(IPaymentRequest data) | |
{ | |
return new PaymentResponse | |
{ | |
OrderNumber = data.OrderNumber, | |
GatewayRedirectUrl = EWayOffsiteGateway.Process(data), | |
GatewayRedirectUrlPostValues = new System.Collections.Specialized.NameValueCollection(), | |
IsOffsitePayment = true | |
}; | |
} | |
public IPaymentResponse HandleOffsiteNotification(int orderNumber, HttpRequest request, Telerik.Sitefinity.Ecommerce.Orders.Model.PaymentMethod paymentMethod) | |
{ | |
return new PaymentResponse | |
{ | |
IsOffsitePayment = true | |
}; | |
} | |
public IPaymentResponse HandleOffsiteReturn(int orderNumber, HttpRequest request, Telerik.Sitefinity.Ecommerce.Orders.Model.PaymentMethod paymentMethod) | |
{ | |
IPaymentResponse paymentResponse = new PaymentResponse | |
{ | |
IsOffsitePayment = true | |
}; | |
paymentResponse.IsSuccess = EWayOffsiteGateway.ConfirmPayment(request.Params["AccessPaymentCode"]); | |
//work around for a bug in the eway payment provider? makes querystrings lowercase which is very bad | |
request.Params.AsDynamic().IsReadOnly = false; | |
request.Params["landingUrl"] = Compress(string.Format("http://{0}/store/checkout?step=confirmation&steps=3", request.Url.Authority)); | |
return paymentResponse; | |
} | |
internal static string Compress(string plainTextString) | |
{ | |
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(plainTextString); | |
string result; | |
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes)) | |
{ | |
using (System.IO.MemoryStream memoryStream2 = new System.IO.MemoryStream()) | |
{ | |
using (GZipStream gZipStream = new GZipStream(memoryStream2, CompressionMode.Compress)) | |
{ | |
memoryStream.CopyTo(gZipStream); | |
} | |
result = System.Convert.ToBase64String(memoryStream2.ToArray()); | |
} | |
} | |
return result; | |
} | |
#region NotImplemented | |
public IPaymentResponse AuthVoidCapture(IPaymentRequest data) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IPaymentResponse Authorize(IPaymentRequest data) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IPaymentResponse Capture(IPaymentRequest data, string originalTransactionID) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IPaymentResponse Credit(IPaymentRequest data) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IPaymentResponse PerformValidations(IPaymentRequest data, bool avsAddress, bool avsZip, bool csc) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IPaymentResponse TestConnection(IPaymentRequest data) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IPaymentResponse Void(IPaymentRequest data, string originalTransactionID) | |
{ | |
throw new NotImplementedException(); | |
} | |
#endregion | |
} | |
public static class EWayOffsiteGateway | |
{ | |
private static string _gateway = "https://nz.ewaygateway.com"; | |
public static string Process(IPaymentRequest data) | |
{ | |
TransactionRequest result = null; | |
try | |
{ | |
var client = new System.Net.Http.HttpClient(); | |
client.BaseAddress = new Uri(_gateway); | |
var parameters = new Dictionary<string, string>() { | |
{ "CustomerID", ConfigurationManager.AppSettings["EWayCustomerId"] }, | |
{ "UserName", ConfigurationManager.AppSettings["EWayUsername"] }, | |
{ "CompanyName", ConfigurationManager.AppSettings["CompanyName"] }, | |
{ "Amount", data.Amount.ToString("F2") }, | |
{ "Currency", data.CurrencyCode }, | |
//Customer Details (customer is null??) | |
{ "CustomerFirstName", data.BillingFirstName }, | |
{ "CustomLastName", data.BillingLastName }, | |
{ "CustomerEmail", data.CustomerEmail }, | |
//Billing Address Details | |
{ "CustomerAddress", data.BillingStreet}, | |
{ "CustomerCity", data.BillingCity}, | |
{ "CustomerState", data.BillingState }, | |
{ "CustomerPostCode", data.BillingZip }, | |
{ "CustomerCountry", data.BillingCountry }, | |
{ "ModifiableCustomerDetails", "false" }, | |
{ "ReturnURL", data.GatewayRedirectSuccessUrl }, | |
{ "CancelURL", data.GatewayRedirectCancelUrl }, | |
{ "MerchantReference", data.OrderID.ToString() }, | |
}; | |
HttpQueryString queryString = new HttpQueryString(); | |
foreach (string key in parameters.Keys) | |
{ | |
queryString.Add(key, parameters[key]); | |
} | |
var response = client.PostAsync(queryString.MakeQueryString(new Uri("/Request/", UriKind.Relative)), null).Result; | |
response.EnsureSuccessStatusCode(); | |
var xmlMediaTypeFormatter = new XmlMediaTypeFormatter(); | |
xmlMediaTypeFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); | |
xmlMediaTypeFormatter.UseXmlSerializer = true; | |
result = response.Content.ReadAsAsync<TransactionRequest>(new[] { xmlMediaTypeFormatter }).Result; | |
if (!"True".Equals(result.Result, StringComparison.OrdinalIgnoreCase)) | |
{ | |
HttpContext.Current.Response.Redirect("/payment-failure"); | |
} | |
} | |
catch (Exception e) | |
{ | |
//todo log error | |
Enlighten.Net.ErrorHandler.ErrorHandlerFactory.HandleError(e); | |
HttpContext.Current.Response.Redirect("/payment-failure"); | |
} | |
return result.URI; | |
} | |
public static bool ConfirmPayment(string accessPaymentCode) | |
{ | |
var client = new System.Net.Http.HttpClient(); | |
client.BaseAddress = new Uri(_gateway); | |
var parameters = new Dictionary<string, string>() { | |
{ "CustomerID", ConfigurationManager.AppSettings["EWayCustomerId"] }, | |
{ "UserName", ConfigurationManager.AppSettings["EWayUsername"] }, | |
{ "AccessPaymentCode", accessPaymentCode } | |
}; | |
HttpQueryString queryString = new HttpQueryString(); | |
foreach (string key in parameters.Keys) | |
{ | |
queryString.Add(key, parameters[key]); | |
} | |
var response = client.PostAsync(queryString.MakeQueryString(new Uri("/Result/", UriKind.Relative)), null).Result; | |
response.EnsureSuccessStatusCode(); | |
var xmlMediaTypeFormatter = new XmlMediaTypeFormatter(); | |
xmlMediaTypeFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); | |
xmlMediaTypeFormatter.UseXmlSerializer = true; | |
var result = response.Content.ReadAsAsync<TransactionResponse>(new[] { xmlMediaTypeFormatter }).Result; | |
return result.ResponseCode == "00"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment