Subversion Repositories SmartDukaan

Rev

Rev 3063 | Rev 6050 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.services;

import in.shop2020.payments.Attribute;
import in.shop2020.payments.PaymentStatus;
import in.shop2020.thrift.clients.PaymentClient;

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

import org.apache.log4j.Logger;

public class CodPaymentService implements IPaymentService {

    private static Logger log = Logger.getLogger(Class.class);
    
    private static int gatewayId=4;
    private long paymentId;
    
    @Override
    public long createPayment(long currentCartId, long userId, long txnId, String paymentOption) {
        log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through COD");
        CommonPaymentService cps = new CommonPaymentService();
        if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
            log.error("Error while creating the basic payment");
            return PAYMENT_NOT_CREATED;
        }
        paymentId = cps.getPaymentId();
        
        if(paymentOption != null){
            List<Attribute> attributes = new ArrayList<Attribute>();
            attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
            
            try {
                PaymentClient paymentServiceClient = new PaymentClient();
                paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
            } catch (Exception e) {
                log.error("Error while saving payment option attribute", e);
                // TODO: We've already created the payment. We could allow the
                // payment to go through. The customer will be a little
                // annoyed to have to select from a host of options again but
                // will be better than completely disallowing him.
                return PAYMENT_NOT_CREATED;
            }
        }

        return paymentId;
    }
}