$versions{'payment_solutions_mgr-ext_lib.pl'} = '06.6.00.0000';

# Contains all sub routines (functions) for enabling and disabling payment gateways,
# displaying gateway managers, etc.

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

{
    local ($modname) = 'payments_mgr';
    register_extension( $modname, 'Payment Gateways Manager', $versions{$modname} );
    register_menu(
        'SelectGatewayScreen', 'show_gateway_settings_screen',
        $modname,  'Select Which Gateway to Edit'
    );
    register_menu(
        'show_gateway_settings_screen',
        'show_gateway_settings_screen',
        $modname, 'Display Gateway Settings Screen'
    );
    add_settings_choice(
        'Payment Gateway',
        ' Payment Gateway Settings ',
        'select_gateway_settings_screen'
    );
    register_menu(
        'select_gateway_settings_screen',
        'select_gateway_settings_screen',
        $modname, 'Display Which Gateway to Edit choices'
    );
    register_menu(
        'GatewaySettings', 'write_gateway_settings',
        $modname,  'Write Gateway Settings'
    );
    register_menu(
        'SaveGatewaysList', 'save_gateway_choices',
        $modname,  'Choose Gateways and Settings'
    );
}

#######################################################################

sub write_gateway_settings {

    local ( $admin_email, $order_email );

    if ( $in{'gateway'} ) {
        $sc_gateway_name = $in{'gateway'};
        $sc_gateway_name =~ /([^\n]+)/;
        $sc_gateway_name = $1;
    }
    else { # gateway name not supplied
        show_gateway_settings_screen();
    }

    $gateway_settings = "$sc_userpay_conf_dir/$sc_gateway_name-user_lib.pl";
    $order_email = $in{'email_address_for_orders'};
    $order_email =~ s/\@/\\@/g;
    $admin_email = $in{'admin_email'};
    $admin_email =~ s/\@/\\@/g;

    codehook( 'gateway_admin_settings' );

    show_gateway_settings_screen();

}

#######################################################################

sub select_gateway_settings_screen {

    make_lists_of_various_options();

    if ( $sc_set_paypal_notify_script_to_test eq '' ) {
        $sc_set_paypal_notify_script_to_test = 'yes';
    }
    if ( $sc_show_multi_gateways_per_row eq '' ) {
        $sc_show_multi_gateways_per_row = "2";
    }

    print &$manager_page_header( 'Select Payment Solutions or to Edit', '', '', '', '' );

    print <<ENDOFTEXT;
<h1 class="mgr-page-title"><b>Payment Gateway Section</b></h1>
<p><br></p>

<p class="well">This is a list of gateways installed in your store. You can select one or more gataways from the list below.
<b>Disable Checkout by unselecting all active gateways.</b> Click on the <b><i class="fa fa-pencil-square-o"></i></b> icon after the name of a gateway option to edit the settings for that particular gateway. Click on the <i class="fa fa-info-circle"></i> icon next to each setting for information about that setting OR the one below for more information about activating gateways on this screen.</p>
<p><br></p>

ENDOFTEXT
    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-success">
        <h4 class="text-primary mgr-center">The Active Payment Gateway List has been successfully updated.</h4>
    </div>
</div>
<div class="row col-xs-12 col-sm-12"><p><br></p></div>
ENDOFTEXT
    }

    print <<ENDOFTEXT;
<p><b>Please select the Payment Gateways you wish to use &nbsp; <a data-toggle="collapse" href="#paymentGatewayInfo" aria-expanded="false" aria-controls="paymentGatewayInfo"><i class="fa fa-info-circle"></i></a></b></p>
<div class="collapse" id="paymentGatewayInfo">
  <div class="well">
  <p>This is a list of gateways installed in your store. You can select one or more gataways from the list below. If more than one is selected, the store will automatically use the <b>combo-orderform.html</b> file so that customers can choose their preferred payment type.</p>
  <p><b>Disable Checkout by unselecting all active gateways.</b></p>
  <p>Primary and additional gateways have been deprecated and removed starting with Route 66 versions, so the files do not need to be added to the "add-ons" subdirectory, making it easier to use multiple gateways over the original method of enabling them. Also, gateways are no longer automatically enabled with plug-n-play and must be enabled from the list below for extra security.</p>
  <p>You may install additional gateways using the gateway installer in the upgrade/update manager in the settings section of the store management area. Advanced users can upload gateway files using FTP to the appropriate sub-directories of your store:<ul><li>order forms ( <i>gatewayname-orderform.html</i> ) go into: <i>view66/order_forms/languagename/</i></li><li>order libraries ( <i>gatewayname-order_lib.pl</i> ) go into: <i>system66/payment_gateways/</i></li><li>management libraries ( <i>gatewayname-mgr_lib.pl</i> ) go into: <i>system66/payment_gateways/managers/</i></li></ul></p>
  </div>
</div>
<form action="manager.cgi" method="post">
<input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

<div class="row">
$mylist_of_gateways_installed
</div>

<p><hr></p>

<p><b>If Multiple Gateways - Shown per Row: &nbsp; <a data-toggle="collapse" href="#enableGateLogging" aria-expanded="false" aria-controls="enableGateLogging"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="sc_show_multi_gateways_per_row">
<option selected>$sc_show_multi_gateways_per_row</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select></p>
<div class="collapse" id="enableGateLogging">
    <div class="well">
    <p>Default is 2, which splits the payment options row in half on larger screens</p>
    <p>Vertical mobile screen sizes use one (1) payment option per row (centered).</p>
    </div>
</div>

<p><hr></p>

<p><b>Enable Missing Gateway Error Logging? &nbsp; <a data-toggle="collapse" href="#enableGateLogging" aria-expanded="false" aria-controls="enableGateLogging"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name=sc_enable_missing_gateway_logging>
<option selected>$sc_enable_missing_gateway_logging</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="enableGateLogging">
    <div class="well">
    <p>Default is yes.</p>
    <p>This logs an error in the main store's error log, and sends the store admin an error notice via email if no gateways are selected.</p>
    <p>Disable logging if you purposely disable checkout so that logging and email notices are not sent/logged needlessly.</p>
    </div>
</div>

<p><hr></p>

<p><b>Replace Order Form Tags? &nbsp; <a data-toggle="collapse" href="#replaceOrderFormTags" aria-expanded="false" aria-controls="replaceOrderFormTags"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name=sc_replace_orderform_form_tags>
<option selected>$sc_replace_orderform_form_tags</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="replaceOrderFormTags">
    <div class="well">
    <p>Default is yes. This handles the submit button when displaying the order form. Select yes to run a normal store operation. Select "no" if having problems with multiple gateways going to the wrong gateway or back to the cart. Leave this as "yes" unless it corrects multiple gateway forms submission problems.</p>
    </div>
</div>

<p><hr></p>

<p><b>Set PayPal Listener to Sandbox / Test mode? &nbsp; <a data-toggle="collapse" href="#setPaypalNotifyScriptToTest" aria-expanded="false" aria-controls="setPaypalNotifyScriptToTest"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name=sc_set_paypal_notify_script_to_test>
<option selected>$sc_set_paypal_notify_script_to_test</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="setPaypalNotifyScriptToTest">
    <div class="well">
    <p>The listener is a script that PayPal sends payment notifications to for such things as telling the cart the payment is valid as well as any other changes to the payment transcaction status.  Currently it only verifies payment statuses for being completed, even if delayed such as for an echeck payment that could take several days to complete and be funded.</p>
    <p>Only applies if any of the following PayPal gateways are enabled: IPN, Advanced, Express, Pro, or Checkout gateways are enabled above. Does not apply to basic or legacy button based PayPal gateways.</p>
    <p>Default is <b>yes</b>. This will allow testing of PayPal IPN (instant payment notifications). Set to NO for production (live) use.</p>
    <p>Note: to test any PayPal gateway, you will still need to set each gateway to "test" or "snadbox" mode within each gateway's settings manager.</p>
    </div>
</div>

<p><br><br></p>
<input name="SaveGatewaysList" type="hidden" value="Submit">
<input type="hidden" name="system_edit_success" value="yes">
<button class="btn btn-primary btn-lg mgr-center" style="display: block;">Update Active Payment Gateways</button>
</form>
<p><br><br><br></p>
ENDOFTEXT
    print &$manager_page_footer;
}

#######################################################################

sub show_gateway_settings_screen {

    local ($msg);
    local ($errs) = html_eval_settings();

    if ( $in{'gateway'} ) {
        $sc_gateway_name = $in{'gateway'};
        $sc_gateway_name =~ /([^\n]+)/;
        $sc_gateway_name = $1;
    }
    eval("require '$sc_userpay_conf_dir/$sc_gateway_name-user_lib.pl'");
    if ( $@ ) {
        $msg = qq|
          <div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
              <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-danger">
                  <h4 class="text-danger mgr-center">Payment Solution User Lib Not Found!</h4>
                  <p class="text-danger">Could not open the $sc_userpay_conf_dir/$sc_gateway_name-user_lib.pl for the $sc_gateway_name payment gateway.</p>
              </div>
          </div>
          <div class="row col-xs-12 col-sm-12"><p><br><br></p></div>
|;
    }

    # this is where the gateway manger latches on and will display for the
    # specific gateway found above in $sc_gateway_name
    codehook( 'gateway_admin_screen' );

}


#######################################################################

sub save_gateway_choices {

    local ($myset, @values, $gatelist_string, $mc_mgr_gateways_enabled ) = q{};
    my $gate_count = 0;
    my $ofn_yes_no = 'no';

    @values = param('gateways_enabled');
    foreach $value ( @values ) {
        if ( $gate_count == 0 ) {
            $gatelist_string .= qq|'$value'|;
            $mc_mgr_gateways_enabled .= "$value";
        }
        else {
            $gatelist_string .= ",'$value'";
            $mc_mgr_gateways_enabled .= "|$value";
        }
        $gate_count++;
    }
    if ( $gate_count > 1 ) {
        $ofn_yes_no = 'yes';
    }

    $myset .= "\@sc_active_gateways = ($gatelist_string);\n";
    $myset .= "\$mc_mgr_gateways_enabled = '$mc_mgr_gateways_enabled';\n";
    $myset .= "\$sc_allow_ofn_choice = \"$ofn_yes_no\";\n";
    $myset .= "\$sc_show_multi_gateways_per_row = \"$in{'sc_show_multi_gateways_per_row'}\";\n";
    $myset .= "\$sc_replace_orderform_form_tags = \"$in{'sc_replace_orderform_form_tags'}\";\n";
    $myset .= "\$sc_enable_missing_gateway_logging = \"$in{'sc_enable_missing_gateway_logging'}\";\n";
    $myset .= "\$sc_set_paypal_notify_script_to_test = \"$in{'sc_set_paypal_notify_script_to_test'}\";\n";

    update_store_settings( 'PAYMENTGATEWAYS', $myset );
    select_gateway_settings_screen();
}

#######################################################################

1;
