Source:
<code>public Response submitRefund(@ApiParam(value = "accountId", hidden = true) @Auth String accountId, @ApiParam(value = "paymentId", required = true) @PathParam(PATH_PAYMENT_KEY) String paymentId, @ApiParam(value = "requestPayload", required = true) CreatePaymentRefundRequest requestPayload) { logger.info("Create a refund for payment request - paymentId={}", paymentId); Integer refundAmountAvailable = requestPayload.getRefundAmountAvailable(); if (refundAmountAvailable == null) { Response getChargeResponse = client .target(getConnectorUrl(format(CONNECTOR_CHARGE_RESOURCE, accountId, paymentId))) .request() .get(); <START> ChargeFromResponse chargeFromResponse = getChargeResponse.readEntity(ChargeFromResponse.class); <END> refundAmountAvailable = Long.valueOf(chargeFromResponse.getRefundSummary().getAmountAvailable()).intValue(); } ImmutableMap<String, Object> payloadMap = ImmutableMap.of("amount", requestPayload.getAmount(), "refund_amount_available", refundAmountAvailable); String connectorPayload = new GsonBuilder().create().toJson( payloadMap); Response connectorResponse = client .target(getConnectorUrl(format(CONNECTOR_CHARGE_REFUNDS_RESOURCE, accountId, paymentId))) .request() .post(json(connectorPayload)); if (connectorResponse.getStatus() == ACCEPTED.getStatusCode()) { RefundFromConnector refundFromConnector = connectorResponse.readEntity(RefundFromConnector.class); logger.debug("created refund returned - [ ]", refundFromConnector); RefundResponse refundResponse = RefundResponse.valueOf(refundFromConnector, paymentId, baseUrl); return Response.accepted(refundResponse.serialize()).build(); } throw new CreateRefundException(connectorResponse); }</code><technical_language>This block a reusable function/method Response getChargeResponse = client .target(getConnectorUrl(format(CONNECTOR_CHARGE_RESOURCE, accountId, paymentId))) .request() .get(); ChargeFromResponse chargeFromResponse = getChargeResponse.readEntity(ChargeFromResponse.class);</technical_language>

Target:
public Response submitRefund(@ApiParam(value = "accountId", hidden = true) @Auth String accountId, @ApiParam(value = "paymentId", required = true) @PathParam(PATH_PAYMENT_KEY) String paymentId, @ApiParam(value = "requestPayload", required = true) CreatePaymentRefundRequest requestPayload) { logger.info("Create a refund for payment request - paymentId={}", paymentId); Integer refundAmountAvailable = requestPayload.getRefundAmountAvailable() .orElseGet(() -> { Response getChargeResponse = client .target(getConnectorUrl(format(CONNECTOR_CHARGE_RESOURCE, accountId, paymentId))) .request() .get(); ChargeFromResponse chargeFromResponse = getChargeResponse.readEntity(ChargeFromResponse.class); return Long.valueOf(chargeFromResponse.getRefundSummary().getAmountAvailable()).intValue(); }); ImmutableMap<String, Object> payloadMap = ImmutableMap.of("amount", requestPayload.getAmount(), "refund_amount_available", refundAmountAvailable); String connectorPayload = new GsonBuilder().create().toJson( payloadMap); Response connectorResponse = client .target(getConnectorUrl(format(CONNECTOR_CHARGE_REFUNDS_RESOURCE, accountId, paymentId))) .request() .post(json(connectorPayload)); if (connectorResponse.getStatus() == ACCEPTED.getStatusCode()) { RefundFromConnector refundFromConnector = connectorResponse.readEntity(RefundFromConnector.class); logger.debug("created refund returned - [ ]", refundFromConnector); RefundResponse refundResponse = RefundResponse.valueOf(refundFromConnector, paymentId, baseUrl); return Response.accepted(refundResponse.serialize()).build(); } throw new CreateRefundException(connectorResponse); }