1 25 package org.ofbiz.accounting.thirdparty.valuelink; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 import javax.transaction.xa.XAException ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.HttpClientException; 37 import org.ofbiz.base.util.StringUtil; 38 import org.ofbiz.base.util.UtilDateTime; 39 import org.ofbiz.base.util.UtilMisc; 40 import org.ofbiz.base.util.UtilProperties; 41 import org.ofbiz.base.util.UtilValidate; 42 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.util.EntityUtil; 47 import org.ofbiz.order.order.OrderReadHelper; 48 import org.ofbiz.product.store.ProductStoreWorker; 49 import org.ofbiz.service.DispatchContext; 50 import org.ofbiz.service.GenericServiceException; 51 import org.ofbiz.service.LocalDispatcher; 52 import org.ofbiz.service.ModelService; 53 import org.ofbiz.service.ServiceUtil; 54 import org.ofbiz.service.ServiceXaWrapper; 55 56 63 public class ValueLinkServices { 64 65 public static final String module = ValueLinkServices.class.getName(); 66 67 public static Map createKeys(DispatchContext dctx, Map context) { 69 GenericDelegator delegator = dctx.getDelegator(); 70 Properties props = getProperties(context); 71 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 72 vl.reload(); 73 74 Boolean kekOnly = context.get("kekOnly") != null ? (Boolean ) context.get("kekOnly") : new Boolean (false); 75 String kekTest = (String ) context.get("kekTest"); 76 Debug.log("KEK Only : " + kekOnly.booleanValue(), module); 77 78 StringBuffer buf = vl.outputKeyCreation(kekOnly.booleanValue(), kekTest); 79 String output = buf.toString(); 80 Debug.log(":: Key Generation Output ::\n\n" + output, module); 81 82 Map result = ServiceUtil.returnSuccess(); 83 result.put("output", output); 84 return result; 85 } 86 87 public static Map testKekEncryption(DispatchContext dctx, Map context) { 89 GenericDelegator delegator = dctx.getDelegator(); 90 Properties props = getProperties(context); 92 93 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 95 vl.reload(); 96 97 String testString = (String ) context.get("kekTest"); 98 Integer mode = (Integer ) context.get("mode"); 99 byte[] testBytes = StringUtil.fromHexString(testString); 100 101 byte[] testEncryption = new byte[0]; 103 String desc = ""; 104 105 if (mode.intValue() == 1) { 106 testEncryption = vl.encryptViaKek(testBytes); 108 desc = "Encrypted"; 109 } else { 110 testEncryption = vl.decryptViaKek(testBytes); 112 desc = "Decrypted"; 113 } 114 115 StringBuffer buf = new StringBuffer (); 117 buf.append("======== Begin Test String (" + testString.length() + ") ========\n"); 118 buf.append(testString + "\n"); 119 buf.append("======== End Test String ========\n\n"); 120 121 buf.append("======== Begin Test Bytes (" + testBytes.length + ") ========\n"); 122 buf.append(StringUtil.toHexString(testBytes) + "\n"); 123 buf.append("======== End Test Bytes ========\n\n"); 124 125 buf.append("======== Begin Test Bytes " + desc + " (" + testEncryption.length + ") ========\n"); 126 buf.append(StringUtil.toHexString(testEncryption) + "\n"); 127 buf.append("======== End Test Bytes " + desc + " ========\n\n"); 128 129 String output = buf.toString(); 130 Debug.log(":: KEK Test Output ::\n\n" + output, module); 131 132 Map result = ServiceUtil.returnSuccess(); 133 result.put("output", output); 134 return result; 135 } 136 137 public static Map assignWorkingKey(DispatchContext dctx, Map context) { 139 GenericDelegator delegator = dctx.getDelegator(); 140 GenericValue userLogin = (GenericValue) context.get("userLogin"); 141 Properties props = getProperties(context); 142 143 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 145 vl.reload(); 146 147 byte[] mwk = null; 149 150 String desHexString = (String ) context.get("desHexString"); 152 if (desHexString == null || desHexString.length() == 0) { 153 mwk = vl.generateMwk(); 154 } else { 155 mwk = vl.generateMwk(StringUtil.fromHexString(desHexString)); 156 } 157 158 String mwkHex = StringUtil.toHexString(vl.encryptViaKek(mwk)); 160 161 Map request = vl.getInitialRequestMap(context); 163 request.put("Interface", "Encrypt"); 164 request.put("EncryptKey", mwkHex); 165 request.put("EncryptID", new Long (vl.getWorkingKeyIndex().longValue() + 1)); 166 167 Map response = null; 169 try { 170 response = vl.send(request); 171 } catch(HttpClientException e) { 172 Debug.logError(e, "Problem communicating with VL"); 173 return ServiceUtil.returnError("Unable to update MWK"); 174 } 175 Debug.log("Response : " + response, module); 176 177 if (response != null) { 179 String responseCode = (String ) response.get("responsecode"); 180 if (responseCode.equals("00")) { 181 GenericValue vlKeys = GenericValue.create(vl.getGenericValue()); 182 vlKeys.set("lastWorkingKey", vlKeys.get("workingKey")); 183 vlKeys.set("workingKey", StringUtil.toHexString(mwk)); 184 vlKeys.set("workingKeyIndex", request.get("EncryptID")); 185 vlKeys.set("lastModifiedDate", UtilDateTime.nowTimestamp()); 186 vlKeys.set("lastModifiedByUserLogin", userLogin != null ? userLogin.get("userLoginId") : null); 187 try { 188 vlKeys.store(); 189 } catch (GenericEntityException e) { 190 Debug.logError(e, "Unable to store updated keys; the keys were changed with ValueLink : " + vlKeys, module); 191 return ServiceUtil.returnError("Unable to store updated keys"); 192 } 193 vl.reload(); 194 return ServiceUtil.returnSuccess(); 195 } else { 196 return ServiceUtil.returnError("Transaction failed with response code : " + responseCode); 197 } 198 } else { 199 return ServiceUtil.returnError("Recevied back an empty response"); 200 } 201 } 202 203 public static Map activate(DispatchContext dctx, Map context) { 204 GenericDelegator delegator = dctx.getDelegator(); 205 Properties props = getProperties(context); 206 String vlPromoCode = (String ) context.get("vlPromoCode"); 207 String cardNumber = (String ) context.get("cardNumber"); 208 String pin = (String ) context.get("pin"); 209 String currency = (String ) context.get("currency"); 210 String orderId = (String ) context.get("orderId"); 211 String partyId = (String ) context.get("partyId"); 212 Double amount = (Double ) context.get("amount"); 213 214 String iFace = (String ) context.get("Interface"); 216 217 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 219 Map request = vl.getInitialRequestMap(context); 220 request.put("Interface", iFace != null ? iFace : "Activate"); 221 if (vlPromoCode != null && vlPromoCode.length() > 0) { 222 request.put("PromoCode", vlPromoCode); 223 } 224 request.put("Amount", vl.getAmount(amount)); 225 request.put("LocalCurr", vl.getCurrency(currency)); 226 227 if (cardNumber != null && cardNumber.length() > 0) { 228 request.put("CardNo", cardNumber); 229 } 230 if (pin != null && pin.length() > 0) { 231 request.put("PIN", vl.encryptPin(pin)); 232 } 233 234 if (orderId != null && orderId.length() > 0) { 236 request.put("User1", orderId); 237 } 238 239 if (partyId != null && partyId.length() > 0) { 241 request.put("User2", partyId); 242 } 243 244 setTimeoutReversal(dctx, context, request); 246 247 Map response = null; 249 try { 250 response = vl.send(request); 251 } catch(HttpClientException e) { 252 Debug.logError(e, "Problem communicating with VL"); 253 return ServiceUtil.returnError("Unable to activate gift card"); 254 } 255 256 if (response != null) { 257 String responseCode = (String ) response.get("responsecode"); 258 Map result = ServiceUtil.returnSuccess(); 259 if (responseCode.equals("00")) { 260 result.put("processResult", new Boolean (true)); 261 result.put("pin", vl.decryptPin((String ) response.get("pin"))); 262 } else { 263 result.put("processResult", new Boolean (false)); 264 result.put("pin", response.get("PIN")); 265 } 266 result.put("responseCode", responseCode); 267 result.put("authCode", response.get("authcode")); 268 result.put("cardNumber", response.get("cardno")); 269 result.put("amount", vl.getAmount((String ) response.get("currbal"))); 270 result.put("expireDate", response.get("expiredate")); 271 result.put("cardClass", response.get("cardclass")); 272 result.put("referenceNum", response.get("traceno")); 273 Debug.log("Activate Result : " + result, module); 274 return result; 275 } else { 276 return ServiceUtil.returnError("Empty response returned from ValueLink"); 277 } 278 } 279 280 public static Map linkPhysicalCard(DispatchContext dctx, Map context) { 281 GenericDelegator delegator = dctx.getDelegator(); 282 Properties props = getProperties(context); 283 String virtualCard = (String ) context.get("virtualCard"); 284 String virtualPin = (String ) context.get("virtualPin"); 285 String physicalCard = (String ) context.get("physicalCard"); 286 String physicalPin = (String ) context.get("physicalPin"); 287 String partyId = (String ) context.get("partyId"); 288 289 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 291 Map request = vl.getInitialRequestMap(context); 292 request.put("Interface", "Link"); 293 request.put("VCardNo", virtualCard); 294 request.put("VPIN", vl.encryptPin(virtualPin)); 295 request.put("PCardNo", physicalCard); 296 request.put("PPIN", vl.encryptPin(physicalPin)); 297 298 if (partyId != null && partyId.length() > 0) { 300 request.put("User2", partyId); 301 } 302 303 Map response = null; 305 try { 306 response = vl.send(request); 307 } catch(HttpClientException e) { 308 Debug.logError(e, "Problem communicating with VL"); 309 return ServiceUtil.returnError("Unable to link gift card(s)"); 310 } 311 312 if (response != null) { 313 String responseCode = (String ) response.get("responsecode"); 314 Map result = ServiceUtil.returnSuccess("Activation of physical card complete."); 315 if (responseCode.equals("00")) { 316 317 result.put("processResult", new Boolean (true)); 318 } else { 319 result.put("processResult", new Boolean (false)); 320 } 321 result.put("responseCode", responseCode); 322 result.put("authCode", response.get("authcode")); 323 result.put("amount", vl.getAmount((String ) response.get("newbal"))); 324 result.put("expireDate", response.get("expiredate")); 325 result.put("cardClass", response.get("cardclass")); 326 result.put("referenceNum", response.get("traceno")); 327 Debug.log("Link Result : " + result, module); 328 return result; 329 } else { 330 return ServiceUtil.returnError("Empty response returned from ValueLink"); 331 } 332 } 333 334 public static Map disablePin(DispatchContext dctx, Map context) { 335 GenericDelegator delegator = dctx.getDelegator(); 336 Properties props = getProperties(context); 337 String cardNumber = (String ) context.get("cardNumber"); 338 String pin = (String ) context.get("pin"); 339 String orderId = (String ) context.get("orderId"); 340 String partyId = (String ) context.get("partyId"); 341 Double amount = (Double ) context.get("amount"); 342 343 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 345 Map request = vl.getInitialRequestMap(context); 346 request.put("Interface", "Disable"); 347 request.put("CardNo", cardNumber); 348 request.put("PIN", vl.encryptPin(pin)); 349 request.put("Amount", vl.getAmount(amount)); 350 351 if (orderId != null && orderId.length() > 0) { 353 request.put("User1", orderId); 354 } 355 356 if (partyId != null && partyId.length() > 0) { 358 request.put("User2", partyId); 359 } 360 361 Map response = null; 363 try { 364 response = vl.send(request); 365 } catch(HttpClientException e) { 366 Debug.logError(e, "Problem communicating with VL"); 367 return ServiceUtil.returnError("Unable to call disble pin"); 368 } 369 370 if (response != null) { 371 String responseCode = (String ) response.get("responsecode"); 372 Map result = ServiceUtil.returnSuccess("PIN disabled."); 373 if (responseCode.equals("00")) { 374 result.put("processResult", new Boolean (true)); 375 } else { 376 result.put("processResult", new Boolean (false)); 377 } 378 result.put("responseCode", responseCode); 379 result.put("balance", vl.getAmount((String ) response.get("currbal"))); 380 result.put("expireDate", response.get("expiredate")); 381 result.put("cardClass", response.get("cardclass")); 382 result.put("referenceNum", response.get("traceno")); 383 Debug.log("Disable Result : " + result, module); 384 return result; 385 } else { 386 return ServiceUtil.returnError("Empty response returned from ValueLink"); 387 } 388 } 389 390 public static Map redeem(DispatchContext dctx, Map context) { 391 GenericDelegator delegator = dctx.getDelegator(); 392 Properties props = getProperties(context); 393 String cardNumber = (String ) context.get("cardNumber"); 394 String pin = (String ) context.get("pin"); 395 String currency = (String ) context.get("currency"); 396 String orderId = (String ) context.get("orderId"); 397 String partyId = (String ) context.get("partyId"); 398 Double amount = (Double ) context.get("amount"); 399 400 String iFace = (String ) context.get("Interface"); 402 403 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 405 Map request = vl.getInitialRequestMap(context); 406 request.put("Interface", iFace != null ? iFace : "Redeem"); 407 request.put("CardNo", cardNumber); 408 request.put("PIN", vl.encryptPin(pin)); 409 request.put("Amount", vl.getAmount(amount)); 410 request.put("LocalCurr", vl.getCurrency(currency)); 411 412 if (orderId != null && orderId.length() > 0) { 414 request.put("User1", orderId); 415 } 416 417 if (partyId != null && partyId.length() > 0) { 419 request.put("User2", partyId); 420 } 421 422 setTimeoutReversal(dctx, context, request); 424 425 Map response = null; 427 try { 428 response = vl.send(request); 429 } catch(HttpClientException e) { 430 Debug.logError(e, "Problem communicating with VL"); 431 return ServiceUtil.returnError("Unable to redeem gift card"); 432 } 433 434 if (response != null) { 435 String responseCode = (String ) response.get("responsecode"); 436 Map result = ServiceUtil.returnSuccess(); 437 if (responseCode.equals("00")) { 438 result.put("processResult", new Boolean (true)); 439 } else { 440 result.put("processResult", new Boolean (false)); 441 } 442 result.put("responseCode", responseCode); 443 result.put("authCode", response.get("authcode")); 444 result.put("previousAmount", vl.getAmount((String ) response.get("prevbal"))); 445 result.put("amount", vl.getAmount((String ) response.get("newbal"))); 446 result.put("expireDate", response.get("expiredate")); 447 result.put("cardClass", response.get("cardclass")); 448 result.put("cashBack", vl.getAmount((String ) response.get("cashback"))); 449 result.put("referenceNum", response.get("traceno")); 450 Debug.log("Redeem Result : " + result, module); 451 return result; 452 } else { 453 return ServiceUtil.returnError("Empty response returned from ValueLink"); 454 } 455 } 456 457 public static Map reload(DispatchContext dctx, Map context) { 458 GenericDelegator delegator = dctx.getDelegator(); 459 Properties props = getProperties(context); 460 String cardNumber = (String ) context.get("cardNumber"); 461 String pin = (String ) context.get("pin"); 462 String currency = (String ) context.get("currency"); 463 String orderId = (String ) context.get("orderId"); 464 String partyId = (String ) context.get("partyId"); 465 Double amount = (Double ) context.get("amount"); 466 467 String iFace = (String ) context.get("Interface"); 469 470 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 472 Map request = vl.getInitialRequestMap(context); 473 request.put("Interface", iFace != null ? iFace : "Reload"); 474 request.put("CardNo", cardNumber); 475 request.put("PIN", vl.encryptPin(pin)); 476 request.put("Amount", vl.getAmount(amount)); 477 request.put("LocalCurr", vl.getCurrency(currency)); 478 479 if (orderId != null && orderId.length() > 0) { 481 request.put("User1", orderId); 482 } 483 484 if (partyId != null && partyId.length() > 0) { 486 request.put("User2", partyId); 487 } 488 489 setTimeoutReversal(dctx, context, request); 491 492 Map response = null; 494 try { 495 response = vl.send(request); 496 } catch(HttpClientException e) { 497 Debug.logError(e, "Problem communicating with VL"); 498 return ServiceUtil.returnError("Unable to reload gift card"); 499 } 500 501 if (response != null) { 502 String responseCode = (String ) response.get("responsecode"); 503 Map result = ServiceUtil.returnSuccess(); 504 if (responseCode.equals("00")) { 505 result.put("processResult", new Boolean (true)); 506 } else { 507 result.put("processResult", new Boolean (false)); 508 } 509 result.put("responseCode", responseCode); 510 result.put("authCode", response.get("authcode")); 511 result.put("previousAmount", vl.getAmount((String ) response.get("prevbal"))); 512 result.put("amount", vl.getAmount((String ) response.get("newbal"))); 513 result.put("expireDate", response.get("expiredate")); 514 result.put("cardClass", response.get("cardclass")); 515 result.put("referenceNum", response.get("traceno")); 516 Debug.log("Reload Result : " + result, module); 517 return result; 518 } else { 519 return ServiceUtil.returnError("Empty response returned from ValueLink"); 520 } 521 } 522 523 public static Map balanceInquire(DispatchContext dctx, Map context) { 524 GenericDelegator delegator = dctx.getDelegator(); 525 Properties props = getProperties(context); 526 String cardNumber = (String ) context.get("cardNumber"); 527 String pin = (String ) context.get("pin"); 528 String currency = (String ) context.get("currency"); 529 String orderId = (String ) context.get("orderId"); 530 String partyId = (String ) context.get("partyId"); 531 532 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 534 Map request = vl.getInitialRequestMap(context); 535 request.put("Interface", "Balance"); 536 request.put("CardNo", cardNumber); 537 request.put("PIN", vl.encryptPin(pin)); 538 request.put("LocalCurr", vl.getCurrency(currency)); 539 540 if (orderId != null && orderId.length() > 0) { 542 request.put("User1", orderId); 543 } 544 545 if (partyId != null && partyId.length() > 0) { 547 request.put("User2", partyId); 548 } 549 550 Map response = null; 552 try { 553 response = vl.send(request); 554 } catch(HttpClientException e) { 555 Debug.logError(e, "Problem communicating with VL"); 556 return ServiceUtil.returnError("Unable to call balance inquire"); 557 } 558 559 if (response != null) { 560 String responseCode = (String ) response.get("responsecode"); 561 Map result = ServiceUtil.returnSuccess(); 562 if (responseCode.equals("00")) { 563 result.put("processResult", new Boolean (true)); 564 } else { 565 result.put("processResult", new Boolean (false)); 566 } 567 result.put("responseCode", responseCode); 568 result.put("balance", vl.getAmount((String ) response.get("currbal"))); 569 result.put("expireDate", response.get("expiredate")); 570 result.put("cardClass", response.get("cardclass")); 571 result.put("referenceNum", response.get("traceno")); 572 Debug.log("Balance Result : " + result, module); 573 return result; 574 } else { 575 return ServiceUtil.returnError("Empty response returned from ValueLink"); 576 } 577 } 578 579 public static Map transactionHistory(DispatchContext dctx, Map context) { 580 GenericDelegator delegator = dctx.getDelegator(); 581 Properties props = getProperties(context); 582 String cardNumber = (String ) context.get("cardNumber"); 583 String pin = (String ) context.get("pin"); 584 String orderId = (String ) context.get("orderId"); 585 String partyId = (String ) context.get("partyId"); 586 587 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 589 Map request = vl.getInitialRequestMap(context); 590 request.put("Interface", "History"); 591 request.put("CardNo", cardNumber); 592 request.put("PIN", vl.encryptPin(pin)); 593 594 if (orderId != null && orderId.length() > 0) { 596 request.put("User1", orderId); 597 } 598 599 if (partyId != null && partyId.length() > 0) { 601 request.put("User2", partyId); 602 } 603 604 Map response = null; 606 try { 607 response = vl.send(request); 608 } catch(HttpClientException e) { 609 Debug.logError(e, "Problem communicating with VL"); 610 return ServiceUtil.returnError("Unable to call history inquire"); 611 } 612 613 if (response != null) { 614 String responseCode = (String ) response.get("responsecode"); 615 Map result = ServiceUtil.returnSuccess(); 616 if (responseCode.equals("00")) { 617 result.put("processResult", new Boolean (true)); 618 } else { 619 result.put("processResult", new Boolean (false)); 620 } 621 result.put("responseCode", responseCode); 622 result.put("balance", vl.getAmount((String ) response.get("currbal"))); 623 result.put("history", response.get("history")); 624 result.put("expireDate", response.get("expiredate")); 625 result.put("cardClass", response.get("cardclass")); 626 result.put("referenceNum", response.get("traceno")); 627 Debug.log("History Result : " + result, module); 628 return result; 629 } else { 630 return ServiceUtil.returnError("Empty response returned from ValueLink"); 631 } 632 } 633 634 public static Map refund(DispatchContext dctx, Map context) { 635 GenericDelegator delegator = dctx.getDelegator(); 636 Properties props = getProperties(context); 637 String cardNumber = (String ) context.get("cardNumber"); 638 String pin = (String ) context.get("pin"); 639 String currency = (String ) context.get("currency"); 640 String orderId = (String ) context.get("orderId"); 641 String partyId = (String ) context.get("partyId"); 642 Double amount = (Double ) context.get("amount"); 643 644 String iFace = (String ) context.get("Interface"); 646 647 ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props); 649 Map request = vl.getInitialRequestMap(context); 650 request.put("Interface", iFace != null ? iFace : "Refund"); 651 request.put("CardNo", cardNumber); 652 request.put("PIN", vl.encryptPin(pin)); 653 request.put("Amount", vl.getAmount(amount)); 654 request.put("LocalCurr", vl.getCurrency(currency)); 655 656 if (orderId != null && orderId.length() > 0) { 658 request.put("User1", orderId); 659 } 660 661 if (partyId != null && partyId.length() > 0) { 663 request.put("User2", partyId); 664 } 665 666 setTimeoutReversal(dctx, context, request); 668 669 Map response = null; 671 try { 672 response = vl.send(request); 673 } catch(HttpClientException e) { 674 Debug.logError(e, "Problem communicating with VL"); 675 return ServiceUtil.returnError("Unable to refund gift card"); 676 } 677 678 if (response != null) { 679 String responseCode = (String ) response.get("responsecode"); 680 Map result = ServiceUtil.returnSuccess(); 681 if (responseCode.equals("00")) { 682 result.put("processResult", new Boolean (true)); 683 } else { 684 result.put("processResult", new Boolean (false)); 685 } 686 result.put("responseCode", responseCode); 687 result.put("authCode", response.get("authcode")); 688 result.put("previousAmount", vl.getAmount((String ) response.get("prevbal"))); 689 result.put("amount", vl.getAmount((String ) response.get("newbal"))); 690 result.put("expireDate", response.get("expiredate")); 691 result.put("cardClass", response.get("cardclass")); 692 result.put("referenceNum", response.get("traceno")); 693 Debug.log("Refund Result : " + result, module); 694 return result; 695 } else { 696 return ServiceUtil.returnError("Empty response returned from ValueLink"); 697 } 698 } 699 700 public static Map voidRedeem(DispatchContext dctx, Map context) { 701 context.put("Interface", "Redeem/Void"); 702 return redeem(dctx, context); 703 } 704 705 public static Map voidRefund(DispatchContext dctx, Map context) { 706 context.put("Interface", "Refund/Void"); 707 return refund(dctx, context); 708 } 709 710 public static Map voidReload(DispatchContext dctx, Map context) { 711 context.put("Interface", "Reload/Void"); 712 return reload(dctx, context); 713 } 714 715 public static Map voidActivate(DispatchContext dctx, Map context) { 716 context.put("Interface", "Activate/Void"); 717 return activate(dctx, context); 718 } 719 720 public static Map timeOutReversal(DispatchContext dctx, Map context) { 721 String vlInterface = (String ) context.get("Interface"); 722 Debug.log("704 Interface : " + vlInterface, module); 723 if (vlInterface != null) { 724 if (vlInterface.startsWith("Activate")) { 725 if (vlInterface.equals("Activate/Rollback")) { 726 return ServiceUtil.returnError("This transaction is not supported by ValueLink"); 727 } 728 return activate(dctx, context); 729 } else if (vlInterface.startsWith("Redeem")) { 730 return redeem(dctx, context); 731 } else if (vlInterface.startsWith("Reload")) { 732 return reload(dctx, context); 733 } else if (vlInterface.startsWith("Refund")) { 734 return refund(dctx, context); 735 } 736 } 737 738 return ServiceUtil.returnError("Not a valid 0704 transaction"); 739 } 740 741 private static void setTimeoutReversal(DispatchContext dctx, Map ctx, Map request) { 743 String vlInterface = (String ) request.get("Interface"); 744 Map context = new HashMap (ctx); 746 747 if (!vlInterface.endsWith("Rollback")) { 749 context.put("Interface", vlInterface + "/Rollback"); 750 } else { 751 return; 753 } 754 755 context.put("MerchTime", request.get("MerchTime")); 757 context.put("TermTxnNo", request.get("TermTxnNo")); 758 759 if (!vlInterface.equals("Activate")) { 761 ServiceXaWrapper xaw = new ServiceXaWrapper(dctx); 763 xaw.setRollbackService("vlTimeOutReversal", context); 764 Debug.log("Set 704 context : " + context, module); 766 try { 767 xaw.enlist(); 768 } catch (XAException e) { 769 Debug.logError(e, "Unable to setup 0704 Timeout Reversal", module); 770 } 771 } 772 } 773 774 private static Properties getProperties(Map context) { 775 String paymentProperties = (String ) context.get("paymentConfig"); 776 if (paymentProperties == null) { 777 paymentProperties = "payment.properties"; 778 } 779 return UtilProperties.getProperties(paymentProperties); 780 } 781 782 783 785 public static Map giftCardProcessor(DispatchContext dctx, Map context) { 786 LocalDispatcher dispatcher = dctx.getDispatcher(); 787 GenericValue userLogin = (GenericValue) context.get("userLogin"); 788 789 GenericValue giftCard = (GenericValue) context.get("giftCard"); 790 GenericValue party = (GenericValue) context.get("billToParty"); 791 String paymentConfig = (String ) context.get("paymentConfig"); 792 String currency = (String ) context.get("currency"); 793 String orderId = (String ) context.get("orderId"); 794 Double amount = (Double ) context.get("processAmount"); 795 796 if (currency == null) { 798 currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); 799 } 800 801 Map redeemCtx = new HashMap (); 802 redeemCtx.put("userLogin", userLogin); 803 redeemCtx.put("paymentConfig", paymentConfig); 804 redeemCtx.put("cardNumber", giftCard.get("cardNumber")); 805 redeemCtx.put("pin", giftCard.get("pinNumber")); 806 redeemCtx.put("currency", currency); 807 redeemCtx.put("orderId", orderId); 808 redeemCtx.put("partyId", party.get("partyId")); 809 redeemCtx.put("amount", amount); 810 811 Map redeemResult = null; 813 try { 814 redeemResult = dispatcher.runSync("redeemGiftCard", redeemCtx); 815 } catch (GenericServiceException e) { 816 Debug.logError(e, "Problem calling the redeem service", module); 817 return ServiceUtil.returnError("Redeem service failed"); 818 } 819 820 Map result = ServiceUtil.returnSuccess(); 821 if (redeemResult != null) { 822 Boolean processResult = (Boolean ) redeemResult.get("processResult"); 823 if (processResult.booleanValue()) { 825 Double previous = (Double ) redeemResult.get("previousAmount"); 826 if (previous == null) previous = new Double (0); 827 Double current = (Double ) redeemResult.get("amount"); 828 if (current == null) current = new Double (0); 829 double redeemed = (((double) Math.round((previous.doubleValue() - current.doubleValue()) * 100)) / 100); 830 Debug.logInfo("Redeemed (" + amount + "): " + redeemed + " / " + previous + " : " + current, module); 831 if (redeemed < amount.doubleValue()) { 832 Map voidResult = null; 834 try { 835 voidResult = dispatcher.runSync("voidRedeemGiftCard", redeemCtx); 836 } catch (GenericServiceException e) { 837 Debug.logError(e, module); 838 } 839 if (ServiceUtil.isError(voidResult)) { 840 return voidResult; 841 } 842 processResult = new Boolean (false); 843 amount = new Double (redeemed); 844 result.put("authMessage", "Gift card did not contain enough funds"); 845 } 846 } 847 result.put("processAmount", amount); 848 result.put("authFlag", redeemResult.get("responseCode")); 849 result.put("authResult", processResult); 850 result.put("captureResult", processResult); 851 result.put("authCode", redeemResult.get("authCode")); 852 result.put("captureCode", redeemResult.get("authCode")); 853 result.put("authRefNum", redeemResult.get("referenceNum")); 854 result.put("captureRefNum", redeemResult.get("referenceNum")); 855 } 856 857 return result; 858 } 859 860 public static Map giftCardRelease(DispatchContext dctx, Map context) { 861 LocalDispatcher dispatcher = dctx.getDispatcher(); 862 GenericValue userLogin = (GenericValue) context.get("userLogin"); 863 864 GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); 865 String paymentConfig = (String ) context.get("paymentConfig"); 866 String currency = (String ) context.get("currency"); 867 Double amount = (Double ) context.get("releaseAmount"); 868 869 String orderId = paymentPref.getString("orderId"); 871 872 GenericValue giftCard = null; 874 try { 875 giftCard = paymentPref.getRelatedOne("GiftCard"); 876 } catch (GenericEntityException e) { 877 Debug.logError("Unable to get GiftCard from OrderPaymentPreference", module); 878 return ServiceUtil.returnError("Unable to locate GiftCard Information"); 879 } 880 881 if (giftCard == null) { 882 return ServiceUtil.returnError("Attempt to release GiftCard payment faild; not a valid GiftCard record"); 883 } 884 885 if (currency == null) { 887 currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); 888 } 889 890 Map redeemCtx = new HashMap (); 891 redeemCtx.put("userLogin", userLogin); 892 redeemCtx.put("paymentConfig", paymentConfig); 893 redeemCtx.put("cardNumber", giftCard.get("cardNumber")); 894 redeemCtx.put("pin", giftCard.get("pinNumber")); 895 redeemCtx.put("currency", currency); 896 redeemCtx.put("orderId", orderId); 897 redeemCtx.put("amount", amount); 898 899 Map redeemResult = null; 901 try { 902 redeemResult = dispatcher.runSync("voidRedeemGiftCard", redeemCtx); 903 } catch (GenericServiceException e) { 904 Debug.logError(e, "Problem calling the redeem service", module); 905 return ServiceUtil.returnError("Redeem service failed"); 906 } 907 908 Map result = ServiceUtil.returnSuccess(); 909 if (redeemResult != null) { 910 Boolean processResult = (Boolean ) redeemResult.get("processResult"); 911 result.put("releaseAmount", redeemResult.get("amount")); 912 result.put("releaseFlag", redeemResult.get("responseCode")); 913 result.put("releaseResult", processResult); 914 result.put("releaseCode", redeemResult.get("authCode")); 915 result.put("releaseRefNum", redeemResult.get("referenceNum")); 916 } 917 918 return result; 919 } 920 921 public static Map giftCardRefund(DispatchContext dctx, Map context) { 922 LocalDispatcher dispatcher = dctx.getDispatcher(); 923 GenericValue userLogin = (GenericValue) context.get("userLogin"); 924 925 GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference"); 926 String paymentConfig = (String ) context.get("paymentConfig"); 927 String currency = (String ) context.get("currency"); 928 Double amount = (Double ) context.get("refundAmount"); 929 930 String orderId = paymentPref.getString("orderId"); 932 933 GenericValue giftCard = null; 935 try { 936 giftCard = paymentPref.getRelatedOne("GiftCard"); 937 } catch (GenericEntityException e) { 938 Debug.logError("Unable to get GiftCard from OrderPaymentPreference", module); 939 return ServiceUtil.returnError("Unable to locate GiftCard Information"); 940 } 941 942 if (giftCard == null) { 943 return ServiceUtil.returnError("Attempt to release GiftCard payment faild; not a valid GiftCard record"); 944 } 945 946 if (currency == null) { 948 currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); 949 } 950 951 Map refundCtx = new HashMap (); 952 refundCtx.put("userLogin", userLogin); 953 refundCtx.put("paymentConfig", paymentConfig); 954 refundCtx.put("cardNumber", giftCard.get("cardNumber")); 955 refundCtx.put("pin", giftCard.get("pinNumber")); 956 refundCtx.put("currency", currency); 957 refundCtx.put("orderId", orderId); 958 refundCtx.put("amount", amount); 959 960 Map redeemResult = null; 962 try { 963 redeemResult = dispatcher.runSync("refundGiftCard", refundCtx); 964 } catch (GenericServiceException e) { 965 Debug.logError(e, "Problem calling the refund service", module); 966 return ServiceUtil.returnError("Refund service failed"); 967 } 968 969 Map result = ServiceUtil.returnSuccess(); 970 if (redeemResult != null) { 971 Boolean processResult = (Boolean ) redeemResult.get("processResult"); 972 result.put("refundAmount", redeemResult.get("amount")); 973 result.put("refundFlag", redeemResult.get("responseCode")); 974 result.put("refundResult", processResult); 975 result.put("refundCode", redeemResult.get("authCode")); 976 result.put("refundRefNum", redeemResult.get("referenceNum")); 977 } 978 979 return result; 980 } 981 982 984 public static Map giftCardPurchase(DispatchContext dctx, Map context) { 985 LocalDispatcher dispatcher = dctx.getDispatcher(); 987 GenericDelegator delegator = dctx.getDelegator(); 988 GenericValue userLogin = (GenericValue) context.get("userLogin"); 989 GenericValue orderItem = (GenericValue) context.get("orderItem"); 990 Locale locale = (Locale ) context.get("locale"); 991 992 String orderId = orderItem.getString("orderId"); 994 995 GenericValue orderHeader = null; 997 try { 998 orderHeader = orderItem.getRelatedOne("OrderHeader"); 999 } catch (GenericEntityException e) { 1000 Debug.logError(e, "Unable to get OrderHeader from OrderItem",module); 1001 return ServiceUtil.returnError("Unable to get OrderHeader from OrderItem"); 1002 } 1003 1004 OrderReadHelper orh = new OrderReadHelper(orderHeader); 1006 1007 String currency = orh.getCurrency(); 1009 1010 if (currency == null) { 1012 currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); 1013 } 1014 1015 String productStoreId = null; 1017 if (orderHeader != null) { 1018 productStoreId = orh.getProductStoreId(); 1019 } 1020 if (productStoreId == null) { 1021 return ServiceUtil.returnError("Unable to process gift card purchase; no productStoreId on OrderHeader : " + orderId); 1022 } 1023 1024 GenericValue paymentSetting = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStoreId, "GIFT_CARD", null, true); 1026 String paymentConfig = null; 1027 if (paymentSetting != null) { 1028 paymentConfig = paymentSetting.getString("paymentPropertiesPath"); 1029 } 1030 if (paymentConfig == null) { 1031 return ServiceUtil.returnError("Unable to get payment configuration file"); 1032 } 1033 1034 GenericValue placingParty = orh.getPlacingParty(); 1036 String partyId = null; 1037 if (placingParty != null) { 1038 partyId = placingParty.getString("partyId"); 1039 } 1040 1041 Double amount = orderItem.getDouble("unitPrice"); 1043 Double quantity = orderItem.getDouble("quantity"); 1044 1045 GenericValue product = null; 1047 try { 1048 product = orderItem.getRelatedOne("Product"); 1049 } catch (GenericEntityException e) { 1050 Debug.logError("Unable to get Product from OrderItem", module); 1051 } 1052 if (product == null) { 1053 return ServiceUtil.returnError("No product associated with OrderItem, cannot fulfill gift card"); 1054 } 1055 1056 GenericValue typeFeature = null; 1058 try { 1059 Map fields = UtilMisc.toMap("productId", product.get("productId"), "productFeatureTypeId", "TYPE"); 1060 List order = UtilMisc.toList("-fromDate"); 1061 List featureAppls = delegator.findByAndCache("ProductFeatureAndAppl", fields, order); 1062 featureAppls = EntityUtil.filterByDate(featureAppls); 1063 typeFeature = EntityUtil.getFirst(featureAppls); 1064 } catch (GenericEntityException e) { 1065 Debug.logError(e, module); 1066 return ServiceUtil.returnError("Unable to get the required feature type TYPE from Product"); 1067 } 1068 if (typeFeature == null) { 1069 return ServiceUtil.returnError("Required feature type TYPE not found for product : " + product.get("productId")); 1070 } 1071 1072 String promoCode = typeFeature.getString("idCode"); 1074 if (promoCode == null || promoCode.length() == 0) { 1075 return ServiceUtil.returnError("Invalid promo code set on idCode field of feature type TYPE"); 1076 } 1077 1078 String surveyId = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.purchase.surveyId"); 1080 1081 GenericValue surveyResponse = null; 1083 try { 1084 Map fields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItem.get("orderItemSeqId"), "surveyId", surveyId); 1085 List order = UtilMisc.toList("-responseDate"); 1086 List responses = delegator.findByAnd("SurveyResponse", fields, order); 1087 surveyResponse = EntityUtil.getFirst(responses); 1089 } catch (GenericEntityException e) { 1090 Debug.logError(e, module); 1091 return ServiceUtil.returnError("Unable to get survey response information; cannot fulfill gift card"); 1092 } 1093 1094 List responseAnswers = null; 1096 try { 1097 responseAnswers = surveyResponse.getRelated("SurveyResponseAnswer"); 1098 } catch (GenericEntityException e) { 1099 Debug.logError(e, module); 1100 return ServiceUtil.returnError("Unable to get survey response answers from survey response; cannot fulfill gift card"); 1101 } 1102 1103 Map answerMap = new HashMap (); 1105 if (responseAnswers != null) { 1106 Iterator rai = responseAnswers.iterator(); 1107 while (rai.hasNext()) { 1108 GenericValue answer = (GenericValue) rai.next(); 1109 GenericValue question = null; 1110 try { 1111 question = answer.getRelatedOne("SurveyQuestion"); 1112 } catch (GenericEntityException e) { 1113 Debug.logError(e, module); 1114 return ServiceUtil.returnError("Unable to get survey question from answer"); 1115 } 1116 if (question != null) { 1117 String desc = question.getString("description"); 1118 String ans = answer.getString("textResponse"); answerMap.put(desc, ans); 1120 } 1121 } 1122 } 1123 1124 String sendToKey = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.purchase.survey.sendToEmail"); 1126 String sendToEmail = (String ) answerMap.get(sendToKey); 1127 1128 String orderEmails = orh.getOrderEmailString(); 1130 String copyMeField = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.purchase.survey.copyMe"); 1131 String copyMeResp = copyMeField != null ? (String ) answerMap.get(copyMeField) : null; 1132 boolean copyMe = (UtilValidate.isNotEmpty(copyMeField) 1133 && UtilValidate.isNotEmpty(copyMeResp) && "true".equalsIgnoreCase(copyMeResp)) ? true : false; 1134 1135 int qtyLoop = quantity.intValue(); 1136 for (int i = 0; i < qtyLoop; i++) { 1137 Map activateCtx = new HashMap (); 1139 activateCtx.put("paymentConfig", paymentConfig); 1140 activateCtx.put("vlPromoCode", promoCode); 1141 activateCtx.put("currency", currency); 1142 activateCtx.put("partyId", partyId); 1143 activateCtx.put("orderId", orderId); 1144 activateCtx.put("amount", amount); 1145 activateCtx.put("userLogin", userLogin); 1146 1147 boolean failure = false; 1148 Map activateResult = null; 1149 try { 1150 activateResult = dispatcher.runSync("activateGiftCard", activateCtx); 1151 } catch (GenericServiceException e) { 1152 Debug.logError(e, "Unable to activate gift card(s)", module); 1153 return ServiceUtil.returnError("Problem running activation service"); 1154 } 1155 1156 Boolean processResult = (Boolean ) activateResult.get("processResult"); 1157 if (activateResult == null || activateResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) { 1158 failure = true; 1159 } 1160 1161 if (!failure) { 1162 ServiceXaWrapper xaw = new ServiceXaWrapper(dctx); 1164 activateCtx.put("cardNumber", activateResult.get("cardNumber")); 1165 activateCtx.put("pin", activateResult.get("pin")); 1166 xaw.setRollbackService("voidActivateGiftCard", activateCtx); 1167 try { 1168 xaw.enlist(); 1169 } catch (XAException e) { 1170 Debug.logError(e, "Unable to setup Activate/Void on error", module); 1171 } 1172 } 1173 1174 Map vlFulFill = new HashMap (); 1176 vlFulFill.put("typeEnumId", "GC_ACTIVATE"); 1177 vlFulFill.put("merchantId", UtilProperties.getPropertyValue(paymentConfig, "payment.valuelink.merchantId")); 1178 vlFulFill.put("partyId", partyId); 1179 vlFulFill.put("orderId", orderId); 1180 vlFulFill.put("orderItemSeqId", orderItem.get("orderItemSeqId")); 1181 vlFulFill.put("surveyResponseId", surveyResponse.get("surveyResponseId")); 1182 vlFulFill.put("cardNumber", activateResult.get("cardNumber")); 1183 vlFulFill.put("pinNumber", activateResult.get("pin")); 1184 vlFulFill.put("amount", activateResult.get("amount")); 1185 vlFulFill.put("responseCode", activateResult.get("responseCode")); 1186 vlFulFill.put("referenceNum", activateResult.get("referenceNum")); 1187 vlFulFill.put("authCode", activateResult.get("authCode")); 1188 vlFulFill.put("userLogin", userLogin); 1189 try { 1190 dispatcher.runAsync("createGcFulFillmentRecord", vlFulFill, true); 1191 } catch (GenericServiceException e) { 1192 Debug.logError(e, module); 1193 return ServiceUtil.returnError("Unable to store fulfillment info"); 1194 } 1195 1196 if (failure) { 1197 return ServiceUtil.returnError("Activate Failed"); 1198 } 1199 1200 answerMap.put("cardNumber", activateResult.get("cardNumber")); 1202 answerMap.put("pinNumber", activateResult.get("pin")); 1203 answerMap.put("amount", activateResult.get("amount")); 1204 1205 GenericValue productStoreEmail = null; 1207 String emailType = "PRDS_GC_PURCHASE"; 1208 try { 1209 productStoreEmail = delegator.findByPrimaryKey("ProductStoreEmailSetting", UtilMisc.toMap("productStoreId", productStoreId, "emailType", emailType)); 1210 } catch (GenericEntityException e) { 1211 Debug.logError(e, "Unable to get product store email setting for gift card purchase", module); 1212 } 1213 if (productStoreEmail == null) { 1214 Debug.logError("No gift card purchase email setting found for this store; cannot send gift card information", module); 1215 } else { 1216 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("EcommerceUiLabels", locale); 1217 uiLabelMap.addBottomResourceBundle("OrderUiLabels"); 1218 uiLabelMap.addBottomResourceBundle("CommonUiLabels"); 1219 answerMap.put("uiLabelMap", uiLabelMap); 1220 answerMap.put("locale", locale); 1221 1222 String bcc = productStoreEmail.getString("bccAddress"); 1224 if (copyMe) { 1225 if (UtilValidate.isNotEmpty(bcc)) { 1226 bcc = bcc + "," + orderEmails; 1227 } else { 1228 bcc = orderEmails; 1229 } 1230 } 1231 1232 Map emailCtx = new HashMap (); 1233 String bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation"); 1234 if (UtilValidate.isEmpty(bodyScreenLocation)) { 1235 bodyScreenLocation = ProductStoreWorker.getDefaultProductStoreEmailScreenLocation(emailType); 1236 } 1237 emailCtx.put("bodyScreenUri", bodyScreenLocation); 1238 emailCtx.put("bodyParameters", answerMap); 1239 emailCtx.put("sendTo", sendToEmail); 1240 emailCtx.put("contentType", productStoreEmail.get("contentType")); 1241 emailCtx.put("sendFrom", productStoreEmail.get("fromAddress")); 1242 emailCtx.put("sendCc", productStoreEmail.get("ccAddress")); 1243 emailCtx.put("sendBcc", bcc); 1244 emailCtx.put("subject", productStoreEmail.getString("subject")); 1245 emailCtx.put("userLogin", userLogin); 1246 1247 try { 1249 dispatcher.runAsync("sendMailFromScreen", emailCtx); 1250 } catch (GenericServiceException e) { 1251 Debug.logError(e, "Problem sending mail", module); 1252 return ServiceUtil.returnError("Error sending Gift Card notice email: " + e.toString()); 1254 } 1255 } 1256 } 1257 1258 return ServiceUtil.returnSuccess(); 1259 } 1260 1261 public static Map giftCardReload(DispatchContext dctx, Map context) { 1262 LocalDispatcher dispatcher = dctx.getDispatcher(); 1264 GenericDelegator delegator = dctx.getDelegator(); 1265 GenericValue userLogin = (GenericValue) context.get("userLogin"); 1266 GenericValue orderItem = (GenericValue) context.get("orderItem"); 1267 Locale locale = (Locale ) context.get("locale"); 1268 1269 String orderId = orderItem.getString("orderId"); 1271 1272 GenericValue orderHeader = null; 1274 try { 1275 orderHeader = orderItem.getRelatedOne("OrderHeader"); 1276 } catch (GenericEntityException e) { 1277 Debug.logError(e, "Unable to get OrderHeader from OrderItem",module); 1278 return ServiceUtil.returnError("Unable to get OrderHeader from OrderItem"); 1279 } 1280 1281 OrderReadHelper orh = new OrderReadHelper(orderHeader); 1283 1284 String currency = orh.getCurrency(); 1286 1287 if (currency == null) { 1289 currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD"); 1290 } 1291 1292 String productStoreId = null; 1294 if (orderHeader != null) { 1295 productStoreId = orh.getProductStoreId(); 1296 } 1297 if (productStoreId == null) { 1298 return ServiceUtil.returnError("Unable to process gift card reload; no productStoreId on OrderHeader : " + orderId); 1299 } 1300 1301 GenericValue paymentSetting = ProductStoreWorker.getProductStorePaymentSetting(delegator, productStoreId, "GIFT_CARD", null, true); 1303 String paymentConfig = null; 1304 if (paymentSetting != null) { 1305 paymentConfig = paymentSetting.getString("paymentPropertiesPath"); 1306 } 1307 if (paymentConfig == null) { 1308 return ServiceUtil.returnError("Unable to get payment configuration file"); 1309 } 1310 1311 GenericValue placingParty = orh.getPlacingParty(); 1313 String partyId = null; 1314 if (placingParty != null) { 1315 partyId = placingParty.getString("partyId"); 1316 } 1317 1318 Double amount = orderItem.getDouble("unitPrice"); 1320 1321 String surveyId = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.reload.surveyId"); 1323 1324 GenericValue surveyResponse = null; 1326 try { 1327 Map fields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItem.get("orderItemSeqId"), "surveyId", surveyId); 1328 List order = UtilMisc.toList("-responseDate"); 1329 List responses = delegator.findByAnd("SurveyResponse", fields, order); 1330 surveyResponse = EntityUtil.getFirst(responses); 1332 } catch (GenericEntityException e) { 1333 Debug.logError(e, module); 1334 return ServiceUtil.returnError("Unable to get survey response information; cannot fulfill gift card reload"); 1335 } 1336 1337 List responseAnswers = null; 1339 try { 1340 responseAnswers = surveyResponse.getRelated("SurveyResponseAnswer"); 1341 } catch (GenericEntityException e) { 1342 Debug.logError(e, module); 1343 return ServiceUtil.returnError("Unable to get survey response answers from survey response; cannot fulfill gift card reload"); 1344 } 1345 1346 Map answerMap = new HashMap (); 1348 if (responseAnswers != null) { 1349 Iterator rai = responseAnswers.iterator(); 1350 while (rai.hasNext()) { 1351 GenericValue answer = (GenericValue) rai.next(); 1352 GenericValue question = null; 1353 try { 1354 question = answer.getRelatedOne("SurveyQuestion"); 1355 } catch (GenericEntityException e) { 1356 Debug.logError(e, module); 1357 return ServiceUtil.returnError("Unable to get survey question from answer"); 1358 } 1359 if (question != null) { 1360 String desc = question.getString("description"); 1361 String ans = answer.getString("textResponse"); answerMap.put(desc, ans); 1363 } 1364 } 1365 } 1366 1367 String cardNumberKey = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.reload.survey.cardNumber"); 1368 String pinNumberKey = UtilProperties.getPropertyValue(paymentConfig, "payment.giftcert.reload.survey.pinNumber"); 1369 String cardNumber = (String ) answerMap.get(cardNumberKey); 1370 String pinNumber = (String ) answerMap.get(pinNumberKey); 1371 1372 Map reloadCtx = new HashMap (); 1374 reloadCtx.put("paymentConfig", paymentConfig); 1375 reloadCtx.put("currency", currency); 1376 reloadCtx.put("partyId", partyId); 1377 reloadCtx.put("orderId", orderId); 1378 reloadCtx.put("cardNumber", cardNumber); 1379 reloadCtx.put("pin", pinNumber); 1380 reloadCtx.put("amount", amount); 1381 reloadCtx.put("userLogin", userLogin); 1382 1383 Map reloadResult = null; 1384 try { 1385 reloadResult = dispatcher.runSync("reloadGiftCard", reloadCtx); 1386 } catch (GenericServiceException e) { 1387 Debug.logError(e, "Unable to reload gift card", module); 1388 return ServiceUtil.returnError("Problem running reload service"); 1389 } 1390 1391 Map vlFulFill = new HashMap (); 1393 vlFulFill.put("typeEnumId", "GC_RELOAD"); 1394 vlFulFill.put("merchantId", UtilProperties.getPropertyValue(paymentConfig, "payment.valuelink.merchantId")); 1395 vlFulFill.put("partyId", partyId); 1396 vlFulFill.put("orderId", orderId); 1397 vlFulFill.put("orderItemSeqId", orderItem.get("orderItemSeqId")); 1398 vlFulFill.put("surveyResponseId", surveyResponse.get("surveyResponseId")); 1399 vlFulFill.put("cardNumber", cardNumber); 1400 vlFulFill.put("pinNumber", pinNumber); 1401 vlFulFill.put("amount", amount); 1402 vlFulFill.put("responseCode", reloadResult.get("responseCode")); 1403 vlFulFill.put("referenceNum", reloadResult.get("referenceNum")); 1404 vlFulFill.put("authCode", reloadResult.get("authCode")); 1405 vlFulFill.put("userLogin", userLogin); 1406 try { 1407 dispatcher.runAsync("createGcFulFillmentRecord", vlFulFill, true); 1408 } catch (GenericServiceException e) { 1409 Debug.logError(e, module); 1410 return ServiceUtil.returnError("Unable to store fulfillment info"); 1411 } 1412 1413 Boolean processResult = (Boolean ) reloadResult.get("processResult"); 1414 if (reloadResult == null || reloadResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) { 1415 Debug.logError("Reload Failed Need to Refund : " + reloadResult, module); 1416 1417 try { 1419 Map refundCtx = UtilMisc.toMap("orderItem", orderItem, "partyId", partyId, "userLogin", userLogin); 1420 dispatcher.runAsync("refundGcPurchase", refundCtx, null, true, 300, true); 1421 } catch (GenericServiceException e) { 1422 Debug.logError(e, "ERROR! Unable to call create refund service; this failed reload will NOT be refunded", module); 1423 } 1424 1425 String responseCode = "-1"; 1426 if (processResult != null) { 1427 responseCode = (String ) reloadResult.get("responseCode"); 1428 } 1429 if ("17".equals(responseCode)) { 1430 Debug.logError("Error code : " + responseCode + " : Max Balance Exceeded", module); 1431 return ServiceUtil.returnError("Gift Card Reload Failed : Max Balance Exceeded; charges will be refunded"); 1432 } else { 1433 Debug.logError("Error code : " + responseCode + " : Processing Error", module); 1434 return ServiceUtil.returnError("Gift Card Reload Failed : Processing Error; charges will be refunded"); 1435 } 1436 } 1437 1438 answerMap.put("processResult", reloadResult.get("processResult")); 1440 answerMap.put("responseCode", reloadResult.get("responseCode")); 1441 answerMap.put("previousAmount", reloadResult.get("previousAmount")); 1442 answerMap.put("amount", reloadResult.get("amount")); 1443 1444 GenericValue productStoreEmail = null; 1446 String emailType = "PRDS_GC_RELOAD"; 1447 try { 1448 productStoreEmail = delegator.findByPrimaryKey("ProductStoreEmailSetting", UtilMisc.toMap("productStoreId", productStoreId, "emailType", emailType)); 1449 } catch (GenericEntityException e) { 1450 Debug.logError(e, "Unable to get product store email setting for gift card purchase", module); 1451 } 1452 if (productStoreEmail == null) { 1453 Debug.logError("No gift card purchase email setting found for this store; cannot send gift card information", module); 1454 } else { 1455 Map emailCtx = new HashMap (); 1456 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("EcommerceUiLabels", locale); 1457 uiLabelMap.addBottomResourceBundle("OrderUiLabels"); 1458 uiLabelMap.addBottomResourceBundle("CommonUiLabels"); 1459 answerMap.put("uiLabelMap", uiLabelMap); 1460 answerMap.put("locale", locale); 1461 1462 String bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation"); 1463 if (UtilValidate.isEmpty(bodyScreenLocation)) { 1464 bodyScreenLocation = ProductStoreWorker.getDefaultProductStoreEmailScreenLocation(emailType); 1465 } 1466 emailCtx.put("bodyScreenUri", bodyScreenLocation); 1467 emailCtx.put("bodyParameters", answerMap); 1468 emailCtx.put("sendTo", orh.getOrderEmailString()); 1469 emailCtx.put("contentType", productStoreEmail.get("contentType")); 1470 emailCtx.put("sendFrom", productStoreEmail.get("fromAddress")); 1471 emailCtx.put("sendCc", productStoreEmail.get("ccAddress")); 1472 emailCtx.put("sendBcc", productStoreEmail.get("bccAddress")); 1473 emailCtx.put("subject", productStoreEmail.getString("subject")); 1474 emailCtx.put("userLogin", userLogin); 1475 1476 try { 1478 dispatcher.runAsync("sendMailFromScreen", emailCtx); 1479 } catch (GenericServiceException e) { 1480 Debug.logError(e, "Problem sending mail", module); 1481 return ServiceUtil.returnError("Error sending Gift Card notice email: " + e.toString()); 1483 } 1484 } 1485 1486 return ServiceUtil.returnSuccess(); 1487 } 1488} 1489 | Popular Tags |