This repository has been archived by the owner on Jun 19, 2019. It is now read-only.
/
Create.java
144 lines (125 loc) · 4.68 KB
/
Create.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package examples;
import com.klarna.checkout.Connector;
import com.klarna.checkout.ErrorResponseException;
import com.klarna.checkout.IConnector;
import com.klarna.checkout.Order;
import org.json.simple.JSONObject;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* The create checkout example.
*/
final class Create {
/**
* Empty constructor.
*/
private Create() {
}
/**
* Runs the example code.
*
* @param args Command line arguments
* @throws URISyntaxException If URIs are incorrect
* @throws NoSuchAlgorithmException If connector couldn't be created
* @throws IOException If api call failed
*/
public static void main(final String[] args)
throws URISyntaxException, NoSuchAlgorithmException, IOException {
final String eid = "0";
final String secret = "sharedSecret";
IConnector connector = Connector.create(
secret, IConnector.TEST_BASE_URL);
Order order = new Order(connector);
// Start a new session.
final Map<String, Object> merchant;
merchant = new HashMap<String, Object>() {
{
put("id", eid);
put("terms_uri", "http://example.com/terms.html");
put("checkout_uri", "http://example.com/checkout.jsp");
put("confirmation_uri",
"http://example.com/thank-you.jsp"
+ "?klarna_order_id={checkout.order.id}");
// You can not receive push notification on a
// non-publicly available uri.
put("push_uri",
"http://example.com/push.jsp"
+ "?klarna_order_id={checkout.order.id}");
}
};
final Map<String, Object> cart = new HashMap<String, Object>() {
{
put("items", new ArrayList<HashMap<String, Object>>() {
{
add(new HashMap<String, Object>() {
{
put("quantity", 2);
put("reference", "123456789");
put("name", "Klarna t-shirt");
put("unit_price", 12300);
put("discount_rate", 1000);
put("tax_rate", 2500);
}
});
add(new HashMap<String, Object>() {
{
put("quantity", 1);
put("type", "shipping_fee");
put("reference", "SHIPPING");
put("name", "Shipping Fee");
put("unit_price", 4900);
put("tax_rate", 2500);
}
});
}
});
}
};
// Add gui settings
final Map<String, Object> gui = new HashMap<String, Object>() {
{
put("layout", "desktop");
} // or mobile
};
Map<String, Object> data = new HashMap<String, Object>() {
{
put("purchase_country", "SE");
put("purchase_currency", "SEK");
put("locale", "sv-se");
put("merchant", merchant);
put("cart", cart);
put("gui", gui);
}
};
//data.put("recurring", true);
try {
order.create(data);
order.fetch();
String orderID = (String) order.get("id");
System.out.println(orderID);
} catch (ErrorResponseException e) {
JSONObject json = e.getJson();
System.out.println(json.get("http_status_message"));
System.out.println(json.get("internal_message"));
}
}
}