use of com.paypal.sdk.core.nvp.NVPDecoder in project ofbiz-framework by apache.
the class PayPalServices method sendNVPRequest.
private static NVPDecoder sendNVPRequest(GenericValue payPalConfig, NVPEncoder encoder) throws PayPalException {
NVPCallerServices caller = new NVPCallerServices();
try {
APIProfile profile = ProfileFactory.createSignatureAPIProfile();
profile.setAPIUsername(payPalConfig.getString("apiUserName"));
profile.setAPIPassword(payPalConfig.getString("apiPassword"));
profile.setSignature(payPalConfig.getString("apiSignature"));
profile.setEnvironment(payPalConfig.getString("apiEnvironment"));
caller.setAPIProfile(profile);
} catch (PayPalException e) {
Debug.logError(e.getMessage(), module);
}
String requestMessage = encoder.encode();
String responseMessage = caller.call(requestMessage);
NVPDecoder decoder = new NVPDecoder();
decoder.decode(responseMessage);
if (!"Success".equals(decoder.get("ACK"))) {
Debug.logError("A response other than success was received from PayPal: " + responseMessage, module);
}
return decoder;
}
use of com.paypal.sdk.core.nvp.NVPDecoder in project ofbiz-framework by apache.
the class PayPalServices method doRefund.
public static Map<String, Object> doRefund(DispatchContext dctx, Map<String, Object> context) {
Locale locale = (Locale) context.get("locale");
GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, null);
if (payPalConfig == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPayPalPaymentGatewayConfigCannotFind", locale));
}
GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
GenericValue captureTrans = PaymentGatewayServices.getCaptureTransaction(orderPaymentPreference);
BigDecimal refundAmount = (BigDecimal) context.get("refundAmount");
NVPEncoder encoder = new NVPEncoder();
encoder.add("METHOD", "RefundTransaction");
encoder.add("TRANSACTIONID", captureTrans.getString("referenceNum"));
encoder.add("REFUNDTYPE", "Partial");
encoder.add("CURRENCYCODE", captureTrans.getString("currencyUomId"));
encoder.add("AMT", refundAmount.setScale(2, RoundingMode.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(UtilProperties.getMessage(resource, "AccountingPayPalUnknownError", locale));
}
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;
}
use of com.paypal.sdk.core.nvp.NVPDecoder in project ofbiz-framework by apache.
the class PayPalServices method doAuthorization.
public static Map<String, Object> doAuthorization(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
String orderId = (String) context.get("orderId");
BigDecimal processAmount = (BigDecimal) context.get("processAmount");
GenericValue payPalPaymentMethod = (GenericValue) context.get("payPalPaymentMethod");
OrderReadHelper orh = new OrderReadHelper(delegator, orderId);
GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, PaymentGatewayServices.AUTH_SERVICE_TYPE);
Locale locale = (Locale) context.get("locale");
NVPEncoder encoder = new NVPEncoder();
encoder.add("METHOD", "DoAuthorization");
encoder.add("TRANSACTIONID", payPalPaymentMethod.getString("transactionId"));
encoder.add("AMT", processAmount.setScale(2, RoundingMode.HALF_UP).toPlainString());
encoder.add("TRANSACTIONENTITY", "Order");
String currency = (String) context.get("currency");
if (currency == null) {
currency = orh.getCurrency();
}
encoder.add("CURRENCYCODE", currency);
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(UtilProperties.getMessage(resource, "AccountingPayPalUnknownError", locale));
}
Map<String, Object> result = ServiceUtil.returnSuccess();
Map<String, String> errors = getErrorMessageMap(decoder);
if (UtilValidate.isNotEmpty(errors)) {
result.put("authResult", false);
result.put("authRefNum", "N/A");
result.put("processAmount", BigDecimal.ZERO);
if (errors.size() == 1) {
Map.Entry<String, String> error = errors.entrySet().iterator().next();
result.put("authCode", error.getKey());
result.put("authMessage", error.getValue());
} else {
result.put("authMessage", "Multiple errors occurred, please refer to the gateway response messages");
result.put("internalRespMsgs", errors);
}
} else {
result.put("authResult", true);
result.put("processAmount", new BigDecimal(decoder.get("AMT")));
result.put("authRefNum", decoder.get("TRANSACTIONID"));
}
// TODO: Look into possible PAYMENTSTATUS and PENDINGREASON return codes, it is unclear what should be checked for this type of transaction
return result;
}
Aggregations