<?php
function plugin_2checkout_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"=>"2Checkout"
                                   ),
               "In Signup" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you want to include this payment processor in the signup script.",
                                    "value"=>"0"
                                   ),
               "Seller ID" => array (
                                    "type"=>"text",
                                    "description"=>"ID used to identify you to 2checkout.com.<br>NOTE: This ID is required if you have selected 2checkout as a payment gateway for any of your clients.",
                                    "value"=>""
                                   ),
                "Demo Mode" => array (
                    "type"=>"yesno",
                    "description"=>"Select YES if you want to set 2checkout into Demo Mode for testing. (<b>NOTE:</b> You must set to NO before accepting actual payments through this processor.)",
                    "value"=>"0"
                    ),
               "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"=>"0"
                                   ),
               "Visa" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES to allow Visa card acceptance with this plugin.  No will prevent this card type.",
                                    "value"=>"0"
                                   ),
               "MasterCard" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES to allow MasterCard acceptance with this plugin. No will prevent this card type.",
                                    "value"=>"0"
                                   ),
               "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"
                                   ),
               "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"=>"0"
                                   ),
               "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_2checkout_singlepayment($params) {
    //Function needs to build the url to the payment processor, then redirect
    //Plugin variables can be accesses via $params["plugin_[pluginname]_[variable]"] (ex. $params["plugin_2checkout_SellerID"])

    $return_url = substr($params['clientExecURL'],-1,1) == "//" ? $params['clientExecURL']."plugins/gateways/2checkout/callback.php" : $params['clientExecURL']."/plugins/gateways/2checkout/callback.php";

    if ($params["userCountry"]=="US") $params["userCountry"]="USA";

    //if ($params["invoiceDomainCharge"] == "1")
    //  $tPrice = $params["invoiceTotal"] - $params["invoiceSetup"] - $params["invoiceDomainSetup"];
    //else
        $tPrice = $params["invoiceTotal"] - $params["invoiceSetup"];

    // Start building the URL that will be used to send customers to 2CO for payment.
    $strURL = "https://www2.2checkout.com/2co/buyer/purchase";

    // Basic parameters
    $strURL .= "?x_login=".$params["plugin_2checkout_Seller ID"];
    $strURL .= "&x_invoice_num=".$params["invoiceNumber"];
    $strURL .= "&x_amount=".sprintf("%.2f", $params["invoiceTotal"]);
    $strURL .= "&id_type=1";

    // Product Creation code (so CE can send 2CO on-the-fly orders,
    // as it does not store 2CO information yet.)
    $strURL .= "&c_prod=ce_".$params["invoiceNumber"];
    $strURL .= "&c_name=".$params["companyName"]." - Subscription";
    $strURL .= "&c_description=".$params["companyName"]." - Subscription";
    $strURL .= "&c_price=".sprintf("%.2f", $tPrice);
    $strURL .= "&c_tangible=N";

    // If Demo Mode is set, pass appropriate parameter.
    if ($params["plugin_2checkout_Demo Mode"]==1)
        $strURL .= "&demo=Y";

    $strURL .= "&acc_can=Y&acc_int=Y&diff_ship=N&can_handling=0.00&int_handling=0.00";

    // Billing Information so the 2checkout form is pre-filled.
    //$strURL .= "&card_holder_name=".$params["userFirstName"]." ".$params["userLastName"];
    $strURL .= "&x_First_Name=".$params["userFirstName"];
    $strURL .= "&x_Last_Name=".$params["userLastName"];
    $strURL .= "&x_Email=".$params["userEmail"];
    $strURL .= "&x_Address=".$params["userAddress"];
    $strURL .= "&x_City=".$params["userCity"];
    $strURL .= "&x_State=".$params["userState"];
    $strURL .= "&x_Zip=".$params["userZipcode"];
    $strURL .= "&x_Phone=".$params["userPhone"];
    $strURL .= "&x_Country=".$params["userCountry"];

    //$strURL .= "&credit_card_processed=";
    $strURL .= "&x_receipt_link_url=".$return_url;

    // Custom Parameters Passed thru 2CO back to CE
    if ($params['isSignup']==1)
        $strURL .= "&signup=1";
    else
        $strURL .= "&signup=0";
    $strURL .= "&ce_invoice_num=".$params['invoiceNumber'];

    // Send to 2CO for payment
    header("Location: $strURL");
    exit;
 }
?>
