Subversion Repositories SmartDukaan

Rev

Rev 4008 | Rev 4179 | Go to most recent revision | 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.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 implements Runnable {
    private static Log        log                          = LogFactory
                                                                   .getLog(PaymentProcessorTask.class);
    private static final long ADMIN_AGENT_ID               = 1;
    private static final long OUTBOUND_DEFAULT_ASSIGNEE_ID = 12;

    private TicketHandler     ticketHandler;
    private ActivityHandler   activityHandler;

    public PaymentProcessorTask(TicketHandler ticketHandler,
            ActivityHandler activityHandler) {
        this.ticketHandler = ticketHandler;
        this.activityHandler = activityHandler;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    public void run() {
        try {
            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) {
                    processPaymentFailure(client.getPayment(paymentId).getUserId());
                    client = new PaymentClient().getClient();
                    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) {
        try {
            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);
            }
        } catch (Exception e) {
            log.error("Exception processing payment failure for customer Id: "
                    + customerId, e);
        }
    }

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