| 12589 |
manish.sha |
1 |
package in.shop2020.serving.services;
|
|
|
2 |
|
|
|
3 |
import java.io.BufferedReader;
|
| 13243 |
manish.sha |
4 |
import java.io.BufferedWriter;
|
| 12596 |
manish.sha |
5 |
import java.io.File;
|
| 13243 |
manish.sha |
6 |
import java.io.FileWriter;
|
| 12589 |
manish.sha |
7 |
import java.io.IOException;
|
|
|
8 |
import java.io.InputStreamReader;
|
| 13243 |
manish.sha |
9 |
import java.text.SimpleDateFormat;
|
| 12589 |
manish.sha |
10 |
import java.util.ArrayList;
|
| 12596 |
manish.sha |
11 |
import java.util.Calendar;
|
| 13243 |
manish.sha |
12 |
import java.util.Date;
|
| 12596 |
manish.sha |
13 |
import java.util.GregorianCalendar;
|
| 12589 |
manish.sha |
14 |
import java.util.List;
|
| 13189 |
manish.sha |
15 |
import java.util.concurrent.TimeUnit;
|
| 12589 |
manish.sha |
16 |
|
|
|
17 |
import in.shop2020.model.v1.order.FlipkartOrder;
|
|
|
18 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
19 |
|
|
|
20 |
import in.shop2020.serving.model.Order;
|
|
|
21 |
import in.shop2020.serving.model.OrderItems;
|
|
|
22 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 12596 |
manish.sha |
23 |
import in.shop2020.utils.GmailUtils;
|
| 12589 |
manish.sha |
24 |
|
| 13243 |
manish.sha |
25 |
import org.apache.commons.lang.StringUtils;
|
| 12589 |
manish.sha |
26 |
import org.apache.http.HttpResponse;
|
|
|
27 |
import org.apache.http.NameValuePair;
|
|
|
28 |
import org.apache.http.client.HttpClient;
|
|
|
29 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
30 |
import org.apache.http.client.methods.HttpGet;
|
|
|
31 |
import org.apache.http.client.methods.HttpPost;
|
|
|
32 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
33 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
34 |
import org.apache.thrift.TException;
|
|
|
35 |
import org.apache.thrift.transport.TTransportException;
|
|
|
36 |
import org.json.JSONArray;
|
|
|
37 |
import org.json.JSONException;
|
|
|
38 |
import org.json.JSONObject;
|
|
|
39 |
|
|
|
40 |
import com.google.gson.Gson;
|
|
|
41 |
|
|
|
42 |
public class FlipkartHoldOrdersReconciliation{
|
| 12605 |
manish.sha |
43 |
private static String sendTo[] = new String[]{ "sandeep.sachdeva@shop2020.in", "eng@shop2020.in" ,
|
| 12596 |
manish.sha |
44 |
"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
|
| 12605 |
manish.sha |
45 |
"yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in"};
|
| 13196 |
manish.sha |
46 |
//private static String sendTo1[] = new String[]{"manish.sharma@shop2020.in"};
|
| 13243 |
manish.sha |
47 |
private static java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| 12596 |
manish.sha |
48 |
private static Calendar cal=GregorianCalendar.getInstance();
|
| 12605 |
manish.sha |
49 |
private static String emailFromAddress = "build-staging@shop2020.in";
|
|
|
50 |
private static String password = "shop2020";
|
| 12596 |
manish.sha |
51 |
private static GmailUtils mailer = new GmailUtils();
|
| 13667 |
manish.sha |
52 |
private static String cookies;
|
| 12596 |
manish.sha |
53 |
|
| 13243 |
manish.sha |
54 |
public static void main(String[] args) throws IOException{
|
|
|
55 |
File file = new File("/home/manish/alerts/fkhold.txt");
|
|
|
56 |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
|
|
|
57 |
|
| 12589 |
manish.sha |
58 |
HttpClient client = new DefaultHttpClient();
|
|
|
59 |
HttpPost post = new HttpPost("https://seller.flipkart.com/login");
|
|
|
60 |
BufferedReader rd= null;
|
|
|
61 |
try {
|
| 13667 |
manish.sha |
62 |
HttpGet get_new;
|
|
|
63 |
get_new = new HttpGet("https://seller.flipkart.com/");
|
|
|
64 |
get_new.addHeader("Host","seller.flipkart.com");
|
|
|
65 |
get_new.addHeader("User-agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");
|
|
|
66 |
get_new.addHeader("Connection","keep-alive");
|
|
|
67 |
|
|
|
68 |
HttpResponse response = client.execute(get_new);
|
|
|
69 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
70 |
String line = "";
|
|
|
71 |
while ((line = rd.readLine()) != null) {
|
|
|
72 |
System.out.println(line);
|
|
|
73 |
}
|
|
|
74 |
cookies = response.getFirstHeader("Set-Cookie") == null ? "" :
|
|
|
75 |
response.getFirstHeader("Set-Cookie").getValue();
|
|
|
76 |
|
|
|
77 |
System.out.println("Cookies Before Login "+ cookies);
|
|
|
78 |
|
| 12589 |
manish.sha |
79 |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
|
|
80 |
nameValuePairs.add(new BasicNameValuePair("authName",
|
|
|
81 |
"flipkart"));
|
|
|
82 |
nameValuePairs.add(new BasicNameValuePair("username",
|
|
|
83 |
"flipkart-support@saholic.com"));
|
|
|
84 |
nameValuePairs.add(new BasicNameValuePair("password",
|
| 13181 |
manish.sha |
85 |
"bestmobiledeals2010"));
|
| 13667 |
manish.sha |
86 |
post.addHeader("Cookie",cookies);
|
| 12756 |
manish.sha |
87 |
post.addHeader("User-agent", "Mozilla/4.0");
|
|
|
88 |
post.addHeader("Referer", "seller.flipkart.com");
|
| 12589 |
manish.sha |
89 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
|
| 13667 |
manish.sha |
90 |
response = client.execute(post);
|
|
|
91 |
cookies = response.getFirstHeader("Set-Cookie") == null ? "" :
|
|
|
92 |
response.getFirstHeader("Set-Cookie").getValue();
|
|
|
93 |
System.out.println("Cookies After Login "+cookies);
|
| 12589 |
manish.sha |
94 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
| 13667 |
manish.sha |
95 |
line = "";
|
| 12589 |
manish.sha |
96 |
|
| 12596 |
manish.sha |
97 |
List<FlipkartOrder> pendingOrderList = new ArrayList<FlipkartOrder>();
|
|
|
98 |
List<FlipkartOrder> acceptedOrderList = new ArrayList<FlipkartOrder>();
|
|
|
99 |
List<FlipkartOrder> cancelledOrderList = new ArrayList<FlipkartOrder>();
|
|
|
100 |
List<FlipkartOrder> errorOrderList = new ArrayList<FlipkartOrder>();
|
|
|
101 |
|
| 12589 |
manish.sha |
102 |
while ((line = rd.readLine()) != null) {
|
|
|
103 |
System.out.println(line);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
//https://seller.flipkart.com/order_management/search_order_items?order_id=OD40827062297&sellerId=m2z93iskuj81qiid
|
|
|
107 |
TransactionClient transactionServiceClient = new TransactionClient();
|
|
|
108 |
in.shop2020.model.v1.order.TransactionService.Client tClient = transactionServiceClient.getClient();
|
|
|
109 |
List<FlipkartOrder> pendingFkOrdersList = tClient.getVerificationPendingOrdersFK();
|
|
|
110 |
StringBuffer sb = new StringBuffer();
|
| 12596 |
manish.sha |
111 |
StringBuffer resB = new StringBuffer();
|
| 12589 |
manish.sha |
112 |
for(FlipkartOrder fkOrder : pendingFkOrdersList){
|
|
|
113 |
get_new = new HttpGet("https://seller.flipkart.com/order_management/search_order_items?order_id="+fkOrder.getFlipkartOrderId()+"&sellerId=m2z93iskuj81qiid");
|
| 13667 |
manish.sha |
114 |
get_new.addHeader("Cookie",cookies+"; __utmt=1; __utma=143439159.1675929865.1422546011.1422546011.1422546011.1; __utmb=143439159.1.10.1422546011; __utmc=143439159; __utmz=143439159.1422546011.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); is_login=true; sellerId=m2z93iskuj81qiid");
|
|
|
115 |
get_new.addHeader("Host","seller.flipkart.com");
|
|
|
116 |
get_new.addHeader("User-agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");
|
|
|
117 |
get_new.addHeader("Connection","keep-alive");
|
| 12589 |
manish.sha |
118 |
response = client.execute(get_new);
|
|
|
119 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
120 |
line = "";
|
|
|
121 |
line = rd.readLine();
|
| 13194 |
manish.sha |
122 |
System.out.println("Response... "+line);
|
| 12589 |
manish.sha |
123 |
JSONObject jsonDataObj = null;
|
|
|
124 |
JSONArray jsonObjArr = null;
|
|
|
125 |
if(line == null || line.isEmpty()){
|
|
|
126 |
sb.append("Flipkart Order Id -"+ fkOrder.getFlipkartOrderId()+" Response - "+line);
|
|
|
127 |
sb.append("\n");
|
|
|
128 |
continue;
|
|
|
129 |
} else if (!line.startsWith("{")){
|
|
|
130 |
sb.append("Flipkart Order Id -"+ fkOrder.getFlipkartOrderId()+" Response - "+line);
|
|
|
131 |
sb.append("\n");
|
|
|
132 |
continue;
|
|
|
133 |
}
|
|
|
134 |
else{
|
|
|
135 |
jsonDataObj = new JSONObject(line);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
if(jsonDataObj!=null){
|
|
|
139 |
jsonObjArr = new JSONArray(jsonDataObj.get("items").toString());
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
if(jsonObjArr!=null && jsonObjArr.length()>0){
|
|
|
143 |
JSONObject jsonObj = null;
|
|
|
144 |
jsonObj = jsonObjArr.getJSONObject(0);
|
|
|
145 |
if(jsonObj!=null){
|
|
|
146 |
if(jsonObj.get("status")!=null){
|
|
|
147 |
String status = jsonObj.get("status").toString();
|
| 12607 |
manish.sha |
148 |
String statusLabel = jsonObj.get("statusLabel").toString();
|
|
|
149 |
if("on_hold".equalsIgnoreCase(status) || "On hold".equalsIgnoreCase(statusLabel)){
|
| 12596 |
manish.sha |
150 |
pendingOrderList.add(fkOrder);
|
| 12589 |
manish.sha |
151 |
}
|
| 12607 |
manish.sha |
152 |
if("approved".equalsIgnoreCase(status) || "Approved".equalsIgnoreCase(statusLabel) || "confirmed".equalsIgnoreCase(statusLabel)){
|
| 12589 |
manish.sha |
153 |
if(tClient ==null || !tClient.isAlive()){
|
|
|
154 |
tClient = transactionServiceClient.getClient();
|
|
|
155 |
}
|
|
|
156 |
tClient.verifyOrder(fkOrder.getOrderId());
|
|
|
157 |
tClient.acceptOrder(fkOrder.getOrderId());
|
| 12596 |
manish.sha |
158 |
acceptedOrderList.add(fkOrder);
|
| 12589 |
manish.sha |
159 |
}
|
| 12607 |
manish.sha |
160 |
if("cancelled".equalsIgnoreCase(status) || "Cancelled".equalsIgnoreCase(statusLabel)){
|
| 12589 |
manish.sha |
161 |
if(tClient ==null || !tClient.isAlive()){
|
|
|
162 |
tClient = transactionServiceClient.getClient();
|
|
|
163 |
}
|
|
|
164 |
tClient.refundOrder(fkOrder.getOrderId(), "flipkart", "As per Buyer's Request");
|
| 12596 |
manish.sha |
165 |
cancelledOrderList.add(fkOrder);
|
| 12589 |
manish.sha |
166 |
}
|
|
|
167 |
}else{
|
| 12596 |
manish.sha |
168 |
errorOrderList.add(fkOrder);
|
| 12589 |
manish.sha |
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
| 13191 |
manish.sha |
172 |
Thread.sleep(3000);
|
| 12589 |
manish.sha |
173 |
}
|
|
|
174 |
|
| 12596 |
manish.sha |
175 |
resB.append("Still Pending Orders:-\n");
|
| 12605 |
manish.sha |
176 |
System.out.println("Still Pending Orders:-");
|
| 12596 |
manish.sha |
177 |
for(FlipkartOrder order: pendingOrderList){
|
|
|
178 |
resB.append(order.getOrderId()+" "+ order.getFlipkartOrderId()+"\n");
|
| 12605 |
manish.sha |
179 |
System.out.println(order.getOrderId()+" "+ order.getFlipkartOrderId());
|
| 12596 |
manish.sha |
180 |
}
|
|
|
181 |
resB.append("Accepeted Orders:-\n");
|
| 12605 |
manish.sha |
182 |
System.out.println("Accepeted Orders:-");
|
| 12596 |
manish.sha |
183 |
for(FlipkartOrder order: acceptedOrderList){
|
|
|
184 |
resB.append(order.getOrderId()+" "+ order.getFlipkartOrderId()+"\n");
|
| 12605 |
manish.sha |
185 |
System.out.println(order.getOrderId()+" "+ order.getFlipkartOrderId());
|
| 12596 |
manish.sha |
186 |
}
|
|
|
187 |
resB.append("Cancelled Orders:-\n");
|
| 12605 |
manish.sha |
188 |
System.out.println("Cancelled Orders:-");
|
| 12596 |
manish.sha |
189 |
for(FlipkartOrder order: cancelledOrderList){
|
|
|
190 |
resB.append(order.getOrderId()+" "+ order.getFlipkartOrderId()+"\n");
|
| 12605 |
manish.sha |
191 |
System.out.println(order.getOrderId()+" "+ order.getFlipkartOrderId());
|
| 12596 |
manish.sha |
192 |
}
|
|
|
193 |
resB.append("Response not found Orders:-\n");
|
| 12605 |
manish.sha |
194 |
System.out.println("Response not found Orders:-");
|
| 12596 |
manish.sha |
195 |
for(FlipkartOrder order: errorOrderList){
|
|
|
196 |
resB.append(order.getOrderId()+" "+ order.getFlipkartOrderId()+"\n");
|
| 12605 |
manish.sha |
197 |
System.out.println(order.getOrderId()+" "+ order.getFlipkartOrderId());
|
| 12596 |
manish.sha |
198 |
}
|
|
|
199 |
String emailSubjectTxt = " Flipkart Hold Order Reconcilaition Process "+sdf.format(cal.getTime());
|
|
|
200 |
mailer.sendSSLMessage(sendTo, emailSubjectTxt, resB.toString(), emailFromAddress, password, new ArrayList<File>());
|
| 12589 |
manish.sha |
201 |
System.out.println(sb.toString());
|
| 13243 |
manish.sha |
202 |
|
|
|
203 |
bufferedWriter.write(StringUtils.join(new String[] {"FlipkartHoldUpdationDate",sdf.format(new Date())}, '\t'));
|
|
|
204 |
bufferedWriter.close();
|
| 12596 |
manish.sha |
205 |
} catch (Exception e) {
|
| 12589 |
manish.sha |
206 |
e.printStackTrace();
|
| 13182 |
manish.sha |
207 |
try{
|
| 13196 |
manish.sha |
208 |
mailer.sendSSLMessage(sendTo, "Error in Hold Order Recon Process", "Please inform Engineering Team", emailFromAddress, password, new ArrayList<File>());
|
| 13182 |
manish.sha |
209 |
}catch (Exception e1) {
|
|
|
210 |
e1.printStackTrace();
|
|
|
211 |
}
|
| 12589 |
manish.sha |
212 |
}
|
|
|
213 |
}
|
|
|
214 |
}
|