$versions{'PayPalIPN-mgr_lib.pl'} = '06.6.00.0000';
#
# PayPal Settings manager for the 'PayPal Standard with IPN' Payment Gateway
# Requires AgoraCart version 06.6.00.000 or above.
#
# Additional Contributions: Andy Smith
# Redesigned by Mister Ed at AgoraCart - September 2018 for version 6.6+
#
#####################################################################
#
# 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.
#
#######################################################################
#                    Order Form Definition Variables
#######################################################################
add_codehook( 'gateway_admin_screen', 'PayPalIPN_mgr_check' );
add_codehook( 'gateway_admin_settings', 'PayPalIPN_settings' );
$mc_gateways .= '|PayPalIPN::PayPal Standard IPN';
#######################################################################
sub PayPalIPN_settings {
    local ($custom_logic);
    if ( $sc_gateway_name eq 'PayPalIPN' ) {

        my $listener_name = "pplistnr.cgi";

        $PayPalIPN_top_message_temp = my_escape( $in{'PayPalIPN_top_message'} );
        $PayPalIPN_shipping_message_temp = my_escape( $in{'PayPalIPN_shipping_message'} );
        $PayPalIPN_special_message_temp = my_escape( $in{'PayPalIPN_special_message'} );
        $PayPalIPN_order_name_tmp = my_escape( $in{'PayPalIPN_order_name'} );
        $PayPalIPN_pending_order_subject_line_tmp = my_escape( $in{'PayPalIPN_pending_order_subject_line'} );
        $PayPalIPN_tos_display_address_temp = my_escape( $in{'PayPalIPN_tos_display_address'} );
        $sc_order_ok_tmp = my_escape( $in{'PayPalIPN_verify_message'} );
        $messages{'paypalipn_canceled'} = my_escape( $in{'paypalipn_canceled'} );

        open( GW, "> $gateway_settings" )  || my_die("Can't Open $gateway_settings");
        print( GW "\$PayPalIPN_set_to_test = \'$in{'PayPalIPN_set_to_test'}\';\n" );
        print( GW "\$PayPalIPN_username = \'$in{'PayPalIPN_username'}\';\n" );
        print( GW "\$PayPalIPN_currency_id = \"$in{'PayPalIPN_currency_id'}\";\n" );
        print( GW "\$PayPalIPN_order_notify_url = \"$sc_SSL_base_URL$listener_name\";\n" );
        print( GW "\$PayPalIPN_order_return_url = \"$sc_stepone_order_script_url?\";\n" );
        print( GW "\$PayPalIPN_top_message = qq'$PayPalIPN_top_message_temp';\n"  );
        print( GW "\$PayPalIPN_special_message = qq'$PayPalIPN_special_message_temp';\n"  );
        print( GW "\$PayPalIPN_order_name = qq'$PayPalIPN_order_name_tmp';\n" );
        print( GW "\$PayPalIPN_pending_order_subject_line = qq|$PayPalIPN_pending_order_subject_line_tmp|;\n" );
        print( GW "\$PayPalIPN_display_checkout_tos = \"$in{'PayPalIPN_display_checkout_tos'}\";\n" );
        print( GW "\$PayPalIPN_tos_display_address = q|$PayPalIPN_tos_display_address_temp|;\n"  );
        print( GW "\$PayPalIPN_verify_message = qq'$sc_order_ok_tmp';\n" );
        print( GW "\$messages{'paypalipn_canceled'} = qq~$messages{'paypalipn_canceled'}~;\n" );
        print( GW "\$PayPalIPN_email_template = \"$in{'PayPalIPN_email_template'}\";\n" );
        print( GW "\$PayPalIPN_admin_email_template = \"$in{'PayPalIPN_admin_email_template'}\";\n" );
        print( GW "\$PayPalIPN_submit = '$in{'PayPalIPN_submit'}';\n" );
        print( GW "\$PayPalIPN_combo_form = q|$in{'PayPalIPN_combo_form'}|;\n" );
        print( GW "\$PayPalIPN_verifytable = \"$in{'PayPalIPN_verifytable'}\";\n" );
        print( GW "\$PayPalIPN_thankstable = \"$in{'PayPalIPN_thankstable'}\";\n" );

        codehook( 'manager_write_gateway_settings' );
        codehook( 'PayPalIPN_mgr_write_settings' );
        print( GW "#\n1\;# We are a Library\n" );
        close(GW);
    }
}
#######################################################################
sub PayPalIPN_mgr_check {
    if ( $sc_gateway_name eq 'PayPalIPN' ) {
        print_PayPalIPN_mgr_form();
        call_exit();
    }
}
#######################################################################
sub print_PayPalIPN_mgr_form {

    if ( $sc_display_checkout_tos eq '' ) {
        $sc_display_checkout_tos = 'yes';
    }

    if ( $messages{'paypalipn_canceled'} eq '' ) {
        $messages{'paypalipn_canceled'} =
"You have canceled your payment at PayPal. You may continue your shopping session by using the links on this page<br><br>\n\n";
    }
    if ( $PayPalIPN_combo_form eq '' ) {
        $PayPalIPN_combo_form = q{https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal"};
    }

    $page_output = &$manager_page_header( "PayPal Standard w/ IPN Gateway", '', '', '', '' );

    $mc_email_admin_plates = read_templatedirs($sc_emailPlates_dir, 'admin_emailPlate');
    $mc_email_plates = read_templatedirs($sc_emailPlates_dir, 'emailPlate');
    $mc_verifytable_plates = read_templatedirs($sc_cartverifyplates_dir, 'verifyTable');
    $mc_thanktable_plates = read_templatedirs($sc_thankyouplates_dir, 'thanksTable');

    $page_output .= <<ENDOFTEXT;
<h1 class='mgr-page-title'>PayPal Standard IPN</h1>
<p><br></p>

<p class="well">$msg<br><br><b>How this solution works:</b> This is solution is an advanced implementation of PayPal Payments Standard where the customer is sent to the PayPal website to complete the payment after filling out order information in the cart checkout. Once the payment information is completed at PayPal, the customer returns to the cart and Paypal also sends a separate a confirmation of the payment details via IPN ( Instant Payment Notification ) to the IPN listner script, a new feature beginning with the Route 66 version.
<br><br>Email confirmations are sent to both the customer and you on Completed, Pending, Denial, Cancellations, and Refund notifications from PayPal. Those serve as the official receipts.  As of Route 66, the thank you page no longer shows cart contents nor the buyer details as those are sent in the confirmation emails due to the nature of how IPN works now (the cart contents are erased or moved before the customer returns to the site in 99.99% of the cases, making the IPN processes more reliable overall). Other emails are sent only to the admin when other order changes are reported back from PayPal, such as partial refunds, disputes, etc.
<br><br>This gateway has it's own verifyTable and thanksTable files located in the ./html/html-templates/ sub-directory of your store, so these are automatically used instead of allowing you to chose the templates like other gateway settings allow you to do.  Changes can be made to those files manually. You can still select the customer and site admin email templates below.
<br><br><b>Legacy Change:</b> when using this payment solution, you no longer need to set the customer's cart contents duration to at least 5 days in the main/core settings.</p>
<p>$msg</p>
<p><br></p>

ENDOFTEXT

    if ( $in{'system_edit_success'} ) {
        $page_output .= 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-success">
    <h4 class="text-primary mgr-center">PayPal IPN processing settings have been successfully updated</h4>
    </div>
</div>
<p><br><br><br><br><br></p>
|;
    }

    $page_output .= <<ENDOFTEXT;
<form action="manager.cgi" method="post"><input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

<p><b>Set PayPal IPN to Sandbox / Test mode? &nbsp; <a data-toggle="collapse" href="#setPaypalIPNtoTest" aria-expanded="false" aria-controls="setPaypalIPNtoTest"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name=PayPalIPN_set_to_test>
<option selected>$PayPalIPN_set_to_test</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="setPaypalIPNtoTest">
    <div class="well">
    <p><b>yes</b> switches the PayPal Standard IPN gateway to a test mode and goes to a sandbox account and site.</p>
    <p><b>no</b> switches the PayPal Standard IPN gateway to live (aka production) mode.</p>
    <p>NOTE: the IPN listener script needs to be set to production or test mode in the main payment gateways manager as it applies to multiple payment gatewys: PayPal IPN and PayPal Express.</p>
    </div>
</div>

<p><hr></p>

<p><b>PayPal Email Address: &nbsp; <a data-toggle="collapse" href="#ppipnEmail" aria-expanded="false" aria-controls="ppipnEmail"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="PayPalIPN_username" type="email" class="form-control" maxlength="128" value="$PayPalIPN_username" required></p>
<div class="collapse" id="ppipnEmail">
  <div class="well">
    <p>Can be your primary or any other confirmed email address linked to your paypal account.</p>
    <p><b>Recommendation:</b> Use a secondary email address not used to login at PayPal.</p>
  </div>
</div>

<p><hr></p>

<p><b>PayPal Currency: &nbsp; <a data-toggle="collapse" href="#ppipnCurrency" aria-expanded="false" aria-controls="ppipnCurrency"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="PayPalIPN_currency_id">
<option selected>$PayPalIPN_currency_id</option>
<option>USD</option>
<option>AUD</option>
<option>CAD</option>
<option>GBP</option>
<option>EUR</option>
<option>JPY</option>
</select></p>
<div class="collapse" id="ppipnCurrency">
    <div class="well">
    <p>Defines the currency to use at the PayPal site</p>
    </div>
</div>

<p><hr></p>

<p><b>Pending Email Subject Line: &nbsp; <a data-toggle="collapse" href="#ppipnPendSubject" aria-expanded="false" aria-controls="ppipnPendSubject"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="PayPalIPN_pending_order_subject_line" type="text" class="form-control" maxlength="65" value="$PayPalIPN_pending_order_subject_line" required></p>
<div class="collapse" id="ppipnPendSubject">
  <div class="well">
    <p>This subject line is used only for emails sent to you (the admin) for pending orders, such as echecks and orders under review by PayPal. The customer email subject line is set in the language files, which can be changed by entering a replacement in the user version of the language file or adding to freeform logic.</p>
  </div>
</div>

<p><hr></p>

<p><b>Select Customer Email Template - Successful Order: &nbsp; <a data-toggle="collapse" href="#ppipnCustMailPlate" aria-expanded="false" aria-controls="ppipnCustMailPlate"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="PayPalIPN_email_template">
<option selected>$PayPalIPN_email_template</option>
$mc_email_plates
</select></p>
<div class="collapse" id="ppipnCustMailPlate">
    <div class="well">
    <p>This is the template (emailPlate) that is used to create the confirmation email sent to the customer after a successful order & payment is received.</p>
    </div>
</div>

<p><br></p>

<p><b>Select Site Admin Email Template - Successful Order: &nbsp; <a data-toggle="collapse" href="#ppipnAdminMailPlate" aria-expanded="false" aria-controls="ppipnAdminMailPlate"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="PayPalIPN_admin_email_template">
<option selected>$PayPalIPN_admin_email_template</option>
$mc_email_admin_plates
</select></p>
<div class="collapse" id="ppipnAdminMailPlate">
    <div class="well">
    <p>This is the template (emailPlate) that is used to create the confirmation email sent to you (the admin) after a successful order & payment is received.</p>
    </div>
</div>

<p><hr></p>

<p><b>Select the Verify Table Template for the Checkout Verify Page: &nbsp; <a data-toggle="collapse" href="#ppipnVerifyCheckoutTemp" aria-expanded="false" aria-controls="ppipnVerifyCheckoutTemp"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="PayPalIPN_verifytable" required>
<option>$PayPalIPN_verifytable</option>
$mc_verifytable_plates
</select></p>
<div class="collapse" id="ppipnVerifyCheckoutTemp">
    <div class="well">
        <p>This is the template used to generate the verify page that the customer uses to verify their order information.</p>
        <p>It is the page displayed immediately before going to the the hosted payment page at PayPal.com</p>
    </div>
</div>

<p><hr></p>

<p><b>Select the Thankyou Table Template for the Successful Payment Return Page: &nbsp; <a data-toggle="collapse" href="#ppipnThanksTemp" aria-expanded="false" aria-controls="ppipnThanksTemp"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="PayPalIPN_thankstable" required>
<option>$PayPalIPN_thankstable</option>
$mc_thanktable_plates
</select></p>
<div class="collapse" id="ppipnThanksTemp">
    <div class="well">
        <p>This is the template used to generate the thankyou page that the customer lands on at your store after completing their payment at the hosted form at PayPal.com.</p>
    </div>
</div>

<p><hr></p>

<p><b>URL of 'Submit to PayPal' button: &nbsp; <a data-toggle="collapse" href="#ppipnSubmitButton" aria-expanded="false" aria-controls="ppipnSubmitButton"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="PayPalIPN_submit" type="url" class="form-control" maxlength="128" value="$PayPalIPN_submit"></p>
<div class="collapse" id="ppipnSubmitButton">
  <div class="well">
    <p>Enter the secure URL (HTTPS) to the button image you wish to use on the verify page to send the customer to PayPal to make their payment.</p>
    <p>Leave blank to use the default button image hosted by PayPal.</p>
  </div>
</div>

<p><hr></p>

<p><b>URL of combo form PayPal button image: &nbsp; <a data-toggle="collapse" href="#ppipnComboButtonImage" aria-expanded="false" aria-controls="ppipnComboButtonImage"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="PayPalIPN_combo_form" type="url" class="form-control" maxlength="128" value="$PayPalIPN_combo_form"></p>
<div class="collapse" id="ppipnComboButtonImage">
  <div class="well">
    <p>Enter the secure URL (HTTPS) to the button image you wish to use on the <b>Combo Order Form</b> where the customer chooses between multiple payment options.</p>
    <p>Only used if more than one payment gateway is enabled.</p>
    <p>Current Default: https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal"</p>
  </div>
</div>

<p><hr></p>

<p><b>Top Message - in the PayPal IPN order form: &nbsp; <a data-toggle="collapse" href="#ppipnTopMessage" aria-expanded="false" aria-controls="ppipnTopMessage"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><textarea name="PayPalIPN_top_message" type="text" class="form-control" maxlength="400" rows=3 wrap=on>$PayPalIPN_top_message</textarea></p>
<div class="collapse" id="ppipnTopMessage">
  <div class="well">
    <p>Allows you to place a message above the Payment Information form areas.</p>
    <p>HTML formatting (for text color, font, spacing, etc) is accepted.</p>
    <p>Leave blank if not needed.</p>
  </div>
</div>

<p><hr></p>

<p><b>Special Message - in the PayPal IPN order form: &nbsp; <a data-toggle="collapse" href="#ppipnSpecialMessage" aria-expanded="false" aria-controls="ppipnSpecialMessage"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><textarea name="PayPalIPN_special_message" type="text" class="form-control" maxlength="400" rows=3 wrap=on>$PayPalIPN_special_message</textarea></p>
<div class="collapse" id="ppipnSpecialMessage">
  <div class="well">
    <p>Allows you to place a message above the Special Notes (aka XComment areas) of the order form, usually the last section towards the bottom of the form.</p>
    <p>HTML formatting (for text color, font, spacing, etc) is accepted.</p>
    <p>Leave blank if not needed.</p>
  </div>
</div>

<p><hr></p>

<p><b>Display ToS &amp; Refund Policy checkbox? &nbsp; <a data-toggle="collapse" href="#ppipnDisplayTOS" aria-expanded="false" aria-controls="ppipnDisplayTOS"><i class="fa fa-info-circle"></i></a></b> <span style="padding-left: 20px;"></span><select name="PayPalIPN_display_checkout_tos">
<option>$PayPalIPN_display_checkout_tos</option>
<option>yes</option>
<option>no</option>
</select></p>
<div class="collapse" id="ppipnDisplayTOS">
    <div class="well">
    <p><b>yes</b> displays <b>Terms of Service and Refund Policy</b> checkbox, and the field is set as <i>required</i> in order for customer to proceed with the additional checkout steps.</p>
    <p><b>no</b> nothing is displayed.</p>
    <p><b>Best Practice:</b> set to <i>YES</i>. Some countries and/or payment processors require customers to be notified of the return and shipping policies, and this is a good place to remind them to read and agree your store/site policies.</p>
    </div>
</div>

<p><br></p>

<p><b>Business address &amp; contact information for your business: &nbsp; <a data-toggle="collapse" href="#ppipnTOSaddress" aria-expanded="false" aria-controls="ppipnTOSaddress"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><textarea name="PayPalIPN_tos_display_address" type="text" class="form-control" maxlength="400" rows=5 wrap=on>$PayPalIPN_tos_display_address</textarea></p>
<div class="collapse" id="ppipnTOSaddress">
  <div class="well">
    <p>Displays the required business address and contact information if the <b>Display TOS</b> option is enabled above.</p>
    <p>Should be legal address for the business location for full disclosure and other privacy requirements mandated by credit card companies as well as government enitities that require such notices.</p>
    <p>HTML formatting (for text color, font, spacing, etc) is accepted.</p>
  </div>
</div>

<p><hr></p>

<p><b>Top Message - on the PayPal IPN order verification page: &nbsp; <a data-toggle="collapse" href="#ppipnTopMessageVerifyPage" aria-expanded="false" aria-controls="ppipnTopMessageVerifyPage"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><textarea name="PayPalIPN_verify_message" type="text" class="form-control" maxlength="400" rows=3 wrap=on>$PayPalIPN_verify_message</textarea></p>
<div class="collapse" id="ppipnTopMessageVerifyPage">
  <div class="well">
    <p>Displays a top message on the order form confirmation page (where customer verifies form submission/details), the page just before going to the actual payment gateway.</p>
    <p>HTML formatting (for text color, font, spacing, etc) is accepted.</p>
    <p>Leave blank if not needed.</p>
  </div>
</div>

<p><hr></p>

<p><b>Cancel Message: &nbsp; <a data-toggle="collapse" href="#ppipnCancelMessage" aria-expanded="false" aria-controls="ppipnCancelMessage"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><textarea name="paypalipn_canceled" type="text" class="form-control" required maxlength="400" rows=3 wrap=on>$messages{'paypalipn_canceled'}</textarea></p>
<div class="collapse" id="ppipnCancelMessage">
  <div class="well">
    <p>Displays the cancel message to your customer if they cancel the payment process at the PayPal website.</p>
    <p>HTML formatting (for text color, font, spacing, etc) is accepted.</p>
  </div>
</div>

<p><br></p>
<h2 class='mgr-page-title'>Brand/Customize PayPal.com Checkout Experience</h2>
<p><br></p>

<p><b>Order Item Name to display on the PayPal site: &nbsp; <a data-toggle="collapse" href="#ppipnItemName" aria-expanded="false" aria-controls="ppipnItemName"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input name="PayPalIPN_order_name" type="text" class="form-control" maxlength="75" required value="$PayPalIPN_order_name"></p>
<div class="collapse" id="ppipnItemName">
  <div class="well">
    <p>Enter a generic item name for the order which is displayed on the PayPal site.</p>
    <p><b>Recommendation:</b> Use your business or site name.</p>
  </div>
</div>

<p><br></p>

<input type="hidden" name="system_edit_success" value="yes">
<input name="GatewaySettings" type="hidden" value="Submit">
<input type="hidden" name="gateway" value="PayPalIPN">
<button class="btn btn-primary btn-lg mgr-center" style="display: block;">Submit PayPal IPN Settings Updates</button>
<p><br><br><br></p>
</form>

ENDOFTEXT

    $page_output .= &$manager_page_footer;
    print $page_output;

}
#######################################################################
1;
