Subversion Repositories SmartDukaan

Rev

Rev 18203 | 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;
20356 amit.gupta 41
    private static final long OUTBOUND_DEFAULT_ASSIGNEE_ID = 33;
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) {
18203 amit.gupta 66
                	try{
67
	                    newTask.processPaymentFailure(client.getPayment(paymentId).getUserId());
68
	                    client.markPaymentAsProcessed(paymentId, ExtraPaymentProcessingType.FAILED_PAYMENTS);
69
                	} catch(Exception e){
70
                    	client = new PaymentClient().getClient();
71
                    	newTask.processPaymentFailure(client.getPayment(paymentId).getUserId());
72
                    	client.markPaymentAsProcessed(paymentId, ExtraPaymentProcessingType.FAILED_PAYMENTS);
16207 manish.sha 73
                	}
4008 mandeep.dh 74
                }
75
            } else {
76
                log.info("No payments to process");
77
            }
78
        } catch (TTransportException e) {
79
            log.error("Could not create Payment client", e);
80
        } catch (TException e) {
81
            log.error("Could not fetch payments for processing", e);
82
        } catch (PaymentException e) {
83
            log.error("Could not fetch payment", e);
84
        }
85
    }
86
 
87
    /**
88
     * Method to create tickets for failed payments.
89
     */
90
    public void processPaymentFailure(long customerId) {
4191 mandeep.dh 91
        log.info("Processing Payment failure for customerId: " + customerId);
92
        SearchFilter searchFilter = new SearchFilter();
93
        searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
94
        searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
95
        searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
96
        searchFilter.setTicketCategory(TicketCategory.FAILED_PAYMENTS);
97
        searchFilter.setCustomerId(customerId);
4008 mandeep.dh 98
 
4191 mandeep.dh 99
        // No need to create a ticket if there exists one for the customer!
100
        if (ticketHandler.getTickets(searchFilter).isEmpty()) {
101
            Ticket ticket = new Ticket();
102
            ticket.setCategory(TicketCategory.FAILED_PAYMENTS);
103
            ticket.setCreatorId(ADMIN_AGENT_ID);
104
            ticket.setCustomerId(customerId);
105
            ticket.setDescription("Requires payment failures' follow-up!");
106
            ticket.setPriority(TicketPriority.HIGH);
107
            ticket.setStatus(TicketStatus.OPEN);
108
            ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);
4008 mandeep.dh 109
 
4191 mandeep.dh 110
            Activity activity = new Activity();
111
            activity.setCreatorId(ticket.getCreatorId());
112
            activity.setCustomerId(ticket.getCustomerId());
113
            activity.setDescription("Creating ticket");
114
            activity.setTicketCategory(ticket.getCategory());
115
            activity.setTicketDescription(ticket.getDescription());
116
            activity.setTicketPriority(ticket.getPriority());
117
            activity.setTicketStatus(ticket.getStatus());
118
            activity.setType(ActivityType.OTHER);
119
            activity.setTicketAssigneeId(ticket.getAssigneeId());
4008 mandeep.dh 120
 
4191 mandeep.dh 121
            createTicket(ticket, activity);
4008 mandeep.dh 122
        }
123
    }
124
 
125
    @Transactional
126
    private void createTicket(Ticket ticket, Activity activity) {
127
        ticketHandler.insertTicket(ticket);
4179 mandeep.dh 128
        activity.setTicketId(ticket.getId());
4008 mandeep.dh 129
        activityHandler.insertActivity(activity);
130
    }
131
}