var _ccp = toolbox.getCreditCardProcessor();
if (_ccp == null)
qsTr("Could not get a credit card processor"));
var _currid = -1;
var _transactions = mywindow.findChild("_transactions");
var _void = mywindow.findChild("_void");
var _url =
new QUrl(metrics.value(
"CCServer"));
if (metrics.value("CCPort") > 0)
_url.
setPort(metrics.value(
"CCPort"));
else
_url.setPort(_ccp.defaultPort());
_netmgr.authenticationRequired.connect(sHandleAuthenticationRequired);
_netmgr.proxyAuthenticationRequired.connect(sHandleProxyAuthenticationRequired);
_netmgr.sslErrors.connect(sHandleSslError);
_netmgr.finished.connect(sHandleResponse);
_void.clicked.connect(sVoid);
_transactions.addColumn("Cust. #", -1, Qt.AlignLeft, true, "cust_number");
_transactions.addColumn("Name", -1, Qt.AlignLeft, true, "cust_name");
_transactions.addColumn("Type", -1, Qt.AlignLeft, true, "type");
_transactions.addColumn("Transaction", -1, Qt.AlignLeft, true, "status");
_transactions.addColumn("Order-Seq.", -1, Qt.AlignRight, true, "docnumber");
_transactions.addColumn("Amount", -1, Qt.AlignRight, true, "ccpay_amount");
_transactions.addColumn("Currency", -1, Qt.AlignLeft, true, "currAbbr");
function sHandleAuthenticationRequired(reply, authenticator)
{
print("sHandleAuthenticationRequired called");
toolbox.listProperties(reply);
toolbox.listProperties(authenticator);
}
function sHandleProxyAuthenticationRequired(proxy, authenticator)
{
print("sHandleProxyAuthenticationRequired called");
toolbox.listProperties(proxy);
toolbox.listProperties(authenticator);
}
function sHandleSslError(reply, errors)
{
print("sHandleSslError called");
toolbox.listProperties(reply);
toolbox.listProperties(errors);
}
function sPopulateTransactions()
{
var params = new Object;
params.preauth = "Preauthorization";
params.charge = "Charge";
params.refund = "Refund/Credit";
params.authorized = "Authorized";
params.approved = "Approved";
params.declined = "Declined/Denied";
params.voided = "Voided";
params.noapproval = "Not Approved";
if (metrics.value("CCValidDays") > 0)
params.ccValidDays = metrics.value("CCValidDays");
else
params.ccValidDays = 7;
_transactions.populate(toolbox.executeDbQuery("ccpayments", "list", params));
}
function APPENDFIELD(request, name, content)
{
if (request != "")
request = request + "&";
return request + name + "=" + content;
}
function sVoid()
{
try {
if (_transactions.currentItem().text("status") == "Voided")
{
qsTr("This transaction cannot be voided a second time."));
return;
}
var params = new Object;
params.ccpayid = _transactions.id();
params.key = mainwindow.key;
var anq = toolbox.executeQuery(
"SELECT ccard_active,"
+ " formatbytea(decrypt(setbytea(ccard_number), setbytea(<? value('key' ?>),'bf')) AS ccard_number,"
+ " formatccnumber(decrypt(setbytea(ccard_number),setbytea(<? value('key' ?>),'bf')) AS ccard_number_x,"
+ " formatbytea(decrypt(setbytea(ccard_name), setbytea(<? value('key' ?>),'bf')) AS ccard_name,"
+ " formatbytea(decrypt(setbytea(ccard_address1), setbytea(<? value('key' ?>),'bf')) AS ccard_address1,"
+ " formatbytea(decrypt(setbytea(ccard_address2), setbytea(<? value('key' ?>),'bf')) AS ccard_address2,"
+ " formatbytea(decrypt(setbytea(ccard_city), setbytea(<? value('key' ?>),'bf')) AS ccard_city,"
+ " formatbytea(decrypt(setbytea(ccard_state), setbytea(<? value('key' ?>),'bf')) AS ccard_state,"
+ " formatbytea(decrypt(setbytea(ccard_zip), setbytea(<? value('key' ?>),'bf')) AS ccard_zip,"
+ " formatbytea(decrypt(setbytea(ccard_country), setbytea(<? value('key' ?>),'bf')) AS ccard_country,"
+ " formatbytea(decrypt(setbytea(ccard_month_expired),setbytea(<? value('key' ?>),'bf')) AS ccard_month_expired,"
+ " formatbytea(decrypt(setbytea(ccard_year_expired),setbytea(<? value('key' ?>), 'bf')) AS ccard_year_expired,"
+ " custinfo.*,"
+ " ccpay.*,"
+ " curr_symbol.*"
+ " FROM ccard "
+ " JOIN custinfo ON (ccard_cust_id=cust_id)"
+ " JOIN ccpay ON (ccard_id=ccpay_ccard_id)"
+ " JOIN curr_symbol ON (curr_id=ccpay_curr_id)"
+ "WHERE (ccpay_id=<? value('ccpayid') ?>);",
params);
if (anq.first())
{
if (! anq.value("ccard_active"))
{
_ccp.errorMsg(-10).replace("%1", anq.value("ccard_number_x")));
return -10;
}
}
else if (anq.lastError().type != 0)
{
qsTr("A database error occurred: %1").arg(anq.lastError().text));
return -1;
}
else
{
return -17;
}
var prequest = "";
_currid = anq.value("ccpay_curr_id");
if (metrics.value("CCANDelim").length > 0)
prequest = APPENDFIELD(prequest, "x_delim_char", metrics.value("CCANDelim"));
prequest = APPENDFIELD(prequest, "x_version", metrics.value("CCANVer"));
prequest = APPENDFIELD(prequest, "x_delim_data", "TRUE");
if (metrics.value("CCANEncap").length > 0)
prequest = APPENDFIELD(prequest, "x_encap_char", metrics.value("CCANEncap"));
prequest = APPENDFIELD(prequest, "x_login", metricsenc.value("CCLogin"));
prequest = APPENDFIELD(prequest, "x_tran_key", metricsenc.value("CCPassword"));
prequest = APPENDFIELD(prequest, "x_amount", _transactions.column("ccpay_amount").text);
prequest = APPENDFIELD(prequest, "x_card_num", anq.value("ccard_number"));
prequest = APPENDFIELD(prequest, "x_test_request", _ccp.isLive() ? "FALSE" : "TRUE");
var work_month = anq.value("ccard_month_expired");
if (work_month < 10 && work_month > 0)
work_month = "0" + work_month;
prequest = APPENDFIELD(prequest, "x_exp_date",
work_month + anq.value("ccard_year_expired") % 100);
prequest = APPENDFIELD(prequest, "x_relay_response", "FALSE");
prequest = APPENDFIELD(prequest, "x_duplicate_window", metrics.value("CCANDuplicateWindow"));
var name = anq.value("ccard_name").split(/\s+/);
prequest = APPENDFIELD(prequest, "x_first_name", name[0]);
prequest = APPENDFIELD(prequest, "x_last_name", name[name.length - 1]);
prequest = APPENDFIELD(prequest, "x_company", anq.value("cust_name"));
prequest = APPENDFIELD(prequest, "x_address", anq.value("ccard_address1"));
prequest = APPENDFIELD(prequest, "x_city", anq.value("ccard_city"));
prequest = APPENDFIELD(prequest, "x_state", anq.value("ccard_state"));
prequest = APPENDFIELD(prequest, "x_zip", anq.value("ccard_zip"));
prequest = APPENDFIELD(prequest, "x_country", anq.value("ccard_country"));
if (metrics.CCANWellsFargoSecureSource)
{
prequest = APPENDFIELD(prequest, "x_phone", anq.value("cust_phone"));
prequest = APPENDFIELD(prequest, "x_email", anq.value("cust_email"));
}
prequest = APPENDFIELD(prequest, "x_currency_code", anq.value("curr_abbr"));
prequest = APPENDFIELD(prequest, "x_method", "CC");
prequest = APPENDFIELD(prequest, "x_type", "VOID");
prequest = APPENDFIELD(prequest, "x_trans_id",
_ccp.isLive() ? anq.value("ccpay_r_ordernum") : 12345);
_transactions.enabled = false;
_void.enabled = false;
netreq.setUrl(_url);
_netmgr.post(netreq, prequest);
}
catch (e)
{
print("sVoid exception at " + e.lineNumber + ": " + e);
_transactions.enabled = true;
_void.enabled = true;
}
}
function sHandleResponse(netresponse)
{
try
{
print("sHandleResponse entered");
if (netresponse.error())
{
qsTr("<p>There was a network error trying to post the "
+ "transaction: %1."
+ "Look up this error code at "
+ " <a>http://doc.trolltech.com/4.4/qnetworkreply.html"
+ "#NetworkError-enum</a>").arg(netresponse.error()));
return;
}
var response = netresponse.readAll().toString();
if (response.match(/^<HTML>/i))
{
response);
return;
}
var field = response.split(metrics.value("CCANDelim").length > 0 ?
metrics.value("CCANDelim") : ",");
var params = new Object;
if (field[0] == 2)
{
qsTr("The void transaction was declined"));
return;
}
else if (field[0] == 3)
{
qsTr("The void transaction received an error: %1")
.arg(field[3]));
return;
}
else if (field[0] == 1)
params.approved = "APPROVED";
else if (field[0] == 4)
params.approved = "HELDFORREVIEW";
else
{
qsTr("The void transaction received an unknown "
+ "approval value: %1").arg(field[0]));
return;
}
params.amount = _transactions.currentItem().rawValue("ccpay_amount");
params.ccpay_id = _transactions.id;
params.currid = _currid;
params.status = "V";
params.auth_charge= "V";
params.type = "V";
params.reforder = _transactions.column("docnumber").text;
params.avs = field[5];
params.ordernum = "";
params.xactionid = field[6];
params.code = field[4];
params.shipping = field[34];
params.tax = field[32];
params.ref = "";
params.message = field[5];
params.auth = false;
var ccq = toolbox.executeQuery(
'UPDATE ccpay SET'
+ '<? if exists("fromcurr") ?>'
+ ' ccpay_amount=ROUND(currToCurr(<? value("fromcurr") ?>,'
+ ' <? value("tocurr") ?>,'
+ ' <? value("amount") ?>,'
+ ' CURRENT_DATE), 2),'
+ ' ccpay_curr_id=<? value("currid") ?>,'
+ '<? else ?>'
+ ' ccpay_amount=ROUND(<? value("amount") ?>, 2),'
+ ' ccpay_curr_id=<? value("currid") ?>,'
+ '<? endif ?>'
+ ' ccpay_auth=<? value("auth") ?>,'
+ ' ccpay_r_approved=<? value("approved") ?>,'
+ ' ccpay_r_avs=<? value("avs") ?>,'
+ ' ccpay_r_code=<? value("code") ?>,'
+ ' ccpay_r_error=<? value("error") ?>,'
+ ' ccpay_r_message=<? value("message") ?>,'
+ ' ccpay_r_ordernum=<? value("ordernum") ?>,'
+ ' ccpay_r_ref=<? value("ref") ?>,'
+ '<? if exists("shipping") ?>'
+ ' ccpay_r_shipping=<? value("shipping") ?>,'
+ '<? endif ?>'
+ '<? if exists("score") ?>'
+ ' ccpay_yp_r_score=<? value("score") ?>,'
+ '<? endif ?>'
+ '<? if exists("tax") ?>'
+ ' ccpay_r_tax=<? value("tax") ?>,'
+ '<? endif ?>'
+ '<? if exists("tdate") ?>'
+ ' ccpay_yp_r_tdate=<? value("tdate") ?>,'
+ '<? endif ?>'
+ '<? if exists("time") ?>'
+ ' ccpay_yp_r_time=<? value("time")?>,'
+ '<? endif ?>'
+ ' ccpay_status=<? value("status") ?>'
+ ' WHERE (ccpay_id=<? value("ccpay_id") ?>);',
params);
if (ccq.lastError().type > 0)
{
qsTr("There was an error querying the database: %1")
.arg(ccq.lastError().text));
return;
}
qsTr("<p>The ccpay table has been updated after "
+ "completing the Void transaction."));
sPopulateTransactions();
}
catch (e)
{
print("sHandleResponse exception at " + e.lineNumber + ": " + e);
}
finally
{
print("sHandleResponse entered finally block");
_transactions.enabled = true;
_void.enabled = (_transactions.id != -1);
}
}
sPopulateTransactions();