Subversion Repositories SmartDukaan

Rev

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