use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class PaymentGatewayServices method retryFailedAuths.
public static Map<String, Object> retryFailedAuths(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
EntityQuery eq = EntityQuery.use(delegator).from("OrderPaymentPreference").where(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PAYMENT_NOT_AUTH"), EntityCondition.makeCondition("processAttempt", EntityOperator.GREATER_THAN, Long.valueOf(0))).orderBy("orderId");
try (EntityListIterator eli = eq.queryIterator()) {
List<String> processList = new LinkedList<>();
if (eli != null) {
Debug.logInfo("Processing failed order re-auth(s)", module);
GenericValue value = null;
while (((value = eli.next()) != null)) {
String orderId = value.getString("orderId");
if (!processList.contains(orderId)) {
// just try each order once
try {
// each re-try is independent of each other; if one fails it should not effect the others
dispatcher.runAsync("retryFailedOrderAuth", UtilMisc.<String, Object>toMap("orderId", orderId, "userLogin", userLogin));
processList.add(orderId);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
}
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ValueLinkServices method giftCardRefund.
public static Map<String, Object> giftCardRefund(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
String paymentConfig = (String) context.get("paymentConfig");
String currency = (String) context.get("currency");
BigDecimal amount = (BigDecimal) context.get("refundAmount");
// get the orderId for tracking
String orderId = paymentPref.getString("orderId");
// get the GiftCard VO
GenericValue giftCard = null;
try {
giftCard = paymentPref.getRelatedOne("GiftCard", false);
} catch (GenericEntityException e) {
Debug.logError("Unable to get GiftCard from OrderPaymentPreference", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingGiftCerticateNumberCannotLocateItFromOrderPaymentPreference", locale));
}
if (giftCard == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkUnableToReleaseGiftCard", locale));
}
// make sure we have a currency
if (currency == null) {
currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
}
Map<String, Object> refundCtx = new HashMap<String, Object>();
refundCtx.put("userLogin", userLogin);
refundCtx.put("paymentConfig", paymentConfig);
refundCtx.put("cardNumber", giftCard.get("cardNumber"));
refundCtx.put("pin", giftCard.get("pinNumber"));
refundCtx.put("currency", currency);
refundCtx.put("orderId", orderId);
refundCtx.put("amount", amount);
// invoke the refund service
Map<String, Object> redeemResult = null;
try {
redeemResult = dispatcher.runSync("refundGiftCard", refundCtx);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem calling the refund service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkUnableToRefundGiftCardFailure", locale));
}
Map<String, Object> result = ServiceUtil.returnSuccess();
if (redeemResult != null) {
Boolean processResult = (Boolean) redeemResult.get("processResult");
result.put("refundAmount", redeemResult.get("amount"));
result.put("refundFlag", redeemResult.get("responseCode"));
result.put("refundResult", processResult);
result.put("refundCode", redeemResult.get("authCode"));
result.put("refundRefNum", redeemResult.get("referenceNum"));
}
return result;
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ValueLinkServices method giftCardProcessor.
// payment processing wrappers (process/release/refund)
public static Map<String, Object> giftCardProcessor(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
GenericValue giftCard = (GenericValue) context.get("giftCard");
GenericValue party = (GenericValue) context.get("billToParty");
String paymentConfig = (String) context.get("paymentConfig");
String currency = (String) context.get("currency");
String orderId = (String) context.get("orderId");
BigDecimal amount = (BigDecimal) context.get("processAmount");
// make sure we have a currency
if (currency == null) {
currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
}
Map<String, Object> redeemCtx = new HashMap<String, Object>();
redeemCtx.put("userLogin", userLogin);
redeemCtx.put("paymentConfig", paymentConfig);
redeemCtx.put("cardNumber", giftCard.get("cardNumber"));
redeemCtx.put("pin", giftCard.get("pinNumber"));
redeemCtx.put("currency", currency);
redeemCtx.put("orderId", orderId);
redeemCtx.put("partyId", party.get("partyId"));
redeemCtx.put("amount", amount);
// invoke the redeem service
Map<String, Object> redeemResult = null;
try {
redeemResult = dispatcher.runSync("redeemGiftCard", redeemCtx);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem calling the redeem service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingValueLinkUnableToRedeemGiftCardFailure", locale));
}
Map<String, Object> result = ServiceUtil.returnSuccess();
if (redeemResult != null) {
Boolean processResult = (Boolean) redeemResult.get("processResult");
// confirm the amount redeemed; since VL does not error in insufficient funds
if (processResult.booleanValue()) {
BigDecimal previous = (BigDecimal) redeemResult.get("previousAmount");
if (previous == null)
previous = BigDecimal.ZERO;
BigDecimal current = (BigDecimal) redeemResult.get("amount");
if (current == null)
current = BigDecimal.ZERO;
BigDecimal redeemed = previous.subtract(current);
Debug.logInfo("Redeemed (" + amount + "): " + redeemed + " / " + previous + " : " + current, module);
if (redeemed.compareTo(amount) < 0) {
// we didn't redeem enough void the transaction and return false
Map<String, Object> voidResult = null;
try {
voidResult = dispatcher.runSync("voidRedeemGiftCard", redeemCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
if (ServiceUtil.isError(voidResult)) {
return voidResult;
}
processResult = Boolean.FALSE;
amount = redeemed;
result.put("authMessage", "Gift card did not contain enough funds");
}
}
result.put("processAmount", amount);
result.put("authFlag", redeemResult.get("responseCode"));
result.put("authResult", processResult);
result.put("captureResult", processResult);
result.put("authCode", redeemResult.get("authCode"));
result.put("captureCode", redeemResult.get("authCode"));
result.put("authRefNum", redeemResult.get("referenceNum"));
result.put("captureRefNum", redeemResult.get("referenceNum"));
}
return result;
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class WorldPayEvents method worldPayNotify.
/**
* WorldPay notification
*/
public static String worldPayNotify(HttpServletRequest request, HttpServletResponse response) {
Locale locale = UtilHttp.getLocale(request);
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
Map<String, Object> parametersMap = UtilHttp.getParameterMap(request);
String orderId = request.getParameter("cartId");
for (String name : parametersMap.keySet()) {
String value = request.getParameter(name);
Debug.logError("### Param: " + name + " => " + value, module);
}
// get the user
if (userLogin == null) {
String userLoginId = "system";
try {
userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get UserLogin for: " + userLoginId + "; cannot continue", module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "worldPayEvents.problemsGettingAuthenticationUser", locale));
return "error";
}
}
// get the order header
GenericValue orderHeader = null;
if (UtilValidate.isNotEmpty(orderId)) {
try {
orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get the order header for order: " + orderId, module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "worldPayEvents.problemsGettingOrderHeader", locale));
return "error";
}
} else {
Debug.logError("WorldPay did not callback with a valid orderId!", module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "worldPayEvents.noValidOrderIdReturned", locale));
return "error";
}
if (orderHeader == null) {
Debug.logError("Cannot get the order header for order: " + orderId, module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "worldPayEvents.problemsGettingOrderHeader", locale));
return "error";
}
// get the transaction status
String paymentStatus = request.getParameter("transStatus");
// attempt to start a transaction
boolean okay = true;
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
// authorized
if ("Y".equals(paymentStatus)) {
okay = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId);
// cancelled
} else if ("C".equals(paymentStatus)) {
okay = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId);
}
if (okay) {
// set the payment preference
okay = setPaymentPreferences(delegator, dispatcher, userLogin, orderId, request);
}
} catch (Exception e) {
String errMsg = "Error handling WorldPay notification";
Debug.logError(e, errMsg, module);
try {
TransactionUtil.rollback(beganTransaction, errMsg, e);
} catch (GenericTransactionException gte2) {
Debug.logError(gte2, "Unable to rollback transaction", module);
}
} finally {
if (!okay) {
try {
TransactionUtil.rollback(beganTransaction, "Failure in processing WorldPay callback", null);
} catch (GenericTransactionException gte) {
Debug.logError(gte, "Unable to rollback transaction", module);
}
} else {
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException gte) {
Debug.logError(gte, "Unable to commit transaction", module);
}
}
}
if (okay) {
// call the email confirm service
Map<String, Object> emailContext = UtilMisc.toMap("orderId", orderId, "userLogin", userLogin);
try {
dispatcher.runSync("sendOrderConfirmation", emailContext);
} catch (GenericServiceException e) {
Debug.logError(e, "Problems sending email confirmation", module);
}
}
return "success";
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ContentManagementServices method updateContentSubscription.
public static Map<String, Object> updateContentSubscription(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException {
Map<String, Object> result = new HashMap<String, Object>();
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Map<String, Object> thisResult = new HashMap<String, Object>();
String partyId = (String) context.get("partyId");
String webPubPt = (String) context.get("contentId");
String roleTypeId = (String) context.get("useRoleTypeId");
GenericValue userLogin = (GenericValue) context.get("userLogin");
Integer useTime = (Integer) context.get("useTime");
String useTimeUomId = (String) context.get("useTimeUomId");
boolean hasExistingContentRole = false;
GenericValue contentRole = null;
try {
contentRole = EntityQuery.use(delegator).from("ContentRole").where("partyId", partyId, "contentId", webPubPt, "roleTypeId", roleTypeId).orderBy("fromDate DESC").cache().filterByDate().queryFirst();
if (contentRole != null) {
hasExistingContentRole = true;
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
}
if (contentRole == null) {
contentRole = delegator.makeValue("ContentRole");
contentRole.set("contentId", webPubPt);
contentRole.set("partyId", partyId);
contentRole.set("roleTypeId", roleTypeId);
contentRole.set("fromDate", nowTimestamp);
}
Timestamp thruDate = (Timestamp) contentRole.get("thruDate");
if (thruDate == null) {
// no thruDate? start with NOW
thruDate = nowTimestamp;
} else {
// don't want to penalize for skipping time, in other words if they had a subscription last year for a month and buy another month, we want that second month to start now and not last year
if (thruDate.before(nowTimestamp)) {
thruDate = nowTimestamp;
}
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(thruDate);
int field = Calendar.MONTH;
if ("TF_day".equals(useTimeUomId)) {
field = Calendar.DAY_OF_YEAR;
} else if ("TF_wk".equals(useTimeUomId)) {
field = Calendar.WEEK_OF_YEAR;
} else if ("TF_mon".equals(useTimeUomId)) {
field = Calendar.MONTH;
} else if ("TF_yr".equals(useTimeUomId)) {
field = Calendar.YEAR;
} else {
Debug.logWarning("Don't know anything about useTimeUomId [" + useTimeUomId + "], defaulting to month", module);
}
calendar.add(field, useTime.intValue());
thruDate = new Timestamp(calendar.getTimeInMillis());
contentRole.set("thruDate", thruDate);
try {
if (hasExistingContentRole) {
contentRole.store();
} else {
Map<String, Object> map = new HashMap<String, Object>();
map.put("partyId", partyId);
map.put("roleTypeId", roleTypeId);
map.put("userLogin", userLogin);
thisResult = dispatcher.runSync("ensurePartyRole", map);
if (ServiceUtil.isError(thisResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(thisResult));
}
contentRole.create();
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
}
return result;
}
Aggregations