Subversion Repositories SmartDukaan

Rev

Rev 12978 | 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;
12978 kshitij.so 17
import java.util.Arrays;
6050 anupam.sin 18
import java.util.Date;
6266 anupam.sin 19
import java.util.HashMap;
12978 kshitij.so 20
import java.util.List;
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
 
6266 anupam.sin 30
//    private static final String SHOOGLOO_AFF_NAME = "shoogloo";
6050 anupam.sin 31
 
32
    private static Logger log = Logger.getLogger(Class.class);
33
    private FormattingUtils formattingUtils = new FormattingUtils();
34
 
6266 anupam.sin 35
    //private boolean isShooglooAff  = false;
36
 
6056 amit.gupta 37
    private static Map<Long,String> allProviders;
12978 kshitij.so 38
    private static List<Long> asyncOperators = Arrays.asList(9l);
12985 kshitij.so 39
    private boolean rechargeModeAsynchronous = false;
12978 kshitij.so 40
    private String rechargeOrderId;
41
    private String isFinal;
42
    private String newStatus;
43
 
44
    public String getIsFinal() {
45
        return isFinal;
46
    }
47
 
48
    public void setIsFinal(String isFinal) {
49
        this.isFinal = isFinal;
50
    }
51
 
6056 amit.gupta 52
    static {
53
    	try {
6206 rajveer 54
			allProviders = new TransactionClient().getClient().getServiceProviders(RechargeType.DTH, true);
55
			allProviders.putAll(new TransactionClient().getClient().getServiceProviders(RechargeType.MOBILE, true));
6056 amit.gupta 56
		} catch (TTransportException e) {
57
			// TODO Auto-generated catch block
58
			e.printStackTrace();
59
		} catch (TException e) {
60
			// TODO Auto-generated catch block
61
			e.printStackTrace();
62
		}
63
    }
6266 anupam.sin 64
 
65
    private static Map<String, String> FailureReasonMap;
66
    static {
67
        FailureReasonMap = new HashMap<String, String>();
68
        FailureReasonMap.put("Invalid Amount", "the amount you tried seems to be invalid. Please try another amount.");
69
        FailureReasonMap.put("Customer Exceeded Daily Limit", "you have exceeded daily recharge limit for this number. Please try after 12 hours.");
6398 rajveer 70
        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 71
    }
72
 
6050 anupam.sin 73
    long merchantPaymentId;
74
    RechargeOrder rechargeOrder = null;
75
    String message = null;
76
 
77
    public RechargeResultController(){
78
        super();
79
    }
6434 anupam.sin 80
 
6050 anupam.sin 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;
11829 amit.gupta 164
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false, userinfo.isPrivateDealUser());
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(){
8076 anupam.sin 185
        RechargeOrderStatus status = null;
186
        if(rechargeOrder == null) {
187
            status = RechargeOrderStatus.INIT;
188
        } else {
189
            status = rechargeOrder.getStatus();
190
        }
191
 
6056 amit.gupta 192
    	if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
12616 anikendra 193
//    		DataLogger.logData(EventType.RECHARGE_PAYMENT_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
194
//    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
195
//                    rechargeOrder.getRechargeType().toString());
6145 anupam.sin 196
    		return new String[]{"red", "PAYMENT FAILED", "Payment failed at the payment gateway. Try to <a href='/recharge'>recharge again</a>"};
6056 amit.gupta 197
    	}
8076 anupam.sin 198
    	else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL) || status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
12978 kshitij.so 199
    	    if (status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)){
12616 anikendra 200
//    	    DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
201
//                    Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
202
//                    rechargeOrder.getRechargeType().toString());
6215 anupam.sin 203
    	    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>" +
204
                    "We have put your recharge under process. As soon as we get a confirmation on this transaction, we will notify you.<br>" +
205
                    "In case of recharge failure your entire amount will be credited to your " +
206
                    "<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
207
                    "will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
208
                    " Saholic.com please use your username and password to Login.<br> In case you are " +
209
                    "a first time user, we have sent your login details on the emailId you gave us. " +
210
                    "<br>For more information <a href='/static/recharge-faq'>click here</a>"};
12978 kshitij.so 211
    	    }
212
    	    else{
12985 kshitij.so 213
    	        setRechargeModeAsynchronous(true);
12978 kshitij.so 214
    	        return new String[]{"green", "RECHARGE IN PROCESS", "Your Payment is successful.We have put your recharge under process." +
215
                        "Please wait while we check with the operator.<b>Please do not close</b> this window or click the Back button on your browser.<br>"};
216
    	    }
6215 anupam.sin 217
    	}
8076 anupam.sin 218
    	else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED) || status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)){
12616 anikendra 219
//    		DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
220
//    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
221
//    				rechargeOrder.getRechargeType().toString());
6266 anupam.sin 222
    		String displayReason = FailureReasonMap.get(rechargeOrder.getDescription());
223
    		if (!(displayReason == null || displayReason.isEmpty())) {
224
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed because " + displayReason + "<br>";
225
    		} else {
226
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed.<br>";
227
    		}
228
			return new String[]{"red", "RECHARGE FAILED", displayReason +
6145 anupam.sin 229
					"Don't worry your payment is safe with us. The entire Amount has been refunded to your " +
230
					"<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
231
					"will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
232
					" Saholic.com please use your username and password to Login.<br> In case you are " +
233
					"a first time user, we have sent your login details on the emailId you gave us. " +
6146 anupam.sin 234
					"<br>For more information <a href='/static/recharge-faq'>click here</a>"};
8076 anupam.sin 235
    	} else if(status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)){
12616 anikendra 236
//    		DataLogger.logData(EventType.RECHARGE_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
237
//    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
238
//    				rechargeOrder.getRechargeType().toString());
6323 rajveer 239
 
6336 anupam.sin 240
    		String operator = "";
241
    		if(isDTH()) {
242
    		    operator = getProvider();
243
    		} else {
244
    		    operator = getProvider() + " mobile";
245
    		}
246
 
247
    		String original_referer = "http://www.saholic.com/recharge-result?paymentId=" + this.request.getParameter("paymentId");
6323 rajveer 248
    		String fbUrl = "";
6336 anupam.sin 249
    		String twitterUrl = "";
250
    		String shareMsg = "";
6323 rajveer 251
    		String giftMessage = "Congratulations! Your device is successfully recharged.";
6438 rajveer 252
    	    shareMsg = "I just recharged my " + operator + " at Saholic.com and am happy with their services.";
6490 rajveer 253
			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 254
			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";
255
			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 256
					"<br><br>" +
6447 rajveer 257
                    "Advertising is expensive ! If you like our site, please help us spread the word by Tweeting and posting to Facebook." +
6438 rajveer 258
					"<div style=\"margin-top: 40px;text-align: center;\">" +
259
					"<a target='_blank' href='" + twitterUrl + "'>" + 
6447 rajveer 260
							"<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 261
					"<a target='_blank' href='" + fbUrl + "'>" +
6447 rajveer 262
							"<input type=\"button\" onclick=\"trackEventWithGA('Recharge', 'Facebook Share', '');\" style=\"background:url('/unversioned/images/Post-On-Facebook.jpg');width: 160px;height: 40px;\">" +
6438 rajveer 263
							"</a></div>";
264
		return new String[] {"", "SUCCESS", giftMessage};
8076 anupam.sin 265
    	} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED) || status.equals(RechargeOrderStatus.REFUNDED)) {
266
            return new String[]{"red", "PAYMENT REFUNDED",
267
                    "The payment associated with this recharge order has been refunded. You can check the details by " +
268
                    "<a href='/my-recharges'>clicking here</a>.<br><br>"};
269
    	} else {
270
    	    return new String[]{"red", "ERROR", "INVALID INPUT"};
6056 amit.gupta 271
    	}
272
    }
12978 kshitij.so 273
 
274
    public String getRechargeStatus() throws NumberFormatException, TException{
275
        TransactionClient transactionServiceClient = null;
276
        transactionServiceClient = new TransactionClient();
277
        RechargeOrder t_rechargeOrder = transactionServiceClient.getClient().getRcgOrderStatus(Long.valueOf(rechargeOrderId), Boolean.valueOf(isFinal));
278
        setNewStatus(t_rechargeOrder.getStatus().name());
279
        return "recharge-status";
280
    }
281
 
12985 kshitij.so 282
    public void setRechargeModeAsynchronous(boolean rechargeModeAsynchronous) {
283
        this.rechargeModeAsynchronous = rechargeModeAsynchronous;
12978 kshitij.so 284
    }
285
 
12985 kshitij.so 286
    public boolean isRechargeModeAsynchronous() {
287
        return rechargeModeAsynchronous;
12978 kshitij.so 288
    }
289
 
290
    public String getRechargeOrderId() {
291
        return rechargeOrderId;
292
    }
293
 
294
    public void setRechargeOrderId(String rechargeOrderId) {
295
        this.rechargeOrderId = rechargeOrderId;
296
    }
297
 
298
    public void setNewStatus(String newStatus) {
299
        this.newStatus = newStatus;
300
    }
301
 
302
    public String getNewStatus() {
303
        return newStatus;
304
    }
6050 anupam.sin 305
}