Взаимодействие BGBilling и CryptoGuard
Скрипт для взаимодействия биллинговой системы BGBilling и CryptoGuard CAS. В настоящее время реализовано только редактирование продуктов у аккаунта. В дальнейшем возможны доработки.
package ru.bitel.bgbilling.modules.tv.dyn.cryptoguard;
import java.net.*;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import ru.bitel.bgbilling.kernel.container.managed.ServerContext;
import ru.bitel.bgbilling.modules.tv.access.TvAccess;
import ru.bitel.bgbilling.modules.tv.access.om.OrderManager;
import ru.bitel.bgbilling.modules.tv.access.om.OrderManagerAdapter;
import ru.bitel.bgbilling.modules.tv.access.om.ProductOrderEvent;
import ru.bitel.bgbilling.modules.tv.api.common.bean.TvDevice;
import ru.bitel.bgbilling.modules.tv.api.common.bean.TvDeviceType;
import ru.bitel.common.ParameterMap;
import ru.bitel.common.sql.ConnectionSet;
import ru.bitel.oss.systems.inventory.product.common.bean.ProductSpec;
import java.net.URLConnection;
public class CryptoGuardOrderManager
extends OrderManagerAdapter
implements OrderManager {
private static final Logger logger = Logger.getLogger(CryptoGuardOrderManager.class);
@Resource(name = "access")
private TvAccess access;
private URL api_url;
private String api_key;
private String send_http(String data)
throws Exception {
URLConnection urlConnection = this.api_url.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
out.write(data); // XML
} finally {
if (out != null) try {
out.close();
} catch (IOException logOrIgnore) {
}
}
String s = "";
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
s += (inputLine);
} finally {
if (in != null) try {
in.close();
} catch (IOException logOrIgnore) {
}
}
return s;
}
@Override
public Object init(ServerContext ctx, int moduleId, TvDevice tvDevice, TvDeviceType tvDeviceType, ParameterMap config)
throws Exception {
super.init(ctx, moduleId, tvDevice, tvDeviceType, config);
logger.info("init");
this.api_url = new URL(config.get("om.url", config.get("cryptoguard.api.url", "")));
this.api_key = config.get("om.api_key", config.get("cryptoguard.api.key", ""));
return null;
}
@Override
public Object destroy()
throws Exception {
return null;
}
private ConnectionSet connectionSet;
@Override
public Object connect(ServerContext ctx)
throws Exception {
super.connect(ctx);
this.connectionSet = ctx.getConnectionSet();
return null;
}
@Override
public Object disconnect(ServerContext ctx)
throws Exception {
if (connectionSet != null) {
connectionSet.recycle();
connectionSet = null;
}
return super.disconnect(ctx);
}
@Override
public Object productsModify(ProductOrderEvent e, ServerContext ctx)
throws Exception {
logger.debug("productsModify");
Date today = new Date();
Date yest = new Date(today.getTime() - (1000 * 60 * 60 * 24));
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
for (ProductSpec productSpec : e.getProductSpecSetToAdd()) {
logger.info("ON " + productSpec.getIdentifier().trim());
String query = String.format(
"<xmldata method=\"addsubscription\" token=\"%s\" cardnumber=\"%s\" artnr=\"%s\" startdate=\"%s\">",
this.api_key, e.getTvAccount().getIdentifier(), productSpec.getIdentifier().trim(), fmt.format(today)
);
logger.info(query);
String res = this.send_http(query);
logger.info(res);
}
for (ProductSpec productSpec : e.getProductSpecSetToRemove()) {
logger.info("OFF " + productSpec.getIdentifier().trim());
String query = String.format(
"<xmldata method=\"stopsubscription\" token=\"%s\" cardnumber=\"%s\" artnr=\"%s\" stopdate=\"%s\">",
this.api_key, e.getTvAccount().getIdentifier(), productSpec.getIdentifier().trim(), fmt.format(yest)
);
logger.info(query);
String res = this.send_http(query);
logger.info(res);
}
return null;
}
}