Subversion Repositories SmartDukaan

Rev

Rev 6270 | Rev 10968 | 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
 
6228 anupam.sin 145
 
6050 anupam.sin 146
        if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || 
147
                paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
148
            // User has selected Visa or MasterCard
149
            HdfcPaymentService hdfcPaymentService = new HdfcPaymentService();
6228 anupam.sin 150
            paymentId = hdfcPaymentService.createPayment(rechargeOrder, paymentOption, phone);
6050 anupam.sin 151
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
152
                log.error("Unable to process payment through HDFC. Falling through to EBS.");
153
            } else {
154
                this.redirectURL = hdfcPaymentService.getRedirectUrl();
155
                log.info(this.redirectURL);
156
                return "success";
157
            }
158
        }
159
 
160
        if(paymentOption.equals(IPaymentService.HDFC_VISA))
161
            paymentOption = IPaymentService.EBS_VISA;
162
        else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT))
163
            paymentOption = IPaymentService.EBS_MASTERCARD;
164
        else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
165
            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.
166
 
167
        EbsPaymentService ebsPaymentService = new EbsPaymentService();
168
        paymentId = ebsPaymentService.createPayment(rechargeOrder, paymentOption);
169
        if(paymentId == IPaymentService.PAYMENT_NOT_CREATED){
170
            addActionError("We are experiencing some problems. Please try later.");
171
            log.error("Unable to process payment through EBS.");
8733 anupam.sin 172
            setErrorMessage("There was some problem. Please try again.");
6215 anupam.sin 173
            return "recharge-redirect";
6050 anupam.sin 174
        } else {
175
            log.info("Successfully created payment for EBS to process. Redirecting to /ebs-pay/" + paymentId);
176
            return "ebs-pay-redirect";
177
        }           
178
    }
179
 
180
    public String getMyaccountHeaderSnippet(){
181
        return htmlSnippets.get("MYACCOUNT_HEADER");
182
    }
183
 
184
    public String getOrderDetailsSnippet(){
185
        return htmlSnippets.get("ORDER_DETAILS");
186
    }
187
 
188
    public long getTxn(){
189
        return this.txnId;
190
    }
191
 
192
    public String getRedirectURL(){
193
        return this.redirectURL;
194
    }
195
 
196
    public void setRechargeOrderId(String rechargeOrderId) {
197
        this.rechargeOrderId = rechargeOrderId;
198
    }
199
 
200
    public String getRechargeOrderId() {
201
        return rechargeOrderId;
202
    }
203
 
204
    public String getPhone() {
205
        return phone;
206
    }
207
 
208
    public void setPhone(String phone) {
209
        this.phone = phone;
210
    }
6228 anupam.sin 211
 
212
    public String getName() {
213
        return name;
214
    }
215
 
216
    public void setName(String name) {
217
        this.name = name;
218
    }
219
 
220
    public String getLine1() {
221
        return line1;
222
    }
223
 
224
    public void setLine1(String line1) {
225
        this.line1 = line1;
226
    }
227
 
228
    public String getLine2() {
229
        return line2;
230
    }
231
 
232
    public void setLine2(String line2) {
233
        this.line2 = line2;
234
    }
235
 
236
    public String getCity() {
237
        return city;
238
    }
239
 
240
    public void setCity(String city) {
241
        this.city = city;
242
    }
243
 
244
    public String getState() {
245
        return state;
246
    }
247
 
248
    public void setState(String state) {
249
        this.state = state;
250
    }
251
 
252
    public String getPincode() {
253
        return pincode;
254
    }
255
 
256
    public void setPincode(String pincode) {
257
        this.pincode = pincode;
258
    }
8733 anupam.sin 259
 
260
    public void setErrorMessage(String errorMessage) {
261
        this.errorMessage = errorMessage;
262
    }
263
 
264
    public String getErrorMessage() {
265
        return errorMessage;
266
    }
6050 anupam.sin 267
}