Rev 20112 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;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;import in.shop2020.serving.controllers.PayuPayController.ContactDetails;import in.shop2020.serving.services.IPaymentService;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import java.math.BigInteger;import java.security.MessageDigest;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.TreeMap;import org.apache.log4j.Logger;import com.opensymphony.xwork2.ValidationAwareSupport;public class HdfcPayController extends ValidationAwareSupport {static String SECURE_SECRET = "16da8a86fcf626ed37045d1fb9714d0f"; // hdfc gateway secret keyprivate static Logger log = Logger.getLogger(Class.class);// key;private final static String V3URL = "https://secure.ebs.in/pg/ma/payment/request";private ContactDetails billingDetails;private String paymentOption = null;private double amount;private String accountId = "21137";private StringBuilder description;private final String CHANNEL = "10";private final static String CURRENCY = "INR";private final static String RETURNURL = "http://www.shop2020.in/hdfc-pay-response";private final static String MODE = "LIVE";private final static String ALGO = "MD5";private final static String COUNTRY = "IND";private final static String PAGEID = "1";private String id;private HDFCPayPojo hpp;public HDFCPayPojo getHpp() {return hpp;}public void setHpp(HDFCPayPojo hpp) {this.hpp = hpp;}public void setId(String id) {this.id = id;}public String getId() {return id;}// hdfc api code. DO NOT CHANGEprivate String md5(String str) throws Exception {MessageDigest m = MessageDigest.getInstance("MD5");byte[] data = str.getBytes();m.update(data, 0, data.length);BigInteger i = new BigInteger(1, m.digest());String hash = String.format("%1$032X", i);return hash;}public String show() {String md5HashData = SECURE_SECRET;PaymentClient paymentServiceClient = null;Payment payment = null;try {long paymentId = Long.parseLong(this.id);paymentServiceClient = new PaymentClient();payment = paymentServiceClient.getClient().getPayment(paymentId);} catch (Exception e) {log.error("Error while getting payment client", e);addActionError("We are experiencing some problems. Please try later.");return "shipping-redirect";}Order order = null;try {long txnId = payment.getMerchantTxnId();TransactionClient transactionServiceClient = new TransactionClient();in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();Transaction transaction = txnClient.getTransaction(txnId);order = transaction.getOrders().get(0);} catch (Exception e) {log.error("Error while getting transaction information", e);addActionError("We are experiencing some problems. Please try later.");return "shipping-redirect";}try{setDescription(order);setPaymentOption(payment);this.amount = payment.getAmount();// set all the fields in pojohpp = new HDFCPayPojo(accountId, CHANNEL, getId(), "", PAGEID, CURRENCY,RETURNURL, MODE, ALGO, amount + "", getDescription(),order.getCustomer_name(), order.getCustomer_address1() + " "+ order.getCustomer_address2(),order.getCustomer_city(), order.getCustomer_state(), COUNTRY,order.getCustomer_pincode(), order.getCustomer_email(),order.getCustomer_mobilenumber(), order.getCustomer_name(),order.getCustomer_address1() + " "+ order.getCustomer_address2(),order.getCustomer_city(), order.getCustomer_state(), COUNTRY,order.getCustomer_pincode(), order.getCustomer_mobilenumber());hpp.setPostUrl(V3URL);// hpp.setAccountId(accountId);/* HDFC GATEWAY CODE STARTS-- CHECK CAREFULLY BEFORE CHANGE*/HashMap testMap = new HashMap();testMap.put("V3URL", V3URL);testMap.put("account_id", accountId + "");testMap.put("channel", CHANNEL);testMap.put("page_id", "1");testMap.put("return_url", RETURNURL);testMap.put("currency", CURRENCY);testMap.put("mode", MODE);testMap.put("algo", ALGO);testMap.put("reference_no", hpp.getTxnid());testMap.put("amount", hpp.getAmount());testMap.put("description", hpp.getDescription());testMap.put("name", hpp.getBillName());testMap.put("address", hpp.getAddress());testMap.put("city", hpp.getCity());testMap.put("state", hpp.getState());testMap.put("postal_code", hpp.getZipcode());testMap.put("country", hpp.getCountry());testMap.put("email", hpp.getEmail());testMap.put("phone", hpp.getPhone());testMap.put("ship_name", hpp.getShipName());testMap.put("ship_address", hpp.getShipAddress());testMap.put("ship_city", hpp.getShipCity());testMap.put("ship_state", hpp.getShipState());testMap.put("ship_country", hpp.getShipCountry());testMap.put("ship_phone", hpp.getShipPhone());testMap.put("ship_postal_code", hpp.getShipZipcode());testMap.put("submit", "SUBMIT");// Sort the HashMapMap requestFields = new TreeMap(testMap);String V3URL = (String) requestFields.remove("V3URL");requestFields.remove("submit");for (Iterator i = requestFields.keySet().iterator(); i.hasNext();) {String key = (String) i.next();String value = (String) requestFields.get(key);md5HashData += "|" + value;}String hvalue = ALGO;String hashedvalue = "";if (hvalue.equals("MD5")) {hashedvalue = md5(md5HashData);}/* HDFC GATEWAY CODE ENDS*/hpp.setHash(hashedvalue);}catch (Exception e) {log.error("Error while creating HDFCPayPojo information", e);addActionError("We are experiencing some problems. Please try later.");return "shipping-redirect";}return "show";}public String getDescription() {if (this.description.length() >= 255)return this.description.substring(0, 255).trim();elsereturn this.description.toString().trim();}private void setDescription(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 = desc.replaceAll("[^a-zA-Z0-9\\s\\-\\@\\/\\.]", "");this.description = new StringBuilder(desc);}public void setPaymentOption(Payment payment) {this.paymentOption = null;List<Attribute> attributes = payment.getAttributes();if (attributes == null)return;for (Attribute attr : attributes) {if (attr.getName().equals(IPaymentService.PAYMENT_METHOD))this.paymentOption = attr.getValue();}}public String getPaymentOption() {return paymentOption;}public class HDFCPayPojo {private String postUrl;private String accountId;private String channel;private String txnid;private String hash;private String pageId;private String currency;private String returnUrl;private String mode;private String algo;private String amount;private String description;private String billName;private String address;private String city;private String state;private String country;private String zipcode;private String email;private String phone;private String shipName;private String shipAddress;private String shipCity;private String shipState;private String shipCountry;private String shipZipcode;private String shipPhone;public HDFCPayPojo(String accountId, String channel, String txnid,String hash, String pageId, String currency, String returnUrl,String mode, String algo, String amount, String description,String billName, String address, String city, String state,String country, String zipcode, String email, String phone,String shipName, String shipAddress, String shipCity,String shipState, String shipCountry, String shipZipcode,String shipPhone) {super();this.accountId = accountId;this.channel = channel;this.txnid = txnid;this.hash = hash;this.pageId = pageId;this.currency = currency;this.returnUrl = returnUrl;this.mode = mode;this.algo = algo;this.amount = amount;this.description = description;this.billName = billName;this.address = address;this.city = city;this.state = state;this.country = country;this.zipcode = zipcode;this.email = email;this.phone = phone;this.shipName = shipName;this.shipAddress = shipAddress;this.shipCity = shipCity;this.shipState = shipState;this.shipCountry = shipCountry;this.shipZipcode = shipZipcode;this.shipPhone = shipPhone;}public String getAccountId() {return accountId;}public void setAccountId(String accountId) {this.accountId = accountId;}public String getChannel() {return channel;}public void setChannel(String channel) {this.channel = channel;}public String getTxnid() {return txnid;}public void setTxnid(String txnid) {this.txnid = txnid;}public String getHash() {return hash;}public void setHash(String hash) {this.hash = hash;}public String getPageId() {return pageId;}public void setPageId(String pageId) {this.pageId = pageId;}public String getCurrency() {return currency;}public void setCurrency(String currency) {this.currency = currency;}public String getReturnUrl() {return returnUrl;}public void setReturnUrl(String returnUrl) {this.returnUrl = returnUrl;}public String getMode() {return mode;}public void setMode(String mode) {this.mode = mode;}public String getAlgo() {return algo;}public void setAlgo(String algo) {this.algo = algo;}public String getAmount() {return amount;}public void setAmount(String amount) {this.amount = amount;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getBillName() {return billName;}public void setBillName(String billName) {this.billName = billName;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String getZipcode() {return zipcode;}public void setZipcode(String zipcode) {this.zipcode = zipcode;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getShipName() {return shipName;}public void setShipName(String shipName) {this.shipName = shipName;}public String getShipAddress() {return shipAddress;}public void setShipAddress(String shipAddress) {this.shipAddress = shipAddress;}public String getShipCity() {return shipCity;}public void setShipCity(String shipCity) {this.shipCity = shipCity;}public String getShipState() {return shipState;}public void setShipState(String shipState) {this.shipState = shipState;}public String getShipCountry() {return shipCountry;}public void setShipCountry(String shipCountry) {this.shipCountry = shipCountry;}public String getShipZipcode() {return shipZipcode;}public void setShipZipcode(String shipZipcode) {this.shipZipcode = shipZipcode;}public String getShipPhone() {return shipPhone;}public void setShipPhone(String shipPhone) {this.shipPhone = shipPhone;}public String getPostUrl() {return postUrl;}public void setPostUrl(String postUrl) {this.postUrl = postUrl;}}}