}
public static Map giftCertificateReload(DispatchContext dctx, Map context) {
// this service should always be called via FULFILLMENT_EXTSYNC
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 reload; no productStoreId on OrderHeader : " + orderId);
}
// 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("Unable to get payment configuration file");
}
// party ID for tracking
GenericValue placingParty = orh.getPlacingParty();
String partyId = null;
if (placingParty != null) {
partyId = placingParty.getString("partyId");
}
// amount of the gift card reload
BigDecimal amount = orderItem.getBigDecimal("unitPrice");
// survey information
String surveyId = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.reload.surveyId");
// 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 reload");
}
// 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 reload");
}
// 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);
}
}
}
String cardNumberKey = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.reload.survey.cardNumber");
String pinNumberKey = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.reload.survey.pinNumber");
String cardNumber = (String) answerMap.get(cardNumberKey);
String pinNumber = (String) answerMap.get(pinNumberKey);
// reload the gift card
Map reloadCtx = new HashMap();
reloadCtx.put("productStoreId", productStoreId);
reloadCtx.put("currency", currency);
reloadCtx.put("partyId", partyId);
//reloadCtx.put("orderId", orderId);
reloadCtx.put("cardNumber", cardNumber);
reloadCtx.put("pinNumber", pinNumber);
reloadCtx.put("amount", amount);
reloadCtx.put("userLogin", userLogin);
String errorMessage = null;
Map reloadGcResult = null;
try {
reloadGcResult = dispatcher.runSync("addFundsToGiftCertificate", reloadCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
errorMessage = "Unable to call reload service!";
}
if (ServiceUtil.isError(reloadGcResult)) {
errorMessage = ServiceUtil.getErrorMessage(reloadGcResult);
}
// create the fulfillment record
Map gcFulFill = new HashMap();
gcFulFill.put("typeEnumId", "GC_RELOAD");
gcFulFill.put("userLogin", userLogin);
gcFulFill.put("partyId", partyId);
gcFulFill.put("orderId", orderId);
gcFulFill.put("orderItemSeqId", orderItem.get("orderItemSeqId"));
gcFulFill.put("surveyResponseId", surveyResponse.get("surveyResponseId"));
gcFulFill.put("cardNumber", cardNumber);
gcFulFill.put("pinNumber", pinNumber);
gcFulFill.put("amount", amount);
if (reloadGcResult != null) {
gcFulFill.put("responseCode", reloadGcResult.get("responseCode"));
gcFulFill.put("referenceNum", reloadGcResult.get("referenceNum"));
}
try {
dispatcher.runAsync("createGcFulFillmentRecord", gcFulFill, true);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to store fulfillment info");
}
if (errorMessage != null) {
// there was a problem
Debug.logError("Reload Failed Need to Refund : " + reloadGcResult, module);
// process the return
try {
Map refundCtx = UtilMisc.toMap("orderItem", orderItem, "partyId", partyId, "userLogin", userLogin);
dispatcher.runAsync("refundGcPurchase", refundCtx, null, true, 300, true);
} catch (GenericServiceException e) {
Debug.logError(e, "ERROR! Unable to call create refund service; this failed reload will NOT be refunded", module);
}
return ServiceUtil.returnError(errorMessage);
}
// add some information to the answerMap for the email
answerMap.put("processResult", reloadGcResult.get("processResult"));
answerMap.put("responseCode", reloadGcResult.get("responseCode"));
answerMap.put("previousAmount", reloadGcResult.get("previousBalance"));
answerMap.put("amount", reloadGcResult.get("amount"));
// get the email setting for this email type
GenericValue productStoreEmail = null;
String emailType = "PRDS_GC_RELOAD";
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);