Subversion Repositories SmartDukaan

Rev

Rev 5142 | Rev 6304 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4008 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.crm.util;
5
 
6
import in.shop2020.crm.ActivityType;
7
import in.shop2020.crm.TicketCategory;
8
import in.shop2020.crm.TicketPriority;
9
import in.shop2020.crm.TicketStatus;
10
import in.shop2020.crm.domain.Activity;
11
import in.shop2020.crm.domain.SearchFilter;
12
import in.shop2020.crm.domain.Ticket;
13
import in.shop2020.crm.handler.ActivityHandler;
14
import in.shop2020.crm.handler.TicketHandler;
15
import in.shop2020.payments.ExtraPaymentProcessingType;
16
import in.shop2020.payments.PaymentException;
17
import in.shop2020.payments.PaymentService.Client;
18
import in.shop2020.thrift.clients.PaymentClient;
19
 
20
import java.util.ArrayList;
21
import java.util.List;
22
 
23
import org.apache.commons.logging.Log;
24
import org.apache.commons.logging.LogFactory;
25
import org.apache.thrift.TException;
26
import org.apache.thrift.transport.TTransportException;
5142 anupam.sin 27
import org.springframework.context.ApplicationContext;
28
import org.springframework.context.support.ClassPathXmlApplicationContext;
4008 mandeep.dh 29
import org.springframework.transaction.annotation.Transactional;
30
 
31
/**
32
 * @author mandeep
33
 * Process all the payments failed for a customer while booking orders. A ticket per
34
 * customer is created and Outbound team tries to help customers to make a
35
 * successful payment.
36
 */
5142 anupam.sin 37
public class PaymentProcessorTask {
4008 mandeep.dh 38
    private static Log        log                          = LogFactory
39
                                                                   .getLog(PaymentProcessorTask.class);
40
    private static final long ADMIN_AGENT_ID               = 1;
5734 amar.kumar 41
    private static final long OUTBOUND_DEFAULT_ASSIGNEE_ID = 24;
4008 mandeep.dh 42
 
43
    private TicketHandler     ticketHandler;
44
    private ActivityHandler   activityHandler;
45
 
5142 anupam.sin 46
    public PaymentProcessorTask() {
47
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
48
        ticketHandler              = context.getBean(TicketHandler.class);
49
        activityHandler            = context.getBean(ActivityHandler.class);
4008 mandeep.dh 50
    }
51
 
52
    /*
53
     * (non-Javadoc)
54
     * 
55
     * @see java.lang.Runnable#run()
56
     */
5142 anupam.sin 57
    public static void main(String[] args) {
4008 mandeep.dh 58
        try {
5142 anupam.sin 59
            PaymentProcessorTask newTask = new PaymentProcessorTask();
4008 mandeep.dh 60
            Client client = new PaymentClient().getClient();
61
            List<Long> paymentIds = client
62
                    .getPaymentsRequiringExtraProcessing(ExtraPaymentProcessingType.FAILED_PAYMENTS);
63
            if (paymentIds != null && !paymentIds.isEmpty()) {
64
                log.info("Fetched " + paymentIds.size() + " payments");
65
                for (Long paymentId : paymentIds) {
5142 anupam.sin 66
                    newTask.processPaymentFailure(client.getPayment(paymentId).getUserId());
4021 mandeep.dh 67
                    client = new PaymentClient().getClient();
4008 mandeep.dh 68
                    client.markPaymentAsProcessed(paymentId, ExtraPaymentProcessingType.FAILED_PAYMENTS);
69
                }
70
            } else {
71
                log.info("No payments to process");
72
            }
73
        } catch (TTransportException e) {
74
            log.error("Could not create Payment client", e);
75
        } catch (TException e) {
76
            log.error("Could not fetch payments for processing", e);
77
        } catch (PaymentException e) {
78
            log.error("Could not fetch payment", e);
79
        }
80
    }
81
 
82
    /**
83
     * Method to create tickets for failed payments.
84
     */
85
    public void processPaymentFailure(long customerId) {
4191 mandeep.dh 86
        log.info("Processing Payment failure for customerId: " + customerId);
87
        SearchFilter searchFilter = new SearchFilter();
88
        searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
89
        searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
90
        searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
91
        searchFilter.setTicketCategory(TicketCategory.FAILED_PAYMENTS);
92
        searchFilter.setCustomerId(customerId);
4008 mandeep.dh 93
 
4191 mandeep.dh 94
        // No need to create a ticket if there exists one for the customer!
95
        if (ticketHandler.getTickets(searchFilter).isEmpty()) {
96
            Ticket ticket = new Ticket();
97
            ticket.setCategory(TicketCategory.FAILED_PAYMENTS);
98
            ticket.setCreatorId(ADMIN_AGENT_ID);
99
            ticket.setCustomerId(customerId);
100
            ticket.setDescription("Requires payment failures' follow-up!");
101
            ticket.setPriority(TicketPriority.HIGH);
102
            ticket.setStatus(TicketStatus.OPEN);
103
            ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);
4008 mandeep.dh 104
 
4191 mandeep.dh 105
            Activity activity = new Activity();
106
            activity.setCreatorId(ticket.getCreatorId());
107
            activity.setCustomerId(ticket.getCustomerId());
108
            activity.setDescription("Creating ticket");
109
            activity.setTicketCategory(ticket.getCategory());
110
            activity.setTicketDescription(ticket.getDescription());
111
            activity.setTicketPriority(ticket.getPriority());
112
            activity.setTicketStatus(ticket.getStatus());
113
            activity.setType(ActivityType.OTHER);
114
            activity.setTicketAssigneeId(ticket.getAssigneeId());
4008 mandeep.dh 115
 
4191 mandeep.dh 116
            createTicket(ticket, activity);
4008 mandeep.dh 117
        }
118
    }
119
 
120
    @Transactional
121
    private void createTicket(Ticket ticket, Activity activity) {
122
        ticketHandler.insertTicket(ticket);
4179 mandeep.dh 123
        activity.setTicketId(ticket.getId());
4008 mandeep.dh 124
        activityHandler.insertActivity(activity);
125
    }
126
}