Subversion Repositories SmartDukaan

Rev

Rev 6326 | Rev 6336 | 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.payments.PaymentException;
9
import in.shop2020.serving.utils.FormattingUtils;
10
import in.shop2020.thrift.clients.PaymentClient;
11
import in.shop2020.thrift.clients.TransactionClient;
6108 amar.kumar 12
import in.shop2020.utils.DataLogger;
6050 anupam.sin 13
 
14
import java.text.SimpleDateFormat;
15
import java.util.Date;
6266 anupam.sin 16
import java.util.HashMap;
6056 amit.gupta 17
import java.util.Map;
6050 anupam.sin 18
 
19
import org.apache.log4j.Logger;
20
import org.apache.thrift.TException;
6056 amit.gupta 21
import org.apache.thrift.transport.TTransportException;
6050 anupam.sin 22
 
23
public class RechargeResultController extends BaseController {
24
    private static final long serialVersionUID = 1L;
25
 
6266 anupam.sin 26
//    private static final String SHOOGLOO_AFF_NAME = "shoogloo";
6050 anupam.sin 27
 
28
    private static Logger log = Logger.getLogger(Class.class);
29
    private FormattingUtils formattingUtils = new FormattingUtils();
30
 
6266 anupam.sin 31
    //private boolean isShooglooAff  = false;
32
 
6056 amit.gupta 33
    private static Map<Long,String> allProviders;
34
    static {
35
    	try {
6206 rajveer 36
			allProviders = new TransactionClient().getClient().getServiceProviders(RechargeType.DTH, true);
37
			allProviders.putAll(new TransactionClient().getClient().getServiceProviders(RechargeType.MOBILE, true));
6056 amit.gupta 38
		} catch (TTransportException e) {
39
			// TODO Auto-generated catch block
40
			e.printStackTrace();
41
		} catch (TException e) {
42
			// TODO Auto-generated catch block
43
			e.printStackTrace();
44
		}
45
    }
6266 anupam.sin 46
 
47
    private static Map<String, String> FailureReasonMap;
48
    static {
49
        FailureReasonMap = new HashMap<String, String>();
50
        FailureReasonMap.put("Invalid Amount", "the amount you tried seems to be invalid. Please try another amount.");
51
        FailureReasonMap.put("Customer Exceeded Daily Limit", "you have exceeded daily recharge limit for this number. Please try after 12 hours.");
52
        FailureReasonMap.put("Invalid Mobile Number", "it seems that your mobile number is not being recognized by selected operator. Please make sure the number you tried is correct.");
53
    }
54
 
6050 anupam.sin 55
    long merchantPaymentId;
56
    RechargeOrder rechargeOrder = null;
57
    String message = null;
58
 
59
    public RechargeResultController(){
60
        super();
61
    }
62
 
63
    public String index() {
64
        PaymentClient paymentServiceClient = null;
65
        TransactionClient transactionServiceClient = null;
6266 anupam.sin 66
        //UserClient userServiceClient = null;
6050 anupam.sin 67
 
68
        try {
69
            paymentServiceClient = new PaymentClient();
70
            transactionServiceClient = new TransactionClient();
6266 anupam.sin 71
          //  userServiceClient = new UserClient();
6050 anupam.sin 72
        } catch (Exception e1) {
73
            // TODO Nothing to worry
74
            log.error("Unable to initialize the client for either payment or transaction or user service", e1);
75
        }
76
 
6123 anupam.sin 77
        String returningPayIdString = this.request.getParameter("paymentId");
78
        if (returningPayIdString == null || returningPayIdString.isEmpty()) {
79
            this.message = "Payment Failed at payment gateway. Please try again.";
80
            return "index";
81
        }
6050 anupam.sin 82
        merchantPaymentId = Long.parseLong(this.request.getParameter("paymentId"));
83
        long txnId;
84
        try {
85
            txnId = paymentServiceClient.getClient().getPayment(merchantPaymentId).getMerchantTxnId();
86
            rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
87
 
88
            this.message = "You have successfully recharged your mobile.";
89
 
6266 anupam.sin 90
            /*if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
6050 anupam.sin 91
                long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
92
                userServiceClient.getClient().addTrackLog(affId, userinfo.getUserId(), TrackLogType.PAYMENT_SUCCESS, "",
93
                        Long.toString(merchantPaymentId), (new Date()).getTime());
94
                Affiliate aff = userServiceClient.getClient().getAffiliateById(affId);
95
                if (aff.getName().equals(SHOOGLOO_AFF_NAME)) {
96
                    isShooglooAff = true;
97
                }
6266 anupam.sin 98
            }*/
6050 anupam.sin 99
 
100
        } catch (PaymentException e) {
101
            log.error("Payment service not responding. Payment id is" + merchantPaymentId, e);
102
        } catch (TException e) {
103
            log.error("Thrift service exception. Payment id is" + merchantPaymentId, e);
104
        } catch (TransactionServiceException e) {
105
            log.error("Transaction service exception. Payment id is" + merchantPaymentId, e);
106
        }
6266 anupam.sin 107
 
108
//        catch (UserAffiliateException e) {
109
//            log.error("Affiliate service exception. Payment id is" + merchantPaymentId, e);
110
//        }
6050 anupam.sin 111
 
112
        return "index";
113
    }
114
 
115
    public String formatPrice(double price)    {
116
        return formattingUtils.formatPrice(price);
117
    }
118
 
119
    public String getMessage(){
120
        return this.message;
121
    }
122
 
6266 anupam.sin 123
//    public boolean getIsShooglooAff() {
124
//        return this.isShooglooAff;
125
//    }
6050 anupam.sin 126
 
127
    public long getMerchantPaymentId() {
128
        return merchantPaymentId;
129
    }
130
 
131
    public static String formatDate(long timestamp){
132
        SimpleDateFormat dateformat = new SimpleDateFormat("dd MMMM yyyy");
133
        return dateformat.format(new Date(timestamp));  
134
    }
135
 
136
 
137
    @Override
138
    public String getHeaderSnippet() {
139
        String url = request.getQueryString();
140
        if (url == null) {
141
            url = "";
142
        } else {
143
            url = "?" + url;
144
        }
145
        url = request.getRequestURI() + url;
6152 amit.gupta 146
        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
6050 anupam.sin 147
    }
148
 
149
    public RechargeOrder getRechargeOrder() {
150
        return rechargeOrder;
151
    }
152
 
153
    public void setRechargeOrder(RechargeOrder rechargeOrder) {
154
        this.rechargeOrder = rechargeOrder;
155
    }
6056 amit.gupta 156
 
157
 
158
    public boolean isDTH() {
159
    	return rechargeOrder.getRechargeType().equals(RechargeType.DTH);
160
    }
161
 
162
    public String getProvider() {
163
    	return allProviders.get(rechargeOrder.getOperatorId());
164
    }
165
 
166
    public String[] getOrderStatus(){
167
    	RechargeOrderStatus status = rechargeOrder.getStatus();
168
    	if(status.equals(RechargeOrderStatus.PAYMENT_FAILED)||status.equals(RechargeOrderStatus.PAYMENT_PENDING)){
6108 amar.kumar 169
    		DataLogger.logData(EventType.RECHARGE_PAYMENT_FAILED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
170
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
171
                    rechargeOrder.getRechargeType().toString());
6145 anupam.sin 172
    		return new String[]{"red", "PAYMENT FAILED", "Payment failed at the payment gateway. Try to <a href='/recharge'>recharge again</a>"};
6056 amit.gupta 173
    	}
6215 anupam.sin 174
    	else if(status.equals(RechargeOrderStatus.PAYMENT_SUCCESSFUL)) {
175
    	    DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
176
                    Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
177
                    rechargeOrder.getRechargeType().toString());
178
    	    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>" +
179
                    "We have put your recharge under process. As soon as we get a confirmation on this transaction, we will notify you.<br>" +
180
                    "In case of recharge failure your entire amount will be credited to your " +
181
                    "<a href='/my-wallet'>recharge wallet</a>.<br><br> You " +
182
                    "will need to <a href='/login?redirectUrl=/recharge'>login</a> to use your Wallet.<br> If you are registered on" + 
183
                    " Saholic.com please use your username and password to Login.<br> In case you are " +
184
                    "a first time user, we have sent your login details on the emailId you gave us. " +
185
                    "<br>For more information <a href='/static/recharge-faq'>click here</a>"};
186
    	}
187
    	else if (status.equals(RechargeOrderStatus.RECHARGE_FAILED)){
6108 amar.kumar 188
    		DataLogger.logData(EventType.RECHARGE_PAYMENT_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
189
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
190
    				rechargeOrder.getRechargeType().toString());
6266 anupam.sin 191
    		String displayReason = FailureReasonMap.get(rechargeOrder.getDescription());
192
    		if (!(displayReason == null || displayReason.isEmpty())) {
193
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed because " + displayReason + "<br>";
194
    		} else {
195
    		    displayReason = "Your Payment was successful but unfortunately the recharge failed.<br>";
196
    		}
197
			return new String[]{"red", "RECHARGE FAILED", displayReason +
6145 anupam.sin 198
					"Don't worry your payment is safe with us. The entire Amount has been refunded 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. " +
6146 anupam.sin 203
					"<br>For more information <a href='/static/recharge-faq'>click here</a>"};
6056 amit.gupta 204
    	} else {
6108 amar.kumar 205
    		DataLogger.logData(EventType.RECHARGE_SUCCESSFUL, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
206
    				Long.toString(rechargeOrder.getOperatorId()), Long.toString(rechargeOrder.getTotalAmount()), Long.toString(rechargeOrder.getWalletAmount()),
207
    				rechargeOrder.getRechargeType().toString());
6323 rajveer 208
 
209
    		String fbUrl = "";
210
    		String giftMessage = "Congratulations! Your device is successfully recharged.";
211
    		if(rechargeOrder.getTotalAmount()>=100){
212
    			if(rechargeOrder.getTotalAmount()>=500){
6327 rajveer 213
    				fbUrl = "http://www.facebook.com/dialog/feed?app_id=291830044248933&link=www.saholic.com/static/giftvoucher-offer?afid=6666&picture=http://i1074.photobucket.com/albums/w407/amathur2k/logo_saholic.png&name=I just won a Rs.500 Coupon Code&caption=Win Assured Gift Coupons worth 100% on every Mobile or DTH Recharge&description=One Click Hassle Free Recharges&redirect_uri=http://saholic.com/?shared=1";
6324 rajveer 214
    				giftMessage = "Congratulations! Your device is successfully recharged and you have won Gift Coupons worth Rs.500 which you can use to shop for Mobiles, Laptops, Cameras, Accessories. Please help us Spread the word by Sharing on Facebook <a href='" + fbUrl + "'><img width='155' height='35' src='/images/Post-On-Facebook-6323.jpg'></a>";
6323 rajveer 215
    			}else{
6327 rajveer 216
    				fbUrl = "http://www.facebook.com/dialog/feed?app_id=291830044248933&link=www.saholic.com/static/giftvoucher-offer?afid=6666&picture=http://i1074.photobucket.com/albums/w407/amathur2k/logo_saholic.png&name=I just won a Rs. " + rechargeOrder.getTotalAmount() + " Coupon Code&caption=Win Assured Gift Coupons worth 100% on every Mobile or DTH Recharge&description=One Click Hassle Free Recharges&redirect_uri=http://saholic.com/?shared=1";
6324 rajveer 217
    				giftMessage = "Congratulations! Your device is successfully recharged and you have won Gift Coupons worth Rs." + rechargeOrder.getTotalAmount() + " which you can use to shop for Mobiles, Laptops, Cameras, Accessories. Please help us Spread the word by Sharing on Facebook <a href='" + fbUrl + "'><img width='155' height='35' src='/images/Post-On-Facebook-6323.jpg'></a>";
6323 rajveer 218
    			}
219
    		}
6325 rajveer 220
    		return new String[] {"", "SUCCESS", giftMessage};
6056 amit.gupta 221
    	}
222
    }
6050 anupam.sin 223
}