Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 rajveer 1
package in.shop2020.serving.controllers;
2
 
693 rajveer 3
import in.shop2020.payments.PaymentService.Client;
419 rajveer 4
import in.shop2020.serving.controllers.BaseController;
693 rajveer 5
import in.shop2020.serving.utils.Utils;
6
import in.shop2020.thrift.clients.PaymentServiceClient;
517 rajveer 7
import in.shop2020.thrift.clients.UserContextServiceClient;
8
 
832 rajveer 9
import org.apache.log4j.Logger;
822 vikas 10
import org.apache.struts2.convention.annotation.InterceptorRef;
11
import org.apache.struts2.convention.annotation.InterceptorRefs;
419 rajveer 12
import org.apache.struts2.convention.annotation.Result;
13
import org.apache.struts2.convention.annotation.Results;
517 rajveer 14
import org.apache.thrift.TException;
419 rajveer 15
 
822 vikas 16
@InterceptorRefs({
17
    @InterceptorRef("myDefault"),
18
    @InterceptorRef("login")
19
})
20
 
419 rajveer 21
@Results({
822 vikas 22
    @Result(name="payredirect", type="redirectAction", 
712 rajveer 23
			params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}", "amount", "${amount}"}),
693 rajveer 24
    @Result(name="shipping-redirect", type="redirectAction", 
712 rajveer 25
    		params = {"actionName" , "shipping"}),
26
    @Result(name="failure", type="redirectAction", 
27
    		params = {"actionName" , "shipping"})    		
681 rajveer 28
 
419 rajveer 29
})
650 rajveer 30
public class OrderController extends BaseController {
419 rajveer 31
 
32
	private static final long serialVersionUID = 1L;
33
 
832 rajveer 34
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 35
	private String id;
712 rajveer 36
	private long txnId = 0;
650 rajveer 37
 
693 rajveer 38
	//FIXME right now only one PG. Once we will have more, need to fix it.
681 rajveer 39
	private String paymentUrl="hdfc-pay";
693 rajveer 40
	private int gatewayId=1;
681 rajveer 41
 
693 rajveer 42
	private long paymentId;
43
 
712 rajveer 44
	private double amount;
45
 
419 rajveer 46
	public OrderController(){
507 rajveer 47
		super();
419 rajveer 48
	}
49
 
507 rajveer 50
    // GET /order/ orderid
51
    public String show() {
52
    	log.info("id=" + id);
822 vikas 53
    	htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
843 chandransh 54
    	htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id)));
507 rajveer 55
    	return "show";
419 rajveer 56
    }
517 rajveer 57
 
572 chandransh 58
    // POST /order/
59
    public String create(){
786 rajveer 60
    	String addressIdString = this.request.getParameter("addressid");
61
    	if(addressIdString == null){
62
    		addActionError("Please login to continue checkout.");
63
    		return "shipping-redirect";
64
    	}
65
    	long addressId = Long.parseLong(addressIdString);
572 chandransh 66
    	long currentCartId = userinfo.getCartId();
712 rajveer 67
 
693 rajveer 68
		try{
69
			amount = Double.parseDouble(request.getParameter("amount"));
70
		}catch(Exception e){
71
			amount = Utils.getPaymentAmount(userinfo.getCartId());
72
		}
73
 
572 chandransh 74
		try {
75
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
76
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
77
 
78
			userClient.addAddressToCart(currentCartId, addressId);
712 rajveer 79
			//TODO validate only item quantity change. Other validations should not be done. 
693 rajveer 80
			if(!userClient.validateCart(currentCartId)){
712 rajveer 81
				addActionError("Some items are added in your cart.");
693 rajveer 82
				return "shipping-redirect";
83
			}
712 rajveer 84
			txnId = userClient.createOrders(currentCartId);
572 chandransh 85
 
693 rajveer 86
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
87
			Client paymentClient = paymentServiceClient.getClient();
88
 
89
			this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
90
			return "payredirect";
91
 
572 chandransh 92
		} catch (TException e) {
853 rajveer 93
			log.error("Thrift exception, payment failed");
572 chandransh 94
			e.printStackTrace();
95
		} catch (Exception e) {
853 rajveer 96
			log.error("Thrift exception, payment failed");
572 chandransh 97
			e.printStackTrace();
98
		}
99
 
712 rajveer 100
		return "failure";
572 chandransh 101
    }
102
 
507 rajveer 103
	public String getId(){
104
		return id;
105
	}
419 rajveer 106
 
507 rajveer 107
	public void setId(String id){
108
		this.id = id;
109
	}
419 rajveer 110
 
507 rajveer 111
	public String getMyaccountHeaderSnippet(){
112
		return htmlSnippets.get("MYACCOUNT_HEADER");
113
	}
114
 
115
	public String getOrderDetailsSnippet(){
116
		return htmlSnippets.get("ORDER_DETAILS");
117
	}
118
 
681 rajveer 119
	public String getUrl(){
120
		return this.paymentUrl;
121
	}
507 rajveer 122
 
712 rajveer 123
	public long getPid(){
693 rajveer 124
		return this.paymentId;
125
	}
126
 
712 rajveer 127
	public long getTxn(){
128
		return this.txnId;
517 rajveer 129
	}
712 rajveer 130
 
131
	public double getAmount(){
132
		return this.amount;
133
	}
134
 
517 rajveer 135
 
419 rajveer 136
}