Subversion Repositories SmartDukaan

Rev

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