| Line 1... |
Line 1... |
| 1 |
package in.shop2020.serving.services;
|
1 |
package in.shop2020.serving.services;
|
| 2 |
|
2 |
|
| 3 |
import in.shop2020.model.v1.catalog.Item;
|
- |
|
| 4 |
import in.shop2020.model.v1.catalog.CatalogService.Client;
|
- |
|
| 5 |
import in.shop2020.model.v1.order.LineItem;
|
3 |
import in.shop2020.model.v1.order.LineItem;
|
| 6 |
import in.shop2020.model.v1.order.Order;
|
4 |
import in.shop2020.model.v1.order.Order;
|
| 7 |
import in.shop2020.model.v1.order.RechargeOrder;
|
5 |
import in.shop2020.model.v1.order.RechargeOrder;
|
| 8 |
import in.shop2020.model.v1.order.Transaction;
|
6 |
import in.shop2020.model.v1.order.Transaction;
|
| 9 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
7 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
| 10 |
import in.shop2020.model.v1.order.TransactionStatus;
|
8 |
import in.shop2020.model.v1.order.TransactionStatus;
|
| 11 |
import in.shop2020.model.v1.order.OrderType;
|
9 |
import in.shop2020.model.v1.order.OrderType;
|
| 12 |
import in.shop2020.model.v1.user.Cart;
|
- |
|
| 13 |
import in.shop2020.model.v1.user.Line;
|
- |
|
| 14 |
import in.shop2020.model.v1.user.PromotionException;
|
10 |
import in.shop2020.model.v1.user.PromotionException;
|
| 15 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
11 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 16 |
import in.shop2020.logistics.PickUpType;
|
12 |
import in.shop2020.logistics.PickUpType;
|
| 17 |
import in.shop2020.payments.Payment;
|
13 |
import in.shop2020.payments.Payment;
|
| 18 |
import in.shop2020.payments.PaymentException;
|
14 |
import in.shop2020.payments.PaymentException;
|
| 19 |
import in.shop2020.payments.PaymentStatus;
|
15 |
import in.shop2020.payments.PaymentStatus;
|
| 20 |
import in.shop2020.thrift.clients.CatalogClient;
|
- |
|
| 21 |
import in.shop2020.thrift.clients.PaymentClient;
|
16 |
import in.shop2020.thrift.clients.PaymentClient;
|
| 22 |
import in.shop2020.thrift.clients.PromotionClient;
|
17 |
import in.shop2020.thrift.clients.PromotionClient;
|
| 23 |
import in.shop2020.thrift.clients.TransactionClient;
|
18 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 24 |
import in.shop2020.thrift.clients.UserClient;
|
19 |
import in.shop2020.thrift.clients.UserClient;
|
| 25 |
|
20 |
|
| 26 |
import java.util.HashMap;
|
21 |
import java.util.HashMap;
|
| 27 |
import java.util.List;
|
- |
|
| 28 |
import java.util.Map;
|
22 |
import java.util.Map;
|
| 29 |
|
23 |
|
| 30 |
import org.apache.log4j.Logger;
|
24 |
import org.apache.log4j.Logger;
|
| 31 |
import org.apache.thrift.TException;
|
25 |
import org.apache.thrift.TException;
|
| 32 |
|
26 |
|
| Line 62... |
Line 56... |
| 62 |
* transaction. Stores the id of the newly created payment and the amount
|
56 |
* transaction. Stores the id of the newly created payment and the amount
|
| 63 |
* for which this payment was created. They can be retrieved later on using
|
57 |
* for which this payment was created. They can be retrieved later on using
|
| 64 |
* {@link #getPaymentId()}getPaymentId() and {@link #getAmount()}getAmount()
|
58 |
* {@link #getPaymentId()}getPaymentId() and {@link #getAmount()}getAmount()
|
| 65 |
* methods respectively later on.
|
59 |
* methods respectively later on.
|
| 66 |
*
|
60 |
*
|
| 67 |
* @param currentCartId
|
- |
|
| 68 |
* The cart for which the payment object has to be created.
|
- |
|
| 69 |
* @param userId
|
61 |
* @param userId
|
| 70 |
* The user for whom the payment has to be created.
|
62 |
* The user for whom the payment has to be created.
|
| 71 |
* @param txnId
|
63 |
* @param txnId
|
| 72 |
* The transaction against which the payment has to be created.
|
64 |
* The transaction against which the payment has to be created.
|
| 73 |
* @param gatewayId
|
65 |
* @param gatewayId
|
| 74 |
* @return True if the payment object is successfully created, False
|
66 |
* @return True if the payment object is successfully created, False
|
| 75 |
* otherwise.
|
67 |
* otherwise.
|
| 76 |
*/
|
68 |
*/
|
| 77 |
public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId, long sourceId){
|
69 |
public boolean createPayment(long userId, long txnId, int gatewayId){
|
| 78 |
PaymentClient paymentServiceClient = null;
|
70 |
PaymentClient paymentServiceClient = null;
|
| 79 |
in.shop2020.payments.PaymentService.Client pclient;
|
71 |
in.shop2020.payments.PaymentService.Client pclient;
|
| 80 |
try {
|
72 |
try {
|
| 81 |
paymentServiceClient = new PaymentClient();
|
73 |
paymentServiceClient = new PaymentClient();
|
| 82 |
pclient = paymentServiceClient.getClient();
|
74 |
pclient = paymentServiceClient.getClient();
|
| Line 84... |
Line 76... |
| 84 |
log.error("Error while getting payment client", e);
|
76 |
log.error("Error while getting payment client", e);
|
| 85 |
return PAYMENT_NOT_CREATED;
|
77 |
return PAYMENT_NOT_CREATED;
|
| 86 |
}
|
78 |
}
|
| 87 |
|
79 |
|
| 88 |
try {
|
80 |
try {
|
| 89 |
amount = calculatePaymentAmount(currentCartId, sourceId);
|
81 |
amount = calculatePaymentAmount(txnId);
|
| 90 |
} catch (ShoppingCartException e1) {
|
82 |
} catch (TransactionServiceException e1) {
|
| 91 |
log.error("Unable to fetch payment amount from cart id.", e1);
|
83 |
log.error("Unable to fetch payment amount from txn id.", e1);
|
| 92 |
return PAYMENT_NOT_CREATED;
|
84 |
return PAYMENT_NOT_CREATED;
|
| 93 |
} catch (TException e1) {
|
85 |
} catch (TException e1) {
|
| 94 |
log.error("Unable to fetch payment amount.", e1);
|
86 |
log.error("Unable to fetch payment amount.", e1);
|
| 95 |
return PAYMENT_NOT_CREATED;
|
87 |
return PAYMENT_NOT_CREATED;
|
| 96 |
} catch (PromotionException e1) {
|
- |
|
| 97 |
log.error("Promotion service is down.", e1);
|
- |
|
| 98 |
return PAYMENT_NOT_CREATED;
|
- |
|
| 99 |
}
|
88 |
}
|
| 100 |
|
89 |
|
| 101 |
try {
|
90 |
try {
|
| 102 |
if(isGv){
|
91 |
if(isGv){
|
| 103 |
paymentId = pclient.createPayment(userId, gvAmount, gvGatewayId, txnId, false);
|
92 |
paymentId = pclient.createPayment(userId, gvAmount, gvGatewayId, txnId, false);
|
| Line 230... |
Line 219... |
| 230 |
log.error("Unable to update status of transaction. Thrift Exception: ", e);
|
219 |
log.error("Unable to update status of transaction. Thrift Exception: ", e);
|
| 231 |
}
|
220 |
}
|
| 232 |
}
|
221 |
}
|
| 233 |
|
222 |
|
| 234 |
/**
|
223 |
/**
|
| 235 |
* Calculates the amount for the payment required for the given cart.
|
224 |
* Calculates the amount for the payment required for the given transaction.
|
| 236 |
*
|
225 |
*
|
| 237 |
* @param cartId
|
226 |
* @param txnId
|
| 238 |
* Id of the cart for which this payment amount has to be
|
227 |
* Id of the transaction for which this payment amount has to be
|
| 239 |
* calculated.
|
228 |
* calculated.
|
| 240 |
* @return The total amount for which a payment should be created.
|
229 |
* @return The total amount for which a payment should be created.
|
| 241 |
* @throws ShoppingCartException
|
230 |
* @throws TransactionServiceException
|
| 242 |
* If no cart can be found for the given id.
|
- |
|
| 243 |
* @throws TException
|
231 |
* @throws TException
|
| 244 |
* @throws PromotionException
|
- |
|
| 245 |
*/
|
232 |
*/
|
| 246 |
private double calculatePaymentAmount(long cartId, long sourceId) throws ShoppingCartException, TException, PromotionException{
|
233 |
private double calculatePaymentAmount(long txnId) throws TransactionServiceException, TException{
|
| 247 |
double totalAmount = 0;
|
234 |
double totalAmount = 0;
|
| 248 |
Cart cart;
|
- |
|
| 249 |
UserClient userContextServiceClient = null;
|
235 |
TransactionClient transactionServiceClient = null;
|
| 250 |
try {
|
236 |
try {
|
| 251 |
userContextServiceClient = new UserClient();
|
237 |
transactionServiceClient = new TransactionClient();
|
| 252 |
} catch (Exception e) {
|
238 |
} catch (Exception e) {
|
| 253 |
log.error("Unable to initialize user context service client", e);
|
239 |
log.error("Unable to initialize user context service client", e);
|
| 254 |
throw new ShoppingCartException(100, "Unable to initialize the user service client");
|
240 |
throw new TransactionServiceException(100, "Unable to initialize the user service client");
|
| 255 |
}
|
241 |
}
|
| 256 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
242 |
in.shop2020.model.v1.order.TransactionService.Client tClient = transactionServiceClient.getClient();
|
| 257 |
cart = userClient.getCart(cartId);
|
243 |
Transaction transaction = tClient.getTransaction(txnId);
|
| 258 |
|
- |
|
| 259 |
List<Line> lineItems = cart.getLines();
|
244 |
Map<Long, Long> miscCharges = tClient.getMiscCharges(txnId);
|
| 260 |
|
- |
|
| 261 |
for (Line line : lineItems) {
|
245 |
if(!miscCharges.isEmpty()){
|
| 262 |
long productId = line.getItemId();
|
- |
|
| 263 |
// FIXME: It's expensive to get the price of each item from the
|
- |
|
| 264 |
// catalog service. We maintain the pricing info in the line items
|
- |
|
| 265 |
// themselves now.
|
- |
|
| 266 |
totalAmount = totalAmount + line.getQuantity() * getItemPrice(productId, sourceId);
|
- |
|
| 267 |
}
|
- |
|
| 268 |
|
- |
|
| 269 |
if(cart.getCouponCode() != null) {
|
- |
|
| 270 |
isGv = (new PromotionClient()).getClient().isGiftVoucher(cart.getCouponCode());
|
- |
|
| 271 |
gvAmount = totalAmount - cart.getDiscountedPrice();
|
246 |
totalAmount = totalAmount + miscCharges.get(1);
|
| 272 |
totalAmount = cart.getDiscountedPrice();
|
- |
|
| 273 |
}
|
247 |
}
|
| 274 |
|
- |
|
| 275 |
return totalAmount;
|
- |
|
| 276 |
}
|
- |
|
| 277 |
|
- |
|
| 278 |
|
- |
|
| 279 |
private double getItemPrice(long itemId, long sourceId){
|
248 |
for(Order order: transaction.getOrders()){
|
| 280 |
CatalogClient catalogClient = null;
|
- |
|
| 281 |
Client client = null;
|
- |
|
| 282 |
Double itemPrice = 0.0;
|
- |
|
| 283 |
try {
|
- |
|
| 284 |
catalogClient = new CatalogClient();
|
249 |
totalAmount = totalAmount + order.getTotal_amount();
|
| 285 |
client = catalogClient.getClient();
|
250 |
gvAmount = gvAmount + order.getGvAmount();
|
| 286 |
Item item = client.getItemForSource(itemId, sourceId);
|
- |
|
| 287 |
itemPrice = item.getSellingPrice();
|
- |
|
| 288 |
|
251 |
}
|
| 289 |
} catch(Exception e){
|
252 |
if(gvAmount>0){
|
| 290 |
log.error("Unable to get the item price because of:", e);
|
253 |
isGv = true;
|
| 291 |
}
|
254 |
}
|
| 292 |
return itemPrice;
|
255 |
return totalAmount;
|
| 293 |
}
|
256 |
}
|
| 294 |
|
257 |
|
| 295 |
|
- |
|
| 296 |
/**
|
258 |
/**
|
| 297 |
* Removes the items processed through the given transaction from the
|
259 |
* Removes the items processed through the given transaction from the
|
| 298 |
* shopping cart.
|
260 |
* shopping cart.
|
| 299 |
*
|
261 |
*
|
| 300 |
* @param transaction
|
262 |
* @param transaction
|
| Line 364... |
Line 326... |
| 364 |
} catch (Exception e) {
|
326 |
} catch (Exception e) {
|
| 365 |
log.error("Error while getting payment client", e);
|
327 |
log.error("Error while getting payment client", e);
|
| 366 |
return PAYMENT_NOT_CREATED;
|
328 |
return PAYMENT_NOT_CREATED;
|
| 367 |
}
|
329 |
}
|
| 368 |
|
330 |
|
| 369 |
|
- |
|
| 370 |
/*******************IS THIS NECESSARY ????
|
- |
|
| 371 |
*
|
- |
|
| 372 |
*
|
- |
|
| 373 |
*
|
- |
|
| 374 |
* try {
|
- |
|
| 375 |
amount = calculatePaymentAmount(currentCartId, sourceId);
|
- |
|
| 376 |
} catch (ShoppingCartException e1) {
|
- |
|
| 377 |
log.error("Unable to fetch payment amount from cart id.", e1);
|
- |
|
| 378 |
return PAYMENT_NOT_CREATED;
|
- |
|
| 379 |
} catch (TException e1) {
|
- |
|
| 380 |
log.error("Unable to fetch payment amount.", e1);
|
- |
|
| 381 |
return PAYMENT_NOT_CREATED;
|
- |
|
| 382 |
}
|
- |
|
| 383 |
*
|
- |
|
| 384 |
*/
|
- |
|
| 385 |
amount = rechargeOrder.getTotalAmount() - rechargeOrder.getWalletAmount();
|
331 |
amount = rechargeOrder.getTotalAmount() - rechargeOrder.getWalletAmount();
|
| 386 |
|
332 |
|
| 387 |
try {
|
333 |
try {
|
| 388 |
paymentId = paymentServiceClient.getClient().createPayment(rechargeOrder.getUserId(), amount, gatewayId, rechargeOrder.getTransactionId(), true);
|
334 |
paymentId = paymentServiceClient.getClient().createPayment(rechargeOrder.getUserId(), amount, gatewayId, rechargeOrder.getTransactionId(), true);
|
| 389 |
// This is being done to ensure that the amount which we pass on to
|
335 |
// This is being done to ensure that the amount which we pass on to
|