Subversion Repositories SmartDukaan

Rev

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