Rev 2907 | Rev 3583 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.payments.Attribute;import in.shop2020.payments.Payment;import in.shop2020.payments.PaymentStatus;import in.shop2020.payments.PaymentService.Client;import in.shop2020.thrift.clients.PaymentClient;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.log4j.Logger;public class EbsPaymentService implements IPaymentService{private static Logger log = Logger.getLogger(Class.class);public static final String TXN_ID = "transactionId";public static final String PAYMENT_ID = "paymentId";public static final String AMOUNT = "amount";public static final String DATE_TIME = "dateTime";public static final String MODE = "mode";public static final String REF_NO = "referenceNo";public static final String TXN_TYPE = "transactionType";private static int gatewayId=2;private long paymentId;@Overridepublic long createPayment(long currentCartId, long userId, long txnId, String paymentOption){log.info("Creating payment for the txn#: " + txnId + " for the user: " + userId + " for processing through EBS");CommonPaymentService cps = new CommonPaymentService();if(!cps.createPayment(currentCartId, 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));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;}/*** Capture the amount which was authorized for this payment Id. Makes* requests over the network and so internet connectivity is must for it to* succeed.** @param amount* The amount to be captured. Must be the same value which was* authorized for this payment.* @param gatewayPaymentId* The payment ID generated by the gateway.* @return A Map. The Map will definitely have status and will have error* information in case of error and txn information in case of* success. In case there is an error in processing, the returned* result map will be empty.*/public static Map<String, String> capturePayment(Payment payment, String gatewayPaymentId){String amount = "" + payment.getAmount();log.info("Capturing amount: Rs " + amount + " for payment Id: " + gatewayPaymentId);Map<String, String> resultMap = new HashMap<String, String>();//Prepare resultMap to elicit failure behaviour in case anything goes wrong.resultMap.put(STATUS, "");try {PaymentClient paymentServiceClient = new PaymentClient();Client paymentClient = paymentServiceClient.getClient();resultMap = paymentClient.captureEbsPayment(payment.getPaymentId());} catch (Exception e) {log.error("Unable to capture payment", e);resultMap.put(ERR_CODE, Errors.CONN_FAILURE.code);resultMap.put(ERROR, "Unable to capture transaction.");}return resultMap;}public static void main(String[] args){//capturePayment(30450.00, "2412653");// <output transactionId="4793507" paymentId="2411078" amount="25005" dateTime="2011-05-16 09:03:15" mode="TEST" referenceNo="4" transactionType="Captured" status="Processing" />";// <output errorCode="2" error="Invalid Account ID/Secret Key" />// <output errorCode="12" error="This payment is failed" />// <output errorCode="13" error="This payment is captured already" />}}