Subversion Repositories SmartDukaan

Rev

Rev 21730 | Rev 22389 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21410 amit.gupta 1
package com.spice.profitmandi.web.controller.checkout;
2
import java.security.MessageDigest;
3
import java.security.NoSuchAlgorithmException;
21469 amit.gupta 4
import java.util.HashMap;
21410 amit.gupta 5
import java.util.List;
21469 amit.gupta 6
import java.util.Map;
21410 amit.gupta 7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
import org.springframework.stereotype.Component;
11
 
12
import com.spice.profitmandi.thrift.clients.PaymentClient;
13
import com.spice.profitmandi.thrift.clients.TransactionClient;
14
import com.spice.profitmandi.thrift.clients.config.ConfigClient;
15
import com.spice.profitmandi.web.payment.IPaymentService;
16
import com.spice.profitmandi.web.payment.PaymentUtils;
17
import com.spice.profitmandi.web.res.order.PayuPayPojo;
18
 
19
import in.shop2020.config.ConfigException;
20
import in.shop2020.model.v1.order.LineItem;
21
import in.shop2020.model.v1.order.Order;
22
import in.shop2020.model.v1.order.Transaction;
23
import in.shop2020.payments.Attribute;
24
import in.shop2020.payments.Payment;
25
 
26
 
27
@Component
28
public class PayuHandler {
29
 
30
	private static final Logger log = LoggerFactory.getLogger(PayuHandler.class);
31
 
32
	private static String accountId;
33
 
21469 amit.gupta 34
	private static String returnUrl = "http://bigustav.com/checkout/payu-pay-response";
21410 amit.gupta 35
 
21469 amit.gupta 36
	private static String cancelUrl = "http://bigustav.com/checkout/payu-pay-cancelled";
21410 amit.gupta 37
 
38
 
39
 
40
	private static String salt;
41
 
42
	private static String postActionUrl;
43
 
44
	static{
45
		try {
46
			ConfigClient client = ConfigClient.getClient();
47
			accountId = client.get("payu_account_id");
48
			salt = client.get("payu_secret_key");
49
			postActionUrl = client.get("payu_post_url");
50
			//"https://test.payu.in/_payment";
51
		} catch (ConfigException e) {
52
			log.error("Unable to get PayU payment configuration.");
53
		}
54
	}
55
 
56
 
57
	public PayuPayPojo getPayuParams(long paymentId) throws Exception{
58
		PaymentClient paymentServiceClient = new PaymentClient();
59
		Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
60
 
61
		long txnId = payment.getMerchantTxnId();
62
		TransactionClient transactionServiceClient = new TransactionClient();
63
		in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
64
		Transaction transaction = txnClient.getTransaction(txnId);
65
		Order order = transaction.getOrders().get(0);
66
 
67
		String paymentOption = getPaymentOption(payment);
68
		PayuPayPojo ppp = new PayuPayPojo();
69
		ppp.setKey(accountId);
70
		ppp.setAmount(payment.getAmount() + "");
71
		ppp.setEmail(order.getCustomer_email());
72
		ppp.setPhone(order.getCustomer_mobilenumber());
73
		ppp.setAddress1(order.getCustomer_address1());
74
		ppp.setAddress2(order.getCustomer_address2());
75
		ppp.setCity(order.getCustomer_city());
76
		ppp.setState(order.getCustomer_state());
77
		ppp.setProductinfo(getDescription(order));
78
		ppp.setZipcode(order.getCustomer_pincode());
79
 
80
		ppp.setPg(PaymentUtils.getPayugatewayCode(paymentOption));
81
		ppp.setBankcode(PaymentUtils.getPayubankCode(paymentOption));
82
		ppp.setFurl(returnUrl);
83
		ppp.setSurl(returnUrl);
84
		ppp.setCurl(cancelUrl);
85
		String[] name = order.getCustomer_name().split(" ");
86
		ppp.setFirstname(name[0]);
87
		if(name.length==2){
88
			ppp.setLastname(name[1]);
89
		}
90
		ppp.setTxnid(Long.toHexString(txnId));
91
		ppp.setCountry("India");
92
 
93
		ppp.setPostActionUrl(postActionUrl);
94
		ppp.setHash(getSecureHash(paymentId, ppp));
95
 
96
		return ppp;
97
	}
98
 
99
	/**
100
	 * This method is used for Recharge payments. It is being called by RechargePaymentController.
101
	 * @return
102
	 */
103
 
104
	private String getDescription (Order order){
105
		StringBuilder descriptionBuilder = new StringBuilder(255);
106
		for(LineItem line: order.getLineitems()){
107
			if(line.getBrand() != null){
108
				descriptionBuilder.append(line.getBrand() + " ");
109
			}
110
			if(line.getModel_name() != null){
111
				descriptionBuilder.append(line.getModel_name() + " "); 
112
			}
113
			if(line.getModel_number() != null){
114
				descriptionBuilder.append(line.getModel_number() + " ");
115
			}
116
			if(line.getColor() != null){
117
				descriptionBuilder.append(line.getColor() + " ");
118
			}
119
		}
120
		String desc = descriptionBuilder.toString();
121
		desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");
122
		descriptionBuilder = new StringBuilder(desc);
123
		if(descriptionBuilder.length() >= 255)
124
			return descriptionBuilder.substring(0, 255).trim();
125
		else
126
			return descriptionBuilder.toString().trim();
127
	}
128
 
129
	private String getPaymentOption(Payment payment) {
130
		String paymentType = null;
131
		String paymentOpt = null;
132
		List<Attribute> attributes = payment.getAttributes();
133
		if(attributes == null)
134
			return "";
135
		for(Attribute attr : attributes){
136
			if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))
137
				paymentOpt = attr.getValue();
138
			if(attr.getName().equals(IPaymentService.PAYMENT_TYPE))
139
				paymentType = attr.getValue();
140
		}
141
		return paymentType+paymentOpt;
142
	}
143
 
144
	private String getSecureHash(long paymentId, PayuPayPojo ppp) throws NoSuchAlgorithmException{
145
		String pass = accountId + "|" + paymentId + "|" + ppp.getAmount()  + "|" + ppp.getProductinfo() + "|" +  ppp.getFirstname() + "|" + ppp.getEmail() + "|||||||||||" + salt;
146
		log.info("Secure hash-->accountId|paymentId|ppp.getAmount()|ppp.getProductinfo()|ppp.getFirstname()|ppp.getEmail()|||||||||||salt");
147
		log.info("Pass-->" + pass);
148
		MessageDigest md = MessageDigest.getInstance("SHA-512");
149
		md.update(pass.getBytes(), 0, pass.getBytes().length);
150
		byte[] mdbytes = md.digest();
151
		//	convert the byte to hex format method
152
		StringBuffer sb = new StringBuffer();
153
		for (int i = 0; i < mdbytes.length; i++) {
154
			sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
155
		}
156
		return sb.toString();
157
	}
21469 amit.gupta 158
 
21410 amit.gupta 159
 
21469 amit.gupta 160
	public boolean validatePaymentParams(double returnedAmount, Payment payment, String hash, Map<String, String> paymentParams) {
161
		if (!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50
162
				&& hash.equals(getSecureHash(paymentParams)))) {
163
			// We did not request this payment or the authorised amount is
164
			// different.
165
			log.error("Checks and balance failed on returned data");
166
			return false;
167
		}
168
		return true;
169
	}
170
 
171
	public String getSecureHash(Map<String, String>  paymentParams){
172
		try{
173
			String pass = salt + "|" + paymentParams.get("status") + "|||||||||||" + paymentParams.get("email") + "|" +  paymentParams.get("firstname") + "|" + paymentParams.get("productinfo") + "|" + paymentParams.get("amount") + "|" + paymentParams.get("txnid") + "|" + accountId;
174
			System.out.println(pass);
175
			MessageDigest md = MessageDigest.getInstance("SHA-512");
176
			md.update(pass.getBytes(), 0, pass.getBytes().length);
177
			byte[] mdbytes = md.digest();
178
			//	convert the byte to hex format method
179
			StringBuffer sb = new StringBuffer();
180
			for (int i = 0; i < mdbytes.length; i++) {
181
				sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
182
			}
183
			return sb.toString();
184
		}catch(NoSuchAlgorithmException nsae){
185
			log.error("No such algorithm exception");
186
			return null;
187
		}
188
	}
189
 
190
	public Map<String, String> getPaymentParams(Map<String, String[]> map) {
191
		Map<String, String> paymentParams = new HashMap<>();
192
		for (Object key : map.keySet()) {
193
			String keyStr = (String) key;
194
			String[] vals = (String[]) map.get(keyStr);
195
			String value = vals[0];
196
			System.out.println("Key " + (String) key + "     :    " + value);
197
			paymentParams.put(keyStr, value);
198
		}
199
		return paymentParams;
200
	}
201
 
21410 amit.gupta 202
	public static class ContactDetails{
203
		private String name;
204
		private String email;
205
		private String address;
206
		private String city;
207
		private String state;
208
		private String postalCode;
209
		private String country;
210
		private String phone;
211
 
212
		public ContactDetails(String name, String email, String address,
213
				String city, String state, String postalCode, String country,
214
				String phone) {
215
			this.name = name;
216
			this.email = email;
217
			this.address = address;
218
			this.city = city;
219
			this.state = state;
220
			this.postalCode = postalCode;
221
			this.country = country;
222
			this.phone = phone;
223
		}
224
 
225
		@Override
226
		public String toString() {
227
			return "ContactDetails [name=" + name + ", email=" + email
228
					+ ", address=" + address + ", city=" + city + ", state="
229
					+ state + ", postalCode=" + postalCode + ", country="
230
					+ country + ", phone=" + phone + "]";
231
		}
232
 
233
		public String getName() {
234
			return name;
235
		}
236
 
237
		public String getEmail() {
238
			return email;
239
		}
240
 
241
		public String getAddress() {
242
			return address;
243
		}
244
 
245
		public String getCity() {
246
			return city;
247
		}
248
 
249
		public String getState() {
250
			return state;
251
		}
252
 
253
		public String getPostalCode() {
254
			return postalCode;
255
		}
256
 
257
		public String getCountry() {
258
			return country;
259
		}
260
 
261
		public String getPhone() {
262
			return phone;
263
		}
264
	}
265
}