Subversion Repositories SmartDukaan

Rev

Rev 2708 | Rev 2907 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 chandransh 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.config.ConfigException;
2159 chandransh 4
import in.shop2020.payments.Attribute;
2708 chandransh 5
import in.shop2020.payments.Payment;
2159 chandransh 6
import in.shop2020.payments.PaymentStatus;
2708 chandransh 7
import in.shop2020.payments.PaymentService.Client;
2159 chandransh 8
import in.shop2020.thrift.clients.PaymentServiceClient;
1905 chandransh 9
import in.shop2020.thrift.clients.config.ConfigClient;
10
 
2159 chandransh 11
import java.util.ArrayList;
1905 chandransh 12
import java.util.HashMap;
2159 chandransh 13
import java.util.List;
1905 chandransh 14
import java.util.Map;
15
 
16
import org.apache.log4j.Logger;
17
 
18
public class EbsPaymentService implements IPaymentService{
19
 
20
	private static Logger log = Logger.getLogger(Class.class);
21
 
22
	public static final String TXN_ID = "transactionId";
23
	public static final String PAYMENT_ID = "paymentId";
24
	public static final String AMOUNT = "amount";
25
	public static final String DATE_TIME = "dateTime";
26
	public static final String MODE = "mode";
27
	public static final String REF_NO = "referenceNo";
28
	public static final String TXN_TYPE = "transactionType";
29
 
30
    private static String accountId;
31
    private static String secretKey;
32
 
33
	static{
34
		try {
35
			accountId = ConfigClient.getClient().get("ebs_account_id");
36
			secretKey = ConfigClient.getClient().get("ebs_secret_key");
37
		} catch (ConfigException e) {
38
			log.error("Unable to get EBS payment configuration.");
39
		}
40
	}
41
 
2118 chandransh 42
	private static int gatewayId=2;
1905 chandransh 43
	private long paymentId;
44
 
1959 chandransh 45
	@Override
2159 chandransh 46
	public long createPayment(long currentCartId, long userId, long txnId, String paymentOption){
2199 chandransh 47
		log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through EBS");
1905 chandransh 48
		CommonPaymentService cps = new CommonPaymentService();
49
		if(!cps.createPayment(currentCartId, userId, txnId, gatewayId)){
50
			log.error("Error while creating the basic payment");
51
			return PAYMENT_NOT_CREATED;
52
		}
2159 chandransh 53
		paymentId = cps.getPaymentId();
1905 chandransh 54
 
2199 chandransh 55
		if(paymentOption != null){
56
			List<Attribute> attributes = new ArrayList<Attribute>();
57
			attributes.add(new Attribute(IPaymentService.PAYMENT_METHOD, paymentOption));
58
 
59
			try {
60
				PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
61
				paymentServiceClient.getClient().updatePaymentDetails(paymentId, null, null, null, null, null, null, null, null, PaymentStatus.INIT, null, attributes);
62
			} catch (Exception e) {
63
				log.error("Error while saving payment option attribute", e);
64
				// TODO: We've already created the payment. We could allow the
65
				// payment to go through. The customer will be a little
66
				// annoyed to have to select from a host of options again but
67
				// will be better than completely disallowing him.
68
				return PAYMENT_NOT_CREATED;
69
			}
2159 chandransh 70
		}
71
 
1905 chandransh 72
		return paymentId;
73
	}
1959 chandransh 74
 
75
	/**
76
	 * Capture the amount which was authorized for this payment Id. Makes
77
	 * requests over the network and so internet connectivity is must for it to
78
	 * succeed.
79
	 * 
80
	 * @param amount
81
	 *            The amount to be captured. Must be the same value which was
82
	 *            authorized for this payment.
2708 chandransh 83
	 * @param gatewayPaymentId
1959 chandransh 84
	 *            The payment ID generated by the gateway.
85
	 * @return A Map. The Map will definitely have status and will have error
86
	 *         information in case of error and txn information in case of
87
	 *         success. In case there is an error in processing, the returned
88
	 *         result map will be empty.
89
	 */
2708 chandransh 90
	public static Map<String, String> capturePayment(Payment payment, String gatewayPaymentId){
91
		String amount = "" + payment.getAmount();
92
		log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);
1959 chandransh 93
	    Map<String, String> resultMap = new HashMap<String, String>();
94
 
2118 chandransh 95
	    //Prepare resultMap to elicit failure behaviour in case anything goes wrong.
96
	    resultMap.put(STATUS, "");
97
 
2708 chandransh 98
		try {
99
			PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
100
			Client paymentClient = paymentServiceClient.getClient();
101
			resultMap = paymentClient.captureEbsPayment(payment.getPaymentId());
102
		} catch (Exception e) {
103
			log.error("Unable to capture payment", e);
2118 chandransh 104
			resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);
2708 chandransh 105
			resultMap.put(ERROR, "Unable to capture transaction.");
1905 chandransh 106
		}
2708 chandransh 107
 
108
		return resultMap;
1905 chandransh 109
	}
110
 
111
	public static void main(String[] args){
2708 chandransh 112
		//capturePayment(30450.00, "2412653");
1905 chandransh 113
 
114
//		<output  transactionId="4793507"  paymentId="2411078"  amount="25005"  dateTime="2011-05-16 09:03:15"  mode="TEST"  referenceNo="4"  transactionType="Captured"  status="Processing"  />";
115
 
116
//		<output  errorCode="2"  error="Invalid Account ID/Secret Key"  />
117
//		<output  errorCode="12"  error="This payment is failed"  />
118
//		<output  errorCode="13"  error="This payment is captured already"  />		
119
	}
120
}