Rev 507 | Blame | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.apache.thrift.TException;import in.shop2020.logistics.LogisticsInfo;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.model.v1.catalog.InventoryServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.order.BillingInfo;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.OrderInfo;import in.shop2020.model.v1.order.OrderStatus;import in.shop2020.model.v1.order.Payment;import in.shop2020.model.v1.order.PaymentInfo;import in.shop2020.model.v1.order.PaymentStatus;import in.shop2020.model.v1.order.Shipment;import in.shop2020.model.v1.order.ShipmentInfo;import in.shop2020.model.v1.order.Transaction;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.model.v1.order.TransactionStatus;import in.shop2020.model.v1.shoppingcart.Cart;import in.shop2020.model.v1.shoppingcart.Line;import in.shop2020.model.v1.shoppingcart.LineStatus;import in.shop2020.model.v1.shoppingcart.ShoppingCartException;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.UserContext;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.payments.PaymentException;import in.shop2020.thrift.clients.CatalogServiceClient;import in.shop2020.thrift.clients.LogisticsServiceClient;import in.shop2020.thrift.clients.PaymentServiceClient;import in.shop2020.thrift.clients.ShoppingCartClient;import in.shop2020.thrift.clients.TransactionServiceClient;import in.shop2020.thrift.clients.UserContextServiceClient;import in.shop2020.utils.Logger;public class PayResponseController extends BaseController{public static String AMOUNT = "amount";public static String TRACKID = "trackid";public static String TRACKID_CANCELLED = "trackId";public static String RESULT = "result";public static String AUTH = "auth";public static String TRANID = "tranid";public static String PAYMENTID = "paymentId";public static String REF = "ref";public static String POSTDATE = "postdate";public static String CANCELED = "CANCELED";public static String APPROVED = "APPROVED";public static String CAPTURED = "CAPTURED";PaymentServiceClient pclient = null;LogisticsServiceClient lsc = null;ShoppingCartClient cl = null;UserContextServiceClient usc = null;CatalogServiceClient csc = null;TransactionServiceClient tsc = null;String amount;String trackId;String result;String postdate;String auth;String ref;String tranId;String paymentId;long transaction_id = 0;String message = "Unable to process the payment. PLease try Again.";in.shop2020.payments.Payment pmnt = null;public PayResponseController(){super();try {pclient = new PaymentServiceClient();lsc = new LogisticsServiceClient();cl = new ShoppingCartClient();usc = new UserContextServiceClient();csc = new CatalogServiceClient();tsc = new TransactionServiceClient();} catch (Exception e) {Logger.log("Could not initialize the paymentservice client", this);}}public HttpHeaders show() throws IOException, SecurityException{//need to ignore id here.amount = this.request.getParameter(AMOUNT);paymentId = this.request.getParameter(PAYMENTID);tranId = this.request.getParameter(TRANID);result = this.request.getParameter(RESULT);if (result.equalsIgnoreCase(CANCELED)){trackId = this.request.getParameter(TRACKID_CANCELLED);}else{trackId = this.request.getParameter(TRACKID);}auth = this.request.getParameter(AUTH);ref = this.request.getParameter(REF);postdate = this.request.getParameter(POSTDATE);//update the payment info objecttry {pclient.getClient().addBankDetails(Long.parseLong(trackId), paymentId, tranId, "", "", postdate, auth, ref);message = "Payment processed. Creating transaction. In case you see this message, please call us with ref no "+ trackId;Transaction t = getTransaction();transaction_id = tsc.getClient().createTransaction(t);message = "Your order id is "+ transaction_id;cl.getClient().commitCart(pmnt.getCart_id());message = "Error commiting the cart, but your order has been placed. Please refer to "+ transaction_id;} catch (NumberFormatException e) {Logger.log(e.toString(), this);} catch (PaymentException e) {Logger.log(e.toString(), this);} catch (TException e) {Logger.log(e.toString(), this);} catch (TransactionServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ShoppingCartException e) {// TODO Auto-generated catch blocke.printStackTrace();}return new DefaultHttpHeaders("show");}private Transaction getTransaction(){try {pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));} catch (NumberFormatException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (PaymentException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (TException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}Transaction t = new Transaction();PaymentInfo paymentInfo = new PaymentInfo();t.setPaymentInfo(paymentInfo);t.setShoppingCartid(pmnt.getCart_id());t.setCustomer_id(pmnt.getUser_id());t.setCreatedOn(pmnt.getBank_ack_timestamp());t.setTransactionStatus(TransactionStatus.INIT);t.setStatusDescription("New order");//create paymentPayment payment = new Payment();payment.setAmount(Double.parseDouble(amount));payment.setBank_tx_id(tranId);payment.setMerchant_tx_id(trackId);payment.setStatus(PaymentStatus.SUCCESS);payment.setMode("credit card");payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());payment.setSubmissionTimestamp(pmnt.getInit_timestamp());Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();paymentMap.put(payment.getCompletionTimestamp(), payment);paymentInfo.setPayments(paymentMap);t.setOrders(getOrders());return t;}private List<Order> getOrders(){List<Order> orders = new ArrayList<Order>();Cart c = null;try {c = cl.getClient().getCart(pmnt.getCart_id());} catch (ShoppingCartException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}List<Line> lines = c.getLines();for(Line line : lines){if(line.getLineStatus() == LineStatus.LINE_ACTIVE){//line is activeLineItem lineItem = getLineItem(line.getItemId());// create orders equivalent to quantity. Create one order per item.for(int i= 0; i<line.getQuantity(); i++){// set orderOrder order = getOrder(c.getAddressId(), lineItem);order.addToLineitems(lineItem);orders.add(order);}}}return orders;}private Order getOrder(long addressId, LineItem lineItem){String sku_id = lineItem.getSku_id();double total_amount = lineItem.getTotal_price();double total_weight = lineItem.getTotal_weight();long customer_id = pmnt.getUser_id();Address address = getAddress(addressId);String customer_email = getEmail(customer_id);String customer_name = address.getName();String customer_address = getAddressString(address);String customer_mobilenumber = address.getPhone();String customer_pincode = address.getPin();// get logistics informationLogisticsInfo logistics_info = null;try {logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LogisticsServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();}long warehouse_id = logistics_info.getWarehouse_id();long logistics_provider_id= logistics_info.getProvider_id();String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?String tracking_id = airwaybill_no; //right now both are samelong expected_delivery_time = (new Date()).getTime() + 60*60*1000*logistics_info.getDelivery_estimate();long created_timestamp = (new Date()).getTime();OrderStatus status = OrderStatus.INIT; // to get from orderstatus fileString statusDescription = "New Order";Order order = new Order();order.setLogistics_provider_id(logistics_provider_id);order.setAirwaybill_no(airwaybill_no);order.setExpected_delivery_time(expected_delivery_time);order.setTracking_id(tracking_id);order.setWarehouse_id(warehouse_id);order.setCreated_timestamp(created_timestamp);order.setStatus(status);order.setStatusDescription(statusDescription);order.setCustomer_id(customer_id);order.setCustomer_name(customer_name);order.setCustomer_address(customer_address);order.setCustomer_email(customer_email);order.setCustomer_mobilenumber(customer_mobilenumber);order.setCustomer_pincode(customer_pincode);order.setTotal_amount(total_amount);order.setTotal_weight(total_weight);return order;}private String getEmail(long userId){String email = null;try {email = usc.getClient().getPrimaryInfo(userId, false).getEmail();} catch (UserContextException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}return email;}private Address getAddress(long addressId){List<Address> addresses = null;try {addresses = usc.getClient().getPrimaryInfo(pmnt.getUser_id(), false).getAddresses();} catch (UserContextException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}Address address = null;for(Address a : addresses){if(a.getId() == addressId){address = a;}}return address;}private LineItem getLineItem(long catelogItemId){LineItem lineItem = new LineItem();Item item = null;try {item = csc.getClient().getItemByCatalogId(catelogItemId);} catch (InventoryServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}String model_name = item.getModelName();String model_number = item.getModelNumber();String brand = item.getManufacturerName();String sku_id = item.getVendorItemId();double unit_price = item.getSellingPrice();double unit_weight = item.getWeight();double total_price = item.getSellingPrice();double total_weight = item.getWeight();String extra_info = item.getFeatureDescription();double quantity = 1; // because now we will create one order per itemlineItem.setSku_id(sku_id);lineItem.setBrand(brand);lineItem.setExtra_info(extra_info);lineItem.setModel_name(model_name);lineItem.setModel_number(model_number);lineItem.setQuantity(quantity);lineItem.setSku_id(sku_id);lineItem.setUnit_price(unit_price);lineItem.setUnit_weight(unit_weight);lineItem.setTotal_price(total_price);lineItem.setTotal_weight(total_weight);return lineItem;}private String getAddressString(Address a){String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();return add;}private String getAddress(Set<Address> address, long id){for(Address a : address){if(a.getId() == id){//Prepare StringString add = a.getName()+",\n"+a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry()+",\n"+a.getPin()+",\n Phone :- "+a.getPhone();return add;}}return "";}public String getMessage(){return this.message;}public long getTransactionId(){return this.transaction_id;}}/*private Transaction getTransaction(){try {pmnt = pclient.getClient().getPayment(Long.parseLong(trackId));} catch (NumberFormatException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (PaymentException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (TException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}Transaction t = new Transaction();PaymentInfo paymentInfo = new PaymentInfo();t.setPaymentInfo(paymentInfo);t.setShoppingCartid(pmnt.getCart_id());t.setCustomer_id(pmnt.getUser_id());t.setCreatedOn(pmnt.getBank_ack_timestamp());t.setTransactionStatus(TransactionStatus.INIT);t.setStatusDescription("New order");//create paymentPayment payment = new Payment();payment.setAmount(Double.parseDouble(amount));payment.setBank_tx_id(tranId);payment.setMerchant_tx_id(trackId);payment.setStatus(PaymentStatus.SUCCESS);payment.setMode("credit card");payment.setCompletionTimestamp(pmnt.getBank_ack_timestamp());payment.setSubmissionTimestamp(pmnt.getInit_timestamp());Map<Long, Payment> paymentMap = new HashMap<Long, Payment>();paymentMap.put(payment.getCompletionTimestamp(), payment);paymentInfo.setPayments(paymentMap);Cart c = null;try {c = cl.getClient().getCart(pmnt.getCart_id());} catch (ShoppingCartException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//Shipping infoShipmentInfo shipmentInfo = new ShipmentInfo();t.setShipmentInfo(shipmentInfo);Shipment shipment = new Shipment();try {shipment.setAirwayBillNo(lsc.getClient().getEmptyAWB(1));} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {shipment.setAddress(getAddress(usc.getClient().getPrimaryInfo(pmnt.getUser_id(), false).getAddresses(),c.getAddressId()));} catch (UserContextException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}shipment.setInsurance(0.0);shipment.setProvider("Aramex");shipment.setTrackingId(shipment.getAirwayBillNo());shipment.setWeight(0.0);shipment.setValue(Double.parseDouble(amount));shipment.setLineItems(null);shipmentInfo.addToShipments(shipment);//billinginfo will be added later by warehouseOrderInfo orderInfo = new OrderInfo();t.setOrderInfo(orderInfo);List<Line> lines = c.getLines();for(Line line : lines){if(line.getLineStatus() == LineStatus.LINE_ACTIVE){//line is activeLineItem lineItem = new LineItem();Item item = null;try {item = csc.getClient().getItemByCatalogId(line.getItemId());} catch (InventoryServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}in.shop2020.model.v1.order.Item oitem = new in.shop2020.model.v1.order.Item();oitem.setId(item.getCatalogItemId());//get price for itlong warehouse = getWarehouse();oitem.setPrice(item.getPrice().get(warehouse));oitem.setWeight(item.getWeight());lineItem.setItem(oitem);lineItem.setAddedOn(pmnt.getBank_ack_timestamp());orderInfo.addToLineitems(lineItem);}}return t;}*/