<?php

require_once 'classes/GatewayPlugin.php';

/**
* @package Plugins
*/
class PluginStormpay extends GatewayPlugin
{
    function getVariables()
    {
        /* 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 (
                   /*T*/"Plugin Name"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"How CE sees this plugin (not to be confused with the Signup Name)"/*/T*/,
                                        "value"         =>/*T*/"StormPay"/*/T*/
                                       ),
                   /*T*/"In Signup"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES if you want to include this payment processor in the signup script."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"One-Time Payments"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES if you want to allow customers to make one-time payments through this payment processor."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"User ID"/*/T*/ => array (
                                        "type"          =>"text",
                                        "description"   =>/*T*/"ID used to identify you to StormPay.<br>NOTE: This ID is required if you have selected StormPay as a payment gateway for any of your clients."/*/T*/,
                                        "value"         =>""
                                       ),
                   /*T*/"Visa"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES to allow Visa card acceptance with this plugin.  No will prevent this card type."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"MasterCard"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES to allow MasterCard acceptance with this plugin. No will prevent this card type."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"AmericanExpress"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES to allow American Express card acceptance with this plugin. No will prevent this card type."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"Discover"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES to allow Discover card acceptance with this plugin. No will prevent this card type."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"Invoice After Signup"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES if you want an invoice sent to customer after signup is complete.  Only used for offline plugins"/*/T*/,
                                        "value"         =>"1"
                                       ),
                   /*T*/"Signup Name"/*/T*/ => array (
                                        "type"          =>"text",
                                        "description"   =>/*T*/"Select the name to display in the signup process for this payment type. Example: eCheck or Credit Card."/*/T*/,
                                        "value"         =>"Stormpay"
                                       ),
                   /*T*/"Accept CC Number"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"Selecting YES allows the entering of CC numbers when using this plugin type. No will prevent entering of cc information"/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"Dummy Plugin"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"1 = Only used to specify a billing type for a customer. 0 = full fledged plugin requiring complete functions"/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"Auto Payment"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"No description"/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"30 Day Billing"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"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."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"buttonurl"/*/T*/ => array (
                                        "type"          =>"text",
                                        "description"   =>/*T*/"Button URL of the StormPay Buy Now Button."/*/T*/,
                                        "value"         =>"https://www.stormpay.com/stormpay/images/BuyNowSP1.gif"
                                       ),
                   /*T*/"secretcode"/*/T*/ => array (
                                        "type"          =>"text",
                                        "description"   =>/*T*/"Secret Code as configured in your StormPay Accounts IPN configurations."/*/T*/,
                                        "value"         =>""
                                       ),
                   /*T*/"test"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Test mode Switch for the StormPay Module."/*/T*/,
                                        "value"         =>"0"
                                       ),
                   /*T*/"Check CVV2"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"Select YES if you want to accept CVV2 for this plugin."/*/T*/,
                                        "value"         =>"0"
                                       )
        );
    
        return $variables;
    
    }
    
    function 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_stormpay_UserID"])
        $stat_url = substr($params['clientExecURL'],-1,1) == "//" ? $params['clientExecURL']."plugins/gateways/stormpay/callback.php" : $params['clientExecURL']."/plugins/gateways/stormpay/callback.php";
    
        $strRet = "<html>\n";
        $strRet .= "<head></head>\n";
        $strRet .= "<body>\n";
        $strRet .= "<form name=frmStormpay action=\"https://www.stormpay.com/stormpay/handle_gen.php\" method=\"post\">";
        $strRet .= "<INPUT type=hidden name=\"generic\" value=\"1\">";
            $strRet .= "<INPUT type=hidden name=\"vendor_email\" value=\"".$params["User ID"]."\">";
            $strRet .= "<INPUT type=hidden name=\"payee_email\" value=\"".$params["plugin_stormpay_User ID"]."\">";
        $strRet .= "<INPUT type=hidden name=\"product_name\" value=\"".$params["companyName"]." Invoice\">";
        $strRet .= "<INPUT type=hidden name=\"description\" value=\"Invoice Number:".$params['invoiceNumber']."\">";
            $strRet .= "<INPUT type=hidden name=\"amount\" value=\"".sprintf("%01.2f", round($params["invoiceTotal"], 2))."\">";
            $strRet .= "<INPUT type=hidden name=\"user1\" value=\"".$params["invoiceNumber"]."\">";// USER VARIABLE FOR IPN
    
        //Need to check to see if user is coming from signup
        if ($params['isSignup']==1) {
              $returnURL=$params["clientExecURL"]."/signup.php?step=6&pass=1";
              $returnURL_Cancel=$params["clientExecURL"]."/signup.php?step=6&pass=0";
        }else {
              $returnURL=$params["clientExecURL"];
              $returnURL_Cancel=$params["clientExecURL"];
        }
    
        $strRet .= "<INPUT type=hidden name=\"return_URL\" value=\"".$returnURL."\">";
        $strRet .= "<input type=hidden name=\"cancel_URL\" value=\"".$returnURL_Cancel."\">";
        $strRet .= "<input type=hidden name=\"notify_URL\" value=\"".$stat_url."\">";
            $strRet .= "<input type=hidden name=\"require_IPN\" value=\"1\">";
            $strRet .= "<input type=hidden name=\"test_mode\" value=\"".$params["plugin_stormpay_test"]."\">";
        $strRet .= "<input type=hidden name=\"secret_code\" value=\"".$params["plugin_stormpay_secretcode"]."\">";
        $strRet .= "<INPUT type=hidden name=\"first_name\" value=\"".$params["userFirstName"]."\">";
        $strRet .= "<INPUT type=hidden name=\"last_name\" value=\"".$params["userLastName"]."\">";
        $strRet .= "<INPUT type=hidden name=\"address1\" value=\"".$params["userAddress"]."\">";
        $strRet .= "<INPUT type=hidden name=\"city\" value=\"".$params["userCity"]."\">";
        $strRet .= "<INPUT type=hidden name=\"state\" value=\"".$params["userState"]."\">";
        $strRet .= "<INPUT type=hidden name=\"zip\" value=\"".$params["userZipcode"]."\">";
        $strRet .= "<INPUT type=hidden name=\"require_shipping\" value=\"0\">";
    //    $strRet .= "<INPUT type=hidden name=\"no_note\" value=\"1\">";
    //    $strRet .= "<INPUT type=hidden name=\"currency_code\" value=\"".$params["currencytype"]."\">";
            //<input type="image" src="https://www.stormpay.com/stormpay/images/BuyNowSP1.gif">
        $strRet .= "<script language=\"JavaScript\">\n";
        $strRet .= "document.forms['frmStormpay'].submit();\n";
        $strRet .= "</script>\n";
            $strRet .= "<INPUT type=image src=\"".$params["plugin_stormpay_buttonurl"]."\">";
        $strRet .= "</form>\n";
        $strRet .= "</body></html>";
        echo $strRet;
        exit;
    }
}
?>