Subversion Repositories SmartDukaan

Rev

Rev 12788 | Rev 13380 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12788 Rev 13372
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.config.ConfigException;
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.datalogger.EventType;
4
import in.shop2020.model.v1.order.RechargeOrder;
-
 
5
import in.shop2020.model.v1.order.RechargeOrderStatus;
5
import in.shop2020.payments.Attribute;
6
import in.shop2020.payments.Attribute;
6
import in.shop2020.payments.Payment;
7
import in.shop2020.payments.Payment;
7
import in.shop2020.payments.PaymentException;
8
import in.shop2020.payments.PaymentException;
8
import in.shop2020.payments.PaymentStatus;
9
import in.shop2020.payments.PaymentStatus;
9
import in.shop2020.serving.services.CommonPaymentService;
10
import in.shop2020.serving.services.CommonPaymentService;
10
import in.shop2020.thrift.clients.PaymentClient;
11
import in.shop2020.thrift.clients.PaymentClient;
11
import in.shop2020.thrift.clients.TransactionClient;
12
import in.shop2020.thrift.clients.TransactionClient;
12
import in.shop2020.thrift.clients.UserClient;
-
 
13
import in.shop2020.thrift.clients.config.ConfigClient;
13
import in.shop2020.thrift.clients.config.ConfigClient;
14
import in.shop2020.utils.DataLogger;
-
 
15
 
14
 
16
import java.security.MessageDigest;
15
import java.security.MessageDigest;
17
import java.security.NoSuchAlgorithmException;
16
import java.security.NoSuchAlgorithmException;
-
 
17
import java.text.SimpleDateFormat;
18
import java.util.ArrayList;
18
import java.util.ArrayList;
-
 
19
import java.util.Date;
19
import java.util.List;
20
import java.util.List;
20
import java.util.Map;
21
import java.util.Map;
21
import java.util.TreeMap;
22
import java.util.TreeMap;
22
 
23
 
23
import javax.servlet.http.HttpServletRequest;
24
import javax.servlet.http.HttpServletRequest;
Line 28... Line 29...
28
@SuppressWarnings("serial")
29
@SuppressWarnings("serial")
29
public class PayuPayResponseController extends BaseController{
30
public class PayuPayResponseController extends BaseController{
30
 
31
 
31
	private static Logger log = Logger.getLogger(Class.class);
32
	private static Logger log = Logger.getLogger(Class.class);
32
 
33
 
33
	private static final String BANK_REF_ID = "BankRefId";
-
 
34
	private static final String UNMAPPED_STATUS = "UnMappedStatus";
34
	private static final String UNMAPPED_STATUS = "UnMappedStatus";
35
	
35
	
36
	private static String successUrl;
36
	private static String successUrl;
37
	private static String errorUrl;
37
	private static String errorUrl;
-
 
38
	private static String rechargeResultUri;
38
 
39
 
39
	/**
40
	/**
40
	 * The secret key used to decode RC4 encoded data.
41
	 * The secret key used to decode RC4 encoded data.
41
	 */
42
	 */
42
	private static String accountKey;
43
	private static String accountKey;
43
 
44
 
44
	private static String salt;
45
	private static String salt;
45
	
46
	
46
	private String redirectUrl;
47
	private String redirectUrl;
-
 
48
	
-
 
49
	private String eurl;
-
 
50
	private String surl;
47
 
51
 
48
	static{
52
	static{
49
		try {
53
		try {
-
 
54
			ConfigClient cc = ConfigClient.getClient();
50
			successUrl = ConfigClient.getClient().get("ebs_success_url");
55
			successUrl = cc.get("payu_success_url");
51
			errorUrl = ConfigClient.getClient().get("ebs_error_url");
56
			errorUrl = cc.get("payu_error_url");
52
			accountKey = ConfigClient.getClient().get("payu_account_id");
57
			accountKey = cc.get("payu_account_id");
-
 
58
			salt = cc.get("payu_secret_key");
53
			salt = ConfigClient.getClient().get("payu_secret_key");
59
			rechargeResultUri = ConfigClient.getClient().get("recharge_result_uri");
54
		} catch (ConfigException e) {
60
		} catch (ConfigException e) {
55
			log.error("Unable to get success and error usr info from config server.");
61
			log.error("Unable to get success and error usr info from config server.");
56
		}
62
		}
57
	}
63
	}
58
 
64
 
59
	private Map<String, String> paymentParams = new TreeMap<String, String>();
65
	private Map<String, String> paymentParams = new TreeMap<String, String>();
60
 
66
 
61
	public String create() {
67
	public String index() {
62
//		String gatewayTxnId = request.getParameter("mihpayid");
-
 
63
//		String status = request.getParameter("status");
-
 
64
//		String key = request.getParameter("key");
68
		eurl = errorUrl;
65
//		String mode = request.getParameter("mode");
69
		surl = successUrl;
66
//		String txnid = request.getParameter("txnid");
-
 
67
//		String amount = request.getParameter("amount");
-
 
68
//		String hash  = request.getParameter("hash");
-
 
69
//		String bank_ref_num  = request.getParameter("bank_ref_num");
-
 
70
//		String PG_TYPE  = request.getParameter("PG_TYPE");
-
 
71
//		String Error  = request.getParameter("Error");
-
 
72
//		String unmappedstatus  = request.getParameter("unmappedstatus");
-
 
73
//		
-
 
74
		updatePaymentParams(request.getParameterMap());
70
		updatePaymentParams(request.getParameterMap());
75
 
71
 
76
		PaymentClient paymentServiceClient = null;
72
		PaymentClient paymentServiceClient = null;
77
		TransactionClient transactionServiceClient = null;
73
		TransactionClient transactionServiceClient = null;
78
		UserClient userServiceClient = null;
-
 
79
		try {
74
		try {
80
			paymentServiceClient = new PaymentClient();
75
			paymentServiceClient = new PaymentClient();
81
			transactionServiceClient = new TransactionClient();
76
			transactionServiceClient = new TransactionClient();
82
			userServiceClient = new UserClient();
-
 
83
		} catch (Exception e) {
77
		} catch (Exception e) {
84
			log.error("Unable to initialize one of the clients", e);
78
			log.error("Unable to initialize one of the clients", e);
85
		}
79
		}
86
 
80
 
87
 
81
 
Line 93... Line 87...
93
		String bankRefId = paymentParams.get("bank_ref_num");
87
		String bankRefId = paymentParams.get("bank_ref_num");
94
		String unmappedStatus  = paymentParams.get("unmappedstatus");
88
		String unmappedStatus  = paymentParams.get("unmappedstatus");
95
		
89
		
96
 
90
 
97
		List<Attribute> attributes = new ArrayList<Attribute>();
91
		List<Attribute> attributes = new ArrayList<Attribute>();
98
		attributes.add(new Attribute(BANK_REF_ID, bankRefId));
-
 
99
		attributes.add(new Attribute(UNMAPPED_STATUS, unmappedStatus));
92
		attributes.add(new Attribute(UNMAPPED_STATUS, unmappedStatus));
100
 
93
 
101
		Payment payment = null;
94
		Payment payment = null;
102
		Long txnId = null;
95
		Long txnId = null;
103
		try {
96
		try {
Line 106... Line 99...
106
		} catch (PaymentException e1) {
99
		} catch (PaymentException e1) {
107
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
100
			log.error("Payment exception. It is serious, check merchant payment id + " + merchantPaymentId, e1);
108
		} catch (TException e1) {
101
		} catch (TException e1) {
109
			log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
102
			log.error("Thrift exception. Check payment id "+ merchantPaymentId, e1);
110
		}
103
		}
-
 
104
		
-
 
105
		if(payment.getStatus() != PaymentStatus.INIT){
-
 
106
			// We have already processed a response for this payment. Processing
-
 
107
			// it again may fail his orders. So, let's ask him to check his
-
 
108
			// account.
-
 
109
			return "ebs-pay-response-maybe";
-
 
110
		}
111
 
111
 
-
 
112
		if(payment.isIsDigital()){
-
 
113
			eurl = rechargeResultUri;
-
 
114
			surl = rechargeResultUri;
-
 
115
		}
-
 
116
		
112
		if(!validatePaymentParams(amount, payment, hash)){
117
		if(!validatePaymentParams(amount, payment, hash)){
113
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
118
			this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
114
			return "index";
119
			return "index";
115
		}
120
		}
116
 
121
 
117
		if(gatewayTxnStatus.equalsIgnoreCase("SUCCESS")){
122
		if(gatewayTxnStatus.equalsIgnoreCase("SUCCESS")){
118
			//Update payment status as authorized if payment is authorized.
-
 
119
			try {
123
			try {
120
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
124
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
121
						"", gatewayTxnStatus, "Transaction Authorized at PG", "", "", "", "", PaymentStatus.AUTHORIZED, "", attributes);
125
						"", gatewayTxnStatus, unmappedStatus.equals("captured") ? "Transaction captured at PG":"Transaction Authorized at PG", 
-
 
126
						"", "", bankRefId, "", unmappedStatus.equals("captured") ? PaymentStatus.SUCCESS : PaymentStatus.AUTHORIZED, new SimpleDateFormat().format(new Date()), attributes);
122
			} catch (PaymentException e) {
127
			} catch (PaymentException e) {
123
				log.error("Unable to mark the payment as authorized", e);
128
				this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
-
 
129
				return "index";
124
			} catch (TException e) {
130
			} catch (TException e) {
125
			    log.error("Unable to mark the payment as authorized", e);
131
				this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
-
 
132
				return "index";
126
			}
133
			}
127
 
-
 
128
 
-
 
-
 
134
            if(payment.isIsDigital()){
-
 
135
            	RechargeOrder rechargeOrder = null;
-
 
136
                try {
129
			//CommonPaymentService.processSuccessfulTxn(txnId, userServiceClient, transactionServiceClient, false);
137
                    rechargeOrder = transactionServiceClient.getClient().getRechargeOrdersForTransaction(txnId);
-
 
138
                } catch (Exception e1) {
-
 
139
                    log.error("Problem with txn client while getting recharge object", e1);
130
            this.redirectUrl = successUrl + "?paymentId=" + merchantPaymentId;	
140
    				this.redirectUrl = eurl + "?paymentId=" + merchantPaymentId;
-
 
141
    				return "index";
-
 
142
                }
-
 
143
                
-
 
144
		        try {
-
 
145
		            PaymentClient pcl = new PaymentClient();
-
 
146
		            boolean isCaptured = pcl.getClient().capturePayment(txnId, true);
-
 
147
		            
-
 
148
		            //Retry in case we are not able to capture first time
-
 
149
		            if(!isCaptured){
-
 
150
		            	Thread.sleep(2000);
-
 
151
		            	isCaptured = pcl.getClient().capturePayment(txnId, true);
-
 
152
		            }
-
 
153
		            if(!isCaptured){
-
 
154
		            	Thread.sleep(2000);
-
 
155
		            	isCaptured = pcl.getClient().capturePayment(txnId, true);
-
 
156
		            }
-
 
157
		            
-
 
158
		            if(isCaptured) {
-
 
159
		                transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_SUCCESSFUL);
-
 
160
		            } else {
-
 
161
                        transactionServiceClient.getClient().updateRechargeOrderStatus(rechargeOrder.getId(), RechargeOrderStatus.PAYMENT_FAILED);
-
 
162
		            }
-
 
163
		        } catch (Exception e) {
-
 
164
		            log.error("Problem with txn client while trying to recharge", e);
131
 
165
		        }
-
 
166
            }
132
			
167
			
133
		}else{
168
		}else{
134
			try {
169
			try {
135
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
170
				paymentServiceClient.getClient().updatePaymentDetails(merchantPaymentId, gatewayPaymentId,
136
						"", gatewayTxnStatus, "Payment Failed at PG", "", "", "", "", PaymentStatus.FAILED, "", attributes);
171
						"", gatewayTxnStatus, "Payment Failed at PG", "", "", "", "", PaymentStatus.FAILED, new SimpleDateFormat().format(new Date()), attributes);
137
			} catch (PaymentException e) {
172
			} catch (PaymentException e) {
138
			    log.error("Unable to mark the payment as failed", e);
173
			    log.error("Unable to mark the payment as failed", e);
139
			} catch (TException e) {
174
			} catch (TException e) {
140
			    log.error("Unable to mark the payment as failed", e);
175
			    log.error("Unable to mark the payment as failed", e);
141
			}
176
			}
142
 
177
 
143
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
178
			CommonPaymentService.processFailedTxn(txnId, transactionServiceClient);
144
//			DataLogger.logData(EventType.PAYMENT_FAILURE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), Long.toString(merchantPaymentId), gatewayPaymentId,
-
 
145
//                    gatewayTxnStatus, "Payment Failed at PG");
-
 
146
 
179
 
147
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
180
			this.redirectUrl = errorUrl + "?paymentId=" + merchantPaymentId;
148
		}
181
		}
149
 
182
 
150
		log.info("User will be redirected to: " + this.redirectUrl);
183
		log.info("User will be redirected to: " + "?paymentId=" + merchantPaymentId);
-
 
184
		this.redirectUrl = surl + "?paymentId=" + merchantPaymentId;
151
		return "index";
185
		return "index";
152
	}
186
	}
153
 
187
 
154
	private boolean validatePaymentParams(double returnedAmount, Payment payment, String hash){
188
	private boolean validatePaymentParams(double returnedAmount, Payment payment, String hash){
155
		if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50 && hash.equals(getSecureHash()))){
189
		if(!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50 && hash.equals(getSecureHash()))){