Subversion Repositories SmartDukaan

Rev

Rev 3903 | Rev 4061 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
428 ashish 1
package in.shop2020.serving.controllers;
2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
555 chandransh 4
import in.shop2020.model.v1.order.Order;
428 ashish 5
import in.shop2020.model.v1.order.TransactionServiceException;
894 rajveer 6
import in.shop2020.model.v1.user.Cart;
712 rajveer 7
import in.shop2020.model.v1.user.ShoppingCartException;
3378 vikas 8
import in.shop2020.model.v1.user.TrackLogType;
1999 vikas 9
import in.shop2020.model.v1.user.UserAffiliateException;
428 ashish 10
import in.shop2020.payments.PaymentException;
1999 vikas 11
import in.shop2020.serving.interceptors.TrackingInterceptor;
2193 varun.gupt 12
import in.shop2020.serving.utils.FormattingUtils;
3209 vikas 13
import in.shop2020.serving.utils.Utils;
3126 rajveer 14
import in.shop2020.thrift.clients.PaymentClient;
15
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.UserClient;
2511 vikas 17
import in.shop2020.utils.DataLogger;
428 ashish 18
 
2263 vikas 19
import java.util.Date;
20
import java.util.List;
894 rajveer 21
 
2263 vikas 22
import org.apache.log4j.Logger;
23
import org.apache.struts2.convention.annotation.InterceptorRef;
24
import org.apache.struts2.convention.annotation.InterceptorRefs;
25
import org.apache.thrift.TException;
26
 
27
 
822 vikas 28
@InterceptorRefs({
29
    @InterceptorRef("myDefault"),
30
    @InterceptorRef("login")
712 rajveer 31
})
693 rajveer 32
 
719 rajveer 33
public class PaySuccessController extends BaseController{
693 rajveer 34
	private static final long serialVersionUID = 1L;
3830 chandransh 35
 
36
	private static final String ITEM = "ITEM";
37
	private static final String ITEMS = "ITEMS";
38
 
1044 chandransh 39
	private static Logger log = Logger.getLogger(Class.class);
2193 varun.gupt 40
	private FormattingUtils formattingUtils = new FormattingUtils();
41
 
1044 chandransh 42
 
712 rajveer 43
	long merchantPaymentId;
44
	List<Order> orders = null;
719 rajveer 45
	String message = null;
428 ashish 46
 
719 rajveer 47
	public PaySuccessController(){
428 ashish 48
		super();
49
	}
50
 
853 rajveer 51
	public String index() {
3121 chandransh 52
 
3126 rajveer 53
		PaymentClient paymentServiceClient = null;
54
		TransactionClient transactionServiceClient = null;
55
		UserClient userServiceClient = null;
894 rajveer 56
 
57
		try {
3126 rajveer 58
			paymentServiceClient = new PaymentClient();
59
			transactionServiceClient = new TransactionClient();
60
			userServiceClient = new UserClient();
894 rajveer 61
		} catch (Exception e1) {
62
			// TODO Nothing to worry
2944 chandransh 63
			log.error("Unable to initialize the client for either payment or transaction or user service", e1);
894 rajveer 64
		}
65
 
66
 
719 rajveer 67
		merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
68
 
853 rajveer 69
		long txnId;
70
		try {
894 rajveer 71
			txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
1527 ankur.sing 72
			orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
894 rajveer 73
			Cart cart = userServiceClient.getClient().getCart(userinfo.getCartId());
74
			if(cart != null){
3830 chandransh 75
				userinfo.setTotalItems(cart.getLinesSize());
76
				userinfo.setTotalAmount(cart.getTotalPrice());
77
			}else{
78
				userinfo.setTotalItems(0);
79
				userinfo.setTotalAmount(0);
894 rajveer 80
			}
3830 chandransh 81
 
1999 vikas 82
			if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
83
	            long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
3378 vikas 84
	            userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
1999 vikas 85
                        Long.toString(merchantPaymentId), (new Date()).getTime());
86
	        }
3830 chandransh 87
 
88
			String suffix = ITEM;
89
			if(orders.size() > 1)
90
			    suffix = ITEMS;
91
 
3967 rajveer 92
			this.message = "You have successfully ordered <span class=\"orange\">" + orders.size() + " " + suffix + ".</span>";
93
 
853 rajveer 94
		} catch (PaymentException e) {
2944 chandransh 95
			log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
853 rajveer 96
		} catch (TException e) {
2944 chandransh 97
			log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
853 rajveer 98
		} catch (TransactionServiceException e) {
2944 chandransh 99
			log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
853 rajveer 100
		} catch (ShoppingCartException e) {
2944 chandransh 101
			log.error("Shopping cart service exception. Payment id is" + merchantPaymentId, e);
1999 vikas 102
		} catch (UserAffiliateException e) {
2944 chandransh 103
		    log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
1999 vikas 104
        }
853 rajveer 105
 
3209 vikas 106
		DataLogger.logData(EventType.PAYMENT_SUCCESS, getSessionId(), userinfo.getUserId(), 
107
		        userinfo.getEmail(), Long.toString(merchantPaymentId), Utils.getItemIdStringFromOrders(orders));
2087 vikas 108
 
693 rajveer 109
		return "index";
428 ashish 110
	}
111
 
2193 varun.gupt 112
	public String formatPrice(double price)    {
113
	    return formattingUtils.formatPrice(price);
114
	}
719 rajveer 115
 
712 rajveer 116
	public List<Order> getOrders(){
117
		return this.orders;
485 rajveer 118
	}
119
 
120
	public String getMessage(){
121
		return this.message;
3903 varun.gupt 122
	}
123
 
124
	@Override
125
	public String getHeaderSnippet() {
126
		String url = request.getQueryString();
127
		if (url == null) {
128
			url = "";
129
		} else {
130
			url = "?" + url;
131
		}
132
		url = request.getRequestURI() + url;
133
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser(), url , 0, false);
2193 varun.gupt 134
	}	
3903 varun.gupt 135
}