Add new checkout option with iframe below it in WooCommerce

I am trying to add another WooCommerce payment method called Piraeus Bank Gateway on checkout page and when selected, this payment method will load an iframe like below (the iframe will redirect to the banks site lets call it $url).

How can I declare the content that will be displayed below the Checkout option?

Read More

Image of what I need to do:
Image of what I need to do

Here is my code so far:

<?php

/*
  Plugin Name: pireaus_getaway
  Plugin URI: 
  Description: 
  Version: 1.0.0
  Author: 
  Author URI: 
  License: N/W
  License URI: 
*/

/*check if plugin runs inside wordpress if not exit*/
if (!defined('ABSPATH')) 
    exit;

add_action('plugins_loaded', 'initialise', 0);

function initialise(){
    if (!class_exists('WC_Payment_Gateway'))
        return;  /*check if WordPress WooCommerce Payment Exists if Not Exit*/

    load_plugin_textdomain('pireaus_getaway', false, dirname(plugin_basename(__FILE__)) . '/languages/');  /*Load the .MO files with the translations*/


    /* DEFINE GETAWAY CLASS */
    class WC_Piraeusbank_Gateway extends WC_Payment_Gateway {
        public function __construct() {
            global $woocommerce;

            /*================================================== Initialise the WC_Payment_Gateway with Values (USEFULL ONLY FOR WORDPRESS IDE) ========================================================================= */
            $this->id = 'piraeusbank_gateway';
            $this->icon = apply_filters('piraeusbank_icon', plugins_url('assets/PB_blue_GR.png', __FILE__));
            $this->has_fields = false;
            $this->notify_url = WC()->api_request_url('WC_Piraeusbank_Gateway');
            $this->method_description = __('Piraeus bank Payment Gateway allows you to accept payment through various channels such as Maestro, Mastercard, AMex cards, Diners  and Visa cards On your Woocommerce Powered Site.', 'pireaus_getaway');
            $this->redirect_page_id = $this->get_option('redirect_page_id');
            $this->method_title = 'Piraeus bank  Gateway';
            // Load the form fields.
            $this->init_form_fields();
            /*============================================================================================================================================================================================================ */



            /*================================================== Create transaction Table =================================================================================================================================*/
            global $wpdb; // create query later for table creation
            if ($wpdb->get_var("SHOW TABLES LIKE '" . $wpdb->prefix . "piraeusbank_transactions'") === $wpdb->prefix . 'piraeusbank_transactions') {
                // The database table exist
            } else {
                // Table does not exist
                $query = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'piraeusbank_transactions (id int(11) unsigned NOT NULL AUTO_INCREMENT, merch_ref varchar(50) not null, trans_ticket varchar(32) not null , timestamp datetime default null, PRIMARY KEY (id))';
                $wpdb->query($query);
            }
            // Load the settings.
            $this->init_settings();
            /*============================================================================================================================================================================================================ */

            /* get_option == A safe way of getting values for a named option from the options database table. If the desired option does not exist, or no value is associated with it, FALSE will be returned.] */
            // Define user set variables for Setting Of Woocommerce Piraeus GETAWAY in Admin Panel
            $this->title = $this->get_option('title');
            $this->description = $this->get_option('description');
            $this->pb_PayMerchantId = $this->get_option('pb_PayMerchantId');
            $this->pb_AcquirerId = $this->get_option('pb_AcquirerId');
            $this->pb_PosId = $this->get_option('pb_PosId');
            $this->pb_Username = $this->get_option('pb_Username');
            $this->pb_Password = $this->get_option('pb_Password');
            $this->pb_authorize = $this->get_option('pb_authorize');
            $this->pb_installments = $this->get_option('pb_installments');
            //Actions
            add_action('woocommerce_receipt_piraeusbank_gateway', array($this, 'receipt_page')); //receipt_page($order_id) == > returns receipt page for the order
            add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); // process_admin_options() == > Admin Panel Options Processing – Saves the options to the DB
            // Payment listener/API hook
            add_action('woocommerce_api_wc_piraeusbank_gateway', array($this, 'check_piraeusbank_response'));
        }  //end of construct !


        //========================================================CREATE THE HTML OF THE SETTING PAGE OF ADMIN PANEL (Contains Fuctions USED ONLY FOR ADMIN PANEL FORMATION) ============================================
        public function admin_options() {
                echo '<h3>' . __('Piraeus Bank Gateway', 'pireaus_getaway') . '</h3>';
                echo '<p>' . __('Piraeus Bank Gateway allows you to accept payment through various channels such as Maestro, Mastercard, AMex cards, Diners  and Visa cards.', 'pireaus_getaway') . '</p>';


                echo '<table class="form-table">';
                $this->generate_settings_html();
                echo '</table>';
        }
            // init_form_fields() ==> Create the Admin Panel form specifying the datatypes 
            //The WooCommerce settings API is used by shipping methods and payment gateways to display, save and load options.
        function init_form_fields() {       
                $this->form_fields = array(
                    'enabled' => array(
                        'title' => __('Enable/Disable', 'pireaus_getaway'),
                        'type' => 'checkbox',
                        'label' => __('Enable Piraeus Bank Gateway', 'pireaus_getaway'),
                        'description' => __('Enable or disable the gateway.', 'pireaus_getaway'),
                        'desc_tip' => true,
                        'default' => 'yes'
                    ),
                    'title' => array(
                        'title' => __('Title', 'pireaus_getaway'),
                        'type' => 'text',
                        'description' => __('This controls the title which the user sees during checkout.', 'pireaus_getaway'),
                        'desc_tip' => false,
                        'default' => __('Piraeus Bank Gateway', 'pireaus_getaway')
                    ),
                    'description' => array(
                        'title' => __('Description', 'pireaus_getaway'),
                        'type' => 'textarea',
                        'description' => __('This controls the description which the user sees during checkout.', 'pireaus_getaway'),
                        'default' => __('Pay Via Piraeus Bank: Accepts  Mastercard, Visa cards and etc.', 'pireaus_getaway')
                    ),
                    'pb_PayMerchantId' => array(
                        'title' => __('Piraeus Bank Merchant ID', 'pireaus_getaway'),
                        'type' => 'text',
                        'description' => __('Enter Your Piraeus Bank Merchant ID', 'pireaus_getaway'),
                        'default' => '',
                        'desc_tip' => true
                    ),
                    'pb_AcquirerId' => array(
                        'title' => __('Piraeus Bank Acquirer ID', 'pireaus_getaway'),
                        'type' => 'text',
                        'description' => __('Enter Your Piraeus Bank Acquirer ID', 'pireaus_getaway'),
                        'default' => '',
                        'desc_tip' => true
                    ),
                    'pb_PosId' => array(
                        'title' => __('Piraeus Bank POS ID', 'pireaus_getaway'),
                        'type' => 'text',
                        'description' => __('Enter your Piraeus Bank POS ID', 'pireaus_getaway'),
                        'default' => '',
                        'desc_tip' => true
                    ), 'pb_Username' => array(
                        'title' => __('Piraeus Bank Username', 'pireaus_getaway'),
                        'type' => 'text',
                        'description' => __('Enter your Piraeus Bank Username', 'pireaus_getaway'),
                        'default' => '',
                        'desc_tip' => true
                    ), 'pb_Password' => array(
                        'title' => __('Piraeus Bank Password', 'pireaus_getaway'),
                        'type' => 'text',
                        'description' => __('Enter your Piraeus Bank Password', 'pireaus_getaway'),
                        'default' => '',
                        'desc_tip' => true
                    ), 'pb_authorize' => array(
                        'title' => __('Pre-Authorize', 'pireaus_getaway'),
                        'type' => 'checkbox',
                        'label' => __('Enable to capture preauthorized payments', 'pireaus_getaway'),
                        'default' => 'yes',
                        'description' => __('Default payment method is Purchase, enable for Pre-Authorized payments. You will then need to accept them from Peiraeus Bank AdminTool', 'pireaus_getaway')
                    ),
                    'redirect_page_id' => array(
                        'title' => __('Return Page', 'pireaus_getaway'),
                        'type' => 'select',
                        'options' => $this->pb_get_pages('Select Page'),
                        'description' => __('URL of success page', 'pireaus_getaway')
                    ),
                    'pb_installments' => array(
                        'title' => __('Max Installments', 'pireaus_getaway'),
                        'type' => 'select',
                        'options' => $this->pb_get_installments('Select Installments'),
                        'description' => __('1 to 24 Installments,1 for one time payment. You must contact Peiraeus Bank first', 'pireaus_getaway')
                    )
                );
        }


        function pb_get_pages($title = false, $indent = true) {
                $wp_pages = get_pages('sort_column=menu_order');
                $page_list = array();
                if ($title)
                    $page_list[] = $title;
                foreach ($wp_pages as $page) {
                    $prefix = '';
                    // show indented child pages?
                    if ($indent) {
                        $has_parent = $page->post_parent;
                        while ($has_parent) {
                            $prefix .= ' - ';
                            $next_page = get_page($has_parent);
                            $has_parent = $next_page->post_parent;
                        }
                    }
                    // add to page list array array
                    $page_list[$page->ID] = $prefix . $page->post_title;
                }
                $page_list[-1] = __('Thank you page', 'pireaus_getaway');
                return $page_list;
        }


        function pb_get_installments($title = false, $indent = true) {      
                for ($i = 1; $i <= 24; $i++) {
                    $installment_list[$i] = $i;
                }
                return $installment_list;
        }
    /*============================================================================================================================================================================================================ */



    //===========================================================================ACTUAL CODE ======================================================================================================================

    function generate_piraeusbank_form($order_id) {
        global $woocommerce;
        global $wpdb;

        $order = new WC_Order($order_id);
        //echo $this->pb_authorize;

        if ($this->pb_authorize == "yes") {
            $requestType = '00';
            $ExpirePreauth = '30';
        } else {
            $requestType = '02';
            $ExpirePreauth = '0';
        }
        $installments = 1;
        try {
            $soap = new SoapClient(url1);
            $ticketRequest = array(
                'Username' => $this->pb_Username,
                'Password' => hash('md5', $this->pb_Password),
                'MerchantId' => $this->pb_PayMerchantId,
                'PosId' => $this->pb_PosId,
                'AcquirerId' => $this->pb_AcquirerId,
                'MerchantReference' => $order_id,
                'RequestType' => $requestType,
                'ExpirePreauth' => $ExpirePreauth,
                'Amount' => $order->get_total(),
                'CurrencyCode' => '978',
                'Installments' => $installments,
                'Bnpl' => '0',
                'Parameters' => ''
            );
            $xml = array(
                'Request' => $ticketRequest
            );
            $oResult = $soap->IssueNewTicket($xml);
            if ($oResult->IssueNewTicketResult->ResultCode == 0) {
                // store TranTicket in table    
                $wpdb->insert($wpdb->prefix . 'piraeusbank_transactions', array('trans_ticket' => $oResult->IssueNewTicketResult->TranTicket, 'merch_ref' => $order_id, 'timestamp' => current_time('mysql', 1)));
                $LanCode = "el-GR";
                return 
                    '<form action="' . esc_url(url) . '" method="post" id="pb_payment_form" target="output_frame">              
                        <input type="hidden" id="AcquirerId" name="AcquirerId" value="' . esc_attr($this->pb_AcquirerId) . '"/>
                        <input type="hidden" id="MerchantId" name="MerchantId" value="' . esc_attr($this->pb_PayMerchantId) . '"/>
                        <input type="hidden" id="PosID" name="PosID" value="' . esc_attr($this->pb_PosId) . '"/>
                        <input type="hidden" id="User" name="User" value="' . esc_attr($this->pb_Username) . '"/>
                        <input type="hidden" id="LanguageCode"  name="LanguageCode" value="' . $LanCode . '"/>
                        <input type="hidden" id="MerchantReference" name="MerchantReference"  value="' . esc_attr($order_id) . '"/>
                    <!-- Button Fallback -->
                    <div class="payment_buttons">
                        <input type="submit" class="button alt" id="submit_pb_payment_form" value="' . __('Pay via Pireaus Bank', 'pireaus_getaway') . '" /> <a class="button cancel" href="' . esc_url($order->get_cancel_order_url()) . '">' . __('Cancel order &amp; restore cart', 'pireaus_getaway') . '</a>

                    </div>
                    <script type="text/javascript">
                    jQuery(".payment_buttons").hide();
                    </script>
                </form>
                <iframe name="output_frame" width="800px" height="455"></iframe>
                ';
            } 
            else 
            {
                echo __('An error occured, please contact the Administrator', 'pireaus_getaway');
                echo ('Result code is '.$oResult->IssueNewTicketResult->ResultCode);
            }
        } catch (SoapFault $fault) {
            $order->add_order_note(__('Error' . $fault, ''));
            echo __('Error' . $fault, '');
        }
    }


     function process_payment($order_id) {

        /*
          get_permalink was used instead of $order->get_checkout_payment_url in redirect in order to have a fixed checkout page to provide to Piraeus Bank
         */

        $order = new WC_Order($order_id);
        return array(
            'result' => 'success',
            'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay'))))
        );
    }

    /**
     * Output for the order received page.
     * */
    function receipt_page($order) {
        echo '<p>' . __('Thank you - your order is now pending payment. You should be automatically redirected to Peiraeus Paycenter to make payment.', 'pireaus_getaway') . '</p>';
        echo $this->generate_piraeusbank_form($order);
    }

   /*============================================================================================================================================================================================================ */

}// END OF  class WordCommerce_Pireaus_Gateway

    //===========================================================================ADD PLUGIN TO ADMIN PANEL ========================================================================================================
    function piraeusbank_message() {
        $order_id = absint(get_query_var('order-received'));
        $order = new WC_Order($order_id);
        $payment_method = $order->payment_method;

        if (is_order_received_page() && ( 'piraeusbank_gateway' == $payment_method )) {

            $piraeusbank_message = get_post_meta($order_id, '_piraeusbank_message', true);
             if (!empty($piraeusbank_message)) {
            $message = $piraeusbank_message['message'];
            $message_type = $piraeusbank_message['message_type'];

            delete_post_meta($order_id, '_piraeusbank_message');


                wc_add_notice($message, $message_type);
            }
        }
    }

    add_action('wp', 'piraeusbank_message');

    function woocommerce_add_piraeusbank_gateway($methods) {
        $methods[] = 'WC_Piraeusbank_Gateway';
        return $methods;
    }

    add_filter('woocommerce_payment_gateways', 'woocommerce_add_piraeusbank_gateway');



    if (version_compare(WOOCOMMERCE_VERSION, "2.1") <= 0) {
        add_filter('plugin_action_links', 'piraeusbank_plugin_action_links', 10, 2);
        function piraeusbank_plugin_action_links($links, $file) {
            static $this_plugin;
            if (!$this_plugin) {
                $this_plugin = plugin_basename(__FILE__);
            }
            if ($file == $this_plugin) {
                $settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=woocommerce_settings&tab=payment_gateways&section=WC_piraeusbank_Gateway">Settings</a>';
                array_unshift($links, $settings_link);
            }
            return $links;
        }
    }
    else {
        add_filter('plugin_action_links', 'piraeusbank_plugin_action_links', 10, 2);
        function piraeusbank_plugin_action_links($links, $file) {
            static $this_plugin;
            if (!$this_plugin) {
                $this_plugin = plugin_basename(__FILE__);
            }
            if ($file == $this_plugin) {
                $settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wc-settings&tab=checkout&section=WC_Piraeusbank_Gateway">Settings</a>';
                array_unshift($links, $settings_link);
            }
            return $links;
        }
    }
    //===================================================================================================================================================================================================================
}

Thank you for your help.

Related posts

Leave a Reply