Subversion Repositories SmartDukaan

Rev

Rev 6475 | Rev 6903 | 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;
6546 rajveer 51
	long txnId;
712 rajveer 52
	List<Order> orders = null;
719 rajveer 53
	String message = null;
428 ashish 54
 
719 rajveer 55
	public PaySuccessController(){
428 ashish 56
		super();
57
	}
58
 
853 rajveer 59
	public String index() {
3121 chandransh 60
 
3126 rajveer 61
		PaymentClient paymentServiceClient = null;
62
		TransactionClient transactionServiceClient = null;
63
		UserClient userServiceClient = null;
4453 varun.gupt 64
 
894 rajveer 65
		try {
3126 rajveer 66
			paymentServiceClient = new PaymentClient();
67
			transactionServiceClient = new TransactionClient();
68
			userServiceClient = new UserClient();
894 rajveer 69
		} catch (Exception e1) {
70
			// TODO Nothing to worry
2944 chandransh 71
			log.error("Unable to initialize the client for either payment or transaction or user service", e1);
894 rajveer 72
		}
73
 
74
 
719 rajveer 75
		merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
76
 
6546 rajveer 77
 
853 rajveer 78
		try {
894 rajveer 79
			txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
1527 ankur.sing 80
			orders = transactionServiceClient.getClient().getOrdersForTransaction(txnId, userinfo.getUserId());
894 rajveer 81
			Cart cart = userServiceClient.getClient().getCart(userinfo.getCartId());
82
			if(cart != null){
3830 chandransh 83
				userinfo.setTotalItems(cart.getLinesSize());
84
				userinfo.setTotalAmount(cart.getTotalPrice());
85
			}else{
86
				userinfo.setTotalItems(0);
87
				userinfo.setTotalAmount(0);
894 rajveer 88
			}
3830 chandransh 89
 
90
			String suffix = ITEM;
91
			if(orders.size() > 1)
92
			    suffix = ITEMS;
93
 
3967 rajveer 94
			this.message = "You have successfully ordered <span class=\"orange\">" + orders.size() + " " + suffix + ".</span>";
95
 
4061 vikas 96
			if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
97
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
98
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
99
                        Long.toString(merchantPaymentId), (new Date()).getTime());
100
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
101
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
102
                    isShooglooAff = true;
103
                }
104
            }
105
 
853 rajveer 106
		} catch (PaymentException e) {
2944 chandransh 107
			log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
853 rajveer 108
		} catch (TException e) {
2944 chandransh 109
			log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
853 rajveer 110
		} catch (TransactionServiceException e) {
2944 chandransh 111
			log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
853 rajveer 112
		} catch (ShoppingCartException e) {
2944 chandransh 113
			log.error("Shopping cart service exception. Payment id is" + merchantPaymentId, e);
1999 vikas 114
		} catch (UserAffiliateException e) {
2944 chandransh 115
		    log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
1999 vikas 116
        }
853 rajveer 117
 
3209 vikas 118
		DataLogger.logData(EventType.PAYMENT_SUCCESS, getSessionId(), userinfo.getUserId(), 
119
		        userinfo.getEmail(), Long.toString(merchantPaymentId), Utils.getItemIdStringFromOrders(orders));
2087 vikas 120
 
693 rajveer 121
		return "index";
428 ashish 122
	}
123
 
2193 varun.gupt 124
	public String formatPrice(double price)    {
125
	    return formattingUtils.formatPrice(price);
126
	}
719 rajveer 127
 
712 rajveer 128
	public List<Order> getOrders(){
129
		return this.orders;
485 rajveer 130
	}
131
 
6475 rajveer 132
	public List<Order> getSuccessfulOrders(){
133
		if((this.orders.get(0).getStatus() == OrderStatus.COD_VERIFICATION_PENDING 
134
				|| this.orders.get(0).getStatus() == OrderStatus.SUBMITTED_FOR_PROCESSING) && this.orders.get(0).getCreated_timestamp() + 10*60*1000 > Calendar.getInstance().getTimeInMillis()){
135
			return this.orders;	
136
		}
137
		return null;
138
	}
139
 
485 rajveer 140
	public String getMessage(){
141
		return this.message;
3903 varun.gupt 142
	}
143
 
4061 vikas 144
	public boolean getIsShooglooAff() {
145
        return this.isShooglooAff;
146
    }
147
 
148
	public long getMerchantPaymentId() {
149
	    return merchantPaymentId;
150
	}
151
 
6546 rajveer 152
	public long getTxnId() {
153
	    return txnId;
154
	}
155
 
5880 rajveer 156
	public static String formatDate(long timestamp){
157
		SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
158
		return dateformat.format(new Date(timestamp));	
159
	}
160
 
161
 
3903 varun.gupt 162
	@Override
163
	public String getHeaderSnippet() {
164
		String url = request.getQueryString();
165
		if (url == null) {
166
			url = "";
167
		} else {
168
			url = "?" + url;
169
		}
170
		url = request.getRequestURI() + url;
6152 amit.gupta 171
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
5880 rajveer 172
	}
3903 varun.gupt 173
}