<?php
function plugin_quantum_variables()
{
    $variables = array (
        'Plugin Name' => array (
            'type'          => 'hidden',
            'description'   => 'How CE sees this plugin (not to be confused with the Signup Name)',
            'value'         => 'Quantum Gateway'
        ),
        'In Signup' => array (
            'type'          => 'yesno',
            'description'   => 'Select YES if you want to include this payment processor in the signup script.',
            'value'         => '0'
        ),
        'Use Verify By Visa and MasterCard SecureCode'   => array(
            'type'          => 'yesno',
            'description'   => 'Be sure to have these services enabled in your gateway before attempting to use them.',
            'value'         => 0,
        ),
        'Use DialVerify Service' => array(
            'type'          => 'yesno',
            'description'   => 'Be sure to have this service enabled in your gateway before attempting to use it.',
            'value'         => 0,
        ),
        'Use Maxmind' => array(
            'type'          => 'yesno',
            'description'   => 'Use Quantum Gateway\'s Maxmind fraud screening services.',
            'value'         => 0,
        ),
        'Quantum Gateway Username' => array (
            'type'          => 'text',
            'description'   => 'Please enter your Quantum Gateway Username Here.',
            'value'         => ''
        ),
        'Quantum Gateway RestrictKey' => array (
            'type'          => 'password',
            'description'   => 'Please enter your Quantum Gateway RestrictKey Here.',
            'value'         => ''
        ),
        'Accept CC Number' => array (
            'type'          => 'hidden',
            'description'   => 'Selecting YES allows the entering of CC numbers when using this plugin type. No will prevent entering of cc information',
            'value'         =>'1'
        ),
        'Visa' => array (
            'type'          => 'yesno',
            'description'   => 'Select YES to allow Visa card acceptance with this plugin.  No will prevent this card type.',
            'value'         => '1'
        ),
        'MasterCard' => array (
            'type'          => 'yesno',
            'description'   => 'Select YES to allow MasterCard acceptance with this plugin. No will prevent this card type.',
            'value'         => '1'
        ),
        'AmericanExpress' => array (
            'type'          => 'yesno',
            'description'   => 'Select YES to allow American Express card acceptance with this plugin. No will prevent this card type.',
            'value'         => '1'
        ),
        'Discover' => array (
            'type'          => 'yesno',
            'description'   => 'Select YES to allow Discover card acceptance with this plugin. No will prevent this card type.',
            'value'         => '0'
        ),
        'Invoice After Signup' => array (
            'type'          => 'yesno',
            'description'   => 'Select YES if you want an invoice sent to the customer after signup is complete.',
            'value'         => '1'
        ),
        'Signup Name' => array (
            'type'          => 'text',
            'description'   => 'Select the name to display in the signup process for this payment type. Example: eCheck or Credit Card.',
            'value'         => 'credit card'
        ),
        'Payment Form Title' => array(
            'type'          => 'text',
            'description'   => 'The Payment Form will only be displayed when extra user input is required, i.e. when using Verified By Visa, MaterCard SecureCode or DialVerify.',
            'value'         => '',
        ),
        'Payment Form Header' => array(
            'type'          => 'textarea',
            'description'   => 'The Payment Form will only be displayed when extra user input is required, i.e. when using Verified By Visa, MaterCard SecureCode or DialVerify. HTML is accepted.',
            'value'         => '',
        ),
        'Receipt Page Header' => array(
            'type'          => 'textarea',
            'description'   => 'The Receipt Page will only be displayed when extra user input is required, i.e. when using Verified By Visa, MaterCard SecureCode or DialVerify. HTML is accepted.',
            'value'         => '',
        ),
        'Receipt Page Footer' => array(
            'type'          => 'textarea',
            'description'   => 'The Receipt Footer will only be displayed when extra user input is required, i.e. when using Verified By Visa, MaterCard SecureCode or DialVerify. HTML is accepted.',
            'value'         => '',
        ),
        'Dummy Plugin' => array (
            'type'          => 'hidden',
            'description'   => '1 = Only used to specify a billing type for a customer. 0 = full fledged plugin requiring complete functions',
            'value'         => '0'
        ),
        'Auto Payment' => array (
            'type'          => 'hidden',
            'description'   => 'No description',
            'value'         => '1'
        ),
        '30 Day Billing' => array (
            'type'          => 'hidden',
            'description'   => 'Select YES if you want ClientExec to treat monthly billing by 30 day intervals.  If you select NO then the same day will be used to determine intervals.',
            'value'         => '0'
        )
    );

    return $variables;
}

function plugin_quantum_singlepayment($params)
{
    // VbV/MCSC and Dialverify (only used in signup) require some interaction from the user with the provider server, 
    // so if those aren't set up then use autopayment which is silent.
    if (!$params['isSignup'] || (!$params['plugin_quantum_Use Verify By Visa and MasterCard SecureCode'] && !$params['plugin_quantum_Use DialVerify Service'])) {
        return plugin_quantum_autopayment($params);
    }

    $params = _array_map_recursive('escapeHTMLAttributeValue', $params);

    $callbackURL = substr($params['clientExecURL'],-1,1) == '//' ? $params['clientExecURL']."plugins/gateways/quantum/callback.php" : $params['clientExecURL']."/plugins/gateways/quantum/callback.php";
    $CCMo = substr($params['userCCExp'], 0, 2);
    $CCYear = substr($params['userCCExp'], 3);
    $useMaxmind = $params['plugin_quantum_Use Maxmind']? 1 : 2;

    $html = "<html>\n<head>\n</head>\n<body>\n"
           ."<form name=\"frmQuantumGateway\" action=\"https://www.quantumgateway.com/cgi/clientexec.php\" method=\"post\">\n"
           ."<input type=\"hidden\" name=\"gwlogin\" value=\"{$params['plugin_quantum_Quantum Gateway Username']}\">\n"
           ."<input type=\"hidden\" name=\"RestrictKey\" value=\"{$params['plugin_quantum_Quantum Gateway RestrictKey']}\">\n"
           ."<input type=\"hidden\" name=\"post_return_url\" value=\"$callbackURL\">\n"
           ."<input type=\"hidden\" name=\"ID\" value=\"{$params['userID']}\">\n"
           ."<input type=\"hidden\" name=\"customer_ip\" value=\"".getRemoteAddr()."\">\n"
           ."<input type=\"hidden\" name=\"amount\" value=\"{$params['invoiceTotal']}\">\n"
           ."<input type=\"hidden\" name=\"FNAME\" value=\"{$params['userFirstName']}\">\n"
           ."<input type=\"hidden\" name=\"LNAME\" value=\"{$params['userLastName']}\">\n"
           ."<input type=\"hidden\" name=\"BADDR1\" value=\"{$params['userAddress']}\">\n"
           ."<input type=\"hidden\" name=\"BZIP1\" value=\"{$params['userZipcode']}\">\n"
           ."<input type=\"hidden\" name=\"BCITY\" value=\"{$params['userCity']}\">\n"
           ."<input type=\"hidden\" name=\"BCUST_EMAIL\" value=\"{$params['userEmail']}\">\n"
           ."<input type=\"hidden\" name=\"BSTATE\" value=\"{$params['userState']}\">\n"
           ."<input type=\"hidden\" name=\"BCOUNTRY\" value=\"{$params['userCountry']}\">\n"
           ."<input type=\"hidden\" name=\"phone\" value=\"{$params['userPhone']}\">\n"
           ."<input type=\"hidden\" name=\"trans_method\" value=\"CC\">\n"
           ."<input type=\"hidden\" name=\"ccnum\" value=\"{$params['userCCNumber']}\">\n"
           ."<input type=\"hidden\" name=\"ccmo\" value=\"$CCMo\">\n"
           ."<input type=\"hidden\" name=\"ccyr\" value=\"$CCYear\">\n"
           ."<input type=\"hidden\" name=\"invoice_num\" value=\"{$params['invoiceNumber']}\">\n"
           ."<input type=\"hidden\" name=\"invoice_description\" value=\"{$params['invoiceDescription']}\">\n"
           ."<input type=\"hidden\" name=\"MAXMIND\" value=\"$useMaxmind\">\n"
           ."<input type=\"hidden\" name=\"page_heading\" value=\"{$params['plugin_quantum_Payment Form Title']}\">\n"
           ."<input type=\"hidden\" name=\"payment_heading\" value=\"{$params['plugin_quantum_Payment Form Header']}\">\n"
           ."<input type=\"hidden\" name=\"header_receipt\" value=\"{$params['plugin_quantum_Receipt Page Header']}\">\n"
           ."<input type=\"hidden\" name=\"footer_receipt\" value=\"{$params['plugin_quantum_Receipt Page Footer']}\">\n"
           ."<script language=\"JavaScript\">\n"
           ."   document.forms['frmQuantumGateway'].submit();\n"
           ."</script>\n"
           ."</form>\n</body>\n</html>\n";

    echo $html;
    exit;
}

function plugin_quantum_autopayment($params)
{
    require_once 'classes/class.gateway.plugin.php';

    $requestArr = array(
        'gwlogin'               => $params['plugin_quantum_Quantum Gateway Username'],
        'trans_method'          => 'CC',
        'cust_id'               => $params['userID'],
        'customer_ip'           => getRemoteAddr(),
        'invoice_num'           => $params['invoiceNumber'],
        'invoice_description'   => $params['invoiceDescription'],
        'ccnum'                 => $params['userCCNumber'],
        'ccmo'                  => substr($params['userCCExp'], 0, 2),
        'ccyr'                  => substr($params['userCCExp'], 3),
        'amount'                => $params['invoiceTotal'],
        'BADDR1'                => $params['userAddress'],
        'BCITY'                 => $params['userCity'],
        'BSTATE'                => $params['userState'],
        'BCOUNTRY'              => $params['userCountry'],
        'BZIP1'                 => $params['userZipcode'],
        'BCUST_EMAIL'           => $params['userEmail'],
        'phone'                 => $params['userPhone'],
        'RestrictKey'           => $params['plugin_quantum_Quantum Gateway RestrictKey'],
        'FNAME'                 => $params['userFirstName'],
        'LNAME'                 => $params['userLastName'],
        'MAXMIND'               => $params['plugin_quantum_Use Maxmind']? 1 : 2,
        'post_return_url'       => substr($params['clientExecURL'],-1,1) == '//' ? $params['clientExecURL']."plugins/gateways/quantum/callback.php" : $params['clientExecURL']."/plugins/gateways/quantum/callback.php",
    );
    $request = '';
    $firstIteration = true;
    foreach ($requestArr as $var => $value) {
        if (!$firstIteration) {
            $request .= '&';
        }
        $request .= "$var=".urlencode($value);
        $firstIteration = false;
    }

    $response = curlRequest('https://www.quantumgateway.com/cgi/clientexecT.php', $request, false, true);
    $response = explode("|", trim($response));

    $cPlugin = new Plugin($params['invoiceNumber']);

    if ($response[0] != '"APPROVED"') {
        $cPlugin->PaymentRejected("Quantum Gateway payment of {$params['invoiceTotal']} was rejected.");
        return 'Payment rejected by credit card gateway provider';
    }

    $cPlugin->PaymentAccepted($params['invoiceTotal'], "Quantum Gateway payment of {$params['invoiceTotal']} was accepted.", substr($response[1], 1, -1));
    return '';
    
}

function plugin_quantum_credit($params)
{
}

?>
