Subversion Repositories SmartDukaan

Rev

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