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