Subversion Repositories SmartDukaan

Rev

Rev 18203 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.crm.util;

import in.shop2020.crm.ActivityType;
import in.shop2020.crm.TicketCategory;
import in.shop2020.crm.TicketPriority;
import in.shop2020.crm.TicketStatus;
import in.shop2020.crm.domain.Activity;
import in.shop2020.crm.domain.SearchFilter;
import in.shop2020.crm.domain.Ticket;
import in.shop2020.crm.handler.ActivityHandler;
import in.shop2020.crm.handler.TicketHandler;
import in.shop2020.payments.ExtraPaymentProcessingType;
import in.shop2020.payments.PaymentException;
import in.shop2020.payments.PaymentService.Client;
import in.shop2020.thrift.clients.PaymentClient;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mandeep
 * Process all the payments failed for a customer while booking orders. A ticket per
 * customer is created and Outbound team tries to help customers to make a
 * successful payment.
 */
public class PaymentProcessorTask {
    private static Log        log                          = LogFactory
                                                                   .getLog(PaymentProcessorTask.class);
    private static final long ADMIN_AGENT_ID               = 1;
    private static final long OUTBOUND_DEFAULT_ASSIGNEE_ID = 33;

    private TicketHandler     ticketHandler;
    private ActivityHandler   activityHandler;

    public PaymentProcessorTask() {
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        ticketHandler              = context.getBean(TicketHandler.class);
        activityHandler            = context.getBean(ActivityHandler.class);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    public static void main(String[] args) {
        try {
            PaymentProcessorTask newTask = new PaymentProcessorTask();
            Client client = new PaymentClient().getClient();
            List<Long> paymentIds = client
                    .getPaymentsRequiringExtraProcessing(ExtraPaymentProcessingType.FAILED_PAYMENTS);
            if (paymentIds != null && !paymentIds.isEmpty()) {
                log.info("Fetched " + paymentIds.size() + " payments");
                for (Long paymentId : paymentIds) {
                        try{
                            newTask.processPaymentFailure(client.getPayment(paymentId).getUserId());
                            client.markPaymentAsProcessed(paymentId, ExtraPaymentProcessingType.FAILED_PAYMENTS);
                        } catch(Exception e){
                        client = new PaymentClient().getClient();
                        newTask.processPaymentFailure(client.getPayment(paymentId).getUserId());
                        client.markPaymentAsProcessed(paymentId, ExtraPaymentProcessingType.FAILED_PAYMENTS);
                        }
                }
            } else {
                log.info("No payments to process");
            }
        } catch (TTransportException e) {
            log.error("Could not create Payment client", e);
        } catch (TException e) {
            log.error("Could not fetch payments for processing", e);
        } catch (PaymentException e) {
            log.error("Could not fetch payment", e);
        }
    }

    /**
     * Method to create tickets for failed payments.
     */
    public void processPaymentFailure(long customerId) {
        log.info("Processing Payment failure for customerId: " + customerId);
        SearchFilter searchFilter = new SearchFilter();
        searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
        searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
        searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
        searchFilter.setTicketCategory(TicketCategory.FAILED_PAYMENTS);
        searchFilter.setCustomerId(customerId);

        // No need to create a ticket if there exists one for the customer!
        if (ticketHandler.getTickets(searchFilter).isEmpty()) {
            Ticket ticket = new Ticket();
            ticket.setCategory(TicketCategory.FAILED_PAYMENTS);
            ticket.setCreatorId(ADMIN_AGENT_ID);
            ticket.setCustomerId(customerId);
            ticket.setDescription("Requires payment failures' follow-up!");
            ticket.setPriority(TicketPriority.HIGH);
            ticket.setStatus(TicketStatus.OPEN);
            ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);

            Activity activity = new Activity();
            activity.setCreatorId(ticket.getCreatorId());
            activity.setCustomerId(ticket.getCustomerId());
            activity.setDescription("Creating ticket");
            activity.setTicketCategory(ticket.getCategory());
            activity.setTicketDescription(ticket.getDescription());
            activity.setTicketPriority(ticket.getPriority());
            activity.setTicketStatus(ticket.getStatus());
            activity.setType(ActivityType.OTHER);
            activity.setTicketAssigneeId(ticket.getAssigneeId());

            createTicket(ticket, activity);
        }
    }

    @Transactional
    private void createTicket(Ticket ticket, Activity activity) {
        ticketHandler.insertTicket(ticket);
        activity.setTicketId(ticket.getId());
        activityHandler.insertActivity(activity);
    }
}