Subversion Repositories SmartDukaan

Rev

Rev 28655 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.payment;

import com.spice.profitmandi.service.wallet.CommonPaymentService;
import com.spice.profitmandi.thrift.clients.PaymentClient;
import com.spice.profitmandi.thrift.clients.TransactionClient;
import in.shop2020.model.v1.order.TransactionService.Client;
import in.shop2020.payments.Attribute;
import in.shop2020.payments.PaymentStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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



public class WalletPaymentService implements IPaymentService {

        private static final Logger log=LogManager.getLogger(WalletPaymentService.class);

        private long paymentId;

        @Override
        public long createPayment(long userId, long txnId, String paymentOption, int gatewayId,String paymentType) {
                log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through Wallet only payment");
                
                if(!checkWalletPayment(txnId)){
                        return PAYMENT_NOT_CREATED;
                }
                CommonPaymentService cps = new CommonPaymentService();
                /*if(!cps.createPayment(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));
                        attributes.add(new Attribute(IPaymentService.PAYMENT_TYPE, paymentType));
                        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;
        }


        private boolean checkWalletPayment(long txnId){
                //First check wallet has been debited
                try{
                        Client tc = new TransactionClient().getClient();
                        double payment_amount = tc.calculatePaymentAmount(txnId);
                        if (payment_amount == 0){
                                return true;
                        }
                }
                catch(Exception e){
                        log.error("Unable to validate wallet payment ",e);
                        return false;
                }
                return false;
        }
}