Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
6050 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.model.v1.order.RechargeOrder;
4
import in.shop2020.model.v1.order.RechargeOrderStatus;
6056 amit.gupta 5
import in.shop2020.model.v1.order.RechargeType;
6050 anupam.sin 6
import in.shop2020.model.v1.order.TransactionServiceException;
7
import in.shop2020.model.v1.user.Affiliate;
8
import in.shop2020.model.v1.user.TrackLogType;
9
import in.shop2020.model.v1.user.UserAffiliateException;
10
import in.shop2020.payments.PaymentException;
11
import in.shop2020.serving.interceptors.TrackingInterceptor;
12
import in.shop2020.serving.utils.FormattingUtils;
13
import in.shop2020.thrift.clients.PaymentClient;
14
import in.shop2020.thrift.clients.TransactionClient;
15
import in.shop2020.thrift.clients.UserClient;
16
 
17
import java.text.SimpleDateFormat;
18
import java.util.Date;
6056 amit.gupta 19
import java.util.HashMap;
20
import java.util.Map;
6050 anupam.sin 21
 
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;
6056 amit.gupta 26
import org.apache.thrift.transport.TTransportException;
6050 anupam.sin 27
 
28
@InterceptorRefs({
29
    @InterceptorRef("myDefault"),
30
    @InterceptorRef("login")
31
})
32
 
33
public class RechargeResultController extends BaseController {
34
    private static final long serialVersionUID = 1L;
35
 
36
    private static final String SHOOGLOO_AFF_NAME = "shoogloo";
37
 
38
    private static Logger log = Logger.getLogger(Class.class);
39
    private FormattingUtils formattingUtils = new FormattingUtils();
40
 
41
    private boolean isShooglooAff  = false;
6056 amit.gupta 42
    private static Map<Long,String> allProviders;
43
    static {
44
    	try {
45
			allProviders = new TransactionClient().getClient().getServiceProviders(RechargeType.DTH);
46
			allProviders.putAll(new TransactionClient().getClient().getServiceProviders(RechargeType.MOBILE));
47
		} catch (TTransportException e) {
48
			// TODO Auto-generated catch block
49
			e.printStackTrace();
50
		} catch (TException e) {
51
			// TODO Auto-generated catch block
52
			e.printStackTrace();
53
		}
54
    }
6050 anupam.sin 55
    long merchantPaymentId;
56
    RechargeOrder rechargeOrder = null;
57
    String message = null;
58
 
59
    public RechargeResultController(){
60
        super();
61
    }
62
 
63
    public String index() {
64
        PaymentClient paymentServiceClient = null;
65
        TransactionClient transactionServiceClient = null;
66
        UserClient userServiceClient = null;
67
 
68
        try {
69
            paymentServiceClient = new PaymentClient();
70
            transactionServiceClient = new TransactionClient();
71
            userServiceClient = new UserClient();
72
        } catch (Exception e1) {
73
            // TODO Nothing to worry
74
            log.error("Unable to initialize the client for either payment or transaction or user service", e1);
75
        }
76
 
77
 
78
        merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
6056 amit.gupta 79
        String operator = "";
6050 anupam.sin 80
        long txnId;
81
        try {
82
            txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
83
            rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
84
 
85
            this.message = "You have successfully recharged your mobile.";
86
 
87
            if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
88
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
89
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
90
                        Long.toString(merchantPaymentId), (new Date()).getTime());
91
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
92
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
93
                    isShooglooAff = true;
94
                }
95
            }
96
 
97
        } catch (PaymentException e) {
98
            log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
99
        } catch (TException e) {
100
            log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
101
        } catch (TransactionServiceException e) {
102
            log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
103
        } catch (UserAffiliateException e) {
104
            log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
105
        }
106
 
107
//        DataLogger.logData(EventType.PAYMENT_SUCCESS, getSessionId(), userinfo.getUserId(), 
108
//                userinfo.getEmail(), Long.toString(merchantPaymentId), Utils.getItemIdStringFromOrders(orders));
109
 
110
        return "index";
111
    }
112
 
113
    public String formatPrice(double price)    {
114
        return formattingUtils.formatPrice(price);
115
    }
116
 
117
    public String getMessage(){
118
        return this.message;
119
    }
120
 
121
    public boolean getIsShooglooAff() {
122
        return this.isShooglooAff;
123
    }
124
 
125
    public long getMerchantPaymentId() {
126
        return merchantPaymentId;
127
    }
128
 
129
    public static String formatDate(long timestamp){
130
        SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
131
        return dateformat.format(new Date(timestamp));  
132
    }
133
 
134
 
135
    @Override
136
    public String getHeaderSnippet() {
137
        String url = request.getQueryString();
138
        if (url == null) {
139
            url = "";
140
        } else {
141
            url = "?" + url;
142
        }
143
        url = request.getRequestURI() + url;
144
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , 0, false);
145
    }
146
 
147
    public RechargeOrder getRechargeOrder() {
148
        return rechargeOrder;
149
    }
150
 
151
    public void setRechargeOrder(RechargeOrder rechargeOrder) {
152
        this.rechargeOrder = rechargeOrder;
153
    }
6056 amit.gupta 154
 
155
 
156
    public boolean isDTH() {
157
    	return rechargeOrder.getRechargeType().equals(RechargeType.DTH);
158
    }
159
 
160
    public String getProvider() {
161
    	return allProviders.get(rechargeOrder.getOperatorId());
162
    }
163
 
164
    public String[] getOrderStatus(){
165
    	RechargeOrderStatus status = rechargeOrder.getStatus();
166
    	if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
167
    		return new String[]{"red", "PAYMENT FAILED", "Payment failed. Try to <a href='/recharge'>recharge again</a>"};
168
    	}
169
    	else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)||status.equals(RechargeOrderStatus.RECHARGE_FAILED)){
170
			return new String[]{"red", "RECHARGE FAILED", "Payment was successful and amount is transferred to your <a href='/wallet'>wallet</a>.<br> <a href='/recharge'>Recharge again</a> using wallet."};
171
    	} else {
172
    		return new String[] {"", "SUCCESS", "Congratulations your mobile is successfully recharged."};
173
    	}
174
    }
6050 anupam.sin 175
}