Subversion Repositories SmartDukaan

Rev

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