| 7473 |
vikram.rag |
1 |
package com.amazonservices.mws.orders.samples;
|
|
|
2 |
|
|
|
3 |
import java.text.SimpleDateFormat;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.Calendar;
|
|
|
6 |
import java.util.Date;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Random;
|
|
|
9 |
|
|
|
10 |
import org.apache.thrift.TException;
|
|
|
11 |
import org.apache.thrift.transport.TTransportException;
|
|
|
12 |
import in.shop2020.logistics.PickUpType;
|
|
|
13 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
14 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
15 |
import in.shop2020.model.v1.order.Order;
|
|
|
16 |
import in.shop2020.model.v1.order.OrderSource;
|
|
|
17 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
18 |
import in.shop2020.model.v1.order.OrderType;
|
|
|
19 |
import in.shop2020.model.v1.order.SourceDetail;
|
|
|
20 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
21 |
import in.shop2020.model.v1.order.TransactionStatus;
|
|
|
22 |
import in.shop2020.model.v1.user.User;
|
|
|
23 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
24 |
import in.shop2020.payments.PaymentStatus;
|
|
|
25 |
import in.shop2020.serving.model.ShipmentUpdate;
|
|
|
26 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
27 |
import in.shop2020.thrift.clients.HelperClient;
|
|
|
28 |
import in.shop2020.thrift.clients.PaymentClient;
|
|
|
29 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
30 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
31 |
|
|
|
32 |
public class AmazonOrder {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
*
|
|
|
36 |
*/
|
|
|
37 |
private static final long serialVersionUID = 1L;
|
|
|
38 |
private String tempUserEmail;
|
|
|
39 |
private List<Order> orders = new ArrayList<Order>();
|
|
|
40 |
|
|
|
41 |
public long persistTransaction(com.amazonservices.mws.orders.model.Order amazonitem,List<com.amazonservices.mws.orders.model.OrderItem> amazonItems) {
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* 0.Create user
|
|
|
45 |
* 1.Make a transaction object comprised of Order which in turn contains lineitem.
|
|
|
46 |
* 2.Call create_transaction
|
|
|
47 |
* 3.retrieve that order
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
Transaction txnObj = new Transaction();
|
|
|
51 |
|
|
|
52 |
long tempUserId = -1;
|
|
|
53 |
|
|
|
54 |
try {
|
|
|
55 |
TransactionClient tcl = new TransactionClient();
|
|
|
56 |
//Source Id for store website is 2 and our normal website is 1
|
| 7510 |
kshitij.so |
57 |
SourceDetail detail = tcl.getClient().getSourceDetail(3);
|
| 7473 |
vikram.rag |
58 |
tempUserEmail=detail.getEmail();
|
|
|
59 |
try {
|
|
|
60 |
UserClient ucl = new UserClient();
|
|
|
61 |
User user = ucl.getClient().getUserByEmail(tempUserEmail);
|
|
|
62 |
tempUserId = user.getUserId();
|
|
|
63 |
} catch (Exception e) {
|
|
|
64 |
//////////////log.error("Unable to get user for id " + tempUserId, e);
|
|
|
65 |
}
|
|
|
66 |
} catch(Exception e) {
|
|
|
67 |
//////////////log.error("Unable to get default source", e);
|
|
|
68 |
}
|
|
|
69 |
txnObj.setShoppingCartid(0);
|
|
|
70 |
txnObj.setCustomer_id(tempUserId);
|
|
|
71 |
txnObj.setCreatedOn(Calendar.getInstance().getTimeInMillis());
|
|
|
72 |
txnObj.setTransactionStatus(TransactionStatus.INIT);
|
|
|
73 |
txnObj.setStatusDescription("New Amazon Order");
|
|
|
74 |
txnObj.setCoupon_code("");
|
|
|
75 |
txnObj.setEmiSchemeId(0);
|
|
|
76 |
|
|
|
77 |
//if(createOrder(tempUserId) == null) {
|
|
|
78 |
//////////////log.error("Unable to create transaction");
|
|
|
79 |
//setErrorMsg("Unable to create order. Please try again.");
|
|
|
80 |
// return "error-result";
|
|
|
81 |
//}
|
|
|
82 |
for(com.amazonservices.mws.orders.model.OrderItem amazonItemlist: amazonItems){
|
|
|
83 |
int netQuantity = amazonItemlist.getQuantityOrdered() - amazonItemlist.getQuantityShipped();
|
|
|
84 |
for (int i=0;i<netQuantity;i++){
|
|
|
85 |
orders.add(createOrder(tempUserEmail,tempUserId,amazonItemlist,amazonitem));
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
txnObj.setOrders(orders);
|
|
|
90 |
TransactionClient tcl;
|
|
|
91 |
long txnId = 0;
|
|
|
92 |
try {
|
|
|
93 |
tcl = new TransactionClient();
|
|
|
94 |
txnId = tcl.getClient().createTransaction(txnObj);
|
|
|
95 |
System.out.println("Transaction ID is : " + txnId);
|
|
|
96 |
return txnId;
|
|
|
97 |
} catch (Exception e) {
|
|
|
98 |
//////////////log.error("Unable to create transaction", e);
|
|
|
99 |
//setErrorMsg("Unable to create order. Please try again.");
|
|
|
100 |
System.out.println("Could not Create Transaction ......");
|
|
|
101 |
return -1;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public Order createOrder(String tempUserEmail,long userId,com.amazonservices.mws.orders.model.OrderItem aitem1,com.amazonservices.mws.orders.model.Order aitem) {
|
|
|
106 |
LineItem lineObj = createLineItem(aitem1);
|
|
|
107 |
if (lineObj == null) {
|
|
|
108 |
return null;
|
|
|
109 |
}
|
|
|
110 |
Order order = new Order();
|
|
|
111 |
|
|
|
112 |
order.setCustomer_id(userId);
|
|
|
113 |
order.setCustomer_email(tempUserEmail);
|
|
|
114 |
order.setCustomer_name(aitem.getBuyerName());
|
|
|
115 |
order.setCustomer_address1(aitem.getShippingAddress().getAddressLine1());
|
|
|
116 |
order.setCustomer_address2(aitem.getShippingAddress().getAddressLine2() +" " +aitem.getShippingAddress().getAddressLine3());
|
|
|
117 |
order.setCustomer_city(aitem.getShippingAddress().getCity());
|
|
|
118 |
order.setCustomer_state(aitem.getShippingAddress().getStateOrRegion());
|
|
|
119 |
//order.setCustomer_pincode("177017");
|
|
|
120 |
order.setCustomer_pincode(aitem.getShippingAddress().getPostalCode());
|
|
|
121 |
order.setCustomer_mobilenumber(aitem.getShippingAddress().getPhone());
|
|
|
122 |
order.setTotal_amount(Double.parseDouble(aitem1.getItemPrice().getAmount())/aitem1.getQuantityOrdered());
|
|
|
123 |
order.setStatus(OrderStatus.PAYMENT_PENDING);
|
|
|
124 |
order.setStatusDescription("Initiated");
|
|
|
125 |
order.setCreated_timestamp(Calendar.getInstance().getTimeInMillis());
|
|
|
126 |
order.setTotal_weight(lineObj.getTotal_weight());
|
|
|
127 |
|
|
|
128 |
order.setSource(3);
|
|
|
129 |
order.setAdvanceAmount(0.0);
|
|
|
130 |
|
|
|
131 |
List<LineItem> lines = new ArrayList<LineItem>();
|
|
|
132 |
lines.add(lineObj);
|
|
|
133 |
order.setLineitems(lines);
|
|
|
134 |
return order;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
public in.shop2020.model.v1.order.LineItem createLineItem(com.amazonservices.mws.orders.model.OrderItem amazonOrderItem) {
|
|
|
138 |
LineItem lineitem = new LineItem();
|
|
|
139 |
|
|
|
140 |
Item item = null;
|
|
|
141 |
try {
|
|
|
142 |
CatalogClient ccl = new CatalogClient();
|
|
|
143 |
item = ccl.getClient().getItem(Long.valueOf(amazonOrderItem.getSellerSKU()));
|
|
|
144 |
} catch (Exception e) {
|
|
|
145 |
//////////////log.error("Unable to get the item from catalog service, ItemId : " + product_id, e);
|
|
|
146 |
//setErrorMsg("Unable to create order. Please try again.");
|
|
|
147 |
return null;
|
|
|
148 |
}
|
|
|
149 |
lineitem.setProductGroup(item.getProductGroup());
|
|
|
150 |
lineitem.setBrand(item.getBrand());
|
|
|
151 |
lineitem.setModel_number(item.getModelNumber());
|
|
|
152 |
lineitem.setModel_name(item.getModelName());
|
|
|
153 |
if(item.getColor() == null || item.getColor().isEmpty()) {
|
|
|
154 |
lineitem.setColor("");
|
|
|
155 |
} else {
|
|
|
156 |
lineitem.setColor(item.getColor());
|
|
|
157 |
}
|
|
|
158 |
lineitem.setExtra_info(item.getFeatureDescription());
|
|
|
159 |
lineitem.setItem_id(item.getId());
|
|
|
160 |
lineitem.setQuantity(1.0);
|
|
|
161 |
lineitem.setUnit_price(Double.valueOf(amazonOrderItem.getItemPrice().getAmount()));
|
|
|
162 |
lineitem.setTotal_price(Double.valueOf(amazonOrderItem.getItemPrice().getAmount()));
|
|
|
163 |
lineitem.setUnit_weight(item.getWeight());
|
|
|
164 |
lineitem.setTotal_weight(item.getWeight());
|
|
|
165 |
lineitem.setDealText("");
|
|
|
166 |
|
|
|
167 |
if(item.getWarrantyPeriod() > 0) {
|
|
|
168 |
//Computing Manufacturer Warranty expiry date
|
|
|
169 |
Calendar cal = Calendar.getInstance();
|
|
|
170 |
cal.add(Calendar.MONTH, item.getWarrantyPeriod());
|
|
|
171 |
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
|
|
|
172 |
lineitem.setWarrantry_expiry_timestamp(cal.getTimeInMillis());
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
return lineitem;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
}
|