| Line 14... |
Line 14... |
| 14 |
import in.shop2020.model.v1.user.PromotionException;
|
14 |
import in.shop2020.model.v1.user.PromotionException;
|
| 15 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
15 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 16 |
import in.shop2020.logistics.PickUpType;
|
16 |
import in.shop2020.logistics.PickUpType;
|
| 17 |
import in.shop2020.payments.Payment;
|
17 |
import in.shop2020.payments.Payment;
|
| 18 |
import in.shop2020.payments.PaymentException;
|
18 |
import in.shop2020.payments.PaymentException;
|
| - |
|
19 |
import in.shop2020.payments.PaymentStatus;
|
| 19 |
import in.shop2020.thrift.clients.CatalogClient;
|
20 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 20 |
import in.shop2020.thrift.clients.PaymentClient;
|
21 |
import in.shop2020.thrift.clients.PaymentClient;
|
| 21 |
import in.shop2020.thrift.clients.PromotionClient;
|
22 |
import in.shop2020.thrift.clients.PromotionClient;
|
| 22 |
import in.shop2020.thrift.clients.TransactionClient;
|
23 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 23 |
import in.shop2020.thrift.clients.UserClient;
|
24 |
import in.shop2020.thrift.clients.UserClient;
|
| Line 39... |
Line 40... |
| 39 |
public class CommonPaymentService {
|
40 |
public class CommonPaymentService {
|
| 40 |
|
41 |
|
| 41 |
private static final boolean PAYMENT_NOT_CREATED = false;
|
42 |
private static final boolean PAYMENT_NOT_CREATED = false;
|
| 42 |
|
43 |
|
| 43 |
private static Logger log = Logger.getLogger(Class.class);
|
44 |
private static Logger log = Logger.getLogger(Class.class);
|
| - |
|
45 |
private static long gvGatewayId = 9;
|
| 44 |
|
46 |
|
| 45 |
private long paymentId;
|
47 |
private long paymentId;
|
| 46 |
private double amount;
|
48 |
private double amount;
|
| - |
|
49 |
private boolean isGv = false;
|
| - |
|
50 |
private double gvAmount = 0;
|
| 47 |
|
51 |
|
| 48 |
public long getPaymentId() {
|
52 |
public long getPaymentId() {
|
| 49 |
return paymentId;
|
53 |
return paymentId;
|
| 50 |
}
|
54 |
}
|
| 51 |
|
55 |
|
| Line 70... |
Line 74... |
| 70 |
* @return True if the payment object is successfully created, False
|
74 |
* @return True if the payment object is successfully created, False
|
| 71 |
* otherwise.
|
75 |
* otherwise.
|
| 72 |
*/
|
76 |
*/
|
| 73 |
public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId, long sourceId){
|
77 |
public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId, long sourceId){
|
| 74 |
PaymentClient paymentServiceClient = null;
|
78 |
PaymentClient paymentServiceClient = null;
|
| - |
|
79 |
in.shop2020.payments.PaymentService.Client pclient;
|
| 75 |
try {
|
80 |
try {
|
| 76 |
paymentServiceClient = new PaymentClient();
|
81 |
paymentServiceClient = new PaymentClient();
|
| - |
|
82 |
pclient = paymentServiceClient.getClient();
|
| 77 |
} catch (Exception e) {
|
83 |
} catch (Exception e) {
|
| 78 |
log.error("Error while getting payment client", e);
|
84 |
log.error("Error while getting payment client", e);
|
| 79 |
return PAYMENT_NOT_CREATED;
|
85 |
return PAYMENT_NOT_CREATED;
|
| 80 |
}
|
86 |
}
|
| 81 |
|
87 |
|
| Line 85... |
Line 91... |
| 85 |
log.error("Unable to fetch payment amount from cart id.", e1);
|
91 |
log.error("Unable to fetch payment amount from cart id.", e1);
|
| 86 |
return PAYMENT_NOT_CREATED;
|
92 |
return PAYMENT_NOT_CREATED;
|
| 87 |
} catch (TException e1) {
|
93 |
} catch (TException e1) {
|
| 88 |
log.error("Unable to fetch payment amount.", e1);
|
94 |
log.error("Unable to fetch payment amount.", e1);
|
| 89 |
return PAYMENT_NOT_CREATED;
|
95 |
return PAYMENT_NOT_CREATED;
|
| - |
|
96 |
} catch (PromotionException e1) {
|
| - |
|
97 |
log.error("Promotion service is down.", e1);
|
| - |
|
98 |
return PAYMENT_NOT_CREATED;
|
| 90 |
}
|
99 |
}
|
| 91 |
|
100 |
|
| 92 |
try {
|
101 |
try {
|
| - |
|
102 |
if(isGv){
|
| - |
|
103 |
paymentId = pclient.createPayment(userId, gvAmount, gvGatewayId, txnId, false);
|
| - |
|
104 |
pclient.updatePaymentDetails(paymentId, "", "", "SUCCESS", "Payment Received", "", "", "", "", PaymentStatus.SUCCESS, "", null);
|
| - |
|
105 |
}
|
| 93 |
paymentId = paymentServiceClient.getClient().createPayment(userId, amount, gatewayId, txnId, false);
|
106 |
paymentId = paymentServiceClient.getClient().createPayment(userId, amount, gatewayId, txnId, false);
|
| 94 |
// This is being done to ensure that the amount which we pass on to
|
107 |
// This is being done to ensure that the amount which we pass on to
|
| 95 |
// the PGs is same as what we have in the database.
|
108 |
// the PGs is same as what we have in the database.
|
| 96 |
Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
|
109 |
Payment payment = paymentServiceClient.getClient().getPayment(paymentId);
|
| 97 |
amount = payment.getAmount();
|
110 |
amount = payment.getAmount();
|
| Line 226... |
Line 239... |
| 226 |
* calculated.
|
239 |
* calculated.
|
| 227 |
* @return The total amount for which a payment should be created.
|
240 |
* @return The total amount for which a payment should be created.
|
| 228 |
* @throws ShoppingCartException
|
241 |
* @throws ShoppingCartException
|
| 229 |
* If no cart can be found for the given id.
|
242 |
* If no cart can be found for the given id.
|
| 230 |
* @throws TException
|
243 |
* @throws TException
|
| - |
|
244 |
* @throws PromotionException
|
| 231 |
*/
|
245 |
*/
|
| 232 |
private double calculatePaymentAmount(long cartId, long sourceId) throws ShoppingCartException, TException{
|
246 |
private double calculatePaymentAmount(long cartId, long sourceId) throws ShoppingCartException, TException, PromotionException{
|
| 233 |
double totalAmount = 0;
|
247 |
double totalAmount = 0;
|
| 234 |
Cart cart;
|
248 |
Cart cart;
|
| 235 |
UserClient userContextServiceClient = null;
|
249 |
UserClient userContextServiceClient = null;
|
| 236 |
try {
|
250 |
try {
|
| 237 |
userContextServiceClient = new UserClient();
|
251 |
userContextServiceClient = new UserClient();
|
| Line 240... |
Line 254... |
| 240 |
throw new ShoppingCartException(100, "Unable to initialize the user service client");
|
254 |
throw new ShoppingCartException(100, "Unable to initialize the user service client");
|
| 241 |
}
|
255 |
}
|
| 242 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
256 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
| 243 |
cart = userClient.getCart(cartId);
|
257 |
cart = userClient.getCart(cartId);
|
| 244 |
|
258 |
|
| 245 |
if(cart.getCouponCode() == null) {
|
- |
|
| 246 |
List<Line> lineItems = cart.getLines();
|
259 |
List<Line> lineItems = cart.getLines();
|
| 247 |
|
260 |
|
| 248 |
for (Line line : lineItems) {
|
261 |
for (Line line : lineItems) {
|
| 249 |
long productId = line.getItemId();
|
262 |
long productId = line.getItemId();
|
| 250 |
// FIXME: It's expensive to get the price of each item from the
|
263 |
// FIXME: It's expensive to get the price of each item from the
|
| 251 |
// catalog service. We maintain the pricing info in the line items
|
264 |
// catalog service. We maintain the pricing info in the line items
|
| 252 |
// themselves now.
|
265 |
// themselves now.
|
| 253 |
totalAmount = totalAmount + line.getQuantity() * getItemPrice(productId, sourceId);
|
266 |
totalAmount = totalAmount + line.getQuantity() * getItemPrice(productId, sourceId);
|
| 254 |
}
|
267 |
}
|
| 255 |
} else {
|
268 |
|
| - |
|
269 |
if(cart.getCouponCode() != null) {
|
| - |
|
270 |
isGv = (new PromotionClient()).getClient().isGiftVoucher(cart.getCouponCode());
|
| - |
|
271 |
gvAmount = totalAmount - cart.getDiscountedPrice();
|
| 256 |
totalAmount = cart.getDiscountedPrice();
|
272 |
totalAmount = cart.getDiscountedPrice();
|
| 257 |
}
|
273 |
}
|
| 258 |
|
274 |
|
| 259 |
return totalAmount;
|
275 |
return totalAmount;
|
| 260 |
}
|
276 |
}
|
| 261 |
|
277 |
|