Subversion Repositories SmartDukaan

Rev

Rev 6398 | Rev 6438 | 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;
6434 anupam.sin 7
import in.shop2020.model.v1.order.Transaction;
6050 anupam.sin 8
import in.shop2020.model.v1.order.TransactionServiceException;
6434 anupam.sin 9
import in.shop2020.model.v1.user.PromotionException;
6050 anupam.sin 10
import in.shop2020.payments.PaymentException;
11
import in.shop2020.serving.utils.FormattingUtils;
12
import in.shop2020.thrift.clients.PaymentClient;
6434 anupam.sin 13
import in.shop2020.thrift.clients.PromotionClient;
6050 anupam.sin 14
import in.shop2020.thrift.clients.TransactionClient;
6108 amar.kumar 15
import in.shop2020.utils.DataLogger;
6050 anupam.sin 16
 
17
import java.text.SimpleDateFormat;
18
import java.util.Date;
6266 anupam.sin 19
import java.util.HashMap;
6056 amit.gupta 20
import java.util.Map;
6050 anupam.sin 21
 
22
import org.apache.log4j.Logger;
23
import org.apache.thrift.TException;
6056 amit.gupta 24
import org.apache.thrift.transport.TTransportException;
6050 anupam.sin 25
 
26
public class RechargeResultController extends BaseController {
27
    private static final long serialVersionUID = 1L;
28
 
6266 anupam.sin 29
//    private static final String SHOOGLOO_AFF_NAME = "shoogloo";
6050 anupam.sin 30
 
31
    private static Logger log = Logger.getLogger(Class.class);
32
    private FormattingUtils formattingUtils = new FormattingUtils();
33
 
6266 anupam.sin 34
    //private boolean isShooglooAff  = false;
35
 
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
    }
6266 anupam.sin 49
 
50
    private static Map<String, String> FailureReasonMap;
51
    static {
52
        FailureReasonMap = new HashMap<String, String>();
53
        FailureReasonMap.put("Invalid Amount", "the amount you tried seems to be invalid. Please try another amount.");
54
        FailureReasonMap.put("Customer Exceeded Daily Limit", "you have exceeded daily recharge limit for this number. Please try after 12 hours.");
6398 rajveer 55
        FailureReasonMap.put("Invalid Device Number", "it seems that your device number is not being recognized by selected operator. Please make sure the number you tried is correct.");
6266 anupam.sin 56
    }
57
 
6050 anupam.sin 58
    long merchantPaymentId;
59
    RechargeOrder rechargeOrder = null;
60
    String message = null;
61
 
62
    public RechargeResultController(){
63
        super();
64
    }
6434 anupam.sin 65
 
66
    private void trackCouponUsage(String couponCode) {
67
        try {
68
            if (couponCode != null && !couponCode.isEmpty()) {
69
                PromotionClient promotionServiceClient = new PromotionClient();
70
                promotionServiceClient.getClient().trackCouponUsage(couponCode, rechargeOrder.getId(), rechargeOrder.getUserId());
71
            }
72
        } catch (PromotionException e) {
73
            log.error("Promotion Exception: " + e);
74
        } catch (TException e)  {
75
            log.error("Transport from Promotion Service failed:", e);
76
        } catch (Exception e) {
77
            log.error("Unexpected exception:", e);
78
        }
79
    }
6050 anupam.sin 80
 
81
    public String index() {
82
        PaymentClient paymentServiceClient = null;
83
        TransactionClient transactionServiceClient = null;
6266 anupam.sin 84
        //UserClient userServiceClient = null;
6050 anupam.sin 85
 
86
        try {
87
            paymentServiceClient = new PaymentClient();
88
            transactionServiceClient = new TransactionClient();
6266 anupam.sin 89
          //  userServiceClient = new UserClient();
6050 anupam.sin 90
        } catch (Exception e1) {
91
            // TODO Nothing to worry
92
            log.error("Unable to initialize the client for either payment or transaction or user service", e1);
93
        }
94
 
6123 anupam.sin 95
        String returningPayIdString = this.request.getParameter("paymentId");
96
        if (returningPayIdString == null || returningPayIdString.isEmpty()) {
97
            this.message = "Payment Failed at payment gateway. Please try again.";
98
            return "index";
99
        }
6050 anupam.sin 100
        merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
101
        long txnId;
102
        try {
103
            txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
104
            rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
105
 
106
            this.message = "You have successfully recharged your mobile.";
107
 
6266 anupam.sin 108
            /*if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
6050 anupam.sin 109
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
110
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
111
                        Long.toString(merchantPaymentId), (new Date()).getTime());
112
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
113
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
114
                    isShooglooAff = true;
115
                }
6266 anupam.sin 116
            }*/
6050 anupam.sin 117
 
118
        } catch (PaymentException e) {
119
            log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
120
        } catch (TException e) {
121
            log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
122
        } catch (TransactionServiceException e) {
123
            log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
124
        }
6266 anupam.sin 125
 
126
//        catch (UserAffiliateException e) {
127
//            log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
128
//        }
6050 anupam.sin 129
 
130
        return "index";
131
    }
132
 
133
    public String formatPrice(double price)    {
134
        return formattingUtils.formatPrice(price);
135
    }
136
 
137
    public String getMessage(){
138
        return this.message;
139
    }
140
 
6266 anupam.sin 141
//    public boolean getIsShooglooAff() {
142
//        return this.isShooglooAff;
143
//    }
6050 anupam.sin 144
 
145
    public long getMerchantPaymentId() {
146
        return merchantPaymentId;
147
    }
148
 
149
    public static String formatDate(long timestamp){
150
        SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
151
        return dateformat.format(new Date(timestamp));  
152
    }
153
 
154
 
155
    @Override
156
    public String getHeaderSnippet() {
157
        String url = request.getQueryString();
158
        if (url == null) {
159
            url = "";
160
        } else {
161
            url = "?" + url;
162
        }
163
        url = request.getRequestURI() + url;
6152 amit.gupta 164
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
6050 anupam.sin 165
    }
166
 
167
    public RechargeOrder getRechargeOrder() {
168
        return rechargeOrder;
169
    }
170
 
171
    public void setRechargeOrder(RechargeOrder rechargeOrder) {
172
        this.rechargeOrder = rechargeOrder;
173
    }
6056 amit.gupta 174
 
175
 
176
    public boolean isDTH() {
177
    	return rechargeOrder.getRechargeType().equals(RechargeType.DTH);
178
    }
179
 
180
    public String getProvider() {
181
    	return allProviders.get(rechargeOrder.getOperatorId());
182
    }
183
 
184
    public String[] getOrderStatus(){
185
    	RechargeOrderStatus status = rechargeOrder.getStatus();
186
    	if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
6108 amar.kumar 187
    		DataLogger.logData(EventType.RECHARGE_PAYMENT_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
188
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
189
                    rechargeOrder.getRechargeType().toString());
6145 anupam.sin 190
    		return new String[]{"red", "PAYMENT FAILED", "Payment failed at the payment gateway. Try to <a href='/recharge'>recharge again</a>"};
6056 amit.gupta 191
    	}
6215 anupam.sin 192
    	else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)) {
193
    	    DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
194
                    Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
195
                    rechargeOrder.getRechargeType().toString());
196
    	    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>" +
197
                    "We have put your recharge under process. As soon as we get a confirmation on this transaction, we will notify you.<br>" +
198
                    "In case of recharge failure your entire amount will be credited to your " +
199
                    "<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
200
                    "will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
201
                    " Saholic.com please use your username and password to Login.<br> In case you are " +
202
                    "a first time user, we have sent your login details on the emailId you gave us. " +
203
                    "<br>For more information <a href='/static/recharge-faq'>click here</a>"};
204
    	}
205
    	else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED)){
6108 amar.kumar 206
    		DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
207
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
208
    				rechargeOrder.getRechargeType().toString());
6266 anupam.sin 209
    		String displayReason = FailureReasonMap.get(rechargeOrder.getDescription());
210
    		if (!(displayReason == null || displayReason.isEmpty())) {
211
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed because " + displayReason + "<br>";
212
    		} else {
213
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed.<br>";
214
    		}
215
			return new String[]{"red", "RECHARGE FAILED", displayReason +
6145 anupam.sin 216
					"Don't worry your payment is safe with us. The entire Amount has been refunded to your " +
217
					"<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
218
					"will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
219
					" Saholic.com please use your username and password to Login.<br> In case you are " +
220
					"a first time user, we have sent your login details on the emailId you gave us. " +
6146 anupam.sin 221
					"<br>For more information <a href='/static/recharge-faq'>click here</a>"};
6056 amit.gupta 222
    	} else {
6108 amar.kumar 223
    		DataLogger.logData(EventType.RECHARGE_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
224
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
225
    				rechargeOrder.getRechargeType().toString());
6323 rajveer 226
 
6434 anupam.sin 227
    		if(rechargeOrder.getCouponCode() != null) {
228
    		    trackCouponUsage(rechargeOrder.getCouponCode());
229
    		}
230
 
6336 anupam.sin 231
    		String operator = "";
232
    		if(isDTH()) {
233
    		    operator = getProvider();
234
    		} else {
235
    		    operator = getProvider() + " mobile";
236
    		}
237
 
238
    		String original_referer = "http://www.saholic.com/recharge-result?paymentId=" + this.request.getParameter("paymentId");
6323 rajveer 239
    		String fbUrl = "";
6336 anupam.sin 240
    		String twitterUrl = "";
241
    		String shareMsg = "";
6323 rajveer 242
    		String giftMessage = "Congratulations! Your device is successfully recharged.";
243
    		if(rechargeOrder.getTotalAmount()>=100){
244
    			if(rechargeOrder.getTotalAmount()>=500){
6336 anupam.sin 245
    			    shareMsg = "I just recharged my " + operator + " at Saholic.com and got Rs.500 cashback";
246
    				fbUrl = "http://www.facebook.com/dialog/feed?app_id=291830044248933&link=www.saholic.com/recharge?afid=61&picture=http://i1074.photobucket.com/albums/w407/amathur2k/logo_saholic.png&name=" + shareMsg + "&caption=Win Assured Gift Coupons worth upto Rs.500 on every Mobile or DTH Recharge&description=One Click Hassle Free Recharges&redirect_uri=http://saholic.com/?shared=1";
247
    				twitterUrl = "https://twitter.com/intent/tweet?hashtags=saholic&original_referer=" + original_referer + "&related=saholic&source=saholic.com&text=" + shareMsg + "&url=http://www.saholic.com/recharge/afid?64&via=saholic";
248
    				giftMessage = "Congratulations! Your device is successfully recharged and you have " +
249
    						"won Gift Coupons worth Rs.500 which you can use to shop for Mobiles, Laptops, Cameras, Accessories.<br><br>" +
250
    						"Voucher details have been sent to your email.<a target='_blank' href='/static/giftvoucher-offer'> Learn more.</a>" +
251
                            "<br><br>Please help us spread the word." +
252
    						"<div style=\"margin-top: 40px;text-align: center;\">" +
253
    						"<a target='_blank' href='" + twitterUrl + "'>" + 
6339 anupam.sin 254
    								"<input type='button' onclick=\"trackEventWithGA('Recharge', 'Twitter Share', '')\" style=\"background: url('/images/tweet-about-us.png') 0px 1px;width: 160px;height: 40px;margin-right: 40px;\"></a>" +
6336 anupam.sin 255
    						"<a target='_blank' href='" + fbUrl + "'>" +
6339 anupam.sin 256
    								"<input type=\"button\" onclick=\"trackEventWithGA('Recharge', 'Facebook Share', '');\" style=\"background:url('/images/Post-On-Facebook.jpg');width: 160px;height: 40px;\">" +
6336 anupam.sin 257
    								"</a></div>";
6323 rajveer 258
    			}else{
6339 anupam.sin 259
 
260
 
261
 
262
 
6336 anupam.sin 263
    			    shareMsg = "I just recharged my " + operator + " at Saholic.com and got full cashback";
264
    				fbUrl = "http://www.facebook.com/dialog/feed?app_id=291830044248933&link=www.saholic.com/recharge?afid=61&picture=http://i1074.photobucket.com/albums/w407/amathur2k/logo_saholic.png&name=" + shareMsg + "&caption=Win Assured Gift Coupons worth upto Rs.500 on every Mobile or DTH Recharge&description=One Click Hassle Free Recharges&redirect_uri=http://saholic.com/?shared=1";
265
    				twitterUrl = "https://twitter.com/intent/tweet?hashtags=saholic&original_referer=" + original_referer + "&related=saholic&source=saholic.com&text=" + shareMsg + "&url=http://www.saholic.com/recharge?afid=64&via=saholic";
266
    				giftMessage = "Congratulations! Your device is successfully recharged and you have " +
267
    						"won Gift Coupons worth Rs." + rechargeOrder.getTotalAmount() + " which you can use to shop for Mobiles, Laptops, Cameras, Accessories.<br><br>" +
268
    						"Voucher details have been sent to your email.<a target='_blank' href='/static/giftvoucher-offer'> Learn more.</a>" +
269
    						"<br><br>Please help us spread the word." +
270
    						"<div style=\"margin-top: 40px;text-align: center;\">" +
271
    						"<a target='_blank' href='" + twitterUrl + "'>" +
6341 anupam.sin 272
                            "<input type='button' onclick=\"trackEventWithGA('Recharge', 'Twitter Share', '')\" style=\"background: url('/unversioned/images/tweet-about-us.png') 0px 1px;width: 160px;height: 40px;margin-right: 40px;\"></a>" + 
6336 anupam.sin 273
                            "<a target='_blank' href='" + fbUrl + "'>" +
6341 anupam.sin 274
                                    "<input type=\"button\" onclick=\"trackEventWithGA('Recharge', 'Facebook Share', '');\" style=\"background:url('/unversioned/images/Post-On-Facebook.jpg');width: 160px;height: 40px;\">" +
6336 anupam.sin 275
                                    "</a></div>";
6323 rajveer 276
    			}
277
    		}
6325 rajveer 278
    		return new String[] {"", "SUCCESS", giftMessage};
6056 amit.gupta 279
    	}
280
    }
6050 anupam.sin 281
}