#include <cstdlib>
#include <cstring>

#include "cfg.h"
#include "clog.h"
#include "coregrade_api.h"
#include "input.h"
#include "paylid.h"
#include "storeface.h"

//*****************************************************************************/
long paylid_getAccountBalance(CVars in, CVars &out)
{
    logfmt( logINFO, "long paylid_getAccountBalance(CVars in, CVars &out)");
    long ret = PHP_API_BAD_PARAM;
    try {
	const int statusSize = 1024;
	char status[statusSize];

	memset(status,32,statusSize-1);status[statusSize] = '\0'; // Prepare/Clean-up status
	long result = 0L;

	result = storeface_getAccountBalance(
	    CfgReadChar("paylid.soap_endpoint"),
	    CfgReadChar("paylid.terminal_id"), 
	    CfgReadChar("paylid.master_key"), 
	    status);

	logfmt(FLOG_MAX, "CHECK INTERRA FUND result= %lu ",result);

	if (result==10000L) {
	    long balance = atol(status);
	    logfmt (FLOG_MAX, "********** AVAL BALANCE=>%ld", balance);
	    ret = PHP_API_OK;
	} else {
	    // we failed to get amount status
	    out["status"] = status;
	}
    } catch (bad_parameter) {
	out["status"] = "Bad parameter";
	ret = PHP_API_BAD_PARAM;
    } catch (...) {
	out["status"] = "Unhandled exception in paylid_getAccountBalance";
	ret = PHP_API_BAD_PARAM;
    }
    logfmt( logINFO, "/long paylid_getAccountBalance(CVars in, CVars &out)");
    return ret;
}

//*****************************************************************************/
long paylid_transferFund(CVars in, CVars &out)
{
    logfmt( logINFO, "long paylid_transferFund(CVars in, CVars &out)");
    long ret = PHP_API_BAD_PARAM;
    try {
// long storeface_transferFund(const char *endpoint, const char *tid, const char *toDestination, const char *key, char *status); 
    } catch (bad_parameter) {
	out["status"] = "Bad parameter";
	ret = PHP_API_BAD_PARAM;
    } catch (...) {
	out["status"] = "Unhandled exception in paylid_transferFund";
	ret = PHP_API_BAD_PARAM;
    }
    logfmt( logINFO, "/long paylid_transferFund(CVars in, CVars &out)");
    return ret;
}

//*****************************************************************************/
long paylid_checkTransferStatus(CVars in, CVars &out)
{
    logfmt( logINFO, "long paylid_checkTransferStatus(CVars in, CVars &out)");
    long ret = PHP_API_BAD_PARAM;
    try {
// long storeface_checkTransferStatus(const char *endpoint, const char *tid, const char *transactionRef, const char *key, char *status);
    } catch (bad_parameter) {
	out["status"] = "Bad parameter";
	ret = PHP_API_BAD_PARAM;
    } catch (...) {
	out["status"] = "Unhandled exception in paylid_checkTransferStatus";
	ret = PHP_API_BAD_PARAM;
    }
    logfmt( logINFO, "/long paylid_checkTransferStatus(CVars in, CVars &out)");
    return ret;
}

//*****************************************************************************/
long paylid_directAirTimeTopUp(CVars in, CVars &out)
{
    logfmt( logINFO, "long paylid_directAirTimeTopUp(CVars in, CVars &out)");
    long ret = PHP_API_BAD_PARAM;
    try {
	const int statusSize = 1024;
	char status[statusSize];

	memset(status,32,statusSize-1);status[statusSize] = '\0'; // Prepare/Clean-up status
	long result = 0L;

		// long storeface_directAirTimeTopUp(const char *endpoint, const char *tid, const char *topupData, const char *key, char *status, long &delivery_status);
/*
	result = storeface_directAirTimeTopUp(
	    CfgReadChar("paylid.soap_endpoint"),
	    CfgReadChar("paylid.terminal_id"), 
	    CfgReadChar("paylid.master_key"),
...data here... 
	    status,
...address of delivery status...);
*/
	logfmt(FLOG_MAX, "CHECK INTERRA FUND result= %lu ",result);

    } catch (bad_parameter) {
	out["status"] = "Bad parameter";
	ret = PHP_API_BAD_PARAM;
    } catch (...) {
	out["status"] = "Unhandled exception in paylid_directAirTimeTopUp";
	ret = PHP_API_BAD_PARAM;
    }
    logfmt( logINFO, "/long paylid_directAirTimeTopUp(CVars in, CVars &out)");
    return ret;
}

//*****************************************************************************/
/*
vi:ts=2
*/
