Subversion Repositories SmartDukaan

Rev

Rev 28655 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21409 amit.gupta 1
package com.spice.profitmandi.web.payment;
2
 
31102 amit.gupta 3
import com.spice.profitmandi.service.wallet.CommonPaymentService;
21409 amit.gupta 4
import com.spice.profitmandi.thrift.clients.PaymentClient;
5
import com.spice.profitmandi.thrift.clients.TransactionClient;
6
import in.shop2020.model.v1.order.TransactionService.Client;
7
import in.shop2020.payments.Attribute;
8
import in.shop2020.payments.PaymentStatus;
31102 amit.gupta 9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
21409 amit.gupta 11
 
31102 amit.gupta 12
import java.util.ArrayList;
13
import java.util.List;
21409 amit.gupta 14
 
15
 
31102 amit.gupta 16
 
21409 amit.gupta 17
public class WalletPaymentService implements IPaymentService {
18
 
23568 govind 19
	private static final Logger log=LogManager.getLogger(WalletPaymentService.class);
21409 amit.gupta 20
 
21
	private long paymentId;
22
 
23
	@Override
24
	public long createPayment(long userId, long txnId, String paymentOption, int gatewayId,String paymentType) {
25
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through Wallet only payment");
26
 
27
		if(!checkWalletPayment(txnId)){
28
			return PAYMENT_NOT_CREATED;
29
		}
30
		CommonPaymentService cps = new CommonPaymentService();
28655 amit.gupta 31
		/*if(!cps.createPayment(userId, txnId, gatewayId)){
21409 amit.gupta 32
			log.error("Error while creating the basic payment");
33
			return PAYMENT_NOT_CREATED;
34
		}
28655 amit.gupta 35
		paymentId = cps.getPaymentId();*/
36
 
21409 amit.gupta 37
		if(paymentOption != null){
38
			List<Attribute> attributes = new ArrayList<Attribute>();
39
			attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
40
			attributes.add(new Attribute(IPaymentService.PAYMENT_TYPE, paymentType));
41
			try {
42
				PaymentClient paymentServiceClient = new PaymentClient();
43
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
44
			} catch (Exception e) {
45
				log.error("Error while saving payment option attribute", e);
46
				// TODO: We've already created the payment. We could allow the
47
				// payment to go through. The customer will be a little
48
				// annoyed to have to select from a host of options again but
49
				// will be better than completely disallowing him.
50
				return PAYMENT_NOT_CREATED;
51
			}
52
		}
53
		return paymentId;
54
	}
55
 
56
 
57
	private boolean checkWalletPayment(long txnId){
58
		//First check wallet has been debited
59
		try{
60
			Client tc = new TransactionClient().getClient();
61
			double payment_amount = tc.calculatePaymentAmount(txnId);
62
			if (payment_amount == 0){
63
				return true;
64
			}
65
		}
66
		catch(Exception e){
67
			log.error("Unable to validate wallet payment ",e);
68
			return false;
69
		}
70
		return false;
71
	}
72
}