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