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();
507 rajveer 54
    		return "redirect";
424 rajveer 55
    	}
650 rajveer 56
    		htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
57
    		htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getOrderDetailsHtml(Long.parseLong(id)));
507 rajveer 58
    	return "show";
419 rajveer 59
    }
517 rajveer 60
 
572 chandransh 61
    // POST /order/
62
    public String create(){
650 rajveer 63
    	if(!userinfo.isLoggedIn()){
64
    		setRedirectUrl();
65
    		return "redirect";
66
    	}
572 chandransh 67
    	long addressId = Long.parseLong(this.request.getParameter("addressid"));
68
    	long currentCartId = userinfo.getCartId();
712 rajveer 69
 
693 rajveer 70
		try{
71
			amount = Double.parseDouble(request.getParameter("amount"));
72
		}catch(Exception e){
73
			amount = Utils.getPaymentAmount(userinfo.getCartId());
74
		}
75
 
572 chandransh 76
		try {
77
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
78
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
79
 
80
			userClient.addAddressToCart(currentCartId, addressId);
712 rajveer 81
			//TODO validate only item quantity change. Other validations should not be done. 
693 rajveer 82
			if(!userClient.validateCart(currentCartId)){
712 rajveer 83
				addActionError("Some items are added in your cart.");
693 rajveer 84
				return "shipping-redirect";
85
			}
712 rajveer 86
			txnId = userClient.createOrders(currentCartId);
572 chandransh 87
 
693 rajveer 88
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
89
			Client paymentClient = paymentServiceClient.getClient();
90
 
91
			this.paymentId = paymentClient.createPayment(userinfo.getUserId(), amount, gatewayId, txnId);
92
			return "payredirect";
93
 
94
			/*
95
			011-46010946
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
}