-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtransact-example.cs
72 lines (64 loc) · 2.03 KB
/
transact-example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Text;
using System.Net;
using System.IO;
namespace CornerstoneTransactExample
{
class Program
{
static void Main()
{
string url = "https://api.cornerstone.cc/v1/transactions";
string user = "sandbox_3xSOjtxSvICXVOKYqbwI";
string key = "key_RdutJGqI50YIwjehGtHBOe1Uu";
// you will probably want to create this xml
// string using a library or class
var postData =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<request>" +
"<amount>15</amount>" +
"<card>" +
"<number>4444333322221111</number>" +
"<expmonth>12</expmonth>" +
"<expyear>23</expyear>" +
"<cvv>999</cvv>" +
"</card>" +
"<customer>" +
"<firstname>Robert</firstname>" +
"<lastname>Parr</lastname>" +
"<email>[email protected]</email>" +
"</customer>" +
"</request>";
var data = Encoding.ASCII.GetBytes(postData);
string authInfo = user + ":" + key;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";
request.ContentLength = data.Length;
request.Headers["Authorization"] = "Basic " + authInfo;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = null;
try {
response = (HttpWebResponse)request.GetResponse();
} catch (WebException e) {
if (e.Status == WebExceptionStatus.ProtocolError) {
response = (HttpWebResponse)e.Response;
Console.Write("Errorcode: {0}\n", (int)response.StatusCode);
} else {
Console.Write("Error: {0}\n", e.Status);
}
} finally {
if (response != null) {
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString);
response.Close();
}
}
}
}
}