Subversion Repositories SmartDukaan

Rev

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