Subversion Repositories SmartDukaan

Rev

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