Subversion Repositories SmartDukaan

Rev

Rev 6152 | Rev 6546 | 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;
6475 rajveer 5
import in.shop2020.model.v1.order.OrderStatus;
428 ashish 6
import in.shop2020.model.v1.order.TransactionServiceException;
4061 vikas 7
import in.shop2020.model.v1.user.Affiliate;
894 rajveer 8
import in.shop2020.model.v1.user.Cart;
712 rajveer 9
import in.shop2020.model.v1.user.ShoppingCartException;
3378 vikas 10
import in.shop2020.model.v1.user.TrackLogType;
1999 vikas 11
import in.shop2020.model.v1.user.UserAffiliateException;
428 ashish 12
import in.shop2020.payments.PaymentException;
1999 vikas 13
import in.shop2020.serving.interceptors.TrackingInterceptor;
2193 varun.gupt 14
import in.shop2020.serving.utils.FormattingUtils;
3209 vikas 15
import in.shop2020.serving.utils.Utils;
3126 rajveer 16
import in.shop2020.thrift.clients.PaymentClient;
17
import in.shop2020.thrift.clients.TransactionClient;
18
import in.shop2020.thrift.clients.UserClient;
2511 vikas 19
import in.shop2020.utils.DataLogger;
428 ashish 20
 
5880 rajveer 21
import java.text.SimpleDateFormat;
6475 rajveer 22
import java.util.Calendar;
2263 vikas 23
import java.util.Date;
24
import java.util.List;
894 rajveer 25
 
2263 vikas 26
import org.apache.log4j.Logger;
27
import org.apache.struts2.convention.annotation.InterceptorRef;
28
import org.apache.struts2.convention.annotation.InterceptorRefs;
29
import org.apache.thrift.TException;
30
 
31
 
822 vikas 32
@InterceptorRefs({
33
    @InterceptorRef("myDefault"),
34
    @InterceptorRef("login")
712 rajveer 35
})
693 rajveer 36
 
719 rajveer 37
public class PaySuccessController extends BaseController{
693 rajveer 38
	private static final long serialVersionUID = 1L;
3830 chandransh 39
 
40
	private static final String ITEM = "ITEM";
41
	private static final String ITEMS = "ITEMS";
42
 
4061 vikas 43
	private static final String SHOOGLOO_AFF_NAME = "shoogloo";
44
 
1044 chandransh 45
	private static Logger log = Logger.getLogger(Class.class);
2193 varun.gupt 46
	private FormattingUtils formattingUtils = new FormattingUtils();
47
 
4061 vikas 48
	private boolean isShooglooAff  = false;
1044 chandransh 49
 
712 rajveer 50
	long merchantPaymentId;
51
	List<Order> orders = null;
719 rajveer 52
	String message = null;
428 ashish 53
 
719 rajveer 54
	public PaySuccessController(){
428 ashish 55
		super();
56
	}
57
 
853 rajveer 58
	public String index() {
3121 chandransh 59
 
3126 rajveer 60
		PaymentClient paymentServiceClient = null;
61
		TransactionClient transactionServiceClient = null;
62
		UserClient userServiceClient = null;
4453 varun.gupt 63
 
894 rajveer 64
		try {
3126 rajveer 65
			paymentServiceClient = new PaymentClient();
66
			transactionServiceClient = new TransactionClient();
67
			userServiceClient = new UserClient();
894 rajveer 68
		} catch (Exception e1) {
69
			// TODO Nothing to worry
2944 chandransh 70
			log.error("Unable to initialize the client for either payment or transaction or user service", e1);
894 rajveer 71
		}
72
 
73
 
719 rajveer 74
		merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
75
 
853 rajveer 76
		long txnId;
77
		try {
894 rajveer 78
			txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
1527 ankur.sing 79
			orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
894 rajveer 80
			Cart cart = userServiceClient.getClient().getCart(userinfo.getCartId());
81
			if(cart != null){
3830 chandransh 82
				userinfo.setTotalItems(cart.getLinesSize());
83
				userinfo.setTotalAmount(cart.getTotalPrice());
84
			}else{
85
				userinfo.setTotalItems(0);
86
				userinfo.setTotalAmount(0);
894 rajveer 87
			}
3830 chandransh 88
 
89
			String suffix = ITEM;
90
			if(orders.size() > 1)
91
			    suffix = ITEMS;
92
 
3967 rajveer 93
			this.message = "You have successfully ordered <span class=\"orange\">" + orders.size() + " " + suffix + ".</span>";
94
 
4061 vikas 95
			if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
96
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
97
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
98
                        Long.toString(merchantPaymentId), (new Date()).getTime());
99
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
100
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
101
                    isShooglooAff = true;
102
                }
103
            }
104
 
853 rajveer 105
		} catch (PaymentException e) {
2944 chandransh 106
			log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
853 rajveer 107
		} catch (TException e) {
2944 chandransh 108
			log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
853 rajveer 109
		} catch (TransactionServiceException e) {
2944 chandransh 110
			log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
853 rajveer 111
		} catch (ShoppingCartException e) {
2944 chandransh 112
			log.error("Shopping cart service exception. Payment id is" + merchantPaymentId, e);
1999 vikas 113
		} catch (UserAffiliateException e) {
2944 chandransh 114
		    log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
1999 vikas 115
        }
853 rajveer 116
 
3209 vikas 117
		DataLogger.logData(EventType.PAYMENT_SUCCESS, getSessionId(), userinfo.getUserId(), 
118
		        userinfo.getEmail(), Long.toString(merchantPaymentId), Utils.getItemIdStringFromOrders(orders));
2087 vikas 119
 
693 rajveer 120
		return "index";
428 ashish 121
	}
122
 
2193 varun.gupt 123
	public String formatPrice(double price)    {
124
	    return formattingUtils.formatPrice(price);
125
	}
719 rajveer 126
 
712 rajveer 127
	public List<Order> getOrders(){
128
		return this.orders;
485 rajveer 129
	}
130
 
6475 rajveer 131
	public List<Order> getSuccessfulOrders(){
132
		if((this.orders.get(0).getStatus() == OrderStatus.COD_VERIFICATION_PENDING 
133
				|| this.orders.get(0).getStatus() == OrderStatus.SUBMITTED_FOR_PROCESSING) && this.orders.get(0).getCreated_timestamp() + 10*60*1000 > Calendar.getInstance().getTimeInMillis()){
134
			return this.orders;	
135
		}
136
		return null;
137
	}
138
 
485 rajveer 139
	public String getMessage(){
140
		return this.message;
3903 varun.gupt 141
	}
142
 
4061 vikas 143
	public boolean getIsShooglooAff() {
144
        return this.isShooglooAff;
145
    }
146
 
147
	public long getMerchantPaymentId() {
148
	    return merchantPaymentId;
149
	}
150
 
5880 rajveer 151
	public static String formatDate(long timestamp){
152
		SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
153
		return dateformat.format(new Date(timestamp));	
154
	}
155
 
156
 
3903 varun.gupt 157
	@Override
158
	public String getHeaderSnippet() {
159
		String url = request.getQueryString();
160
		if (url == null) {
161
			url = "";
162
		} else {
163
			url = "?" + url;
164
		}
165
		url = request.getRequestURI() + url;
6152 amit.gupta 166
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
5880 rajveer 167
	}
3903 varun.gupt 168
}