| 31091 |
amit.gupta |
1 |
package com.spice.profitmandi.service.wallet;
|
| 21410 |
amit.gupta |
2 |
|
| 28653 |
amit.gupta |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.dao.cart.CartService;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.transaction.Payment;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.transaction.Transaction;
|
|
|
8 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
9 |
import com.spice.profitmandi.dao.repository.transaction.PaymentRepository;
|
|
|
10 |
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
|
|
|
11 |
import com.spice.profitmandi.service.order.OrderService;
|
|
|
12 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 31091 |
amit.gupta |
13 |
import org.apache.logging.log4j.LogManager;
|
|
|
14 |
import org.apache.logging.log4j.Logger;
|
|
|
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
16 |
import org.springframework.stereotype.Component;
|
| 21410 |
amit.gupta |
17 |
|
| 31091 |
amit.gupta |
18 |
import java.time.LocalDateTime;
|
|
|
19 |
import java.util.List;
|
|
|
20 |
import java.util.stream.Collectors;
|
|
|
21 |
|
| 28653 |
amit.gupta |
22 |
@Component
|
| 21410 |
amit.gupta |
23 |
public class CommonPaymentService {
|
|
|
24 |
|
| 23568 |
govind |
25 |
private static final Logger log = LogManager.getLogger(CommonPaymentService.class);
|
| 21410 |
amit.gupta |
26 |
|
| 28653 |
amit.gupta |
27 |
@Autowired
|
|
|
28 |
private WalletService walletService;
|
| 21805 |
amit.gupta |
29 |
|
| 28653 |
amit.gupta |
30 |
@Autowired
|
|
|
31 |
private OrderRepository orderRepository;
|
| 21410 |
amit.gupta |
32 |
|
| 28653 |
amit.gupta |
33 |
@Autowired
|
|
|
34 |
private OrderService orderService;
|
| 31091 |
amit.gupta |
35 |
|
| 28653 |
amit.gupta |
36 |
@Autowired
|
|
|
37 |
private TransactionRepository transactionRepository;
|
| 31091 |
amit.gupta |
38 |
|
| 28653 |
amit.gupta |
39 |
@Autowired
|
|
|
40 |
private PaymentRepository paymentRepository;
|
| 21410 |
amit.gupta |
41 |
|
| 28653 |
amit.gupta |
42 |
@Autowired
|
|
|
43 |
private CartService cartService;
|
| 21410 |
amit.gupta |
44 |
|
| 28653 |
amit.gupta |
45 |
public void payThroughWallet(int transactionId) throws ProfitMandiBusinessException {
|
|
|
46 |
Transaction transaction = transactionRepository.selectById(transactionId);
|
|
|
47 |
List<Order> orders = orderRepository.selectAllByTransactionId(transactionId);
|
| 35690 |
amit |
48 |
payThroughWallet(transaction, orders);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public void payThroughWallet(Transaction transaction, List<Order> orders) throws ProfitMandiBusinessException {
|
| 28653 |
amit.gupta |
52 |
double walletAmount = orders.stream().collect(Collectors.summingDouble(Order::getTotalAmount));
|
| 35690 |
amit |
53 |
walletService.consumeAmountFromWallet(transaction.getRetailerId(), transaction.getId(),
|
| 31091 |
amit.gupta |
54 |
WalletReferenceType.PURCHASE, "Against Order purchase", (float) walletAmount, LocalDateTime.now());
|
| 28653 |
amit.gupta |
55 |
this.addPayment(transaction, walletAmount);
|
| 21410 |
amit.gupta |
56 |
}
|
| 31091 |
amit.gupta |
57 |
|
| 28653 |
amit.gupta |
58 |
private void addPayment(Transaction transaction, double walletAmount) {
|
|
|
59 |
Payment payment = new Payment();
|
|
|
60 |
payment.setAmount(walletAmount);
|
|
|
61 |
payment.setGatewayId(8);
|
|
|
62 |
payment.setStatus(2);
|
|
|
63 |
payment.setGatewayTxnStatus("SUCCESS");
|
|
|
64 |
payment.setDescription("Payment Received");
|
|
|
65 |
payment.setUserId(transaction.getRetailerId());
|
|
|
66 |
payment.setInitTimestamp(LocalDateTime.now());
|
|
|
67 |
payment.setSuccessTimestamp(LocalDateTime.now());
|
| 28748 |
amit.gupta |
68 |
payment.setDigital(false);
|
| 28741 |
amit.gupta |
69 |
payment.setMerchantTxnId(transaction.getId());
|
| 28653 |
amit.gupta |
70 |
paymentRepository.persist(payment);
|
| 21410 |
amit.gupta |
71 |
}
|
|
|
72 |
}
|