private HkPaymentResponse createPayment(
      String gatewayOrderId,
      String gatewayReferenceId,
      String rrn,
      String respMsg,
      String txnType,
      String amount,
      String authIdCode) {
    HkPaymentResponse hkPaymentResponse =
        new HkPaymentResponse(
            gatewayOrderId,
            gatewayReferenceId,
            respMsg,
            EnumGateway.EBS.asGateway(),
            null,
            null,
            rrn,
            authIdCode,
            NumberUtils.toDouble(amount));

    if (txnType.equalsIgnoreCase(EnumPaymentTransactionType.SALE.getName())) {
      hkPaymentResponse.setGatewayOrderId(gatewayOrderId);
      hkPaymentResponse.setTransactionType(EnumPaymentTransactionType.SALE.getName());
    } else if (txnType.equalsIgnoreCase(EnumPaymentTransactionType.REFUND.getName())) {
      hkPaymentResponse.setTransactionType(EnumPaymentTransactionType.REFUND.getName());
    }
    return hkPaymentResponse;
  }
 private void updateRefundPaymentStatus(HkPaymentResponse hkPaymentResponse, String status) {
   if (GatewayResponseKeys.EbsConstants.PROCESSING.getKey().equalsIgnoreCase(status)) {
     hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.IN_PROCESS);
   } else if (GatewayResponseKeys.EbsConstants.PROCESSED.getKey().equalsIgnoreCase(status)) {
     hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.SUCCESS);
   } else {
     hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.FAILURE);
   }
 }
 private void updateSalePaymentStatus(
     HkPaymentResponse hkPaymentResponse, String isFlagged, String ebsTransactionType) {
   if (GatewayResponseKeys.EbsConstants.Authorized.getKey().equalsIgnoreCase(ebsTransactionType)) {
     if (isFlagged != null
         && isFlagged.equalsIgnoreCase(
             GatewayResponseKeys.EbsConstants.IS_FLAGGED_FALSE.getKey())) {
       hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.SUCCESS);
     } else if (isFlagged != null
         && isFlagged.equalsIgnoreCase(
             GatewayResponseKeys.EbsConstants.IS_FLAGGED_TRUE.getKey())) {
       hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.AUTHENTICATION_PENDING);
     } else {
       hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.FAILURE);
     }
   } else {
     hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.FAILURE);
     hkPaymentResponse.setResponseMsg(
         GatewayResponseKeys.HKConstants.NO_TRANSACTION_FOUND.getKey());
   }
 }
  private HkPaymentResponse verifyAndCreateHkResponsePayment(
      Element element, String gatewayOrderId, String transactionType) {
    HkPaymentResponse hkPaymentResponse = null;
    if (element != null) {
      hkPaymentResponse =
          createPayment(
              gatewayOrderId,
              null,
              null,
              null,
              transactionType,
              null,
              null); // EnumPaymentTransactionType.SALE.getName()

      String paymentId =
          element.getAttributeValue(GatewayResponseKeys.EbsConstants.TXN_PAYMENT_ID.getKey());
      String transactionId =
          element.getAttributeValue(GatewayResponseKeys.EbsConstants.TXN_TRANSACTION_ID.getKey());
      String amount = element.getAttributeValue(GatewayResponseKeys.EbsConstants.AMOUNT.getKey());
      String status =
          element.getAttributeValue(GatewayResponseKeys.EbsConstants.TXN_STATUS.getKey());
      String isFlagged =
          element.getAttributeValue(GatewayResponseKeys.EbsConstants.IS_FLAGGED.getKey());
      String errorCode = element.getAttributeValue(EbsPaymentGatewayWrapper.TXN_ERROR_CODE);
      String errorMessage = element.getAttributeValue(EbsPaymentGatewayWrapper.TXN_ERROR_MSG);
      String ebsTransactionType =
          element.getAttributeValue(EbsPaymentGatewayWrapper.TXN_TRANSACTION_TYPE);
      // String gatewayOrderId =
      // element.getAttributeValue(EbsPaymentGatewayWrapper.TXN_REFERENCE_NO);

      if (paymentId != null) {

        hkPaymentResponse.setGatewayReferenceId(paymentId);
        hkPaymentResponse.setRrn(transactionId);
        hkPaymentResponse.setAmount(NumberUtils.toDouble(amount));
        hkPaymentResponse.setResponseMsg(status);
        updateResponseStatus(
            hkPaymentResponse, transactionType, isFlagged, status, ebsTransactionType);

      } else {
        hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.FAILURE);
        hkPaymentResponse.setErrorLog(errorMessage);
      }
    }

    return hkPaymentResponse;
  }
  @Override
  public List<HkPaymentResponse> seekPaymentFromGateway(Payment basePayment)
      throws HealthkartPaymentGatewayException {
    String gatewayOrderId = basePayment.getGatewayOrderId();
    List<HkPaymentResponse> gatewayPaymentList = new ArrayList<HkPaymentResponse>();

    try {

      // call payment gateway and get the base payment object
      Element element =
          callPaymentGateway(
              gatewayOrderId, null, null, null, EbsPaymentGatewayWrapper.TXN_ACTION_STATUS);
      HkPaymentResponse baseGatewayPayment =
          verifyAndCreateHkResponsePayment(
              element, gatewayOrderId, EnumPaymentTransactionType.SALE.getName());
      List<Payment> paymentList =
          paymentDao.searchPayments(null, null, null, null, null, null, null, basePayment, null);

      gatewayPaymentList.add(baseGatewayPayment);

      // again call gateway and update all child payment list
      if (paymentList != null) {
        for (Payment payment : paymentList) {
          String gatewayReferenceId = payment.getGatewayReferenceId();
          String rrn = payment.getRrn();
          if (gatewayReferenceId != null && rrn != null) {
            Element ele =
                callPaymentGateway(
                    gatewayOrderId,
                    payment.getGatewayReferenceId(),
                    payment.getRrn(),
                    null,
                    EbsPaymentGatewayWrapper.TXN_ACTION_STATUS_PAYMENT_ID);
            HkPaymentResponse childPayment =
                verifyAndCreateHkResponsePayment(
                    ele, payment.getGatewayOrderId(), EnumPaymentTransactionType.REFUND.getName());
            gatewayPaymentList.add(childPayment);
          } else {
            HkPaymentResponse hkPaymentResponse =
                createPayment(
                    payment.getGatewayOrderId(),
                    null,
                    null,
                    payment.getErrorLog(),
                    EnumPaymentTransactionType.REFUND.getName(),
                    payment.getAmount().toString(),
                    null);
            hkPaymentResponse.setHKPaymentStatus(EnumHKPaymentStatus.FAILURE);
            gatewayPaymentList.add(hkPaymentResponse);
          }
        }
      }

    } catch (Exception e) {
      logger.debug("Ebs Payment gateway exception :", e);
      // throw new
      // HealthkartPaymentGatewayException(HealthkartPaymentGatewayException.Error.UNKNOWN);
    }

    return gatewayPaymentList;
  }