Rev 6060 | Rev 13288 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;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.opensymphony.xwork2.ValidationAwareSupport;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;import in.shop2020.serving.services.IPaymentService;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.thrift.clients.config.ConfigClient;@SuppressWarnings("serial")@InterceptorRefs({@InterceptorRef("myDefault"),@InterceptorRef("login")})@Results({@Result(name="shipping-redirect", type="redirectAction",params = {"actionName" , "shipping"})})public class PayuPayController extends ValidationAwareSupport{private static Logger log = Logger.getLogger(Class.class);private static String accountId;private static String returnUrl = "http://local.shop2020.in:8080/payu-pay-response";private static String salt;static{try {accountId = ConfigClient.getClient().get("payu_account_id");returnUrl = ConfigClient.getClient().get("payu_return_url");salt = ConfigClient.getClient().get("payu_secret_key");//"https://test.payu.in/_payment";} catch (ConfigException e) {log.error("Unable to get EBS payment configuration.");}}private String id;private String paymentOption = 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);this.amount = payment.getAmount();this.billingDetails = new ContactDetails(order.getCustomer_name(),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);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;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 String getSecureHash() throws NoSuchAlgorithmException{String pass = 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 static class ContactDetails{private String name;private String email;private String address1;private String address2;private String city;private String state;private String postalCode;private String country;private String phone;public ContactDetails(String name, String email, String address1, String address2,String city, String state, String postalCode, String country,String phone) {this.name = name;this.email = email;this.address1 = address1;this.address2 = address2;this.city = city;this.state = state;this.postalCode = postalCode;this.country = country;this.phone = phone;}@Overridepublic String toString() {return "ContactDetails [name=" + name + ", email=" + email+ ", address1=" + address1 + ", address2=" + address2 + ",city=" + city + ", state="+ state + ", postalCode=" + postalCode + ", country="+ country + ", phone=" + phone + "]";}public String getName() {return name;}public String getEmail() {return email;}public String getAddress1() {return address1;}public String getAddress2() {return address2;}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;}}}