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