Rev 16287 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;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.RechargeOrder;import in.shop2020.model.v1.order.Transaction;import in.shop2020.payments.Attribute;import in.shop2020.payments.Payment;import in.shop2020.serving.services.IPaymentService;import in.shop2020.serving.utils.PaymentUtils;import in.shop2020.serving.utils.Utils;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.thrift.clients.UserClient;import in.shop2020.thrift.clients.config.ConfigClient;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.List;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import com.google.gson.Gson;import com.opensymphony.xwork2.ValidationAwareSupport;@SuppressWarnings("serial")@InterceptorRefs({@InterceptorRef("myDefault")})@Results({@Result(name="shipping-redirect", type="redirectAction", params = {"actionName" , "shipping"}),@Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge"})})public class PayuPayController extends ValidationAwareSupport{private static Logger log = Logger.getLogger(Class.class);private static String accountId;private static String returnUrl = "/payu-pay-response";private static String cancelUrl = "/pay-options";private static String rechargeCancelUrl = "/recharge";private static String salt;private static String postActionUrl;private String phone;//This is used for recharge orders of value less than 1000.private String resultJson;//This is used for recharge orders of value less than 1000.private PayuPayPojo ppp;public PayuPayPojo getPpp() {return ppp;}public void setPpp(PayuPayPojo ppp) {this.ppp = ppp;}static{try {accountId = ConfigClient.getClient().get("payu_account_id");salt = ConfigClient.getClient().get("payu_secret_key");returnUrl = ConfigClient.getClient().get("payu_return_url");cancelUrl = ConfigClient.getClient().get("payu_return_url");rechargeCancelUrl = ConfigClient.getClient().get("payu_return_url");postActionUrl = ConfigClient.getClient().get("payu_post_url");//"https://test.payu.in/_payment";} catch (ConfigException e) {log.error("Unable to get PayU payment configuration.");}}private String id;private String paymentOption = null;private String paymentType = null;private StringBuilder description;private double amount;private ContactDetails billingDetails;public String show(){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";}setDescription(order);setPaymentOption(payment);ppp = new PayuPayPojo();ppp.setKey(accountId);this.amount = payment.getAmount();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(this.getDescription());ppp.setZipcode(order.getCustomer_pincode());/* if (this.paymentOption.equals(IPaymentService.PAYU_CC)){ppp.setPg("Wallet");ppp.setBankcode("payuw");} else {ppp.setPg("NB");ppp.setBankcode(PaymentUtils.PAYU_NET_BANKING_CODES_MAP.get(this.paymentOption));}*/log.info("paymentOption "+this.paymentOption+" gw " +PaymentUtils.getPayugatewayCode(this.paymentOption));ppp.setPg(PaymentUtils.getPayugatewayCode(this.paymentOption));ppp.setBankcode(PaymentUtils.getPayubankCode(this.paymentOption));ppp.setSurl(returnUrl);ppp.setFurl(returnUrl);ppp.setCurl(cancelUrl);String[] name = order.getCustomer_name().split(" ");ppp.setFirstname(name[0]);if(name.length==2){ppp.setLastname(name[1]);}ppp.setTxnid(getId());ppp.setCountry("India");this.amount = payment.getAmount();this.billingDetails = new ContactDetails(ppp.getFirstname(),order.getCustomer_email(), order.getCustomer_address1()+" "+ order.getCustomer_address2(),order.getCustomer_city(), order.getCustomer_state(),order.getCustomer_pincode(), "India",order.getCustomer_mobilenumber());log.info(billingDetails);ppp.setPostActionUrl(postActionUrl);try {ppp.setHash(getSecureHash());} catch (NoSuchAlgorithmException e) {log.error("Error while evaluating secure hash", e);addActionError("We are experiencing some problems. Please try later.");return "shipping-redirect";}return "show";}/*** This method is used for Recharge payments. It is being called by RechargePaymentController.* @return*/public String edit(){PaymentClient paymentServiceClient = null;Payment payment = null;try {long paymentId = Long.parseLong(this.id);paymentServiceClient = new PaymentClient();payment = paymentServiceClient.getClient().getPayment(paymentId);} catch (Exception e) {addActionError("We are experiencing some problems. Please try later.");log.error("Error while getting payment client", e);return "recharge-redirect";}RechargeOrder rechargeOrder = null;try {long txnId = payment.getMerchantTxnId();TransactionClient transactionServiceClient = new TransactionClient();in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();rechargeOrder = txnClient.getRechargeOrdersForTransaction(txnId);} catch (Exception e) {log.error("Error while getting transaction information", e);addActionError("We are experiencing some problems. Please try later.");log.error("Error while getting payment client", e);return "recharge-redirect";}String desc = "Recharge for Rs. " + rechargeOrder.getTotalAmount() + ", operator : " + rechargeOrder.getOperatorId();this.description = new StringBuilder(desc);setPaymentOption(payment);this.amount = payment.getAmount();if(getPhone() == null || getPhone().isEmpty()) {UserClient userClient;in.shop2020.model.v1.user.Address address = null;try {userClient = new UserClient();long addressId = userClient.getClient().getDefaultAddressId(rechargeOrder.getUserId());address = userClient.getClient().getAddressById(addressId);this.billingDetails = new ContactDetails(address.getName().split("@")[0],rechargeOrder.getUserEmailId(), address.getLine1(),address.getCity(), address.getState(),address.getPin(), "IND",address.getPhone());} catch (Exception e) {log.error("Unable to get address to put in billing details for User : " + rechargeOrder.getUserId());e.printStackTrace();}} else {this.billingDetails = new ContactDetails(rechargeOrder.getUserEmailId().split("@")[0],rechargeOrder.getUserEmailId(), id,id, id,id, "IND",getPhone());}log.info("Billing details of recharge order " + rechargeOrder.getDisplayId() + " : " + billingDetails);ppp = new PayuPayPojo();ppp.setKey(getAccountId());ppp.setTxnid(getId());log.info("paymentOption "+this.paymentOption+" gw " +PaymentUtils.getPayugatewayCode(this.paymentOption));ppp.setPg(PaymentUtils.getPayugatewayCode(this.paymentOption));ppp.setBankcode(PaymentUtils.getPayubankCode(this.paymentOption));String[] name = billingDetails.getName().split(" ");ppp.setFirstname(name[0]);ppp.setEmail(billingDetails.getEmail());ppp.setPhone(billingDetails.getPhone());ppp.setSurl(returnUrl);ppp.setFurl(returnUrl);ppp.setCurl(rechargeCancelUrl);ppp.setAmount(getAmount() + "");ppp.setProductinfo(getDescription());try {ppp.setHash(getSecureHash());} catch (Exception e) {log.error("Could not set securehash" );}ppp.setPostActionUrl(postActionUrl);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 setId(String id) {this.id = id;}public String getId() {return id;}public String getAccountId() {return accountId;}public static String getReturnUrl() {return returnUrl;}public double getAmount() {return amount;}public void setPaymentOption(Payment payment) {this.paymentOption = null;String paymentType="";String paymentOpt="";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();}this.paymentOption = paymentType+paymentOpt;}public String getPaymentOption(){return paymentOption;}public String getSecureHash() throws NoSuchAlgorithmException{String pass = accountId + "|" + id + "|" + amount + "|" + getDescription() + "|" + billingDetails.getName() + "|" + billingDetails.getEmail() + "|||||||||||" + salt;System.out.println("accountId|id|amount|getDescription()|billingDetails.getName()|billingDetails.getEmail()|||||||||||salt");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();}public ContactDetails getBillingDetails() {return billingDetails;}public void setPhone(String phone) {this.phone = phone;}public String getPhone() {return phone;}public void setResultJson(String resultJson) {this.resultJson = resultJson;}public String getResultJson() {log.info(resultJson);return resultJson;}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;}}public class PayuPayPojo {public String getKey() {return key;}public void setKey(String key) {this.key = key;}public String getTxnid() {return txnid;}public void setTxnid(String txnid) {this.txnid = txnid;}public String getAmount() {return amount;}public void setAmount(String amount) {this.amount = amount;}public String getProductinfo() {return productinfo;}public void setProductinfo(String productinfo) {this.productinfo = productinfo;}public String getFirstname() {return firstname;}public void setFirstname(String firstname) {this.firstname = firstname;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public String getAddress1() {return address1;}public void setAddress1(String address1) {this.address1 = address1;}public String getAddress2() {return address2;}public void setAddress2(String address2) {this.address2 = address2;}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 getSurl() {return surl;}public void setSurl(String surl) {this.surl = surl;}public String getFurl() {return furl;}public void setFurl(String furl) {this.furl = furl;}public String getCurl() {return curl;}public void setCurl(String curl) {this.curl = curl;}public String getHash() {return hash;}public void setHash(String hash) {this.hash = hash;}public String getPg() {return pg;}public void setPg(String pg) {this.pg = pg;}public String getCodurl() {return codurl;}public void setCodurl(String codurl) {this.codurl = codurl;}public String getDrop_category() {return drop_category;}public void setDrop_category(String drop_category) {this.drop_category = drop_category;}public void setPhone(String phone) {this.phone = phone;}public String getPhone() {return phone;}public void setBankcode(String bankcode) {this.bankcode = bankcode;}public String getBankcode() {return bankcode;}public void setPostActionUrl(String postActionUrl) {this.postActionUrl = postActionUrl;}public String getPostActionUrl() {return postActionUrl;}private String key;private String txnid;private String amount;private String productinfo;private String firstname;private String email;private String lastname;private String address1;private String address2;private String city;private String state;private String country;private String zipcode;private String surl;private String furl;private String curl;private String hash;private String pg;private String codurl;private String drop_category;private String phone;private String bankcode;private String postActionUrl;private String udf1;private String custom_note;private String offer_key;}}