Subversion Repositories SmartDukaan

Rev

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