Subversion Repositories SmartDukaan

Rev

Rev 4449 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4421 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.payment.service;
5
 
6
import in.shop2020.model.v1.order.AlertType;
7
import in.shop2020.model.v1.order.Order;
8
import in.shop2020.model.v1.order.Transaction;
9
import in.shop2020.model.v1.order.TransactionServiceException;
10
import in.shop2020.payments.Payment;
11
import in.shop2020.payments.PaymentException;
12
import in.shop2020.payments.PaymentService.Client;
13
import in.shop2020.payments.PaymentStatus;
14
import in.shop2020.thrift.clients.PaymentClient;
15
import in.shop2020.thrift.clients.TransactionClient;
16
 
17
import java.util.Calendar;
18
import java.util.Date;
19
import java.util.List;
20
 
21
import org.apache.commons.logging.Log;
22
import org.apache.commons.logging.LogFactory;
23
import org.apache.thrift.TException;
24
import org.apache.thrift.transport.TTransportException;
25
 
26
/**
27
 * @author mandeep
28
 *
29
 */
30
public class PaymentCapturer {
31
    private static final String SPACE = " ";
32
 
33
    private static final Log log = LogFactory.getLog(PaymentCapturer.class);
34
 
35
    private Client paymentClient;
36
    private in.shop2020.model.v1.order.TransactionService.Client transactionClient;
37
 
38
    private PaymentCapturer() {
39
        try {
40
            paymentClient = new PaymentClient().getClient();
41
        } catch (TTransportException e) {
42
            log.error("Could not create payment client", e);
43
        }
44
    }
45
 
46
    private void captureAndCreateAlert(long merchantTxnId) throws PaymentException, TException {
8618 rajveer 47
        paymentClient.capturePayment(merchantTxnId, false);
4421 mandeep.dh 48
    }
49
 
50
    private void createAlertForCapture(long merchantTxnId) throws TransactionServiceException, TException {
51
        transactionClient = new TransactionClient().getClient();
52
        Transaction transaction = transactionClient.getTransaction(merchantTxnId);
53
 
54
        String orderString = SPACE;
55
        for (Order order : transaction.getOrders()) {
56
            orderString.concat(String.valueOf(order.getId()) + SPACE);
57
        }
58
 
4449 rajveer 59
        transactionClient.addAlert(AlertType.ORDER_READY_TO_ACCEPT.getValue(), transaction.getOrders().get(0).getWarehouse_id() , "Payment captured. Order" + 
4421 mandeep.dh 60
                orderString + "now ready to be accepted.");
61
    }
62
 
63
    private void captureAll() throws PaymentException, TException {
64
        for (Payment payment : fetchPaymentsToBeCaptured()) {
65
            try {
66
                captureAndCreateAlert(payment.getMerchantTxnId());
67
                createAlertForCapture(payment.getMerchantTxnId());
68
            } catch (PaymentException e) {
69
                log.error("Could not capture payment id: " + payment.getPaymentId(), e);
70
            } catch (TException e) {
71
                log.error("Could not capture payment id: " + payment.getPaymentId(), e);
72
            } catch (TransactionServiceException e) {
73
                log.error("Could not create alert for payment id: " + payment.getPaymentId(), e);
74
            }
75
        }
76
    }
77
 
78
    private List<Payment> fetchPaymentsToBeCaptured() throws PaymentException, TException {
79
        return paymentClient.getPayments(0, getTomorrow().getTime(), PaymentStatus.CAPTURE_IN_PROCESS, 0);
80
    }
81
 
82
    private Date getTomorrow() {
83
        Calendar calendar = Calendar.getInstance();
84
        calendar.setTime(new Date());
85
        calendar.add(Calendar.DAY_OF_MONTH, 1);
86
        return calendar.getTime();
87
    }
88
 
89
    /**
90
     * @param args
91
     * @throws TException 
92
     * @throws PaymentException 
93
     */
94
    public static void main(String[] args) throws PaymentException, TException {
95
        PaymentCapturer paymentCapturer = new PaymentCapturer();
96
 
97
        if (args != null && args.length != 0) {
98
            for (String arg : args[0].split(",")) {
99
                try {
100
                    long merchantTxnId = Long.parseLong(arg);
101
                    paymentCapturer.captureAndCreateAlert(merchantTxnId);
102
                } catch (PaymentException e) {
103
                    log.error("Could not capture payment for merchant txn id: " + arg, e);
104
                } catch (TException e) {
105
                    log.error("Could not capture payment for merchant txn id: " + arg, e);
106
                }
107
            }
108
        }
109
        else {
110
            paymentCapturer.captureAll();
111
        }
112
    }
113
}