use of org.apache.ofbiz.order.shoppingcart.CheckOutHelper in project ofbiz-framework by apache.
the class PayPalServices method payPalCheckoutUpdate.
public static Map<String, Object> payPalCheckoutUpdate(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
String token = (String) paramMap.get("TOKEN");
WeakReference<ShoppingCart> weakCart = tokenCartMap.get(new TokenWrapper(token));
ShoppingCart cart = null;
if (weakCart != null) {
cart = weakCart.get();
}
if (cart == null) {
Debug.logError("Could locate the ShoppingCart for token " + token, module);
return ServiceUtil.returnSuccess();
}
// Since most if not all of the shipping estimate codes requires a persisted contactMechId we'll create one and
// then delete once we're done, now is not the time to worry about updating everything
String contactMechId = null;
Map<String, Object> inMap = new HashMap<String, Object>();
inMap.put("address1", paramMap.get("SHIPTOSTREET"));
inMap.put("address2", paramMap.get("SHIPTOSTREET2"));
inMap.put("city", paramMap.get("SHIPTOCITY"));
String countryGeoCode = (String) paramMap.get("SHIPTOCOUNTRY");
String countryGeoId = PayPalServices.getCountryGeoIdFromGeoCode(countryGeoCode, delegator);
if (countryGeoId == null) {
return ServiceUtil.returnSuccess();
}
inMap.put("countryGeoId", countryGeoId);
inMap.put("stateProvinceGeoId", parseStateProvinceGeoId((String) paramMap.get("SHIPTOSTATE"), countryGeoId, delegator));
inMap.put("postalCode", paramMap.get("SHIPTOZIP"));
try {
GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").cache().queryOne();
inMap.put("userLogin", userLogin);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
boolean beganTransaction = false;
Transaction parentTransaction = null;
try {
parentTransaction = TransactionUtil.suspend();
beganTransaction = TransactionUtil.begin();
} catch (GenericTransactionException e1) {
Debug.logError(e1, module);
}
try {
Map<String, Object> outMap = dispatcher.runSync("createPostalAddress", inMap);
contactMechId = (String) outMap.get("contactMechId");
} catch (GenericServiceException e) {
Debug.logError(e.getMessage(), module);
return ServiceUtil.returnSuccess();
}
try {
TransactionUtil.commit(beganTransaction);
if (parentTransaction != null)
TransactionUtil.resume(parentTransaction);
} catch (GenericTransactionException e) {
Debug.logError(e, module);
}
// clone the cart so we can modify it temporarily
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
String oldShipAddress = cart.getShippingContactMechId();
coh.setCheckOutShippingAddress(contactMechId);
ShippingEstimateWrapper estWrapper = new ShippingEstimateWrapper(dispatcher, cart, 0);
int line = 0;
NVPEncoder encoder = new NVPEncoder();
encoder.add("METHOD", "CallbackResponse");
for (GenericValue shipMethod : estWrapper.getShippingMethods()) {
BigDecimal estimate = estWrapper.getShippingEstimate(shipMethod);
// Check that we have a valid estimate (allowing zero value estimates for now)
if (estimate == null || estimate.compareTo(BigDecimal.ZERO) < 0) {
continue;
}
cart.setAllShipmentMethodTypeId(shipMethod.getString("shipmentMethodTypeId"));
cart.setAllCarrierPartyId(shipMethod.getString("partyId"));
try {
coh.calcAndAddTax();
} catch (GeneralException e) {
Debug.logError(e, module);
continue;
}
String estimateLabel = shipMethod.getString("partyId") + " - " + shipMethod.getString("description");
encoder.add("L_SHIPINGPOPTIONLABEL" + line, estimateLabel);
encoder.add("L_SHIPPINGOPTIONAMOUNT" + line, estimate.setScale(2, RoundingMode.HALF_UP).toPlainString());
// Just make this first one default for now
encoder.add("L_SHIPPINGOPTIONISDEFAULT" + line, line == 0 ? "true" : "false");
encoder.add("L_TAXAMT" + line, cart.getTotalSalesTax().setScale(2, RoundingMode.HALF_UP).toPlainString());
line++;
}
String responseMsg = null;
try {
responseMsg = encoder.encode();
} catch (PayPalException e) {
Debug.logError(e, module);
}
if (responseMsg != null) {
try {
response.setContentLength(responseMsg.getBytes("UTF-8").length);
} catch (UnsupportedEncodingException e) {
Debug.logError(e, module);
}
try (Writer writer = response.getWriter()) {
writer.write(responseMsg);
} catch (IOException e) {
Debug.logError(e, module);
}
}
// Remove the temporary ship address
try {
GenericValue postalAddress = EntityQuery.use(delegator).from("PostalAddress").where("contactMechId", contactMechId).queryOne();
postalAddress.remove();
GenericValue contactMech = EntityQuery.use(delegator).from("ContactMech").where("contactMechId", contactMechId).queryOne();
contactMech.remove();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
coh.setCheckOutShippingAddress(oldShipAddress);
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.order.shoppingcart.CheckOutHelper in project ofbiz-framework by apache.
the class OrderServices method callProcessOrderPayments.
// generic method for processing an order's payment(s)
public static Map<String, Object> callProcessOrderPayments(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
Transaction trans = null;
try {
// disable transaction processing
trans = TransactionUtil.suspend();
// get the cart
ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
GenericValue userLogin = cart.getUserLogin();
Boolean manualHold = (Boolean) context.get("manualHold");
if (manualHold == null) {
manualHold = Boolean.FALSE;
}
if (!"PURCHASE_ORDER".equals(cart.getOrderType())) {
String productStoreId = cart.getProductStoreId();
GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
// process payment
Map<String, Object> payResp;
try {
payResp = coh.processPayment(productStore, userLogin, false, manualHold.booleanValue());
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(payResp)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderProcessOrderPayments", locale), null, null, payResp);
}
}
return ServiceUtil.returnSuccess();
} catch (GenericTransactionException e) {
return ServiceUtil.returnError(e.getMessage());
} finally {
// resume transaction
try {
TransactionUtil.resume(trans);
} catch (GenericTransactionException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
}
use of org.apache.ofbiz.order.shoppingcart.CheckOutHelper in project ofbiz-framework by apache.
the class OrderServices method createOrderFromShoppingCart.
// generic method for creating an order from a shopping cart
public static Map<String, Object> createOrderFromShoppingCart(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
GenericValue userLogin = cart.getUserLogin();
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
Map<String, Object> createOrder = coh.createOrder(userLogin);
if (ServiceUtil.isError(createOrder)) {
return createOrder;
}
String orderId = (String) createOrder.get("orderId");
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("shoppingCart", cart);
result.put("orderId", orderId);
return result;
}
use of org.apache.ofbiz.order.shoppingcart.CheckOutHelper in project ofbiz-framework by apache.
the class ShoppingListServices method createListReorders.
public static Map<String, Object> createListReorders(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
boolean beganTransaction = false;
EntityQuery eq = EntityQuery.use(delegator).from("ShoppingList").where("shoppingListTypeId", "SLT_AUTO_REODR", "isActive", "Y").orderBy("-lastOrderedDate");
try {
beganTransaction = TransactionUtil.begin();
} catch (GenericTransactionException e1) {
Debug.logError(e1, "[Delegator] Could not begin transaction: " + e1.toString(), module);
}
try (EntityListIterator eli = eq.queryIterator()) {
if (eli != null) {
GenericValue shoppingList;
while (((shoppingList = eli.next()) != null)) {
Timestamp lastOrder = shoppingList.getTimestamp("lastOrderedDate");
RecurrenceInfo recurrence = null;
GenericValue recurrenceInfo = shoppingList.getRelatedOne("RecurrenceInfo", false);
Timestamp startDateTime = recurrenceInfo.getTimestamp("startDateTime");
try {
recurrence = new RecurrenceInfo(recurrenceInfo);
} catch (RecurrenceInfoException e) {
Debug.logError(e, module);
}
// check the next recurrence
if (recurrence != null) {
long next = lastOrder == null ? recurrence.next(startDateTime.getTime()) : recurrence.next(lastOrder.getTime());
Timestamp now = UtilDateTime.nowTimestamp();
Timestamp nextOrder = UtilDateTime.getDayStart(UtilDateTime.getTimestamp(next));
if (nextOrder.after(now)) {
continue;
}
} else {
continue;
}
ShoppingCart listCart = makeShoppingListCart(dispatcher, shoppingList, locale);
CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, listCart);
// store the order
Map<String, Object> createResp = helper.createOrder(userLogin);
if (createResp == null || (createResp != null && ServiceUtil.isError(createResp))) {
Debug.logError("Cannot create order for shopping list - " + shoppingList, module);
} else {
String orderId = (String) createResp.get("orderId");
// authorize the payments
Map<String, Object> payRes = null;
try {
payRes = helper.processPayment(ProductStoreWorker.getProductStore(listCart.getProductStoreId(), delegator), userLogin);
} catch (GeneralException e) {
Debug.logError(e, module);
}
if (payRes != null && ServiceUtil.isError(payRes)) {
Debug.logError("Payment processing problems with shopping list - " + shoppingList, module);
}
shoppingList.set("lastOrderedDate", UtilDateTime.nowTimestamp());
shoppingList.store();
// send notification
try {
dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
// increment the recurrence
recurrence.incrementCurrentCount();
}
}
}
return ServiceUtil.returnSuccess();
} catch (GenericEntityException e) {
try {
// only rollback the transaction if we started one...
TransactionUtil.rollback(beganTransaction, "Error creating shopping list auto-reorders", e);
} catch (GenericEntityException e2) {
Debug.logError(e2, "[Delegator] Could not rollback transaction: " + e2.toString(), module);
}
String errMsg = UtilProperties.getMessage(resource_error, "OrderErrorWhileCreatingNewShoppingListBasedAutomaticReorder", UtilMisc.toMap("errorString", e.toString()), locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
} finally {
try {
// only commit the transaction if we started one... this will throw an exception if it fails
TransactionUtil.commit(beganTransaction);
} catch (GenericEntityException e) {
Debug.logError(e, "Could not commit transaction for creating new shopping list based automatic reorder", module);
}
}
}
Aggregations