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;
822 vikas 14
import org.apache.struts2.convention.annotation.InterceptorRef;
15
import org.apache.struts2.convention.annotation.InterceptorRefs;
419 rajveer 16
import org.apache.struts2.convention.annotation.Result;
17
import org.apache.struts2.convention.annotation.Results;
517 rajveer 18
import org.apache.thrift.TException;
419 rajveer 19
 
822 vikas 20
@InterceptorRefs({
21
    @InterceptorRef("myDefault"),
22
    @InterceptorRef("login")
23
})
24
 
419 rajveer 25
@Results({
822 vikas 26
    @Result(name="payredirect", type="redirectAction", 
712 rajveer 27
			params = {"actionName" , "${url}", "paymentid", "${pid}", "txnid", "${txn}", "amount", "${amount}"}),
693 rajveer 28
    @Result(name="shipping-redirect", type="redirectAction", 
712 rajveer 29
    		params = {"actionName" , "shipping"}),
30
    @Result(name="failure", type="redirectAction", 
31
    		params = {"actionName" , "shipping"})    		
681 rajveer 32
 
419 rajveer 33
})
650 rajveer 34
public class OrderController extends BaseController {
419 rajveer 35
 
36
	private static final long serialVersionUID = 1L;
37
 
38
	private static Log log = LogFactory.getLog(OrderController.class);
507 rajveer 39
	private String id;
712 rajveer 40
	private long txnId = 0;
650 rajveer 41
 
693 rajveer 42
	//FIXME right now only one PG. Once we will have more, need to fix it.
681 rajveer 43
	private String paymentUrl="hdfc-pay";
693 rajveer 44
	private int gatewayId=1;
681 rajveer 45
 
693 rajveer 46
	private long paymentId;
47
 
712 rajveer 48
	private double amount;
49
 
419 rajveer 50
	public OrderController(){
507 rajveer 51
		super();
419 rajveer 52
	}
53
 
507 rajveer 54
    // GET /order/ orderid
55
    public String show() {
56
    	log.info("id=" + id);
822 vikas 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(){
786 rajveer 64
    	String addressIdString = this.request.getParameter("addressid");
65
    	if(addressIdString == null){
66
    		addActionError("Please login to continue checkout.");
67
    		return "shipping-redirect";
68
    	}
69
    	long addressId = Long.parseLong(addressIdString);
572 chandransh 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
}