<?php
/*****************************************************************/
// function plugin_psigate_variables($params) - required function
/*****************************************************************/
function plugin_psigate_variables() {
    /* Specification
          itemkey     - used to identify variable in your other functions
          type        - text,textarea,yesno,password
          description - description of the variable, displayed in ClientExec
    */

    $variables = array (
               "Plugin Name" => array (
                                    "type"=>"hidden",
                                    "description"=>"How CE sees this plugin (not to be confused with the Signup Name)",
                                    "value"=>"PSiGate"
                                   ),
               "In Signup" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you want to include this payment processor in the signup script.",
                                    "value"=>"0"
                                   ),
               "Store Name" => array (
                                    "type"=>"text",
                                    "description"=>"ID used to identify you to PSiGate.<br>NOTE: This ID is required if you have selected PSiGate as a payment gateway for any of your clients.",
                                    "value"=>""
                                   ),
               "Passphrase" => array (
                                    "type"=>"password",
                                    "description"=>"Your PSiGate passphrase used to authenticate a valid transaction.",
                                    "value"=>""
                                   ),
               "Demo Mode" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you want to set this plugin in Demo mode for testing purposes.",
                                    "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"=>"0"
                                   ),
               "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"
                                   ),
               "Accept CC Number" => array (
                                    "type"=>"yesno",
                                    "description"=>"Selecting YES allows the entering of CC numbers when using this plugin type. No will prevent entering of cc information",
                                    "value"=>"1"
                                   ),
               "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_psigate_singlepayment($params) - required function
/*****************************************************************/
function plugin_psigate_singlepayment($params) {
    //Function needs to build the url to the payment processor
    //Plugin variables can be accesses via $params["plugin_[pluginname]_[variable]"] (ex. $params["plugin_paypal_UserID"])

    return plugin_psigate_autopayment($params);

}

/**********************************************************************************/
// function plugin_psigate_autopayment($params) - plugin function, used internally
/**********************************************************************************/
function plugin_psigate_autopayment($params){
    
    require_once('class.psigate_xml.php');
    $psi = new PsiGatePayment();

	$ip = getRemoteAddr();
	$host = @gethostbyaddr($ip);
    
    if ($params["plugin_psigate_Demo Mode"]==0){ 
        $psi->setGatewayURL('https://secure.psigate.com:7934/Messenger/XMLMessenger');
    } else {
        $psi->setGatewayURL('https://dev.psigate.com:7989/Messenger/XMLMessenger');
    }
    
    if ($params["pathCurl"]=="") {
        $psi->setUseLibCurl();
    }else{
        //absolute path to Curl on your system, not using libCurl
        $psi->setUseLibCurl(false,$params['pathCurl']);
    }
    
    $psi->setStoreID($params["plugin_psigate_Store Name"]);
    $psi->setPassPhrase($params["plugin_psigate_Passphrase"]); // Assures authenticity
    $psi->setOrderID(""); // Order ID.  Leave blank to have PSiGate assign
    $psi->setPaymentType('CC');
    $psi->setCardAction('0'); // 1 for Authorize, 0 for Immediate Charge
    $psi->setSubTotal(sprintf("%.2f", $params["invoiceTotal"])); // Amount
    $psi->setCardNumber($params["userCCNumber"]); // Card Number
    $psi->setCardExpMonth(substr($params["userCCExp"],0,2)); // Month in 2-digit format
    $psi->setCardExpYear(substr($params["userCCExp"],strpos($params["userCCExp"],"/")+3));
    $psi->setUserID($params['userID']); // Unique customer identifier set by merchant.
    $psi->setBname($params['userFirstName']." ".$params['userLastName']); // Billing Name
    
    if(isset($params["organization"])){
        $psi->setBcompany($params["organization"]); // Company Name
    }
    
    $psi->setBaddress1($params["userAddress"]); // Billing Address 1
    $psi->setBcity($params["userCity"]); // Billing City
    $psi->setBprovince($params["userState"]); // Billing state or province
    $psi->setBpostalCode($params["userZipcode"]); // Billing Zip
    $psi->setBcountry($params["userCountry"]);
    $psi->setPhone($params["userPhone"]); // Customer Phone
    $psi->setEmail($params["userEmail"]); // Customer Email
    $psi->setCustomerIP($ip); // Customer IP address, for fraud
    $psi->setComments($params["invoiceDescription"]);
    
    // Send transaction data to the gateway
    $psi->doPayment();
    
    if ($params['isSignup']==1){
		$bolInSignup = true;
	}else{
		$bolInSignup = false;
	}
	require_once 'plugins/gateways/psigate/callback.php';
    
	//Return error code
    $tReturnValue = "";
    if ($psi->getTrxnApproved() == 'APPROVED'){ $tReturnValue = ""; }
    else { $tReturnValue = $psi->getTrxnApproved()." Error: ".$psi->getErrorMessage();}
    return $tReturnValue;
}
?>
