<?php
/*****************************************************************/
// function plugin_paysystems_variables($params) - required function
/*****************************************************************/
function plugin_paysystems_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 ClientExec sees this plugin. (not to be confused with the Signup Name)",
                                    "value"=>"Pay Systems"
                                   ),
               "In Signup" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you want to include this payment processor in the signup script.",
                                    "value"=>"0"
                                   ),
               "Company ID" => array (
                                    "type"=>"text",
                                    "description"=>"ID used to identify you to PaySystems.<br>NOTE: This ID is required if you have selected PaySystems as a payment gateway for any of your clients.",
                                    "value"=>""
                                   ),
               "Recurring Plugin" => array (
                                    "type"=>"yesno",
                                    "description"=>"Use processor's recurring capabilities.<br><b>Note</b>: Deleting customer's recurring transaction on ClientExec will still require the admin to delete the recurring transaction on paysystems control panel.",
                                    "value"=>"0"
                                   ),
               "Generate Invoices After Callback Notification" => array (
                                    "type"=>"yesno",
                                    "description"=>"Select YES if you prefer CE to only generate invoices upon notification of payment via the callback supported by this processor.  Setting to NO will generate invoices normally but require you to manually mark them paid as you receive notification from processor.<br><b>Note:</b> Only used in conjuction with the 'Recurring Plugin' setting.",
                                    "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"=>"hidden",
                                    "description"=>"Selecting YES allows the entering of CC numbers when using this plugin type. No will prevent entering of cc information",
                                    "value"=>"0"
                                   ),
               "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"=>"1"
                                   )
    );
    return $variables;
}

/*****************************************************************/
// function plugin_paysystems_singlepayment($params) - required function
/*****************************************************************/
function plugin_paysystems_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"])


    //Paysystems recurring uses
    //30 day based billing cycles
    //Instead of monthly based.
    switch ($params['invoiceBillingCycle']){
          case 0:
                  $params["plugin_paysystems_Recurring Plugin"]=0;
                  $cycle = "0";
                  break;
          case 1:
                  $cycle = "30";
                  break;
          case 3:
                  $cycle = "90";
                  break;
          case 6:
                  $cycle = "180";
                  break;
          case 12:
                  $cycle = "360";
                  break;
          default:
                  $cycle = "30";
                  break;
    }

    //generate post to submit to paysystems
    $strRet = "<html>\n";
    $strRet .= "<head></head>\n";
    $strRet .= "<body>\n";
    $strRet .= "<form name=\"frmPaySystems\" action=\"https://auth.mypaysystems.com/cgi-v310/payment/onlinesale-tpppro.asp\" method=\"post\">\n";
    $strRet .= "<INPUT type=\"hidden\" name=\"product1\" value=\"Invoice Number #".$params['invoiceNumber']."\">";
    $strRet .= "<INPUT type=\"hidden\" name=\"total\" value=\"".sprintf("%.2f", $params["invoiceTotal"])."\">";
    $strRet .= "<INPUT type=hidden name=\"companyid\" value=\"".$params["plugin_paysystems_Company ID"]."\">";
    $strRet .= "<INPUT type=hidden name=\"formget\" value=\"N\">";
    $strRet .= "<INPUT type=hidden name=\"redirect\" value=\"".$params['clientExecURL']."/plugins/gateways/paysystems/callback.php\">";
    $strRet .= "<INPUT type=hidden name=\"redirectfail\" value=\"".$params['clientExecURL']."/plugins/gateways/paysystems/callback_fail.php\">";
    $strRet .= "<INPUT type=hidden name=\"email\" value=\"".$params["userEmail"]."\">";
    $strRet .= "<input type=hidden name=\"delivery\" value=\"N\">";
    $strRet .= "<INPUT type=hidden name=\"b_firstname\" value=\"".$params["userFirstName"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_lastname\" value=\"".$params["userLastName"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_country\" value=\"".$params["userCountry"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_address\" value=\"".$params["userAddress"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_city\" value=\"".$params["userCity"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_state\" value=\"".$params["userState"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_zip\" value=\"".$params["userZipcode"]."\">";
    $strRet .= "<INPUT type=hidden name=\"b_tel\" value=\"".$params["userPhone"]."\">";


    //check to see if this plugin handles recurring charges and is set to yes
    if ($params["plugin_paysystems_Recurring Plugin"]==1){
        $strRet .= "<INPUT type=hidden name=\"reoccur\" value=\"Y\">";
        $strRet .= "<INPUT type=hidden name=\"cycle\" value=\"" .$cycle."\">";
        $strRet .= "<INPUT type=hidden name=\"totalperiod\" value=\"48\">";

        //calculate the recurring amount
        $tRecurringTotal = $params["invoiceTotal"] - $params['invoiceSetup'];
        $strRet .= "<INPUT type=hidden name=\"repeatamount\" value=\"".sprintf("%.2f", $tRecurringTotal)."\">";
    }
    $strRet .= "<INPUT type=hidden name=\"option1\" value=\"".$params['invoiceNumber']."_".$params["plugin_paysystems_Recurring Plugin"]."_".$params["plugin_paysystems_Generate Invoices After Callback Notification"]."\">";

    //Pass custom variable for callback to determine if this is a signup trans or CE internal
    if ($params['isSignup']==1) { $strRet .= "<INPUT type=hidden name=\"option2\" value=\"1\">"; }
    else {$strRet .= "<INPUT type=hidden name=\"option2\" value=\"0\">";}

    $strRet .= "<script language=\"JavaScript\">\n";
    $strRet .= "document.forms['frmPaySystems'].submit();\n";
    $strRet .= "</script>\n";
    $strRet .= "</form>\n";
    $strRet .= "</body></html>";
    echo $strRet;
}
?>
