Subversion Repositories SmartDukaan

Rev

Rev 6228 | Rev 6270 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6050 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import in.shop2020.datalogger.EventType;
7
import in.shop2020.model.v1.order.Order;
8
import in.shop2020.model.v1.order.RechargeOrder;
9
import in.shop2020.model.v1.order.TransactionServiceException;
10
import in.shop2020.model.v1.user.Address;
11
import in.shop2020.model.v1.user.ShoppingCartException;
12
import in.shop2020.model.v1.user.UserContextException;
13
import in.shop2020.serving.interceptors.TrackingInterceptor;
14
import in.shop2020.serving.services.CodPaymentService;
15
import in.shop2020.serving.services.CommonPaymentService;
16
import in.shop2020.serving.services.EbsPaymentService;
17
import in.shop2020.serving.services.HdfcEmiPaymentService;
18
import in.shop2020.serving.services.HdfcPaymentService;
19
import in.shop2020.serving.services.IPaymentService;
20
import in.shop2020.serving.utils.Utils;
21
import in.shop2020.thrift.clients.TransactionClient;
22
import in.shop2020.thrift.clients.UserClient;
23
import in.shop2020.utils.DataLogger;
24
 
25
import nl.captcha.Captcha;
26
 
27
import org.apache.log4j.Logger;
28
import org.apache.struts2.convention.annotation.Result;
29
import org.apache.struts2.convention.annotation.Results;
30
import org.apache.thrift.TException;
31
import org.apache.thrift.transport.TTransportException;
32
 
33
 
34
@Results({
35
    @Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge"}),
36
    @Result(name="recharge-pay-options-redirect", type="redirectAction", params = {"actionName" , "recharge-pay-options", "rechargeOrderId", "${rechargeOrderId}"}),
6233 anupam.sin 37
    @Result(name="ebs-pay-redirect", type="redirectAction", params = {"actionName", "/ebs-pay/${paymentId}/edit", "phone", "${phone}"}),
6050 anupam.sin 38
    @Result(name="cod-redirect", type="redirect", location="/pay-success?paymentId=${paymentId}")
39
})
40
 
41
 
42
public class RechargePaymentController extends BaseController {
43
 
44
    /**
45
     * 
46
     */
47
    private String rechargeOrderId = "";
6228 anupam.sin 48
    private String name = "";
49
    private String line1 = "";
50
    private String line2 = "";
51
    private String city = "";
52
    private String state = "";
53
    private String pincode = "";
6050 anupam.sin 54
    private String phone = "";
55
 
56
 
57
    private static final long serialVersionUID = 2079308723099307749L;
58
 
59
    private static Logger log = Logger.getLogger(Class.class);
60
 
61
    public String index() {
62
        return "index";
63
    }
64
 
65
    /******************************************************
66
     * 
67
     * 
68
     * 
69
     *****************************************************/
70
 
71
 
72
    public long getPaymentId() {
73
        return paymentId;
74
    }
75
 
76
    private String id;
77
    private long txnId = 0;
78
 
79
    private long paymentId;
80
 
81
    private String redirectURL;
82
    private RechargeOrder rechargeOrder;
83
 
84
    // GET /order/ orderid
85
    public String show() {
86
        log.info("id=" + id);
87
        htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
88
        htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id), userinfo));
89
        return "show";
90
    }
91
 
92
    // POST /order/
93
    public String create(){
6228 anupam.sin 94
 
6050 anupam.sin 95
        if(rechargeOrderId == null){
96
            return "recharge-redirect";
97
        }
6091 anupam.sin 98
        try {
99
            TransactionClient tc = new TransactionClient();
100
            rechargeOrder = tc.getClient().getRechargeOrder(Long.parseLong(rechargeOrderId));
6228 anupam.sin 101
        } catch (Exception ee) {
102
            log.error("problem fetching rechargeOrder : " + rechargeOrderId);
103
            return "recharge-redirect";
104
        }
105
        try {
106
            UserClient uc = new UserClient();
107
            if(!name.isEmpty() && !phone.isEmpty()) {
6091 anupam.sin 108
                Address address = new Address();
109
                address.setName(name);
110
                address.setLine1(line1);
111
                address.setLine2(line2);
112
                address.setCity(city);
113
                address.setState(state);
114
                address.setCountry("India");
115
                address.setPin(pincode);
116
                address.setPhone(phone);
6228 anupam.sin 117
 
6050 anupam.sin 118
                uc.getClient().addAddressForUser(rechargeOrder.getUserId(), address, true);
119
            }
6228 anupam.sin 120
 
121
        } catch (Exception e) {
122
            log.error("Problem saving the address", e);
123
            return "recharge-pay-options-redirect";
6050 anupam.sin 124
        }
6091 anupam.sin 125
 
6050 anupam.sin 126
        String paymentOption = request.getParameter("payment_option");
127
        log.info("Payment Option Selected: " + paymentOption);
128
        if(paymentOption == null || paymentOption.equals("")){
129
            addActionError("Please select a payment method to continue.");
130
            return "recharge-pay-options-redirect";
131
        }
132
 
6228 anupam.sin 133
        if (name.isEmpty() && !phone.isEmpty()) {
134
            //This means we only got the phone from UI.
135
            //Lets keep it.
136
        } else {
137
            //For all other cases make it empty.
138
            phone = "";
139
        }
6050 anupam.sin 140
 
6228 anupam.sin 141
 
6050 anupam.sin 142
        if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || 
143
                paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
144
            // User has selected Visa or MasterCard
145
            HdfcPaymentService hdfcPaymentService = new HdfcPaymentService();
6228 anupam.sin 146
            paymentId = hdfcPaymentService.createPayment(rechargeOrder, paymentOption, phone);
6050 anupam.sin 147
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
148
                log.error("Unable to process payment through HDFC. Falling through to EBS.");
149
            } else {
150
                this.redirectURL = hdfcPaymentService.getRedirectUrl();
151
                log.info(this.redirectURL);
152
                return "success";
153
            }
154
        }
155
 
156
        if(paymentOption.equals(IPaymentService.HDFC_VISA))
157
            paymentOption = IPaymentService.EBS_VISA;
158
        else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT))
159
            paymentOption = IPaymentService.EBS_MASTERCARD;
160
        else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
161
            paymentOption = null;           //Since we don't know the bank's name in this case, we'll let the user select the bank on the EBS page.
162
 
163
        EbsPaymentService ebsPaymentService = new EbsPaymentService();
164
        paymentId = ebsPaymentService.createPayment(rechargeOrder, paymentOption);
165
        if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
166
            addActionError("We are experiencing some problems. Please try later.");
167
            log.error("Unable to process payment through EBS.");
6215 anupam.sin 168
            return "recharge-redirect";
6050 anupam.sin 169
        } else {
170
            log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
171
            return "ebs-pay-redirect";
172
        }           
173
    }
174
 
175
    public String getMyaccountHeaderSnippet(){
176
        return htmlSnippets.get("MYACCOUNT_HEADER");
177
    }
178
 
179
    public String getOrderDetailsSnippet(){
180
        return htmlSnippets.get("ORDER_DETAILS");
181
    }
182
 
183
    public long getTxn(){
184
        return this.txnId;
185
    }
186
 
187
    public String getRedirectURL(){
188
        return this.redirectURL;
189
    }
190
 
191
    public void setRechargeOrderId(String rechargeOrderId) {
192
        this.rechargeOrderId = rechargeOrderId;
193
    }
194
 
195
    public String getRechargeOrderId() {
196
        return rechargeOrderId;
197
    }
198
 
199
    public String getPhone() {
200
        return phone;
201
    }
202
 
203
    public void setPhone(String phone) {
204
        this.phone = phone;
205
    }
6228 anupam.sin 206
 
207
    public String getName() {
208
        return name;
209
    }
210
 
211
    public void setName(String name) {
212
        this.name = name;
213
    }
214
 
215
    public String getLine1() {
216
        return line1;
217
    }
218
 
219
    public void setLine1(String line1) {
220
        this.line1 = line1;
221
    }
222
 
223
    public String getLine2() {
224
        return line2;
225
    }
226
 
227
    public void setLine2(String line2) {
228
        this.line2 = line2;
229
    }
230
 
231
    public String getCity() {
232
        return city;
233
    }
234
 
235
    public void setCity(String city) {
236
        this.city = city;
237
    }
238
 
239
    public String getState() {
240
        return state;
241
    }
242
 
243
    public void setState(String state) {
244
        this.state = state;
245
    }
246
 
247
    public String getPincode() {
248
        return pincode;
249
    }
250
 
251
    public void setPincode(String pincode) {
252
        this.pincode = pincode;
253
    }
6050 anupam.sin 254
}