encoder.add("TRANSACTIONID", captureTrans.getString("referenceNum"));
encoder.add("REFUNDTYPE", "Partial");
encoder.add("CURRENCYCODE", captureTrans.getString("currencyUomId"));
encoder.add("AMT", refundAmount.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
encoder.add("NOTE", "Order #" + orderPaymentPreference.getString("orderId"));
NVPDecoder decoder = null;
try {
decoder = sendNVPRequest(payPalConfig, encoder);
} catch (PayPalException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (decoder == null) {
return ServiceUtil.returnError("An unknown error occurred while contacting PayPal");
}
Map<String, Object> result = ServiceUtil.returnSuccess();
Map<String, String> errors = getErrorMessageMap(decoder);
if (UtilValidate.isNotEmpty(errors)) {
result.put("refundResult", false);
result.put("refundRefNum", captureTrans.getString("referenceNum"));
result.put("refundAmount", BigDecimal.ZERO);
if (errors.size() == 1) {
Map.Entry<String, String> error = errors.entrySet().iterator().next();
result.put("refundCode", error.getKey());
result.put("refundMessage", error.getValue());
} else {
result.put("refundMessage", "Multiple errors occurred, please refer to the gateway response messages");
result.put("internalRespMsgs", errors);
}
} else {
result.put("refundResult", true);
result.put("refundAmount", new BigDecimal(decoder.get("GROSSREFUNDAMT")));
result.put("refundRefNum", decoder.get("REFUNDTRANSACTIONID"));
}
return result;
}