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
    	}
786 rajveer 69
    	String addressIdString = this.request.getParameter("addressid");
70
    	if(addressIdString == null){
71
    		addActionError("Please login to continue checkout.");
72
    		return "shipping-redirect";
73
    	}
74
    	long addressId = Long.parseLong(addressIdString);
572 chandransh 75
    	long currentCartId = userinfo.getCartId();
712 rajveer 76
 
693 rajveer 77
		try{
78
			amount = Double.parseDouble(request.getParameter("amount"));
79
		}catch(Exception e){
80
			amount = Utils.getPaymentAmount(userinfo.getCartId());
81
		}
82
 
572 chandransh 83
		try {
84
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
85
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
86
 
87
			userClient.addAddressToCart(currentCartId, addressId);
712 rajveer 88
			//TODO validate only item quantity change. Other validations should not be done. 
693 rajveer 89
			if(!userClient.validateCart(currentCartId)){
712 rajveer 90
				addActionError("Some items are added in your cart.");
693 rajveer 91
				return "shipping-redirect";
92
			}
712 rajveer 93
			txnId = userClient.createOrders(currentCartId);
572 chandransh 94
 
693 rajveer 95
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
96
			Client paymentClient = paymentServiceClient.getClient();
97
 
98
			this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
99
			return "payredirect";
100
 
572 chandransh 101
		} catch (TException e) {
102
			e.printStackTrace();
103
		} catch (Exception e) {
104
			e.printStackTrace();
105
		}
106
 
712 rajveer 107
		return "failure";
572 chandransh 108
    }
109
 
507 rajveer 110
	public String getId(){
111
		return id;
112
	}
419 rajveer 113
 
507 rajveer 114
	public void setId(String id){
115
		this.id = id;
116
	}
419 rajveer 117
 
507 rajveer 118
	public String getMyaccountHeaderSnippet(){
119
		return htmlSnippets.get("MYACCOUNT_HEADER");
120
	}
121
 
122
	public String getOrderDetailsSnippet(){
123
		return htmlSnippets.get("ORDER_DETAILS");
124
	}
125
 
681 rajveer 126
	public String getUrl(){
127
		return this.paymentUrl;
128
	}
507 rajveer 129
 
712 rajveer 130
	public long getPid(){
693 rajveer 131
		return this.paymentId;
132
	}
133
 
712 rajveer 134
	public long getTxn(){
135
		return this.txnId;
517 rajveer 136
	}
712 rajveer 137
 
138
	public double getAmount(){
139
		return this.amount;
140
	}
141
 
517 rajveer 142
 
419 rajveer 143
}