Subversion Repositories SmartDukaan

Rev

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