Subversion Repositories SmartDukaan

Rev

Rev 4021 | Go to most recent revision | Details | 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;
27
import org.springframework.transaction.annotation.Transactional;
28
 
29
/**
30
 * @author mandeep
31
 * Process all the payments failed for a customer while booking orders. A ticket per
32
 * customer is created and Outbound team tries to help customers to make a
33
 * successful payment.
34
 */
35
public class PaymentProcessorTask implements Runnable {
36
    private static Log        log                          = LogFactory
37
                                                                   .getLog(PaymentProcessorTask.class);
38
    private static final long ADMIN_AGENT_ID               = 1;
39
    private static final long OUTBOUND_DEFAULT_ASSIGNEE_ID = 12;
40
 
41
    private TicketHandler     ticketHandler;
42
    private ActivityHandler   activityHandler;
43
 
44
    public PaymentProcessorTask(TicketHandler ticketHandler,
45
            ActivityHandler activityHandler) {
46
        this.ticketHandler = ticketHandler;
47
        this.activityHandler = activityHandler;
48
    }
49
 
50
    /*
51
     * (non-Javadoc)
52
     * 
53
     * @see java.lang.Runnable#run()
54
     */
55
    public void run() {
56
        try {
57
            Client client = new PaymentClient().getClient();
58
            List<Long> paymentIds = client
59
                    .getPaymentsRequiringExtraProcessing(ExtraPaymentProcessingType.FAILED_PAYMENTS);
60
            if (paymentIds != null && !paymentIds.isEmpty()) {
61
                log.info("Fetched " + paymentIds.size() + " payments");
62
                for (Long paymentId : paymentIds) {
63
                    processPaymentFailure(client.getPayment(paymentId).getUserId());
64
                    client.markPaymentAsProcessed(paymentId, ExtraPaymentProcessingType.FAILED_PAYMENTS);
65
                }
66
            } else {
67
                log.info("No payments to process");
68
            }
69
        } catch (TTransportException e) {
70
            log.error("Could not create Payment client", e);
71
        } catch (TException e) {
72
            log.error("Could not fetch payments for processing", e);
73
        } catch (PaymentException e) {
74
            log.error("Could not fetch payment", e);
75
        }
76
    }
77
 
78
    /**
79
     * Method to create tickets for failed payments.
80
     */
81
    public void processPaymentFailure(long customerId) {
82
        try {
83
            log.info("Processing Payment failure for customerId: " + customerId);
84
            SearchFilter searchFilter = new SearchFilter();
85
            searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
86
            searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
87
            searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
88
            searchFilter.setTicketCategory(TicketCategory.FAILED_PAYMENTS);
89
            searchFilter.setCustomerId(customerId);
90
 
91
            // No need to create a ticket if there exists one for the customer!
92
            if (ticketHandler.getTickets(searchFilter).isEmpty()) {
93
                Ticket ticket = new Ticket();
94
                ticket.setCategory(TicketCategory.FAILED_PAYMENTS);
95
                ticket.setCreatorId(ADMIN_AGENT_ID);
96
                ticket.setCustomerId(customerId);
97
                ticket.setDescription("Requires payment failures' follow-up!");
98
                ticket.setPriority(TicketPriority.HIGH);
99
                ticket.setStatus(TicketStatus.OPEN);
100
                ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);
101
 
102
                Activity activity = new Activity();
103
                activity.setCreatorId(ticket.getCreatorId());
104
                activity.setCustomerId(ticket.getCustomerId());
105
                activity.setDescription("Creating ticket");
106
                activity.setTicketCategory(ticket.getCategory());
107
                activity.setTicketDescription(ticket.getDescription());
108
                activity.setTicketPriority(ticket.getPriority());
109
                activity.setTicketStatus(ticket.getStatus());
110
                activity.setType(ActivityType.OTHER);
111
                activity.setTicketAssigneeId(ticket.getAssigneeId());
112
 
113
                createTicket(ticket, activity);
114
            }
115
        } catch (Exception e) {
116
            log.error("Exception processing payment failure for customer Id: "
117
                    + customerId, e);
118
        }
119
    }
120
 
121
    @Transactional
122
    private void createTicket(Ticket ticket, Activity activity) {
123
        ticketHandler.insertTicket(ticket);
124
        activityHandler.insertActivity(activity);
125
    }
126
}