<?php
/*****************************************************************/
// function plugin_authnet_variables($params) - required function
/*****************************************************************/
function plugin_authnet_variables() {
    /* Specification
          itemkey     - used to identify variable in your other functions
          type        - text,textarea,yesno,password,hidden ( hiddens are not visable to the user )
          description - description of the variable, displayed in ClientExec
          value       - default value
    */
    $variables = array (
               "Plugin Name" => array (
                                    "type"=>"hidden",
                                    "description"=>"How CE sees this plugin (not to be confused with the Signup Name)",
                                    "value"=>"Authorize.Net"
                                   ),
               "In Signup" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you want to include this payment processor in the signup script.",
                                    "value"=>"0"
                                   ),
               "Authorize.Net Username" => array (
                                    "type"=>"text",
                                    "description"=>"Please enter your Authorize.Net Username Here.",
                                    "value"=>""
                                   ),
               "Authorize.Net Transaction Key" => array (
                                    "type"=>"password",
                                    "description"=>"Please enter your Authorize.Net Transaction Key Here.",
                                    "value"=>""
                                   ),
               "MD5 Hash Value" => array (
                                    "type"=>"password",
                                    "description"=>"Used to verify valid transactions from Authorize.Net.<br>NOTE: This value has to match the value set in the Authorize.Net Merchant Interface - <i>Optional</i>",
                                    "value"=>""
                                   ),
               "Demo Mode" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you want to set this plugin in Demo mode for testing purposes.",
                                    "value"=>"1"
                                   ),
               "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"
                                   ),
               "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_authnet_singlepayment($params) - required function
/*****************************************************************/
function plugin_authnet_singlepayment($params) { // when set to non recurring
    //Function used to provide users with the ability
    //Plugin variables can be accesses via $params["plugin_[pluginname]_[variable]"] (ex. $params["plugin_paypal_UserID"])
    return plugin_authnet_autopayment($params);
}

/********************************************************************/
// function plugin_authnet_credit($params)
/********************************************************************/
function plugin_authnet_credit($params) { // when set to non recurring
    //Function used to provide users with the ability
    //Plugin variables can be accesses via $params["plugin_[pluginname]_[variable]"] (ex. $params["plugin_paypal_UserID"])

    $authnet['login'] = $params["plugin_authnet_Authorize.Net Username"]; //authorize.net Username
    $authnet['tran_key'] = $params["plugin_authnet_Authorize.Net Transaction Key"];     //authorize.net Password

    //Authnet Configuration
    $authnet['version']= "3.1";
    $authnet['method']="CC";
    $authnet['type']="CREDIT";
    if ($params["plugin_authnet_Demo Mode"]==1){ $authnet['test']="True"; }
    else { $authnet['test']="False"; }

    //Email Configuration
    $authnet['merchant_email']= $params["companyBillingEmail"];
    $authnet['email_customer']="False";

    //Environment Configuration
    $authnet['url']="https://secure.authorize.net/gateway/transact.dll"; //authorize.net url (post)

    //check and see if pathCurl is installed on server
    if ($params["pathCurl"]=="")
    {
          $authnet['useLibCurl']="True";
    }else{
          //absolute path to Curl on your system, not using libCurl
          $authnet['curl_location']=$params["pathCurl"];
          $authnet['useLibCurl']="False";
    }

    //Transaction Information
    $authnet['cardnum']=$params["userCCNumber"];
    $authnet['amount']= $params["invoiceTotal"];
    $authnet['cust_id']=$params["userID"];
    $authnet['invoice_num']=$params["invoiceNumber"];
    $authnet['description']=$params["invoiceDescription"];

    // Authnet's library throws a lot of E_NOTICES...
    $errorReporting = error_reporting();
    error_reporting(E_ALL & ~E_NOTICE);
    include('plugins/gateways/authnet/functions.php');
    error_reporting($errorReporting);

    //Hash Value From Authorize.Net
    if ($params["plugin_authnet_MD5 Hash Value"] != ""){
        //      $tHash =  $params["plugin_authnet_MD5 Hash Value"].$params["plugin_authnet_Authorize.Net Username"].$authnet_results["x_trans_id"].sprintf("%01.2f", $params["invoiceTotal"]);
        //      if (strtoupper(md5($tHash))!=strtoupper($authnet_results["x_md5_hash"])) return; //do not approve
    }

    $bolInSignup = false;

    include('plugins/gateways/authnet/callback.php');

    //Return error code
    $tReturnValue = "";
    if (($authnet_results["x_response_code"]==1)||($authnet_results["x_response_code"]=='*1*')){ $tReturnValue = ""; }
    else { $tReturnValue = $authnet_results["x_response_reason_text"]." Code:".$authnet_results["x_response_code"]; }
    return $tReturnValue;
}

/*****************************************************************/
// function plugin_authnet_autopayment($userid) - required function if plugin is autopayment capable
/*****************************************************************/
function plugin_authnet_autopayment($params){
    //Setup authorize.net script
    //Merchant Configuration

    $authnet['login'] = $params["plugin_authnet_Authorize.Net Username"]; //authorize.net Username
    $authnet['tran_key'] = $params["plugin_authnet_Authorize.Net Transaction Key"];     //authorize.net Password

    //Authnet Configuration
    $authnet['version']= "3.1";
    $authnet['method']="CC";
    $authnet['type']="AUTH_CAPTURE";
    if ($params["plugin_authnet_Demo Mode"]==1){ $authnet['test']="True"; }
    else { $authnet['test']="False"; }

    //Email Configuration
    $authnet['merchant_email']= $params["companyBillingEmail"];
    $authnet['email_customer']="False";

    //Environment Configuration
    $authnet['url']="https://secure.authorize.net/gateway/transact.dll"; //authorize.net url (post)

    //check and see if pathCurl is installed on server
    if ($params["pathCurl"]=="")
    {
          $authnet['useLibCurl']="True";
    }else{
          //absolute path to Curl on your system, not using libCurl
          $authnet['curl_location']=$params["pathCurl"];
          $authnet['useLibCurl']="False";
    }

    //Get information from current user
    //Contact Information
    $authnet['firstname']=$params["userFirstName"];
    $authnet['lastname']=$params["userLastName"];
    $authnet['phone']=$params["userPhone"];
    $authnet['address']=$params["userAddress"];
    $authnet['city']=$params["userCity"];
    $authnet['state']=$params["userState"];
    $authnet['zip']=$params["userZipcode"];
    $authnet['country']=$params["userCountry"];
    $authnet['email']=$params["userEmail"];

    // figure out customer IP
    $authnet['customer_ip'] = getRemoteAddr();

    //If organization is populate then it is
    //used as the company name.  Wells Fargo
    //requires organization type. so we base it off
    //the organization name as well.
    if ($params["userOrganization"]==""){;
        $authnet['company']="NA";
        $authnet['organization_type'] = "I";
    }else{
        $authnet['company']=$params["userOrganization"];
        $authnet['organization_type'] = "B";
    }

    //Transaction Information
    $authnet['cardnum']=$params["userCCNumber"];
    $authnet['expdate']=$params["userCCExp"];;  //MM/YYYYY

    $authnet['amount']= $params["invoiceTotal"];
    $authnet['cust_id']=$params["userID"];
    $authnet['invoice_num']=$params["invoiceNumber"];
    $authnet['description']=$params["invoiceDescription"];


    // Authnet's library throws a lot of E_NOTICES...
    $errorReporting = error_reporting();
    error_reporting(E_ALL & ~E_NOTICE);
    include('plugins/gateways/authnet/functions.php');
    error_reporting($errorReporting);

    //Hash Value From Authorize.Net
    if ($params["plugin_authnet_MD5 Hash Value"] != ""){
        //      $tHash =  $params["plugin_authnet_MD5 Hash Value"].$params["plugin_authnet_Authorize.Net Username"].$authnet_results["x_trans_id"].sprintf("%01.2f", $params["invoiceTotal"]);
        //      if (strtoupper(md5($tHash))!=strtoupper($authnet_results["x_md5_hash"])) return; //do not approve
    }
    if ($params['isSignup']==1){
        $bolInSignup = true;
    }else{
        $bolInSignup = false;
    }
    include('plugins/gateways/authnet/callback.php');
    //Return error code
    $tReturnValue = "";
    if (($authnet_results["x_response_code"]==1)||($authnet_results["x_response_code"]=='*1*')){ $tReturnValue = ""; }
    else { $tReturnValue = $authnet_results["x_response_reason_text"]." Code:".$authnet_results["x_response_code"]; }
    return $tReturnValue;
}
?>
