package com.st.easypost.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.easypost.exception.EasyPostException;
import com.easypost.model.Address;
import com.easypost.model.AddressVerification;
import com.easypost.model.Error;
import com.easypost.model.Parcel;
import com.easypost.model.Rate;
import com.easypost.model.Shipment;
public class ShippingUtil {
public Address getToAddress() {
List<String> verificationList = new ArrayList<>();
verificationList.add("delivery");
Map<String, Object> toAddressHash = new HashMap<String, Object>();
toAddressHash.put("name", "SurekhaTech");
toAddressHash.put("street1", "53 woodward st");
toAddressHash.put("state", "California");
toAddressHash.put("zip", "94103");
toAddressHash.put("country", "US");
toAddressHash.put("verify", verificationList);
Address toAddress = null;
try {
toAddress = Address.create(toAddressHash);
} catch (EasyPostException e) {
e.printStackTrace();
}
addressVerification(toAddress);
return toAddress;
}
public Address getFromAddress() {
List<String> verificationList = new ArrayList<>();
verificationList.add("delivery");
Map<String, Object> fromAddressHash = new HashMap<String, Object>();
fromAddressHash.put("name", "SurekhaTech");
fromAddressHash.put("company", "SurekhaTech");
fromAddressHash.put("street1", "9250 Bendix Rd");
fromAddressHash.put("city", "Columbia");
fromAddressHash.put("state", "MD");
fromAddressHash.put("zip", "21045");
fromAddressHash.put("country", "US");
fromAddressHash.put("verify", verificationList);
Address fromAddress = null;
try {
fromAddress = Address.create(fromAddressHash);
} catch (EasyPostException e) {
e.printStackTrace();
}
addressVerification(fromAddress);
return fromAddress;
}
public Parcel getParcel(String predefinedParcel,float weight) {
Map<String, Object> parcelMap = new HashMap<String, Object>();
parcelMap.put("predefined_package", predefinedParcel);
parcelMap.put("weight", weight);
Parcel parcel = null;
try {
parcel = Parcel.create(parcelMap);
} catch (EasyPostException e) {
e.printStackTrace();
}
return parcel;
}
public Shipment getShipment(Address fromAddress, Address toAddress, Parcel parcel) {
Shipment shipment = null;
try {
Map<String, Object> shipmentMap = new HashMap<String, Object>();
shipmentMap.put("to_address", toAddress);
shipmentMap.put("from_address", fromAddress);
shipmentMap.put("parcel", parcel);
shipment = Shipment.create(shipmentMap);
} catch (EasyPostException e) {
e.printStackTrace();
}
return shipment;
}
public Shipment generateTrackingCodeAndLabel(String shippingId) {
Shipment shipment = null;
try {
shipment = Shipment.retrieve(shippingId);
List<String> buyCarriers = new ArrayList<String>();
buyCarriers.add("USPS");
List<String> buyServices = new ArrayList<String>();
buyServices.add("First");
Rate lowestRate = shipment.lowestRate(buyCarriers, buyServices);
if (shipment.getTrackingCode() == null) {
shipment = shipment.buy(lowestRate);
}
} catch (EasyPostException e) {
e.printStackTrace();
}
return shipment;
}
public List<Error> addressVerification(Address address) {
List<Error> errors = null;
AddressVerification addressVerification = address.getVerifications().get("delivery");
if (addressVerification != null) {
boolean success = addressVerification.getSuccess();
if (!success) {
errors = addressVerification.getErrors();
}
}
return errors;
}
}
|