-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathExample.java
88 lines (74 loc) · 3.05 KB
/
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
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
package com.shippo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.shippo.exception.ShippoException;
import com.shippo.model.Rate;
import com.shippo.model.Shipment;
import com.shippo.model.Transaction;
public class Example {
public static void main(String[] args) throws ShippoException {
// replace with your Shippo Token
// don't have one? get more info here (https://goshippo.com/docs/#overview)
Shippo.setApiKey("<API-KEY>");
Shippo.setApiVersion("2018-02-08");
// Optional defaults to false
// Shippo.setDEBUG(true);
// to address
Map<String, Object> toAddressMap = new HashMap<String, Object>();
toAddressMap.put("name", "Mr Hippo");
toAddressMap.put("company", "Shippo");
toAddressMap.put("street1", "215 Clayton St.");
toAddressMap.put("city", "San Francisco");
toAddressMap.put("state", "CA");
toAddressMap.put("zip", "94117");
toAddressMap.put("country", "US");
toAddressMap.put("phone", "+1 555 341 9393");
toAddressMap.put("email", "mrhippo@goshipppo.com");
// from address
Map<String, Object> fromAddressMap = new HashMap<String, Object>();
fromAddressMap.put("name", "Ms Hippo");
fromAddressMap.put("company", "San Diego Zoo");
fromAddressMap.put("street1", "2920 Zoo Drive");
fromAddressMap.put("city", "San Diego");
fromAddressMap.put("state", "CA");
fromAddressMap.put("zip", "92101");
fromAddressMap.put("country", "US");
fromAddressMap.put("email", "mshippo@goshipppo.com");
fromAddressMap.put("phone", "+1 619 231 1515");
fromAddressMap.put("metadata", "Customer ID 123456");
// parcel
Map<String, Object> parcelMap = new HashMap<String, Object>();
parcelMap.put("length", "5");
parcelMap.put("width", "5");
parcelMap.put("height", "5");
parcelMap.put("distance_unit", "in");
parcelMap.put("weight", "2");
parcelMap.put("mass_unit", "lb");
List<Map<String, Object>> parcels = new ArrayList<Map<String, Object>>();
parcels.add(parcelMap);
Map<String, Object> shipmentMap = new HashMap<String, Object>();
shipmentMap.put("address_to", toAddressMap);
shipmentMap.put("address_from", fromAddressMap);
shipmentMap.put("parcels", parcels);
shipmentMap.put("async", false);
Shipment shipment = Shipment.create(shipmentMap);
// select shipping rate according to your business logic
// we select the first rate in this example
List<Rate> rates = shipment.getRates();
Rate rate = rates.get(0);
System.out.println("Getting shipping label..");
Map<String, Object> transParams = new HashMap<String, Object>();
transParams.put("rate", rate.getObjectId());
transParams.put("async", false);
Transaction transaction = Transaction.create(transParams);
if (transaction.getStatus().equals("SUCCESS")) {
System.out.println(String.format("Label url : %s", transaction.getLabelUrl()));
System.out.println(String.format("Tracking number : %s", transaction.getTrackingNumber()));
} else {
System.out.println(String.format("An Error has occured while generating you label. Messages : %s",
transaction.getMessages()));
}
}
}