Subversion Repositories SmartDukaan

Rev

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