📄 valuelinkservices.java
字号:
if (orderId != null && orderId.length() > 0) {
request.put("User1", orderId);
}
// user defined field #2
if (partyId != null && partyId.length() > 0) {
request.put("User2", partyId);
}
// send the request
Map response = null;
try {
response = vl.send(request);
} catch(HttpClientException e) {
Debug.logError(e, "Problem communicating with VL");
return ServiceUtil.returnError("Unable to call history inquire");
}
if (response != null) {
String responseCode = (String) response.get("responsecode");
Map result = ServiceUtil.returnSuccess();
if (responseCode.equals("00")) {
result.put("processResult", new Boolean(true));
} else {
result.put("processResult", new Boolean(false));
}
result.put("responseCode", responseCode);
result.put("balance", vl.getAmount((String) response.get("currbal")));
result.put("history", response.get("history"));
result.put("expireDate", response.get("expiredate"));
result.put("cardClass", response.get("cardclass"));
result.put("referenceNum", response.get("traceno"));
Debug.log("History Result : " + result, module);
return result;
} else {
return ServiceUtil.returnError("Empty response returned from ValueLink");
}
}
public static Map refund(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
Properties props = getProperties(context);
String cardNumber = (String) context.get("cardNumber");
String pin = (String) context.get("pin");
String currency = (String) context.get("currency");
String orderId = (String) context.get("orderId");
String partyId = (String) context.get("partyId");
Double amount = (Double) context.get("amount");
// override interface for void/rollback
String iFace = (String) context.get("Interface");
// get an api instance
ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
Map request = vl.getInitialRequestMap(context);
request.put("Interface", iFace != null ? iFace : "Refund");
request.put("CardNo", cardNumber);
request.put("PIN", vl.encryptPin(pin));
request.put("Amount", vl.getAmount(amount));
request.put("LocalCurr", vl.getCurrency(currency));
// user defined field #1
if (orderId != null && orderId.length() > 0) {
request.put("User1", orderId);
}
// user defined field #2
if (partyId != null && partyId.length() > 0) {
request.put("User2", partyId);
}
// set the timeout reversal
setTimeoutReversal(dctx, context, request);
// send the request
Map response = null;
try {
response = vl.send(request);
} catch(HttpClientException e) {
Debug.logError(e, "Problem communicating with VL");
return ServiceUtil.returnError("Unable to refund gift card");
}
if (response != null) {
String responseCode = (String) response.get("responsecode");
Map result = ServiceUtil.returnSuccess();
if (responseCode.equals("00")) {
result.put("processResult", new Boolean(true));
} else {
result.put("processResult", new Boolean(false));
}
result.put("responseCode", responseCode);
result.put("authCode", response.get("authcode"));
result.put("previousAmount", vl.getAmount((String) response.get("prevbal")));
result.put("amount", vl.getAmount((String) response.get("newbal")));
result.put("expireDate", response.get("expiredate"));
result.put("cardClass", response.get("cardclass"));
result.put("referenceNum", response.get("traceno"));
Debug.log("Refund Result : " + result, module);
return result;
} else {
return ServiceUtil.returnError("Empty response returned from ValueLink");
}
}
public static Map voidRedeem(DispatchContext dctx, Map context) {
context.put("Interface", "Redeem/Void");
return redeem(dctx, context);
}
public static Map voidRefund(DispatchContext dctx, Map context) {
context.put("Interface", "Refund/Void");
return refund(dctx, context);
}
public static Map voidReload(DispatchContext dctx, Map context) {
context.put("Interface", "Reload/Void");
return reload(dctx, context);
}
public static Map voidActivate(DispatchContext dctx, Map context) {
context.put("Interface", "Activate/Void");
return activate(dctx, context);
}
public static Map timeOutReversal(DispatchContext dctx, Map context) {
String vlInterface = (String) context.get("Interface");
Debug.log("704 Interface : " + vlInterface, module);
if (vlInterface != null) {
if (vlInterface.startsWith("Activate")) {
if (vlInterface.equals("Activate/Rollback")) {
return ServiceUtil.returnError("This transaction is not supported by ValueLink");
}
return activate(dctx, context);
} else if (vlInterface.startsWith("Redeem")) {
return redeem(dctx, context);
} else if (vlInterface.startsWith("Reload")) {
return reload(dctx, context);
} else if (vlInterface.startsWith("Refund")) {
return refund(dctx, context);
}
}
return ServiceUtil.returnError("Not a valid 0704 transaction");
}
// 0704 Timeout Reversal (Supports - Activate/Void, Redeem, Redeem/Void, Reload, Reload/Void, Refund, Refund/Void)
private static void setTimeoutReversal(DispatchContext dctx, Map ctx, Map request) {
String vlInterface = (String) request.get("Interface");
// clone the context
Map context = new HashMap(ctx);
// append the rollback interface
if (vlInterface.endsWith("Rollback")) {
context.put("Interface", vlInterface);
} else {
context.put("Interface", vlInterface + "/Rollback");
}
// set the old tx time and number
context.put("MerchTime", request.get("MerchTime"));
context.put("TermTxnNo", request.get("TermTxnNo"));
// Activate/Rollback is not supported by valuelink
if (!vlInterface.equals("Activate")) {
// create the listener
ServiceXaWrapper xaw = new ServiceXaWrapper(dctx);
xaw.setRollbackService("vlTimeOutReversal", context);
//xaw.setCommitService("vlTimeOutReversal", context);
Debug.log("Set 704 context : " + context, module);
try {
xaw.enlist();
} catch (XAException e) {
Debug.logError(e, "Unable to setup 0704 Timeout Reversal", module);
}
}
}
private static Properties getProperties(Map context) {
String paymentProperties = (String) context.get("paymentConfig");
if (paymentProperties == null) {
paymentProperties = "payment.properties";
}
return UtilProperties.getProperties(paymentProperties);
}
// payment processing wrappers (process/release/refund)
public static Map giftCardProcessor(DispatchContext dctx, Map context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
GenericValue giftCard = (GenericValue) context.get("giftCard");
GenericValue person = (GenericValue) context.get("contactPerson");
String paymentConfig = (String) context.get("paymentConfig");
String currency = (String) context.get("currency");
String orderId = (String) context.get("orderId");
Double amount = (Double) context.get("processAmount");
// make sure we have a currency
if (currency == null) {
currency = UtilProperties.getPropertyValue("general.properties", "currency.uom.id.default", "USD");
}
Map redeemCtx = new HashMap();
redeemCtx.put("userLogin", userLogin);
redeemCtx.put("paymentConfig", paymentConfig);
redeemCtx.put("cardNumber", giftCard.get("cardNumber"));
redeemCtx.put("pin", giftCard.get("pinNumber"));
redeemCtx.put("currency", currency);
redeemCtx.put("orderId", orderId);
redeemCtx.put("partyId", person.get("partyId"));
redeemCtx.put("amount", amount);
// invoke the redeem service
Map redeemResult = null;
try {
redeemResult = dispatcher.runSync("redeemGiftCard", redeemCtx);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem calling the redeem service", module);
return ServiceUtil.returnError("Redeem service failed");
}
Map result = ServiceUtil.returnSuccess();
if (redeemResult != null) {
Boolean processResult = (Boolean) redeemResult.get("processResult");
// confirm the amount redeemed; since VL does not error in insufficient funds
if (processResult.booleanValue()) {
Double previous = (Double) redeemResult.get("previousAmount");
if (previous == null) previous = new Double(0);
Double current = (Double) redeemResult.get("amount");
if (current == null) current = new Double(0);
double redeemed = previous.doubleValue() - current.doubleValue();
if (redeemed < amount.doubleValue()) {
// we didn't redeem enough void the transaction and return false
Map voidResult = null;
try {
voidResult = dispatcher.runSync("voidRedeemGiftCard", redeemCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
if (ServiceUtil.isError(voidResult)) {
return voidResult;
}
processResult = new Boolean(false);
amount = new Double(redeemed);
result.put("authMessage", "Gift card did not contain enough funds");
}
}
result.put("processAmount", amount);
result.put("authFlag", redeemResult.get("responseCode"));
result.put("authResult", processResult);
result.put("captureResult", processResult);
result.put("authCode", redeemResult.get("authCode"));
result.put("captureCode", redeemResult.get("authCode"));
result.put("authRefNum", redeemResult.get("referenceNum"));
result.put("captureRefNum", redeemResult.get("referenceNum"));
}
return result;
}
public static Map giftCardRelease(DispatchContext dctx, Map context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
String paymentConfig = (String) context.get("paymentConfig");
String currency = (String) context.get("currency");
Double amount = (Double) context.get("releaseAmount");
// get the orderId for tracking
String orderId = paymentPref.getString("orderId");
// get the GiftCard VO
GenericValue giftCard = null;
try {
giftCard = paymentPref.getRelatedOne("GiftCard");
} catch (GenericEntityException e) {
Debug.logError("Unable to get GiftCard from OrderPaymentPreference", module);
return ServiceUtil.returnError("Unable to locate GiftCard Information");
}
if (giftCard == null) {
return ServiceUtil.returnError("Attempt to release GiftCard payment faild; not a valid GiftCard record");
}
// make sure we have a currency
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -