Subversion Repositories SmartDukaan

Rev

Rev 18930 | Rev 18937 | 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;
13425 amit.gupta 13
import in.shop2020.serving.utils.Utils;
8733 anupam.sin 14
import in.shop2020.thrift.clients.PaymentClient;
6050 anupam.sin 15
import in.shop2020.thrift.clients.TransactionClient;
16
import in.shop2020.thrift.clients.UserClient;
17
 
18
import org.apache.log4j.Logger;
19
import org.apache.struts2.convention.annotation.Result;
20
import org.apache.struts2.convention.annotation.Results;
21
 
22
 
23
@Results({
8733 anupam.sin 24
    @Result(name="recharge-redirect", type="redirectAction", params = {"actionName" , "recharge", "error", "${errorMessage}"}),
6050 anupam.sin 25
    @Result(name="recharge-pay-options-redirect", type="redirectAction", params = {"actionName" , "recharge-pay-options", "rechargeOrderId", "${rechargeOrderId}"}),
18935 kshitij.so 26
    @Result(name="ebs-pay-redirect", type="redirect", location="/ebs-pay/${paymentId}?phone=${phone}"),
6050 anupam.sin 27
    @Result(name="cod-redirect", type="redirect", location="/pay-success?paymentId=${paymentId}")
28
})
29
 
30
 
31
public class RechargePaymentController extends BaseController {
32
 
33
    /**
34
     * 
35
     */
36
    private String rechargeOrderId = "";
6228 anupam.sin 37
    private String name = "";
38
    private String line1 = "";
39
    private String line2 = "";
40
    private String city = "";
41
    private String state = "";
42
    private String pincode = "";
6050 anupam.sin 43
    private String phone = "";
44
 
45
 
46
    private static final long serialVersionUID = 2079308723099307749L;
47
 
48
    private static Logger log = Logger.getLogger(Class.class);
49
 
50
    public String index() {
51
        return "index";
52
    }
53
 
54
    /******************************************************
55
     * 
56
     * 
57
     * 
58
     *****************************************************/
59
 
60
 
61
    public long getPaymentId() {
62
        return paymentId;
63
    }
64
 
65
    private String id;
66
    private long txnId = 0;
67
 
68
    private long paymentId;
8733 anupam.sin 69
    private String errorMessage;
6050 anupam.sin 70
 
71
    private String redirectURL;
72
    private RechargeOrder rechargeOrder;
73
 
74
    // GET /order/ orderid
75
    public String show() {
76
        log.info("id=" + id);
77
        htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
78
        htmlSnippets.put("ORDER_DETAILS", pageLoader.getOrderDetailsHtml(Long.parseLong(id), userinfo));
79
        return "show";
80
    }
81
 
82
    // POST /order/
83
    public String create(){
6228 anupam.sin 84
 
6050 anupam.sin 85
        if(rechargeOrderId == null){
8733 anupam.sin 86
            setErrorMessage("There was some problem. Please try again.");
6050 anupam.sin 87
            return "recharge-redirect";
88
        }
6091 anupam.sin 89
        try {
90
            TransactionClient tc = new TransactionClient();
91
            rechargeOrder = tc.getClient().getRechargeOrder(Long.parseLong(rechargeOrderId));
6228 anupam.sin 92
        } catch (Exception ee) {
93
            log.error("problem fetching rechargeOrder : " + rechargeOrderId);
8733 anupam.sin 94
            setErrorMessage("There was some problem. Please try again.");
6228 anupam.sin 95
            return "recharge-redirect";
96
        }
8733 anupam.sin 97
 
6228 anupam.sin 98
        try {
8733 anupam.sin 99
            PaymentClient paymentServiceClient = new PaymentClient();
100
            List<Payment> payments = paymentServiceClient.getClient().getPaymentForRechargeTxnId(rechargeOrder.getTransactionId());
101
            if(payments.size() > 0) {
102
                setErrorMessage("There was some problem. Please try again.");
103
                return "recharge-redirect";
104
            }
105
        } catch (Exception ee) {
106
            log.error("problem fetching payment for rechargeId : " + rechargeOrderId);
107
            setErrorMessage("There was some problem. Please try again.");
108
            return "recharge-redirect";
109
        }
110
 
111
        try {
6228 anupam.sin 112
            UserClient uc = new UserClient();
113
            if(!name.isEmpty() && !phone.isEmpty()) {
6091 anupam.sin 114
                Address address = new Address();
115
                address.setName(name);
116
                address.setLine1(line1);
117
                address.setLine2(line2);
118
                address.setCity(city);
119
                address.setState(state);
120
                address.setCountry("India");
121
                address.setPin(pincode);
122
                address.setPhone(phone);
6228 anupam.sin 123
 
6050 anupam.sin 124
                uc.getClient().addAddressForUser(rechargeOrder.getUserId(), address, true);
125
            }
6228 anupam.sin 126
 
127
        } catch (Exception e) {
128
            log.error("Problem saving the address", e);
129
            return "recharge-pay-options-redirect";
6050 anupam.sin 130
        }
6091 anupam.sin 131
 
6050 anupam.sin 132
        String paymentOption = request.getParameter("payment_option");
133
        log.info("Payment Option Selected: " + paymentOption);
134
        if(paymentOption == null || paymentOption.equals("")){
135
            addActionError("Please select a payment method to continue.");
136
            return "recharge-pay-options-redirect";
137
        }
138
 
6228 anupam.sin 139
        if (name.isEmpty() && !phone.isEmpty()) {
140
            //This means we only got the phone from UI.
141
            //Lets keep it.
142
        } else {
143
            //For all other cases make it empty.
144
            phone = "";
145
        }
6050 anupam.sin 146
 
147
        if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || 
148
                paymentOption.equals(IPaymentService.HDFC_VISA) || paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON)) {
149
            // User has selected Visa or MasterCard
150
            HdfcPaymentService hdfcPaymentService = new HdfcPaymentService();
6228 anupam.sin 151
            paymentId = hdfcPaymentService.createPayment(rechargeOrder, paymentOption, phone);
6050 anupam.sin 152
            if (paymentId == IPaymentService.PAYMENT_NOT_CREATED) {
153
                log.error("Unable to process payment through HDFC. Falling through to EBS.");
154
            } else {
155
                this.redirectURL = hdfcPaymentService.getRedirectUrl();
156
                log.info(this.redirectURL);
157
                return "success";
158
            }
159
        }
160
 
161
        if(paymentOption.equals(IPaymentService.HDFC_VISA))
162
            paymentOption = IPaymentService.EBS_VISA;
163
        else if(paymentOption.equals(IPaymentService.HDFC_MASTERCARD_DEBIT) || paymentOption.equals(IPaymentService.HDFC_MASTERCARD_CREDIT))
164
            paymentOption = IPaymentService.EBS_MASTERCARD;
165
        else if(paymentOption.equals(IPaymentService.HDFC_VISA_ELECTRON))
166
            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 167
 
13425 amit.gupta 168
        if(paymentOption.equals(IPaymentService.PAYU_CC) || Utils.NET_BANKING_CODES_MAP.containsKey(paymentOption)){
13373 amit.gupta 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
}