Subversion Repositories SmartDukaan

Rev

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