}
public static Map giftCertificatePurchase(DispatchContext dctx, Map context) {
// this service should always be called via FULFILLMENT_EXTASYNC
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericDelegator 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("Unable to get OrderHeader from OrderItem");
}
// 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("Unable to process gift card purchase; no productStoreId on OrderHeader : " + orderId);
}
// 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(e, "Unable to get Product from OrderItem", module);
}
if (product == null) {
return ServiceUtil.returnError("No product associated with OrderItem, cannot fulfill gift card");
}
// Gift certificate settings are per store in this entity
GenericValue giftCertSettings = null;
try {
giftCertSettings = delegator.findByPrimaryKeyCache("ProductStoreFinActSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId));
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get Product Store FinAccount settings for " + FinAccountHelper.giftCertFinAccountTypeId, module);
ServiceUtil.returnError("Unable to get Product Store FinAccount settings for " + FinAccountHelper.giftCertFinAccountTypeId + ": " + e.getMessage());
}
// survey information
String surveyId = giftCertSettings.getString("purchaseSurveyId");
// get the survey response
GenericValue surveyResponse = null;
try {
Map fields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItem.get("orderItemSeqId"), "surveyId", surveyId);
List order = UtilMisc.toList("-responseDate");
List 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("Unable to get survey response information; cannot fulfill gift card");
}
if (surveyResponse == null) {
return ServiceUtil.returnError("Survey response came back null from the database for order item: " + orderItem);
}
// get the response answers
List responseAnswers = null;
try {
responseAnswers = surveyResponse.getRelated("SurveyResponseAnswer");
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to get survey response answers from survey response; cannot fulfill gift card");
}
// make a map of answer info
Map answerMap = new HashMap();
if (responseAnswers != null) {
Iterator rai = responseAnswers.iterator();
while (rai.hasNext()) {
GenericValue answer = (GenericValue) rai.next();
GenericValue question = null;
try {
question = answer.getRelatedOne("SurveyQuestion");
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to get survey question from answer");
}
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 product store settings entity
String sendToKey = giftCertSettings.getString("purchSurveySendTo");
String sendToEmail = (String) answerMap.get(sendToKey);
// get the copyMe flag and set the order email address
String orderEmails = orh.getOrderEmailString();
String copyMeField = giftCertSettings.getString("purchSurveyCopyMe");
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++) {
// create a gift certificate
Map createGcCtx = new HashMap();
//createGcCtx.put("paymentConfig", paymentConfig);
createGcCtx.put("productStoreId", productStoreId);
createGcCtx.put("currency", currency);
createGcCtx.put("partyId", partyId);
//createGcCtx.put("orderId", orderId);
createGcCtx.put("initialAmount", amount);
createGcCtx.put("userLogin", userLogin);
Map createGcResult = null;
try {
createGcResult = dispatcher.runSync("createGiftCertificate", createGcCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to create gift certificate: " + e.getMessage());
}
if (ServiceUtil.isError(createGcResult)) {
return ServiceUtil.returnError("Create Gift Certificate Failed: " + ServiceUtil.getErrorMessage(createGcResult));
}
// create the fulfillment record
Map gcFulFill = new HashMap();
gcFulFill.put("typeEnumId", "GC_ACTIVATE");
gcFulFill.put("partyId", partyId);
gcFulFill.put("orderId", orderId);
gcFulFill.put("orderItemSeqId", orderItem.get("orderItemSeqId"));
gcFulFill.put("surveyResponseId", surveyResponse.get("surveyResponseId"));
gcFulFill.put("cardNumber", createGcResult.get("cardNumber"));
gcFulFill.put("pinNumber", createGcResult.get("pinNumber"));
gcFulFill.put("amount", createGcResult.get("initialAmount"));
gcFulFill.put("responseCode", createGcResult.get("responseCode"));
gcFulFill.put("referenceNum", createGcResult.get("referenceNum"));
gcFulFill.put("userLogin", userLogin);
try {
dispatcher.runAsync("createGcFulFillmentRecord", gcFulFill, true);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to store fulfillment info: " + e.getMessage());
}
// add some information to the answerMap for the email
answerMap.put("cardNumber", createGcResult.get("cardNumber"));
answerMap.put("pinNumber", createGcResult.get("pinNumber"));
answerMap.put("amount", createGcResult.get("initialAmount"));
// 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);