📄 paymentgatewayservices.java
字号:
return ServiceUtil.returnError("Could not find OrderHeader with orderId: " + orderId + "; not processing payments."); } // get the order amounts OrderReadHelper orh = new OrderReadHelper(orderHeader); double totalRemaining = 0.0; try { totalRemaining = getTotalRemaining(orh); } catch (Exception e) { Debug.logError(e, "Problem getting parsed grand total amount", module); return ServiceUtil.returnError("ERROR: Cannot parse grand total from formatted string; see logs"); } // loop through and auth each order payment preference int finished = 0; int hadError = 0; List messages = new ArrayList(); Iterator payments = paymentPrefs.iterator(); while (payments.hasNext()) { GenericValue paymentPref = (GenericValue) payments.next(); Map authContext = new HashMap(); authContext.put("orderPaymentPreferenceId", paymentPref.getString("orderPaymentPreferenceId")); authContext.put("userLogin", context.get("userLogin")); Map results = null; try { results = dispatcher.runSync("authOrderPaymentPreference", authContext); } catch (GenericServiceException se) { Debug.logError(se, "Error in calling authOrderPaymentPreference from authOrderPayments: " + se.getMessage(), module); hadError += 1; messages.add("Could not authorize OrderPaymentPreference [" + paymentPref.getString("orderPaymentPreferenceId") + "] for order [" + orderId + "]: " + se.getMessage()); continue; } if (ServiceUtil.isError(results)) { hadError += 1; messages.add("Could not authorize OrderPaymentPreference [" + paymentPref.getString("orderPaymentPreferenceId") + "] for order [" + orderId + "]: " + results.get(ModelService.ERROR_MESSAGE)); continue; } if (((Boolean)results.get("finished")).booleanValue()) finished += 1; if (((Boolean)results.get("errors")).booleanValue()) hadError += 1; if (results.get("messages") != null) messages.addAll((List) results.get("messages")); if (results.get("processAmount") != null) totalRemaining -= ((Double) results.get("processAmount")).doubleValue(); } Debug.logInfo("Finished with auth(s) checking results", module); // add messages to the result result.put("authResultMsgs", messages); if (hadError > 0) { Debug.logError("Error(s) (" + hadError + ") during auth; returning ERROR", module); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); result.put("processResult", "ERROR"); return result; } else if (finished == paymentPrefs.size()) { Debug.logInfo("All auth(s) passed total remaining : " + totalRemaining, module); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); result.put("processResult", "APPROVED"); return result; } else { Debug.logInfo("Only (" + finished + ") passed auth; returning FAILED", module); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); result.put("processResult", "FAILED"); return result; } } private static Map authPayment(LocalDispatcher dispatcher, GenericValue userLogin, OrderReadHelper orh, GenericValue paymentPref, double totalRemaining, boolean reauth) { return authPayment(dispatcher, userLogin, orh, paymentPref, totalRemaining, reauth, null); } private static Map authPayment(LocalDispatcher dispatcher, GenericValue userLogin, OrderReadHelper orh, GenericValue paymentPref, double totalRemaining, boolean reauth, Double overrideAmount) { String paymentConfig = null; String serviceName = null; // get the payment settings i.e. serviceName and config properties file name String serviceType = AUTH_SERVICE_TYPE; if (reauth) { serviceType = REAUTH_SERVICE_TYPE; } GenericValue paymentSettings = getPaymentSettings(orh.getOrderHeader(), paymentPref, serviceType, false); if (paymentSettings != null) { serviceName = paymentSettings.getString("paymentService"); paymentConfig = paymentSettings.getString("paymentPropertiesPath"); } else { Debug.logError("Invalid payment settings entity, no payment settings found", module); return null; } // make sure the service name is not null if (serviceName == null) { Debug.logError("Invalid payment processor: + " + paymentSettings, module); return null; } // get the process context Map processContext = null; try { processContext = makeAuthContext(orh, userLogin, paymentPref, paymentConfig, totalRemaining, overrideAmount); } catch (GeneralException e) { Debug.logError(e, "Problems creating the context for the auth service", module); return null; } // invoke the processor. Map processorResult = null; try { // invoke the payment processor; allow 5 minute transaction timeout and require a new tx; we'll capture the error and pass back nicely. processorResult = dispatcher.runSync(serviceName, processContext, TX_TIME, true); } catch (GenericServiceException gse) { Debug.logError("Error occurred on: " + serviceName + " => " + processContext, module); Debug.logError(gse, "Problems invoking payment processor! Will retry later." + "(" + orh.getOrderId() + ")", module); return null; } if (processorResult != null) { // check for errors from the processor implementation String resultResponseCode = (String) processorResult.get(ModelService.RESPONSE_MESSAGE); if (resultResponseCode != null && resultResponseCode.equals(ModelService.RESPOND_ERROR)) { Debug.logError("Processor failed; will retry later : " + processorResult.get(ModelService.ERROR_MESSAGE), module); // log the error message as a gateway response when it fails saveError(dispatcher, userLogin, paymentPref, processorResult, "PRDS_PAY_AUTH", "PGT_AUTHORIZE"); return null; } // pass the payTo partyId to the result processor; we just add it to the result context. String payToPartyId = getPayToPartyId(orh.getOrderHeader()); processorResult.put("payToPartyId", payToPartyId); // add paymentSettings to result; for use by later processors processorResult.put("paymentSettings", paymentSettings); // and pass on the currencyUomId processorResult.put("currencyUomId", orh.getCurrency()); } return processorResult; } private static GenericValue getPaymentSettings(GenericValue orderHeader, GenericValue paymentPreference, String paymentServiceType, boolean anyServiceType) { GenericDelegator delegator = orderHeader.getDelegator(); GenericValue paymentSettings = null; GenericValue paymentMethod = null; try { paymentMethod = paymentPreference.getRelatedOne("PaymentMethod"); } catch (GenericEntityException e) { Debug.logError(e, "Problem getting PaymentMethod from OrderPaymentPreference", module); } if (paymentMethod != null) { String productStoreId = orderHeader.getString("productStoreId"); String paymentMethodTypeId = paymentMethod.getString("paymentMethodTypeId"); if (productStoreId != null && paymentMethodTypeId != null) { paymentSettings = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStoreId, paymentMethodTypeId, paymentServiceType, anyServiceType); } } return paymentSettings; } private static String getPayToPartyId(GenericValue orderHeader) { String payToPartyId = "Company"; // default value GenericValue productStore = null; try { productStore = orderHeader.getRelatedOne("ProductStore"); } catch (GenericEntityException e) { Debug.logError(e, "Unable to get ProductStore from OrderHeader", module); return null; } if (productStore != null && productStore.get("payToPartyId") != null) { payToPartyId = productStore.getString("payToPartyId"); } else { Debug.logWarning("Using default value of [Company] for payToPartyId on order [" + orderHeader.getString("orderId") + "]", module); } return payToPartyId; } private static Map makeAuthContext(OrderReadHelper orh, GenericValue userLogin, GenericValue paymentPreference, String paymentConfig, double totalRemaining, Double overrideAmount) throws GeneralException { Map processContext = new HashMap(); // get the visit record to obtain the client's IP address GenericValue orderHeader = orh.getOrderHeader(); if (orderHeader != null) { String visitId = orderHeader.getString("visitId"); GenericValue visit = null; if (visitId != null) { try { visit = orderHeader.getDelegator().findByPrimaryKey("Visit", UtilMisc.toMap("visitId", visitId)); } catch (GenericEntityException e) { Debug.logError(e, module); } } if (visit != null && visit.get("clientIpAddress") != null) { processContext.put("customerIpAddress", visit.getString("clientIpAddress")); } } processContext.put("userLogin", userLogin); processContext.put("orderId", orh.getOrderId()); processContext.put("orderItems", orh.getOrderItems()); processContext.put("shippingAddress", EntityUtil.getFirst(orh.getShippingLocations())); // TODO refactor the payment API to handle support all addresses processContext.put("paymentConfig", paymentConfig); processContext.put("currency", orh.getCurrency()); processContext.put("orderPaymentPreference", paymentPreference); if (paymentPreference.get("securityCode") != null) { processContext.put("cardSecurityCode", paymentPreference.get("securityCode")); } // get the billing information getBillingInformation(orh, paymentPreference, processContext); // default charge is totalRemaining double thisAmount = totalRemaining; // use override or max amount available if (overrideAmount != null) { thisAmount = overrideAmount.doubleValue(); } else if (paymentPreference.get("maxAmount") != null) { thisAmount = paymentPreference.getDouble("maxAmount").doubleValue(); } // don't authorized more then what is required if (thisAmount > totalRemaining) { thisAmount = totalRemaining; } // format the decimal String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00"); DecimalFormat formatter = new DecimalFormat(currencyFormat); String amountString = formatter.format(thisAmount); Double processAmount = null; try { processAmount = new Double(formatter.parse(amountString).doubleValue()); } catch (ParseException e) { Debug.logError(e, "Problems parsing string formatted double to Double", module); throw new GeneralException("ParseException in number format", e); } if (Debug.verboseOn()) Debug.logVerbose("Charging amount: " + processAmount, module); processContext.put("processAmount", processAmount); return processContext; } private static String getBillingInformation(OrderReadHelper orh, GenericValue paymentPreference, Map toContext) throws GenericEntityException { // gather the payment related objects. GenericValue paymentMethod = paymentPreference.getRelatedOne("PaymentMethod"); if (paymentMethod != null && paymentMethod.getString("paymentMethodTypeId").equals("CREDIT_CARD")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -