-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtransact-example.java
62 lines (51 loc) · 1.8 KB
/
transact-example.java
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
import java.io.*;
import java.net.*;
import java.util.Base64;
class CornerstoneTransactionExample {
public static void main(String[] args) {
String url = "https://api.cornerstone.cc/v1/transactions";
String user = "sandbox_3xSOjtxSvICXVOKYqbwI";
String key = "key_RdutJGqI50YIwjehGtHBOe1Uu";
String result = "";
// you will likely want to use something like jaxb or xstream
// to create xml and possibly write it to the stream
String request =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<request>" +
"<amount>15</amount>" +
"<card>" +
"<number>4444333322221111</number>" +
"<expmonth>12</expmonth>" +
"<expyear>23</expyear>" +
"</card>" +
"<customer>" +
"<firstname>Robert</firstname>" +
"<lastname>Parr</lastname>" +
"<email>[email protected]</email>" +
"</customer>" +
"</request>";
try {
HttpURLConnection con = (HttpURLConnection) (new URL(url)).openConnection();
String auth = Base64.getEncoder().encodeToString((user+":"+key).getBytes("UTF-8"));
con.setRequestProperty("User-Agent", user);
con.setRequestProperty("Authorization", "Basic " + auth);
con.setRequestProperty("Accept", "application/xml");
con.setRequestProperty("Content-Type", "application/xml");
con.setRequestMethod("POST");
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(request);
out.flush();
out.close();
InputStream stream = con.getResponseCode() < 400 ? con.getInputStream() : con.getErrorStream();
BufferedReader buf = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = buf.readLine()) != null) {
result += line + "\n";
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}