Subversion Repositories SmartDukaan

Rev

Rev 11829 | Rev 12978 | 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
 
6050 anupam.sin 65
    public String index() {
66
        PaymentClient paymentServiceClient = null;
67
        TransactionClient transactionServiceClient = null;
6266 anupam.sin 68
        //UserClient userServiceClient = null;
6050 anupam.sin 69
 
70
        try {
71
            paymentServiceClient = new PaymentClient();
72
            transactionServiceClient = new TransactionClient();
6266 anupam.sin 73
          //  userServiceClient = new UserClient();
6050 anupam.sin 74
        } catch (Exception e1) {
75
            // TODO Nothing to worry
76
            log.error("Unable to initialize the client for either payment or transaction or user service", e1);
77
        }
78
 
6123 anupam.sin 79
        String returningPayIdString = this.request.getParameter("paymentId");
80
        if (returningPayIdString == null || returningPayIdString.isEmpty()) {
81
            this.message = "Payment Failed at payment gateway. Please try again.";
82
            return "index";
83
        }
6050 anupam.sin 84
        merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
85
        long txnId;
86
        try {
87
            txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
88
            rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
89
 
90
            this.message = "You have successfully recharged your mobile.";
91
 
6266 anupam.sin 92
            /*if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
6050 anupam.sin 93
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
94
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
95
                        Long.toString(merchantPaymentId), (new Date()).getTime());
96
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
97
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
98
                    isShooglooAff = true;
99
                }
6266 anupam.sin 100
            }*/
6050 anupam.sin 101
 
102
        } catch (PaymentException e) {
103
            log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
104
        } catch (TException e) {
105
            log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
106
        } catch (TransactionServiceException e) {
107
            log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
108
        }
6266 anupam.sin 109
 
110
//        catch (UserAffiliateException e) {
111
//            log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
112
//        }
6050 anupam.sin 113
 
114
        return "index";
115
    }
116
 
117
    public String formatPrice(double price)    {
118
        return formattingUtils.formatPrice(price);
119
    }
120
 
121
    public String getMessage(){
122
        return this.message;
123
    }
124
 
6266 anupam.sin 125
//    public boolean getIsShooglooAff() {
126
//        return this.isShooglooAff;
127
//    }
6050 anupam.sin 128
 
129
    public long getMerchantPaymentId() {
130
        return merchantPaymentId;
131
    }
132
 
133
    public static String formatDate(long timestamp){
134
        SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
135
        return dateformat.format(new Date(timestamp));  
136
    }
137
 
138
 
139
    @Override
140
    public String getHeaderSnippet() {
141
        String url = request.getQueryString();
142
        if (url == null) {
143
            url = "";
144
        } else {
145
            url = "?" + url;
146
        }
147
        url = request.getRequestURI() + url;
11829 amit.gupta 148
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false, userinfo.isPrivateDealUser());
6050 anupam.sin 149
    }
150
 
151
    public RechargeOrder getRechargeOrder() {
152
        return rechargeOrder;
153
    }
154
 
155
    public void setRechargeOrder(RechargeOrder rechargeOrder) {
156
        this.rechargeOrder = rechargeOrder;
157
    }
6056 amit.gupta 158
 
159
 
160
    public boolean isDTH() {
161
    	return rechargeOrder.getRechargeType().equals(RechargeType.DTH);
162
    }
163
 
164
    public String getProvider() {
165
    	return allProviders.get(rechargeOrder.getOperatorId());
166
    }
167
 
168
    public String[] getOrderStatus(){
8076 anupam.sin 169
        RechargeOrderStatus status = null;
170
        if(rechargeOrder == null) {
171
            status = RechargeOrderStatus.INIT;
172
        } else {
173
            status = rechargeOrder.getStatus();
174
        }
175
 
6056 amit.gupta 176
    	if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
12616 anikendra 177
//    		DataLogger.logData(EventType.RECHARGE_PAYMENT_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
178
//    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
179
//                    rechargeOrder.getRechargeType().toString());
6145 anupam.sin 180
    		return new String[]{"red", "PAYMENT FAILED", "Payment failed at the payment gateway. Try to <a href='/recharge'>recharge again</a>"};
6056 amit.gupta 181
    	}
8076 anupam.sin 182
    	else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL) || status.equals(RechargeOrderStatus.RECHARGE_UNKNOWN)) {
12616 anikendra 183
//    	    DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
184
//                    Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
185
//                    rechargeOrder.getRechargeType().toString());
6215 anupam.sin 186
    	    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>" +
187
                    "We have put your recharge under process. As soon as we get a confirmation on this transaction, we will notify you.<br>" +
188
                    "In case of recharge failure your entire amount will be credited to your " +
189
                    "<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
190
                    "will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
191
                    " Saholic.com please use your username and password to Login.<br> In case you are " +
192
                    "a first time user, we have sent your login details on the emailId you gave us. " +
193
                    "<br>For more information <a href='/static/recharge-faq'>click here</a>"};
194
    	}
8076 anupam.sin 195
    	else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED) || status.equals(RechargeOrderStatus.RECHARGE_FAILED_REFUNDED)){
12616 anikendra 196
//    		DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
197
//    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
198
//    				rechargeOrder.getRechargeType().toString());
6266 anupam.sin 199
    		String displayReason = FailureReasonMap.get(rechargeOrder.getDescription());
200
    		if (!(displayReason == null || displayReason.isEmpty())) {
201
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed because " + displayReason + "<br>";
202
    		} else {
203
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed.<br>";
204
    		}
205
			return new String[]{"red", "RECHARGE FAILED", displayReason +
6145 anupam.sin 206
					"Don't worry your payment is safe with us. The entire Amount has been refunded to your " +
207
					"<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
208
					"will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
209
					" Saholic.com please use your username and password to Login.<br> In case you are " +
210
					"a first time user, we have sent your login details on the emailId you gave us. " +
6146 anupam.sin 211
					"<br>For more information <a href='/static/recharge-faq'>click here</a>"};
8076 anupam.sin 212
    	} else if(status.equals(RechargeOrderStatus.RECHARGE_SUCCESSFUL)){
12616 anikendra 213
//    		DataLogger.logData(EventType.RECHARGE_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
214
//    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
215
//    				rechargeOrder.getRechargeType().toString());
6323 rajveer 216
 
6336 anupam.sin 217
    		String operator = "";
218
    		if(isDTH()) {
219
    		    operator = getProvider();
220
    		} else {
221
    		    operator = getProvider() + " mobile";
222
    		}
223
 
224
    		String original_referer = "http://www.saholic.com/recharge-result?paymentId=" + this.request.getParameter("paymentId");
6323 rajveer 225
    		String fbUrl = "";
6336 anupam.sin 226
    		String twitterUrl = "";
227
    		String shareMsg = "";
6323 rajveer 228
    		String giftMessage = "Congratulations! Your device is successfully recharged.";
6438 rajveer 229
    	    shareMsg = "I just recharged my " + operator + " at Saholic.com and am happy with their services.";
6490 rajveer 230
			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 231
			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";
232
			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 233
					"<br><br>" +
6447 rajveer 234
                    "Advertising is expensive ! If you like our site, please help us spread the word by Tweeting and posting to Facebook." +
6438 rajveer 235
					"<div style=\"margin-top: 40px;text-align: center;\">" +
236
					"<a target='_blank' href='" + twitterUrl + "'>" + 
6447 rajveer 237
							"<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 238
					"<a target='_blank' href='" + fbUrl + "'>" +
6447 rajveer 239
							"<input type=\"button\" onclick=\"trackEventWithGA('Recharge', 'Facebook Share', '');\" style=\"background:url('/unversioned/images/Post-On-Facebook.jpg');width: 160px;height: 40px;\">" +
6438 rajveer 240
							"</a></div>";
241
		return new String[] {"", "SUCCESS", giftMessage};
8076 anupam.sin 242
    	} else if (status.equals(RechargeOrderStatus.PARTIALLY_REFUNDED) || status.equals(RechargeOrderStatus.REFUNDED)) {
243
            return new String[]{"red", "PAYMENT REFUNDED",
244
                    "The payment associated with this recharge order has been refunded. You can check the details by " +
245
                    "<a href='/my-recharges'>clicking here</a>.<br><br>"};
246
    	} else {
247
    	    return new String[]{"red", "ERROR", "INVALID INPUT"};
6056 amit.gupta 248
    	}
249
    }
6050 anupam.sin 250
}