Skip to content

Commit 1d64706

Browse files
author
icepaynl
committed
Initial commit
1 parent 6fe71f1 commit 1d64706

15 files changed

+521
-0
lines changed

Classes/Base.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes
8+
{
9+
public abstract class Base
10+
{
11+
public string Timestamp
12+
{
13+
get
14+
{
15+
return DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss");
16+
}
17+
}
18+
19+
/// <summary>
20+
/// In case of errors, this field will contain the error description.
21+
/// </summary>
22+
public string Message { get; set; }
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Payment
8+
{
9+
public class AutomaticCheckoutRequest : CheckoutRequest { }
10+
11+
public class AutomaticCheckoutResponse : CheckoutResponse { }
12+
}

Classes/Payment/Checkout.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Payment
8+
{
9+
public class CheckoutRequest : Base
10+
{
11+
public int Amount { get; set; }
12+
public string Country { get; set; }
13+
public string Currency { get; set; }
14+
public string Description { get; set; }
15+
public string EndUserIP { get; set; }
16+
public string Issuer { get; set; }
17+
public string Language { get; set; }
18+
public string OrderID { get; set; }
19+
public string PaymentMethod { get; set; }
20+
public string Reference { get; set; }
21+
public string URLCompleted { get; set; }
22+
public string URLError { get; set; }
23+
}
24+
25+
public class CheckoutResponse : Base
26+
{
27+
public int Amount { get; set; }
28+
public string Country { get; set; }
29+
public string Currency { get; set; }
30+
public string Description { get; set; }
31+
public string EndUserIP { get; set; }
32+
public string Issuer { get; set; }
33+
public string Language { get; set; }
34+
public string OrderID { get; set; }
35+
public int PaymentID { get; set; }
36+
public string PaymentMethod { get; set; }
37+
public string PaymentScreenURL { get; set; }
38+
public string ProviderTransactionID { get; set; }
39+
public string Reference { get; set; }
40+
public string TestMode { get; set; }
41+
public string URLCompleted { get; set; }
42+
public string URLError { get; set; }
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Payment
8+
{
9+
public class GetMyPaymentMethodsRequest : Base { }
10+
11+
public class GetMyPaymentMethodsResponse : Base
12+
{
13+
public PaymentMethod[] PaymentMethods { get; set; }
14+
}
15+
16+
public class PaymentMethod
17+
{
18+
public string PaymentMethodCode { get; set; }
19+
public string Description { get; set; }
20+
public Issuer[] Issuers { get; set; }
21+
}
22+
23+
public class Issuer
24+
{
25+
public string IssuerKeyword { get; set; }
26+
public string Description { get; set; }
27+
public Country[] Countries { get; set; }
28+
}
29+
30+
public class Country
31+
{
32+
public string CountryCode { get; set; }
33+
public string Currency { get; set; }
34+
public int MinimumAmount { get; set; }
35+
public int MaximumAmount { get; set; }
36+
}
37+
}

Classes/Payment/GetPayment.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Payment
8+
{
9+
public class GetPaymentRequest :Base
10+
{
11+
public int PaymentID { get; set; }
12+
}
13+
14+
public class GetPaymentResponse : Base
15+
{
16+
public int PaymentID { get; set; }
17+
public int Amount { get; set; }
18+
public string Currency { get; set; }
19+
public string Description { get; set; }
20+
public string Duration { get; set; }
21+
public string ConsumerName { get; set; }
22+
public string ConsumerAccountNumber { get; set; }
23+
public string ConsumerAddress { get; set; }
24+
public string ConsumerHouseNumber { get; set; }
25+
public string ConsumerCity { get; set; }
26+
public string ConsumerCountry { get; set; }
27+
public string ConsumerEmail { get; set; }
28+
public string ConsumerPhoneNumber { get; set; }
29+
public string ConsumerIPAddress { get; set; }
30+
public string Issuer { get; set; }
31+
public string OrderID { get; set; }
32+
public string OrderTime { get; set; }
33+
public string PaymentMethod { get; set; }
34+
public string PaymentTime { get; set; }
35+
public string Reference { get; set; }
36+
public string Status { get; set; }
37+
public string StatusCode { get; set; }
38+
public string TestMode { get; set; }
39+
}
40+
}

Classes/Payment/VaultCheckout.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Payment
8+
{
9+
public class VaultCheckoutRequest:CheckoutRequest
10+
{
11+
public string ConsumerID { get; set; }
12+
}
13+
14+
public class VaultCheckoutResponse : CheckoutResponse { }
15+
}

Classes/Refund/CancelRefund.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Refund
8+
{
9+
public class CancelRefundRequest : Base
10+
{
11+
public int RefundID { get; set; }
12+
public int PaymentID { get; set; }
13+
}
14+
15+
public class CancelRefundResponse : Base { }
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Refund
8+
{
9+
public class GetPaymentRefundsRequest : Base
10+
{
11+
public int PaymentID { get; set; }
12+
}
13+
14+
public class GetPaymentRefundsResponse : Base
15+
{
16+
public Refund[] Refunds { get; set; }
17+
}
18+
19+
public class Refund
20+
{
21+
public int RefundID { get; set; }
22+
public int RefundAmount { get; set; }
23+
public string RefundCurrency { get; set; }
24+
public string DateCreated { get; set; }
25+
public string Status { get; set; }
26+
}
27+
}

Classes/Refund/RequestRefund.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IcepayRestClient.Classes.Refund
8+
{
9+
public class RequestRefundRequest : Base
10+
{
11+
public int PaymentID { get; set; }
12+
public int RefundAmount { get; set; }
13+
public string RefundCurrency { get; set; }
14+
}
15+
16+
public class RequestRefundResponse : Base
17+
{
18+
public int RefundID { get; set; }
19+
public int PaymentID { get; set; }
20+
public int RefundAmount { get; set; }
21+
public int RemainingRefundAmount { get; set; }
22+
public string RefundCurrency { get; set; }
23+
}
24+
}

Classes/RestClient.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.IO;
4+
using System.Net;
5+
using System.Security.Cryptography;
6+
using System.Text;
7+
8+
namespace IcepayRestClient.Classes
9+
{
10+
public class RestClient
11+
{
12+
protected const string BaseUrl = "https://connect.icepay.com/webservice/api/v1/";
13+
14+
public static TResponse SendAndReceive<TRequest, TResponse>(string service, string operation, TRequest request, int merchantID, string merchantSecret)
15+
where TRequest : Base, new()
16+
where TResponse : Base, new()
17+
{
18+
//make full URL
19+
var url = BaseUrl + service + "/" + operation;
20+
21+
//serialize JSON without any whitespace
22+
var jsonSerializerSettings = new JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.None, NullValueHandling = NullValueHandling.Ignore, Culture = System.Globalization.CultureInfo.InvariantCulture };
23+
var rawJson = JsonConvert.SerializeObject(request, Formatting.None, jsonSerializerSettings);
24+
25+
//calculate checksum
26+
var signString = url + "POST" + merchantID.ToString() + merchantSecret + rawJson;
27+
var checksum = Sha256(signString);
28+
29+
//initiate request
30+
var webrequest = HttpWebRequest.CreateHttp(url);
31+
webrequest.Method = "POST";
32+
webrequest.ContentType = "application/json";
33+
//add merchant ID and checksum to headers
34+
webrequest.Headers.Add("MerchantID", merchantID.ToString());
35+
webrequest.Headers.Add("Checksum", checksum);
36+
37+
//send request
38+
var requestStream = webrequest.GetRequestStream();
39+
var writer = new StreamWriter(requestStream);
40+
writer.Write(rawJson);
41+
writer.Flush();
42+
43+
//receive response
44+
TResponse response = null;
45+
string rawResponse = string.Empty;
46+
WebResponse webresponse = null;
47+
try
48+
{
49+
webresponse = webrequest.GetResponse();
50+
}
51+
catch (WebException ex)
52+
{
53+
webresponse = ex.Response;
54+
}
55+
var responseStream = webresponse.GetResponseStream();
56+
var reader = new StreamReader(responseStream);
57+
rawResponse = reader.ReadToEnd();
58+
59+
response = JsonConvert.DeserializeObject<TResponse>(rawResponse);
60+
61+
//verify response checksum
62+
if (!string.IsNullOrWhiteSpace(webresponse.Headers["Checksum"]))
63+
{
64+
var responseChecksum = webresponse.Headers["Checksum"];
65+
var responseSignString = webresponse.ResponseUri.AbsoluteUri + "POST" + merchantID.ToString() + merchantSecret + rawResponse;
66+
var responseVerificationChecksum = Sha256(responseSignString);
67+
if (!responseChecksum.Equals(responseVerificationChecksum, System.StringComparison.InvariantCultureIgnoreCase))
68+
{
69+
response = new TResponse { Message = "Response signature invalid." };
70+
}
71+
}
72+
73+
//close streams
74+
writer.Dispose();
75+
reader.Dispose();
76+
webresponse.Dispose();
77+
78+
return response;
79+
}
80+
81+
protected static string Sha256(string signString)
82+
{
83+
byte[] hash;
84+
var sha2 = new SHA256Managed();
85+
hash = sha2.ComputeHash(System.Text.Encoding.UTF8.GetBytes(signString));
86+
87+
StringBuilder sb = new StringBuilder(40);
88+
foreach (byte b in hash)
89+
{
90+
sb.AppendFormat("{0:x2}", b);
91+
}
92+
return sb.ToString();
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)