Example #1
0
function epay_invoice_creation_pre_email($vars)
{
    global $cc_encryption_hash;
    logActivity("epay_invoice_creation_pre_email: " . $vars['invoiceid']);
    //Fetch invoice data
    $values["invoiceid"] = $vars['invoiceid'];
    $invoice_result = localAPI("getinvoice", $values, null);
    //Should this invoice be payed with ePay
    if ($invoice_result['paymentmethod'] == "epay") {
        $gateway = getGatewayVariables("epay");
        //Get client
        $client_query = mysql_query("SELECT id, currency, gatewayid, disableautocc, AES_DECRYPT(cardnum, MD5('" . $cc_encryption_hash . $invoice_result['userid'] . "')) as cardnum FROM tblclients WHERE id = " . $invoice_result['userid']);
        $client_result = mysql_fetch_array($client_query);
        //Set subscription ID
        $subscriptionid = $client_result['gatewayid'];
        //Get currency
        $currency_query = select_query("tblcurrencies", "id, code", array("id" => $client_result['currency']));
        $currency_result = mysql_fetch_array($currency_query);
        $currency_code = convertToEpayCurrency($currency_result["code"]);
        logActivity("epay_invoice_creation_pre_email: " . $currency_code);
        //Process if autocc is not disabled
        if ($client_result['disableautocc'] != "on" && $subscriptionid > 0) {
            $fee = 0;
            logActivity("epay_invoice_creation_pre_email: " . $invoice_result['total'] * 100);
            logActivity("epay_invoice_creation_pre_email: " . $gateway["subscriptionfee"]);
            //Calculate transaction fee
            if ($gateway["subscriptionfee"] == "on") {
                logActivity("epay_invoice_creation_pre_email card: " . $client_result["cardnum"]);
                $epay_params = array();
                $epay_params['merchantnumber'] = $gateway["merchantnumber"];
                $epay_params['cardno_prefix'] = substr($client_result["cardnum"], 0, 6);
                $epay_params['amount'] = $invoice_result['total'] * 100;
                $epay_params['currency'] = $currency_code;
                $epay_params['acquirer'] = "0";
                $epay_params['fee'] = "0";
                $epay_params['cardtype'] = "ALL";
                $epay_params['cardtypetext'] = "-1";
                $epay_params['epayresponse'] = "-1";
                $soap = new SoapClient('https://ssl.ditonlinebetalingssystem.dk/remote/payment.asmx?WSDL');
                $soap_fee_result = $soap->getcardinfo($epay_params);
                $fee = $soap_fee_result->fee;
                logActivity("epay_invoice_creation_pre_email fee: " . $fee);
            }
            //Autorize transaction
            $epay_params = array();
            $epay_params['merchantnumber'] = $gateway["merchantnumber"];
            $epay_params['subscriptionid'] = $subscriptionid;
            $epay_params['orderid'] = $vars['invoiceid'];
            $epay_params['amount'] = $invoice_result['total'] * 100 + $fee;
            $epay_params['currency'] = $currency_code;
            if ($gateway["captureonduedate"] == "no" or array_key_exists("SERVER_ADDR", $_SERVER)) {
                $epay_params['instantcapture'] = "1";
            } else {
                $epay_params['instantcapture'] = "0";
            }
            $epay_params['fraud'] = "0";
            $epay_params['transactionid'] = "-1";
            $epay_params['pbsresponse'] = "-1";
            $epay_params['epayresponse'] = "-1";
            $soap = new SoapClient('https://ssl.ditonlinebetalingssystem.dk/remote/subscription.asmx?WSDL');
            $soap_authorize_result = $soap->authorize($epay_params);
            //Transaction OK
            if ($soap_authorize_result->authorizeResult == true) {
                //Apply fee to invoice
                if ($fee > 0) {
                    $values_fee["invoiceid"] = $vars['invoiceid'];
                    $values_fee["newitemdescription"] = array("Payment Fee");
                    $values_fee["newitemamount"] = array($fee / 100);
                    $values_fee["newitemtaxed"] = array("0");
                    $result_fee = localAPI("updateinvoice", $values_fee, null);
                    logActivity("epay_invoice_creation_pre_email fee result: " . $result_fee["result"]);
                }
                //Add payment to invoice
                $transactionid = $soap_authorize_result->transactionid;
                if ($gateway["captureonduedate"] == "no" or array_key_exists("SERVER_ADDR", $_SERVER)) {
                    addInvoicePayment($vars['invoiceid'], $transactionid, $invoice_result['total'] + $fee / 100, $fee / 100, "epay");
                } else {
                    //Add to capture queue
                    mysql_query("INSERT INTO tblepay (invoiceid, txnid) VALUES ('" . $vars['invoiceid'] . "', " . $transactionid . ")");
                }
            } else {
                //Could not authorize transaction
                //Log and let WHMCS handle further processing
                logActivity("epay_invoice_creation_pre_email: PBS: " . $soap_authorize_result->pbsresponse . " ePay: " . $soap_authorize_result->epayresponse);
            }
        }
    }
}