📄 valuelinkservices.java
字号:
/*
* $Id: ValueLinkServices.java,v 1.5 2004/03/12 21:53:57 ajzeneski Exp $
*
* Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.ofbiz.accounting.thirdparty.valuelink;
import java.util.*;
import org.ofbiz.service.*;
import org.ofbiz.base.util.*;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.product.store.ProductStoreWorker;
import org.ofbiz.order.order.OrderReadHelper;
import javax.transaction.xa.XAException;
/**
* ValueLinkServices - Integration with ValueLink Gift Cards
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.5 $
* @since 3.0
*/
public class ValueLinkServices {
public static final String module = ValueLinkServices.class.getName();
// generate/display new public/private/kek keys
public static Map createKeys(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
Properties props = getProperties(context);
ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
vl.reload();
Boolean kekOnly = context.get("kekOnly") != null ? (Boolean) context.get("kekOnly") : new Boolean(false);
String kekTest = (String) context.get("kekTest");
Debug.log("KEK Only : " + kekOnly.booleanValue(), module);
StringBuffer buf = vl.outputKeyCreation(kekOnly.booleanValue(), kekTest);
String output = buf.toString();
Debug.log(":: Key Generation Output ::\n\n" + output, module);
Map result = ServiceUtil.returnSuccess();
result.put("output", output);
return result;
}
// test the KEK encryption
public static Map testKekEncryption(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Properties props = getProperties(context);
// get an api instance
ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
vl.reload();
String testString = (String) context.get("kekTest");
Integer mode = (Integer) context.get("mode");
byte[] testBytes = StringUtil.fromHexString(testString);
// place holder
byte[] testEncryption = new byte[0];
String desc = "";
if (mode.intValue() == 1) {
// encrypt the test bytes
testEncryption = vl.encryptViaKek(testBytes);
desc = "Encrypted";
} else {
// decrypt the test bytes
testEncryption = vl.decryptViaKek(testBytes);
desc = "Decrypted";
}
// setup the output
StringBuffer buf = new StringBuffer();
buf.append("======== Begin Test String (" + testString.length() + ") ========\n");
buf.append(testString + "\n");
buf.append("======== End Test String ========\n\n");
buf.append("======== Begin Test Bytes (" + testBytes.length + ") ========\n");
buf.append(StringUtil.toHexString(testBytes) + "\n");
buf.append("======== End Test Bytes ========\n\n");
buf.append("======== Begin Test Bytes " + desc + " (" + testEncryption.length + ") ========\n");
buf.append(StringUtil.toHexString(testEncryption) + "\n");
buf.append("======== End Test Bytes " + desc + " ========\n\n");
String output = buf.toString();
Debug.log(":: KEK Test Output ::\n\n" + output, module);
Map result = ServiceUtil.returnSuccess();
result.put("output", output);
return result;
}
// change working key service
public static Map assignWorkingKey(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Properties props = getProperties(context);
// get an api instance
ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
vl.reload();
// place holder
byte[] mwk = null;
// see if we passed in the DES hex string
String desHexString = (String) context.get("desHexString");
if (desHexString == null || desHexString.length() == 0) {
mwk = vl.generateMwk();
} else {
mwk = vl.generateMwk(StringUtil.fromHexString(desHexString));
}
// encrypt the mwk
String mwkHex = StringUtil.toHexString(vl.encryptViaKek(mwk));
// build the request
Map request = vl.getInitialRequestMap(context);
request.put("Interface", "Encrypt");
request.put("EncryptKey", mwkHex);
request.put("EncryptID", new Long(vl.getWorkingKeyIndex().longValue() + 1));
// 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 update MWK");
}
Debug.log("Response : " + response, module);
// on success update the database / reload the cached api
if (response != null) {
String responseCode = (String) response.get("responsecode");
if (responseCode.equals("00")) {
GenericValue vlKeys = new GenericValue(vl.getGenericValue());
vlKeys.set("lastWorkingKey", vlKeys.get("workingKey"));
vlKeys.set("workingKey", StringUtil.toHexString(mwk));
vlKeys.set("workingKeyIndex", request.get("EncryptID"));
vlKeys.set("lastModifiedDate", UtilDateTime.nowTimestamp());
vlKeys.set("lastModifiedByUserLogin", userLogin != null ? userLogin.get("userLoginId") : null);
try {
vlKeys.store();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to store updated keys; the keys were changed with ValueLink : " + vlKeys, module);
return ServiceUtil.returnError("Unable to store updated keys");
}
vl.reload();
return ServiceUtil.returnSuccess();
} else {
return ServiceUtil.returnError("Transaction failed with response code : " + responseCode);
}
} else {
return ServiceUtil.returnError("Recevied back an empty response");
}
}
public static Map activate(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
Properties props = getProperties(context);
String vlPromoCode = (String) context.get("vlPromoCode");
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 : "Activate");
if (vlPromoCode != null && vlPromoCode.length() > 0) {
request.put("PromoCode", vlPromoCode);
}
request.put("Amount", vl.getAmount(amount));
request.put("LocalCurr", vl.getCurrency(currency));
if (cardNumber != null && cardNumber.length() > 0) {
request.put("CardNo", cardNumber);
}
if (pin != null && pin.length() > 0) {
request.put("PIN", vl.encryptPin(pin));
}
// 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 activate 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));
result.put("pin", vl.decryptPin((String) response.get("pin")));
} else {
result.put("processResult", new Boolean(false));
result.put("pin", response.get("PIN"));
}
result.put("responseCode", responseCode);
result.put("authCode", response.get("authcode"));
result.put("cardNumber", response.get("cardno"));
result.put("amount", vl.getAmount((String) response.get("currbal")));
result.put("expireDate", response.get("expiredate"));
result.put("cardClass", response.get("cardclass"));
result.put("referenceNum", response.get("traceno"));
Debug.log("Activate Result : " + result, module);
return result;
} else {
return ServiceUtil.returnError("Empty response returned from ValueLink");
}
}
public static Map linkPhysicalCard(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
Properties props = getProperties(context);
String virtualCard = (String) context.get("virtualCard");
String virtualPin = (String) context.get("virtualPin");
String physicalCard = (String) context.get("physicalCard");
String physicalPin = (String) context.get("physicalPin");
String partyId = (String) context.get("partyId");
// get an api instance
ValueLinkApi vl = ValueLinkApi.getInstance(delegator, props);
Map request = vl.getInitialRequestMap(context);
request.put("Interface", "Link");
request.put("VCardNo", virtualCard);
request.put("VPIN", vl.encryptPin(virtualPin));
request.put("PCardNo", physicalCard);
request.put("PPIN", vl.encryptPin(physicalPin));
// user defined field #2
if (partyId != null && partyId.length() > 0) {
request.put("User2", partyId);
}
// send the request
Map response = null;
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -