Subversion Repositories SmartDukaan

Rev

Rev 6206 | Rev 6266 | 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
 
6108 amar.kumar 3
import in.shop2020.datalogger.EventType;
6050 anupam.sin 4
import in.shop2020.model.v1.order.RechargeOrder;
5
import in.shop2020.model.v1.order.RechargeOrderStatus;
6056 amit.gupta 6
import in.shop2020.model.v1.order.RechargeType;
6050 anupam.sin 7
import in.shop2020.model.v1.order.TransactionServiceException;
8
import in.shop2020.model.v1.user.Affiliate;
9
import in.shop2020.model.v1.user.TrackLogType;
10
import in.shop2020.model.v1.user.UserAffiliateException;
11
import in.shop2020.payments.PaymentException;
12
import in.shop2020.serving.interceptors.TrackingInterceptor;
13
import in.shop2020.serving.utils.FormattingUtils;
14
import in.shop2020.thrift.clients.PaymentClient;
15
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.UserClient;
6108 amar.kumar 17
import in.shop2020.utils.DataLogger;
6050 anupam.sin 18
 
19
import java.text.SimpleDateFormat;
20
import java.util.Date;
6056 amit.gupta 21
import java.util.Map;
6050 anupam.sin 22
 
23
import org.apache.log4j.Logger;
24
import org.apache.thrift.TException;
6056 amit.gupta 25
import org.apache.thrift.transport.TTransportException;
6050 anupam.sin 26
 
27
public class RechargeResultController extends BaseController {
28
    private static final long serialVersionUID = 1L;
29
 
30
    private static final String SHOOGLOO_AFF_NAME = "shoogloo";
31
 
32
    private static Logger log = Logger.getLogger(Class.class);
33
    private FormattingUtils formattingUtils = new FormattingUtils();
34
 
35
    private boolean isShooglooAff  = false;
6056 amit.gupta 36
    private static Map<Long,String> allProviders;
37
    static {
38
    	try {
6206 rajveer 39
			allProviders = new TransactionClient().getClient().getServiceProviders(RechargeType.DTH, true);
40
			allProviders.putAll(new TransactionClient().getClient().getServiceProviders(RechargeType.MOBILE, true));
6056 amit.gupta 41
		} catch (TTransportException e) {
42
			// TODO Auto-generated catch block
43
			e.printStackTrace();
44
		} catch (TException e) {
45
			// TODO Auto-generated catch block
46
			e.printStackTrace();
47
		}
48
    }
6050 anupam.sin 49
    long merchantPaymentId;
50
    RechargeOrder rechargeOrder = null;
51
    String message = null;
52
 
53
    public RechargeResultController(){
54
        super();
55
    }
56
 
57
    public String index() {
58
        PaymentClient paymentServiceClient = null;
59
        TransactionClient transactionServiceClient = null;
60
        UserClient userServiceClient = null;
61
 
62
        try {
63
            paymentServiceClient = new PaymentClient();
64
            transactionServiceClient = new TransactionClient();
65
            userServiceClient = new UserClient();
66
        } catch (Exception e1) {
67
            // TODO Nothing to worry
68
            log.error("Unable to initialize the client for either payment or transaction or user service", e1);
69
        }
70
 
6123 anupam.sin 71
        String returningPayIdString = this.request.getParameter("paymentId");
72
        if (returningPayIdString == null || returningPayIdString.isEmpty()) {
73
            this.message = "Payment Failed at payment gateway. Please try again.";
74
            return "index";
75
        }
6050 anupam.sin 76
        merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
77
        long txnId;
78
        try {
79
            txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
80
            rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
81
 
82
            this.message = "You have successfully recharged your mobile.";
83
 
84
            if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
85
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
86
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
87
                        Long.toString(merchantPaymentId), (new Date()).getTime());
88
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
89
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
90
                    isShooglooAff = true;
91
                }
92
            }
93
 
94
        } catch (PaymentException e) {
95
            log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
96
        } catch (TException e) {
97
            log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
98
        } catch (TransactionServiceException e) {
99
            log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
100
        } catch (UserAffiliateException e) {
101
            log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
102
        }
103
 
104
        return "index";
105
    }
106
 
107
    public String formatPrice(double price)    {
108
        return formattingUtils.formatPrice(price);
109
    }
110
 
111
    public String getMessage(){
112
        return this.message;
113
    }
114
 
115
    public boolean getIsShooglooAff() {
116
        return this.isShooglooAff;
117
    }
118
 
119
    public long getMerchantPaymentId() {
120
        return merchantPaymentId;
121
    }
122
 
123
    public static String formatDate(long timestamp){
124
        SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
125
        return dateformat.format(new Date(timestamp));  
126
    }
127
 
128
 
129
    @Override
130
    public String getHeaderSnippet() {
131
        String url = request.getQueryString();
132
        if (url == null) {
133
            url = "";
134
        } else {
135
            url = "?" + url;
136
        }
137
        url = request.getRequestURI() + url;
6152 amit.gupta 138
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
6050 anupam.sin 139
    }
140
 
141
    public RechargeOrder getRechargeOrder() {
142
        return rechargeOrder;
143
    }
144
 
145
    public void setRechargeOrder(RechargeOrder rechargeOrder) {
146
        this.rechargeOrder = rechargeOrder;
147
    }
6056 amit.gupta 148
 
149
 
150
    public boolean isDTH() {
151
    	return rechargeOrder.getRechargeType().equals(RechargeType.DTH);
152
    }
153
 
154
    public String getProvider() {
155
    	return allProviders.get(rechargeOrder.getOperatorId());
156
    }
157
 
158
    public String[] getOrderStatus(){
159
    	RechargeOrderStatus status = rechargeOrder.getStatus();
160
    	if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
6108 amar.kumar 161
    		DataLogger.logData(EventType.RECHARGE_PAYMENT_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
162
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
163
                    rechargeOrder.getRechargeType().toString());
6145 anupam.sin 164
    		return new String[]{"red", "PAYMENT FAILED", "Payment failed at the payment gateway. Try to <a href='/recharge'>recharge again</a>"};
6056 amit.gupta 165
    	}
6215 anupam.sin 166
    	else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)) {
167
    	    DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
168
                    Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
169
                    rechargeOrder.getRechargeType().toString());
170
    	    return new String[]{"red", "RECHARGE IN PROCESS", "Your Payment was successful but due to some internal error with the operator's system we are not sure if the recharge was successful.<br>" +
171
                    "We have put your recharge under process. As soon as we get a confirmation on this transaction, we will notify you.<br>" +
172
                    "In case of recharge failure your entire amount will be credited to your " +
173
                    "<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
174
                    "will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
175
                    " Saholic.com please use your username and password to Login.<br> In case you are " +
176
                    "a first time user, we have sent your login details on the emailId you gave us. " +
177
                    "<br>For more information <a href='/static/recharge-faq'>click here</a>"};
178
    	}
179
    	else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED)){
6108 amar.kumar 180
    		DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
181
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
182
    				rechargeOrder.getRechargeType().toString());
6145 anupam.sin 183
			return new String[]{"red", "RECHARGE FAILED", "Your Payment was successful but unfortunately the recharge failed.<br>" +
184
					"Don't worry your payment is safe with us. The entire Amount has been refunded to your " +
185
					"<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
186
					"will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
187
					" Saholic.com please use your username and password to Login.<br> In case you are " +
188
					"a first time user, we have sent your login details on the emailId you gave us. " +
6146 anupam.sin 189
					"<br>For more information <a href='/static/recharge-faq'>click here</a>"};
6056 amit.gupta 190
    	} else {
6108 amar.kumar 191
    		DataLogger.logData(EventType.RECHARGE_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
192
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
193
    				rechargeOrder.getRechargeType().toString());
6145 anupam.sin 194
    		return new String[] {"", "SUCCESS", "Congratulations your device is successfully recharged."};
6056 amit.gupta 195
    	}
196
    }
6050 anupam.sin 197
}