Blame | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import java.io.IOException;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.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.OrderInfo;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();}return new DefaultHttpHeaders(result);}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;}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 "";}private long getWarehouse(){return 1l;}public String getMessage(){return this.message;}public long getTransactionId(){return this.transaction_id;}}