Search in sources :

Example 6 with NVPDecoder

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;
}
Also used : NVPCallerServices(com.paypal.sdk.services.NVPCallerServices) NVPDecoder(com.paypal.sdk.core.nvp.NVPDecoder) APIProfile(com.paypal.sdk.profiles.APIProfile) PayPalException(com.paypal.sdk.exceptions.PayPalException)

Example 7 with NVPDecoder

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;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) NVPEncoder(com.paypal.sdk.core.nvp.NVPEncoder) NVPDecoder(com.paypal.sdk.core.nvp.NVPDecoder) PayPalException(com.paypal.sdk.exceptions.PayPalException) Map(java.util.Map) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) BigDecimal(java.math.BigDecimal)

Example 8 with NVPDecoder

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;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) PayPalException(com.paypal.sdk.exceptions.PayPalException) BigDecimal(java.math.BigDecimal) OrderReadHelper(org.apache.ofbiz.order.order.OrderReadHelper) NVPEncoder(com.paypal.sdk.core.nvp.NVPEncoder) Delegator(org.apache.ofbiz.entity.Delegator) NVPDecoder(com.paypal.sdk.core.nvp.NVPDecoder) Map(java.util.Map) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Aggregations

NVPDecoder (com.paypal.sdk.core.nvp.NVPDecoder)8 PayPalException (com.paypal.sdk.exceptions.PayPalException)8 NVPEncoder (com.paypal.sdk.core.nvp.NVPEncoder)7 Locale (java.util.Locale)7 GenericValue (org.apache.ofbiz.entity.GenericValue)7 HashMap (java.util.HashMap)6 WeakHashMap (java.util.WeakHashMap)6 BigDecimal (java.math.BigDecimal)5 Map (java.util.Map)5 Delegator (org.apache.ofbiz.entity.Delegator)3 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)3 OrderReadHelper (org.apache.ofbiz.order.order.OrderReadHelper)2 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)2 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)2 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)2 APIProfile (com.paypal.sdk.profiles.APIProfile)1 NVPCallerServices (com.paypal.sdk.services.NVPCallerServices)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)1 CartItemModifyException (org.apache.ofbiz.order.shoppingcart.CartItemModifyException)1