| 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.InventoryService.Client;
|
| 3 |
import in.shop2020.model.v1.order.LineItem;
|
5 |
import in.shop2020.model.v1.order.LineItem;
|
| 4 |
import in.shop2020.model.v1.order.Order;
|
6 |
import in.shop2020.model.v1.order.Order;
|
| 5 |
import in.shop2020.model.v1.order.Transaction;
|
7 |
import in.shop2020.model.v1.order.Transaction;
|
| 6 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
8 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
| 7 |
import in.shop2020.model.v1.order.TransactionStatus;
|
9 |
import in.shop2020.model.v1.order.TransactionStatus;
|
| Line 9... |
Line 11... |
| 9 |
import in.shop2020.model.v1.user.Line;
|
11 |
import in.shop2020.model.v1.user.Line;
|
| 10 |
import in.shop2020.model.v1.user.PromotionException;
|
12 |
import in.shop2020.model.v1.user.PromotionException;
|
| 11 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
13 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 12 |
import in.shop2020.payments.Payment;
|
14 |
import in.shop2020.payments.Payment;
|
| 13 |
import in.shop2020.payments.PaymentException;
|
15 |
import in.shop2020.payments.PaymentException;
|
| 14 |
import in.shop2020.serving.utils.Utils;
|
16 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 15 |
import in.shop2020.thrift.clients.PaymentClient;
|
17 |
import in.shop2020.thrift.clients.PaymentClient;
|
| 16 |
import in.shop2020.thrift.clients.PromotionClient;
|
18 |
import in.shop2020.thrift.clients.PromotionClient;
|
| 17 |
import in.shop2020.thrift.clients.TransactionClient;
|
19 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 18 |
import in.shop2020.thrift.clients.UserClient;
|
20 |
import in.shop2020.thrift.clients.UserClient;
|
| 19 |
|
21 |
|
| Line 63... |
Line 65... |
| 63 |
* The transaction against which the payment has to be created.
|
65 |
* The transaction against which the payment has to be created.
|
| 64 |
* @param gatewayId
|
66 |
* @param gatewayId
|
| 65 |
* @return True if the payment object is successfully created, False
|
67 |
* @return True if the payment object is successfully created, False
|
| 66 |
* otherwise.
|
68 |
* otherwise.
|
| 67 |
*/
|
69 |
*/
|
| 68 |
public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId){
|
70 |
public boolean createPayment(long currentCartId, long userId, long txnId, int gatewayId, long sourceId){
|
| 69 |
PaymentClient paymentServiceClient = null;
|
71 |
PaymentClient paymentServiceClient = null;
|
| 70 |
try {
|
72 |
try {
|
| 71 |
paymentServiceClient = new PaymentClient();
|
73 |
paymentServiceClient = new PaymentClient();
|
| 72 |
} catch (Exception e) {
|
74 |
} catch (Exception e) {
|
| 73 |
log.error("Error while getting payment client", e);
|
75 |
log.error("Error while getting payment client", e);
|
| 74 |
return PAYMENT_NOT_CREATED;
|
76 |
return PAYMENT_NOT_CREATED;
|
| 75 |
}
|
77 |
}
|
| 76 |
|
78 |
|
| 77 |
try {
|
79 |
try {
|
| 78 |
amount = calculatePaymentAmount(currentCartId);
|
80 |
amount = calculatePaymentAmount(currentCartId, sourceId);
|
| 79 |
} catch (ShoppingCartException e1) {
|
81 |
} catch (ShoppingCartException e1) {
|
| 80 |
log.error("Unable to fetch payment amount from cart id.", e1);
|
82 |
log.error("Unable to fetch payment amount from cart id.", e1);
|
| 81 |
return PAYMENT_NOT_CREATED;
|
83 |
return PAYMENT_NOT_CREATED;
|
| 82 |
} catch (TException e1) {
|
84 |
} catch (TException e1) {
|
| 83 |
log.error("Unable to fetch payment amount.", e1);
|
85 |
log.error("Unable to fetch payment amount.", e1);
|
| Line 213... |
Line 215... |
| 213 |
* @return The total amount for which a payment should be created.
|
215 |
* @return The total amount for which a payment should be created.
|
| 214 |
* @throws ShoppingCartException
|
216 |
* @throws ShoppingCartException
|
| 215 |
* If no cart can be found for the given id.
|
217 |
* If no cart can be found for the given id.
|
| 216 |
* @throws TException
|
218 |
* @throws TException
|
| 217 |
*/
|
219 |
*/
|
| 218 |
private double calculatePaymentAmount(long cartId) throws ShoppingCartException, TException{
|
220 |
private double calculatePaymentAmount(long cartId, long sourceId) throws ShoppingCartException, TException{
|
| 219 |
double totalAmount = 0;
|
221 |
double totalAmount = 0;
|
| 220 |
Cart cart;
|
222 |
Cart cart;
|
| 221 |
UserClient userContextServiceClient = null;
|
223 |
UserClient userContextServiceClient = null;
|
| 222 |
try {
|
224 |
try {
|
| 223 |
userContextServiceClient = new UserClient();
|
225 |
userContextServiceClient = new UserClient();
|
| Line 234... |
Line 236... |
| 234 |
for (Line line : lineItems) {
|
236 |
for (Line line : lineItems) {
|
| 235 |
long productId = line.getItemId();
|
237 |
long productId = line.getItemId();
|
| 236 |
// FIXME: It's expensive to get the price of each item from the
|
238 |
// FIXME: It's expensive to get the price of each item from the
|
| 237 |
// catalog service. We maintain the pricing info in the line items
|
239 |
// catalog service. We maintain the pricing info in the line items
|
| 238 |
// themselves now.
|
240 |
// themselves now.
|
| 239 |
totalAmount = totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
|
241 |
totalAmount = totalAmount + line.getQuantity() * getItemPrice(productId, sourceId);
|
| 240 |
}
|
242 |
}
|
| 241 |
} else {
|
243 |
} else {
|
| 242 |
totalAmount = cart.getDiscountedPrice();
|
244 |
totalAmount = cart.getDiscountedPrice();
|
| 243 |
}
|
245 |
}
|
| 244 |
|
246 |
|
| 245 |
return totalAmount;
|
247 |
return totalAmount;
|
| 246 |
}
|
248 |
}
|
| 247 |
|
249 |
|
| - |
|
250 |
|
| - |
|
251 |
private double getItemPrice(long itemId, long sourceId){
|
| - |
|
252 |
CatalogClient catalogClient = null;
|
| - |
|
253 |
Client client = null;
|
| - |
|
254 |
Double itemPrice = 0.0;
|
| - |
|
255 |
try {
|
| - |
|
256 |
catalogClient = new CatalogClient();
|
| - |
|
257 |
client = catalogClient.getClient();
|
| - |
|
258 |
Item item = client.getItemForSource(itemId, sourceId);
|
| - |
|
259 |
itemPrice = item.getSellingPrice();
|
| - |
|
260 |
|
| - |
|
261 |
} catch(Exception e){
|
| - |
|
262 |
log.error("Unable to get the item price because of:", e);
|
| - |
|
263 |
}
|
| - |
|
264 |
return itemPrice;
|
| - |
|
265 |
}
|
| - |
|
266 |
|
| - |
|
267 |
|
| 248 |
/**
|
268 |
/**
|
| 249 |
* Removes the items processed through the given transaction from the
|
269 |
* Removes the items processed through the given transaction from the
|
| 250 |
* shopping cart.
|
270 |
* shopping cart.
|
| 251 |
*
|
271 |
*
|
| 252 |
* @param transaction
|
272 |
* @param transaction
|