// item fulfillment wrappers (purchase/reload)
public static Map<String, Object> giftCardPurchase(DispatchContext dctx, Map<String, Object> context) {
// this service should always be called via FULFILLMENT_EXTASYNC
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
GenericValue orderItem = (GenericValue) context.get("orderItem");
Locale locale = (Locale) context.get("locale");
// order ID for tracking
String orderId = orderItem.getString("orderId");
// the order header for store info
GenericValue orderHeader = null;
try {
orderHeader = orderItem.getRelatedOne("OrderHeader");
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get OrderHeader from OrderItem",module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder,
"OrderOrderNotFound", UtilMisc.toMap("orderId", orderId), locale));
}
// get the order read helper
OrderReadHelper orh = new OrderReadHelper(orderHeader);
// get the currency
String currency = orh.getCurrency();
// make sure we have a currency
if (currency == null) {
currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD");
}
// get the product store
String productStoreId = null;
if (orderHeader != null) {
productStoreId = orh.getProductStoreId();
}
if (productStoreId == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotProcess", locale));
}
// payment config
GenericValue paymentSetting = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStoreId, "GIFT_CARD", null, true);
String paymentConfig = null;
if (paymentSetting != null) {
paymentConfig = paymentSetting.getString("paymentPropertiesPath");
}
if (paymentConfig == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingFinAccountSetting",
UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", "GIFT_CARD"), locale));
}
// party ID for tracking
GenericValue placingParty = orh.getPlacingParty();
String partyId = null;
if (placingParty != null) {
partyId = placingParty.getString("partyId");
}
// amount/quantity of the gift card(s)
BigDecimal amount = orderItem.getBigDecimal("unitPrice");
BigDecimal quantity = orderItem.getBigDecimal("quantity");
// the product entity needed for information
GenericValue product = null;
try {
product = orderItem.getRelatedOne("Product");
} catch (GenericEntityException e) {
Debug.logError("Unable to get Product from OrderItem", module);
}
if (product == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotFulfill", locale));
}
// get the productFeature type TYPE (VL promo code)
GenericValue typeFeature = null;
try {
Map<String, Object> fields = UtilMisc.toMap("productId", product.get("productId"), "productFeatureTypeId", "TYPE");
List<String> order = UtilMisc.toList("-fromDate");
List<GenericValue> featureAppls = delegator.findByAndCache("ProductFeatureAndAppl", fields, order);
featureAppls = EntityUtil.filterByDate(featureAppls);
typeFeature = EntityUtil.getFirst(featureAppls);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"AccountingValueLinkUnableToGetFeatureType", locale));
}
if (typeFeature == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"AccountingValueLinkFeatureTypeRequested",
UtilMisc.toMap("productId", product.get("productId")), locale));
}
// get the VL promo code
String promoCode = typeFeature.getString("idCode");
if (UtilValidate.isEmpty(promoCode)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"AccountingValueLinkPromoCodeInvalid", locale));
}
// survey information
String surveyId = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.purchase.surveyId");
// get the survey response
GenericValue surveyResponse = null;
try {
Map<String, Object> fields = UtilMisc.<String, Object>toMap("orderId", orderId,
"orderItemSeqId", orderItem.get("orderItemSeqId"), "surveyId", surveyId);
List<String> order = UtilMisc.toList("-responseDate");
List<GenericValue> responses = delegator.findByAnd("SurveyResponse", fields, order);
// there should be only one
surveyResponse = EntityUtil.getFirst(responses);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotFulfill", locale));
}
// get the response answers
List<GenericValue> responseAnswers = null;
try {
responseAnswers = surveyResponse.getRelated("SurveyResponseAnswer");
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotFulfillFromSurveyAnswers", locale));
}
// make a map of answer info
Map<String, Object> answerMap = FastMap.newInstance();
if (responseAnswers != null) {
Iterator<GenericValue> rai = responseAnswers.iterator();
while (rai.hasNext()) {
GenericValue answer = rai.next();
GenericValue question = null;
try {
question = answer.getRelatedOne("SurveyQuestion");
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotFulfillFromSurveyAnswers", locale));
}
if (question != null) {
String desc = question.getString("description");
String ans = answer.getString("textResponse"); // only support text response types for now
answerMap.put(desc, ans);
}
}
}
// get the send to email address - key defined in properties file
String sendToKey = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.purchase.survey.sendToEmail");
String sendToEmail = (String) answerMap.get(sendToKey);
// get the copyMe flag and set the order email address
String orderEmails = orh.getOrderEmailString();
String copyMeField = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.purchase.survey.copyMe");
String copyMeResp = copyMeField != null ? (String) answerMap.get(copyMeField) : null;
boolean copyMe = (UtilValidate.isNotEmpty(copyMeField)
&& UtilValidate.isNotEmpty(copyMeResp) && "true".equalsIgnoreCase(copyMeResp)) ? true : false;
int qtyLoop = quantity.intValue();
for (int i = 0; i < qtyLoop; i++) {
// activate a gift card
Map<String, Object> activateCtx = FastMap.newInstance();
activateCtx.put("paymentConfig", paymentConfig);
activateCtx.put("vlPromoCode", promoCode);
activateCtx.put("currency", currency);
activateCtx.put("partyId", partyId);
activateCtx.put("orderId", orderId);
activateCtx.put("amount", amount);
activateCtx.put("userLogin", userLogin);
boolean failure = false;
Map<String, Object> activateResult = null;
try {
activateResult = dispatcher.runSync("activateGiftCard", activateCtx);
} catch (GenericServiceException e) {
Debug.logError(e, "Unable to activate gift card(s)", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"AccountingValueLinkUnableToActivateGiftCard", locale));
}
Boolean processResult = (Boolean) activateResult.get("processResult");
if (activateResult == null || activateResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) {
failure = true;
}
if (!failure) {
// set the void on rollback wrapper
ServiceXaWrapper xaw = new ServiceXaWrapper(dctx);
activateCtx.put("cardNumber", activateResult.get("cardNumber"));
activateCtx.put("pin", activateResult.get("pin"));
xaw.setRollbackService("voidActivateGiftCard", activateCtx);
try {
xaw.enlist();
} catch (XAException e) {
Debug.logError(e, "Unable to setup Activate/Void on error", module);
}
}
// create the fulfillment record
Map<String, Object> vlFulFill = FastMap.newInstance();
vlFulFill.put("typeEnumId", "GC_ACTIVATE");
vlFulFill.put("merchantId", UtilProperties.getPropertyValue(paymentConfig, "payment.valuelink.merchantId"));
vlFulFill.put("partyId", partyId);
vlFulFill.put("orderId", orderId);
vlFulFill.put("orderItemSeqId", orderItem.get("orderItemSeqId"));
vlFulFill.put("surveyResponseId", surveyResponse.get("surveyResponseId"));
vlFulFill.put("cardNumber", activateResult.get("cardNumber"));
vlFulFill.put("pinNumber", activateResult.get("pin"));
vlFulFill.put("amount", activateResult.get("amount"));
vlFulFill.put("responseCode", activateResult.get("responseCode"));
vlFulFill.put("referenceNum", activateResult.get("referenceNum"));
vlFulFill.put("authCode", activateResult.get("authCode"));
vlFulFill.put("userLogin", userLogin);
try {
dispatcher.runAsync("createGcFulFillmentRecord", vlFulFill, true);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotStoreFulfillmentInfo",
UtilMisc.toMap("errorString", e.toString() ), locale));
}
if (failure) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource,
"AccountingValueLinkUnableToActivateGiftCard", locale));
}
// add some information to the answerMap for the email
answerMap.put("cardNumber", activateResult.get("cardNumber"));
answerMap.put("pinNumber", activateResult.get("pin"));
answerMap.put("amount", activateResult.get("amount"));
// get the email setting for this email type
GenericValue productStoreEmail = null;
String emailType = "PRDS_GC_PURCHASE";
try {
productStoreEmail = delegator.findByPrimaryKey("ProductStoreEmailSetting", UtilMisc.toMap("productStoreId", productStoreId, "emailType", emailType));
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get product store email setting for gift card purchase", module);
}
if (productStoreEmail == null) {
Debug.logError("No gift card purchase email setting found for this store; cannot send gift card information", module);
} else {
ResourceBundleMapWrapper uiLabelMap = UtilProperties.getResourceBundleMap("EcommerceUiLabels", locale);
uiLabelMap.addBottomResourceBundle("OrderUiLabels");
uiLabelMap.addBottomResourceBundle("CommonUiLabels");
answerMap.put("uiLabelMap", uiLabelMap);
answerMap.put("locale", locale);
// set the bcc address(s)
String bcc = productStoreEmail.getString("bccAddress");
if (copyMe) {
if (UtilValidate.isNotEmpty(bcc)) {
bcc = bcc + "," + orderEmails;
} else {
bcc = orderEmails;
}
}
Map<String, Object> emailCtx = FastMap.newInstance();
String bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation");
if (UtilValidate.isEmpty(bodyScreenLocation)) {
bodyScreenLocation = ProductStoreWorker.getDefaultProductStoreEmailScreenLocation(emailType);
}
emailCtx.put("bodyScreenUri", bodyScreenLocation);
emailCtx.put("bodyParameters", answerMap);
emailCtx.put("sendTo", sendToEmail);
emailCtx.put("contentType", productStoreEmail.get("contentType"));
emailCtx.put("sendFrom", productStoreEmail.get("fromAddress"));
emailCtx.put("sendCc", productStoreEmail.get("ccAddress"));
emailCtx.put("sendBcc", bcc);
emailCtx.put("subject", productStoreEmail.getString("subject"));
emailCtx.put("userLogin", userLogin);
// send off the email async so we will retry on failed attempts
try {
dispatcher.runAsync("sendMailFromScreen", emailCtx);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem sending mail", module);
// this is fatal; we will rollback and try again later
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"AccountingGiftCerticateNumberCannotSendEmailNotice",