Rev 26924 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller.checkout;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import com.spice.profitmandi.thrift.clients.PaymentClient;import com.spice.profitmandi.thrift.clients.TransactionClient;import com.spice.profitmandi.thrift.clients.config.ConfigClient;import com.spice.profitmandi.web.res.order.PayuPayPojo;import in.shop2020.config.ConfigException;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.Transaction;import in.shop2020.payments.Attribute;import in.shop2020.payments.Payment;@Componentpublic class PayuHandler {private static final Logger log = LogManager.getLogger(PayuHandler.class);private static String accountId;@Value("${angular.app.url}")private String applicationWebUrl;@Value("${this.app.url}")private String thisApplicationUrl;private static String salt="UNUSED";private static String postActionUrl;public PayuPayPojo getPayuParams(long paymentId) throws Exception{PayuPayPojo ppp = new PayuPayPojo();/*PaymentClient paymentServiceClient = new PaymentClient();Payment payment = paymentServiceClient.getClient().getPayment(paymentId);long txnId = payment.getMerchantTxnId();TransactionClient transactionServiceClient = new TransactionClient();in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();Transaction transaction = txnClient.getTransaction(txnId);Order order = transaction.getOrders().get(0);String paymentOption = getPaymentOption(payment);ppp.setKey(accountId);ppp.setAmount(payment.getAmount() + "");ppp.setEmail(order.getCustomer_email());ppp.setPhone(order.getCustomer_mobilenumber());ppp.setAddress1(order.getCustomer_address1());ppp.setAddress2(order.getCustomer_address2());ppp.setCity(order.getCustomer_city());ppp.setState(order.getCustomer_state());ppp.setProductinfo(getDescription(order));ppp.setZipcode(order.getCustomer_pincode());ppp.setPg(PaymentUtils.getPayugatewayCode(paymentOption));if(paymentOption.startsWith(PaymentUtils.PAYMENT_TYPE.WAL.toString())) {ppp.setBankcode(PaymentUtils.getPayubankCode(paymentOption));}ppp.setFurl(thisApplicationUrl + "/checkout/payu-pay-response");ppp.setSurl(thisApplicationUrl + "/checkout/payu-pay-response");ppp.setCurl(applicationWebUrl + "pages/home/paymentOptions");String[] name = order.getCustomer_name().split(" ");ppp.setFirstname(name[0]);if(name.length==2){ppp.setLastname(name[1]);}ppp.setTxnid(paymentId + "");ppp.setCountry("India");ppp.setPostActionUrl(postActionUrl);ppp.setHash(getSecureHash(paymentId, ppp));*/return ppp;}/*** This method is used for Recharge payments. It is being called by RechargePaymentController.* @return*/private String getDescription (Order order){StringBuilder descriptionBuilder = new StringBuilder(255);for(LineItem line: order.getLineitems()){if(line.getBrand() != null){descriptionBuilder.append(line.getBrand() + " ");}if(line.getModel_name() != null){descriptionBuilder.append(line.getModel_name() + " ");}if(line.getModel_number() != null){descriptionBuilder.append(line.getModel_number() + " ");}if(line.getColor() != null){descriptionBuilder.append(line.getColor() + " ");}}String desc = descriptionBuilder.toString();desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");descriptionBuilder = new StringBuilder(desc);if(descriptionBuilder.length() >= 255)return descriptionBuilder.substring(0, 255).trim();elsereturn descriptionBuilder.toString().trim();}/*private String getPaymentOption(Payment payment) {String paymentType = null;String paymentOpt = null;List<Attribute> attributes = payment.getAttributes();if(attributes == null)return "";for(Attribute attr : attributes){if(attr.getName().equals(IPaymentService.PAYMENT_METHOD))paymentOpt = attr.getValue();if(attr.getName().equals(IPaymentService.PAYMENT_TYPE))paymentType = attr.getValue();}return paymentType+paymentOpt;}*/private String getSecureHash(long paymentId, PayuPayPojo ppp) throws NoSuchAlgorithmException{String pass = accountId + "|" + paymentId + "|" + ppp.getAmount() + "|" + ppp.getProductinfo() + "|" + ppp.getFirstname() + "|" + ppp.getEmail() + "|||||||||||" + salt;log.info("Secure hash-->accountId|paymentId|ppp.getAmount()|ppp.getProductinfo()|ppp.getFirstname()|ppp.getEmail()|||||||||||salt");log.info("Pass-->" + pass);MessageDigest md = MessageDigest.getInstance("SHA-512");md.update(pass.getBytes(), 0, pass.getBytes().length);byte[] mdbytes = md.digest();// convert the byte to hex format methodStringBuffer sb = new StringBuffer();for (int i = 0; i < mdbytes.length; i++) {sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));}return sb.toString();}public boolean validatePaymentParams(double returnedAmount, Payment payment, String hash, Map<String, String> paymentParams) {if (!(payment != null && Math.abs(payment.getAmount() - returnedAmount) <= 0.50&& hash.equals(getSecureHash(paymentParams)))) {// We did not request this payment or the authorised amount is// different.log.error("Checks and balance failed on returned data");return false;}return true;}public String getSecureHash(Map<String, String> paymentParams){try{String pass = salt + "|" + paymentParams.get("status") + "|||||||||||" + paymentParams.get("email") + "|" + paymentParams.get("firstname") + "|" + paymentParams.get("productinfo") + "|" + paymentParams.get("amount") + "|" + paymentParams.get("txnid") + "|" + accountId;System.out.println(pass);MessageDigest md = MessageDigest.getInstance("SHA-512");md.update(pass.getBytes(), 0, pass.getBytes().length);byte[] mdbytes = md.digest();// convert the byte to hex format methodStringBuffer sb = new StringBuffer();for (int i = 0; i < mdbytes.length; i++) {sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));}return sb.toString();}catch(NoSuchAlgorithmException nsae){log.error("No such algorithm exception");return null;}}public Map<String, String> getPaymentParams(Map<String, String[]> map) {Map<String, String> paymentParams = new HashMap<>();for (Object key : map.keySet()) {String keyStr = (String) key;String[] vals = (String[]) map.get(keyStr);String value = vals[0];System.out.println("Key " + (String) key + " : " + value);paymentParams.put(keyStr, value);}return paymentParams;}public static class ContactDetails{private String name;private String email;private String address;private String city;private String state;private String postalCode;private String country;private String phone;public ContactDetails(String name, String email, String address,String city, String state, String postalCode, String country,String phone) {this.name = name;this.email = email;this.address = address;this.city = city;this.state = state;this.postalCode = postalCode;this.country = country;this.phone = phone;}@Overridepublic String toString() {return "ContactDetails [name=" + name + ", email=" + email+ ", address=" + address + ", city=" + city + ", state="+ state + ", postalCode=" + postalCode + ", country="+ country + ", phone=" + phone + "]";}public String getName() {return name;}public String getEmail() {return email;}public String getAddress() {return address;}public String getCity() {return city;}public String getState() {return state;}public String getPostalCode() {return postalCode;}public String getCountry() {return country;}public String getPhone() {return phone;}}}