Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Eurobank WooCommerce Payment Gateway
- Plugin URI: https://www.enartia.com
- Description: Eurobank 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.
- Version: 1.4.1
- Author: Enartia
- Author URI: https://www.enartia.com
- License: GPL-3.0+
- License URI: http://www.gnu.org/licenses/gpl-3.0.txt
- */
- if (!defined('ABSPATH')) {
- exit;
- }
- require_once plugin_dir_path(__FILE__) . 'encryption.php';
- add_action('plugins_loaded', 'woocommerce_eurobank_init', 0);
- function woocommerce_eurobank_init()
- {
- if (!class_exists('WC_Payment_Gateway')) {
- return;
- }
- load_plugin_textdomain('woocommerce-eurobank-payment-gateway', false, dirname(plugin_basename(__FILE__)) . '/languages/');
- /**
- * Gateway class
- */
- class WC_Eurobank_Gateway extends WC_Payment_Gateway
- {
- public function __construct()
- {
- global $woocommerce;
- $this->id = 'eurobank_gateway';
- $this->icon = apply_filters('eurobank_icon', plugins_url('img/eurobank.png', __FILE__));
- $this->has_fields = true;
- $this->notify_url = WC()->api_request_url('WC_Eurobank_Gateway');
- $this->method_description = __('Eurobank 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.', 'woocommerce-eurobank-payment-gateway');
- $this->redirect_page_id = $this->get_option('redirect_page_id');
- $this->method_title = 'Eurobank Gateway';
- // Load the form fields.
- $this->init_form_fields();
- global $wpdb;
- if ($wpdb->get_var("SHOW TABLES LIKE '" . $wpdb->prefix . "eurobank_transactions'") === $wpdb->prefix . 'eurobank_transactions') {
- // The database table exist
- } else {
- // Table does not exist
- $query = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'eurobank_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();
- // Define user set variables
- $this->title = $this->get_option('title');
- $this->description = $this->get_option('description');
- $this->eb_PayMerchantId = $this->get_option('eb_PayMerchantId');
- $this->eb_PayMerchantKey = $this->get_option('eb_PayMerchantKey');
- $this->pb_installments = $this->get_option('pb_installments');
- //Actions
- add_action('woocommerce_receipt_eurobank_gateway', array($this, 'receipt_page'));
- add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
- // Payment listener/API hook
- add_action('woocommerce_api_wc_eurobank_gateway', array($this, 'check_eurobank_response'));
- }
- /**
- * Admin Panel Options
- * */
- public function admin_options()
- {
- echo '<h3>' . __('Eurobank Gateway', 'woocommerce-eurobank-payment-gateway') . '</h3>';
- echo '<p>' . __('Eurobank Gateway allows you to accept payment through various channels such as Maestro, Mastercard, AMex cards, Diners and Visa cards.', 'woocommerce-eurobank-payment-gateway') . '</p>';
- echo '<table class="form-table">';
- $this->generate_settings_html();
- echo '</table>';
- }
- /**
- * Initialise Gateway Settings Form Fields
- * */
- function init_form_fields()
- {
- $this->form_fields = array(
- 'enabled' => array(
- 'title' => __('Enable/Disable', 'woocommerce-eurobank-payment-gateway'),
- 'type' => 'checkbox',
- 'label' => __('Enable Eurobank Gateway', 'woocommerce-eurobank-payment-gateway'),
- 'description' => __('Enable or disable the gateway.', 'woocommerce-eurobank-payment-gateway'),
- 'desc_tip' => true,
- 'default' => 'yes',
- ),
- 'title' => array('title' => __('Title', 'woocommerce-eurobank-payment-gateway'), 'type' => 'text',
- 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce-eurobank-payment-gateway'), 'desc_tip' => false,
- 'default' => __('Eurobank Gateway', 'woocommerce-eurobank-payment-gateway')), 'description' => array('title' => __('Description', 'woocommerce-eurobank-payment-gateway'),
- 'type' => 'textarea', 'description' => __('This controls the description which the user sees during checkout.', 'woocommerce-eurobank-payment-gateway'),
- 'default' => __('Pay Via Eurobank: Accepts Mastercard, Visa cards and etc.', 'woocommerce-eurobank-payment-gateway')),
- 'eb_PayMerchantId' => array('title' => __('Eurobank Merchant ID', 'woocommerce-eurobank-payment-gateway'), 'type' => 'text',
- 'description' => __('Enter Your Eurobank Merchant ID', 'woocommerce-eurobank-payment-gateway'), 'default' => '', 'desc_tip' => true),
- 'eb_PayMerchantKey' => array('title' => __('Eurobank Merchant KEY', 'woocommerce-eurobank-payment-gateway'), 'type' => 'password',
- 'description' => __('Enter Your Eurobank Merchant KEY', 'woocommerce-eurobank-payment-gateway'), 'default' => '', 'desc_tip' => false),
- 'redirect_page_id' => array('title' => __('Return Page', 'woocommerce-eurobank-payment-gateway'), 'type' => 'select', 'options' => $this->pb_get_pages('Select Page'),
- 'description' => __('URL of success page', 'woocommerce-eurobank-payment-gateway')),
- 'pb_installments' => array(
- 'title' => __('Max Installments', 'woocommerce-eurobank-payment-gateway'),
- 'type' => 'select',
- 'options' => $this->pb_get_installments('Select Installments'),
- 'description' => __('1 to 24 Installments,1 for one time payment. You must contact Eurobank first', 'woocommerce-eurobank-payment-gateway'),
- ),
- );
- }
- public function get_option($key, $empty_value = null)
- {
- $option_value = parent::get_option($key, $empty_value);
- if ($key == 'eb_PayMerchantKey') {
- $decrypted = WC_Payment_Gateway_KeyEncryption::decrypt(base64_decode($option_value), substr(NONCE_KEY, 0, 32));
- $option_value = $decrypted;
- }
- return $option_value;
- }
- function payment_fields()
- {
- if ($description = $this->get_description()) {
- echo wpautop(wptexturize($description));
- }
- $installments = $this->pb_installments;
- if ($installments > 1) {
- $doseis_field = '<p class="form-row ">
- <label for="' . esc_attr($this->id) . '-card-doseis">' . __('Choose Installments', 'woocommerce-piraeusbank-payment-gateway') . ' <span class="required">*</span></label>
- <select id="' . esc_attr($this->id) . '-card-doseis" name="' . esc_attr($this->id) . '-card-doseis" class="input-select wc-credit-card-form-card-doseis">
- ';
- for ($i = 1; $i <= $installments; $i++) {
- $doseis_field .= '<option value="' . $i . '">' . $i . '</option>';
- }
- $doseis_field .= '</select>
- </p>';
- echo $doseis_field;
- }
- }
- public function validate_eb_PayMerchantKey_field($key, $value)
- {
- $encrypted = WC_Payment_Gateway_KeyEncryption::encrypt($value, substr(NONCE_KEY, 0, 32));
- return base64_encode($encrypted);
- }
- function pb_get_installments($title = false, $indent = true)
- {
- for ($i = 1; $i <= 24; $i++) {
- $installment_list[$i] = $i;
- }
- return $installment_list;
- }
- 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', 'woocommerce-eurobank-payment-gateway');
- return $page_list;
- }
- function calculate_digest($input)
- {
- $digest = base64_encode(sha1(($input), true));
- return $digest;
- }
- /**
- * Generate the Eurobank Payment button link
- * */
- function generate_eurobank_form($order_id)
- {
- global $woocommerce;
- global $wpdb;
- $order = new WC_Order($order_id);
- //echo $this->pb_authorize;
- $installments = 1;
- /* if ($this->pb_installments > 1) {
- $installments = intval($order->get_total() / 30);
- $installments = min($installments, $this->pb_installments);
- } */
- if (method_exists($order, 'get_meta')) {
- $installments = $order->get_meta('_doseis');
- if ($installments == '') {
- $installments = 1;
- }
- } else {
- $installments = get_post_meta($order_id, '_doseis', 1);
- }
- // store TranTicket in table
- $wpdb->delete($wpdb->prefix . 'eurobank_transactions', array('merch_ref' => $order_id));
- $wpdb->insert($wpdb->prefix . 'eurobank_transactions', array('trans_ticket' => $order_id, 'merch_ref' => $order_id, 'timestamp' => current_time('mysql', 1)));
- //redirect to payment
- wc_enqueue_js('
- $.blockUI({
- message: "' . esc_js(__('Thank you for your order. We are now redirecting you to Eurobank to make payment.', 'woocommerce-eurobank-payment-gateway')) . '",
- baseZ: 99999,
- overlayCSS:
- {
- background: "#fff",
- opacity: 0.6
- },
- css: {
- padding: "20px",
- zindex: "9999999",
- textAlign: "center",
- color: "#555",
- border: "3px solid #aaa",
- backgroundColor:"#fff",
- cursor: "wait",
- lineHeight: "24px",
- }
- });
- jQuery("#eb_payment_form").submit();
- ');
- $billing = $order->get_address();
- if ($installments > 1) {
- $form_data_array = array('mid' => esc_attr($this->eb_PayMerchantId), 'lang' => 'el', 'deviceCategory' => '0', 'orderid' => ($order_id), 'orderDesc' => 'Order #' . $order_id,
- 'orderAmount' => $order->get_total(), 'currency' => 'EUR', 'payerEmail' => $billing['email'],
- 'extInstallmentoffset' => 0, 'extInstallmentperiod' => $installments,
- 'confirmUrl' => get_site_url() . "/?wc-api=WC_Eurobank_Gateway&result=success",
- 'cancelUrl' => get_site_url() . "/?wc-api=WC_Eurobank_Gateway&result=failure");
- } else {
- $form_data_array = array('mid' => esc_attr($this->eb_PayMerchantId), 'lang' => 'el', 'deviceCategory' => '0', 'orderid' => ($order_id), 'orderDesc' => 'Order #' . $order_id,
- 'orderAmount' => $order->get_total(), 'currency' => 'EUR', 'payerEmail' => $billing['email'],
- 'confirmUrl' => get_site_url() . "/?wc-api=WC_Eurobank_Gateway&result=success",
- 'cancelUrl' => get_site_url() . "/?wc-api=WC_Eurobank_Gateway&result=failure");
- }
- $form_secret = $this->eb_PayMerchantKey;
- $form_data = utf8_encode(implode("", $form_data_array)) . $form_secret;
- $digest = $this->calculate_digest($form_data);
- /* $send_it_2 = "https://euro.test.modirum.com/vpos/shophandlermpi";
- if ($this -> test_mode != "yes") {
- $send_it_2 = "https://vpos.eurocommerce.gr/vpos/shophandlermpi";
- } */
- $send_it_2 = "https://vpos.eurocommerce.gr/vpos/shophandlermpi";
- $html = '<form action="' . esc_url($send_it_2) . '" method="post" id="eb_payment_form" target="_top" accept-charset="UTF-8">';
- foreach ($form_data_array as $key => $value) {
- $html .= '<input type="hidden" id="' . $key . '" name="' . $key . '" value="' . utf8_encode($value) . '"/>';
- }
- $html .= '<input type="hidden" id="digest" name="digest" value="' . esc_attr($digest) . '"/>';
- $html .= '</form>';
- return $html;
- }
- /**
- * Process the payment and return the result
- * */
- 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 Eurobank
- */
- $order = new WC_Order($order_id);
- $doseis = intval($_POST[esc_attr($this->id) . '-card-doseis']);
- if ($doseis > 0) {
- $this->generic_add_meta($order_id, '_doseis', $doseis);
- }
- 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 Eurobank Paycenter to make payment.', 'woocommerce-eurobank-payment-gateway') . '</p>';
- echo $this->generate_eurobank_form($order);
- }
- /**
- * Verify a successful Payment!
- * */
- function check_eurobank_response()
- {
- global $woocommerce;
- global $wpdb;
- //$post_data_keys=array('mid','orderid','status','orderAmount','digest');
- $mid = $_POST['mid'];
- $orderid = $_POST['orderid'];
- $orderAmount = $_POST['orderAmount'];
- $status = $_POST['status'];
- $post_digest = $_POST['digest'];
- $form_data = '';
- foreach ($_POST as $k => $v) {
- if (!in_array($k, array('_charset_', 'digest', 'submitButton'))) {
- $form_data .= $_POST[$k];
- }
- }
- $form_data .= $this->eb_PayMerchantKey;
- $digest = $this->calculate_digest($form_data);
- $order = new WC_Order($orderid);
- if ($digest != $post_digest) {
- $message = __('A technical problem occured. <br />The transaction wasn\'t successful, payment wasn\'t received.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'error';
- $pb_message = array('message' => $message, 'message_type' => $message_type);
- $this->generic_add_meta($orderid, '_eurobank_message', $pb_message);
- //Update the order status
- $order->update_status('failed', 'DIGEST');
- $checkout_url = $woocommerce->cart->get_checkout_url();
- wp_redirect($checkout_url);
- exit;
- }
- //AUTHORIZED, CAPTURED,CANCELED,REFUSED,ERROR
- if (isset($_GET['result']) && ($_GET['result'] == 'success')) {
- if ($status == 'ERROR') {
- $message = __('A technical problem occured. <br />The transaction wasn\'t successful, payment wasn\'t received.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'error';
- $pb_message = array('message' => $message, 'message_type' => $message_type);
- $this->generic_add_meta($orderid, '_eurobank_message', $pb_message);
- //Update the order status
- $order->update_status('failed', 'ERROR');
- $checkout_url = $woocommerce->cart->get_checkout_url();
- wp_redirect($checkout_url);
- exit;
- }
- $paymentRef = $_POST['paymentRef'];
- $txId = $_POST['txId'];
- if ($status == 'CAPTURED') {
- if ($order->status == 'processing') {
- $order->add_order_note(__('Payment Via Eurobank Bank<br />Transaction ID: ', 'woocommerce-eurobank-payment-gateway') . $paymentRef);
- //Add customer order note
- $order->add_order_note(__('Payment Received.<br />Your order is currently being processed.<br />We will be shipping your order to you soon.<br />Eurobank Bank ID: ', 'woocommerce-eurobank-payment-gateway') . $paymentRef, 1);
- // Reduce stock levels
- $order->reduce_order_stock();
- // Empty cart
- WC()->cart->empty_cart();
- $message = __('Thank you for shopping with us.<br />Your transaction was successful, payment was received.<br />Your order is currently being processed.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'success';
- } else {
- if ($order->has_downloadable_item()) {
- //Update order status
- $order->update_status('completed', __('Payment received, your order is now complete.', 'woocommerce-eurobank-payment-gateway'));
- //Add admin order note
- $order->add_order_note(__('Payment Via Eurobank Bank<br />Transaction ID: ', 'woocommerce-eurobank-payment-gateway') . $paymentRef);
- //Add customer order note
- $order->add_order_note(__('Payment Received.<br />Your order is now complete.<br />Eurobank Transaction ID: ', 'woocommerce-eurobank-payment-gateway') . $paymentRef, 1);
- $message = __('Thank you for shopping with us.<br />Your transaction was successful, payment was received.<br />Your order is now complete.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'success';
- } else {
- //Update order status
- $order->update_status('processing', __('Payment received, your order is currently being processed.', 'woocommerce-eurobank-payment-gateway'));
- //Add admin order note
- $order->add_order_note(__('Payment Via Eurobank Bank<br />Transaction ID: ', 'woocommerce-eurobank-payment-gateway') . $paymentRef);
- //Add customer order note
- $order->add_order_note(__('Payment Received.<br />Your order is currently being processed.<br />We will be shipping your order to you soon.<br />Eurobank Bank ID: ', 'woocommerce-eurobank-payment-gateway') . $paymentRef, 1);
- $message = __('Thank you for shopping with us.<br />Your transaction was successful, payment was received.<br />Your order is currently being processed.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'success';
- }
- $pb_message = array('message' => $message, 'message_type' => $message_type);
- $this->generic_add_meta($orderid, '_eurobank_message', $pb_message);
- // Reduce stock levels
- $order->reduce_order_stock();
- wc_add_notice($message, $message_type);
- // Empty cart
- WC()->cart->empty_cart();
- }
- } else if ($status == 'CANCELED') {
- $checkout_url = $woocommerce->cart->get_checkout_url();
- $message = __('Thank you for shopping with us. <br />However, the transaction wasn\'t successful, payment was cancelled.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'notice';
- wc_add_notice($message, $message_type);
- //wp_redirect($checkout_url);
- //exit ;
- } else { //Failed Response codes
- $message = __('Thank you for shopping with us. <br />However, the transaction wasn\'t successful, payment wasn\'t received.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'error';
- $pb_message = array('message' => $message, 'message_type' => $message_type);
- $this->generic_add_meta($orderid, '_eurobank_message', $pb_message);
- //wc_add_notice( $message, $message_type );
- $order->update_status('failed', 'UNKNOWN');
- }
- }
- if (isset($_GET['result']) && ($_GET['result'] == 'failure')) {
- $order = new WC_Order($orderid);
- $message = __('Thank you for shopping with us. <br />However, the transaction wasn\'t successful, payment wasn\'t received.', 'woocommerce-eurobank-payment-gateway');
- $message_type = 'error';
- // wc_add_notice( $message, $message_type );
- $transaction_id = $_POST['txId'];
- //Add Customer Order Note
- $order->add_order_note($message . '<br />Eurobank Transaction ID: ' . $transaction_id, 1);
- //Add Admin Order Note
- $order->add_order_note($message . '<br />Eurobank Transaction ID: ' . $transaction_id . '');
- //Update the order status
- $order->update_status('failed', $_POST['message']);
- $pb_message = array('message' => $message, 'message_type' => $message_type);
- $this->generic_add_meta($orderid, '_eurobank_message', $pb_message);
- }
- if ($this->redirect_page_id == "-1") {
- $redirect_url = $this->get_return_url($order);
- } else {
- $redirect_url = ($this->redirect_page_id == "" || $this->redirect_page_id == 0) ? $this->get_return_url($order) : get_permalink($this->redirect_page_id);
- }
- wp_redirect($redirect_url);
- exit;
- }
- function generic_add_meta($orderid, $key, $value)
- {
- $order = new WC_Order($orderid);
- if (method_exists($order, 'add_meta_data') && method_exists($order, 'save_meta_data')) {
- $order->add_meta_data($key, $value, true);
- $order->save_meta_data();
- } else {
- update_post_meta($orderid, $key, $value);
- }
- }
- }
- function eurobank_message()
- {
- $order_id = absint(get_query_var('order-received'));
- $order = new WC_Order($order_id);
- if (method_exists($order, 'get_payment_method')) {
- $payment_method = $order->get_payment_method();
- } else {
- $payment_method = $order->payment_method;
- }
- if (is_order_received_page() && ('eurobank_gateway' == $payment_method)) {
- if (method_exists($order, 'get_meta')) {
- $eurobank_message = $order->get_meta('_eurobank_message', true);
- } else {
- $eurobank_message = get_post_meta($order_id, '_eurobank_message');
- }
- $message = $eurobank_message['message'];
- $message_type = $eurobank_message['message_type'];
- if (method_exists($order, 'delete_meta_data')) {
- $order->delete_meta_data('_eurobank_message');
- $order->save_meta_data();
- } else {
- delete_post_meta($order_id, '_eurobank_message');
- }
- if (!empty($eurobank_message)) {
- wc_add_notice($message, $message_type);
- }
- }
- }
- add_action('wp', 'eurobank_message');
- /**
- * Add Eurobank Gateway to WC
- * */
- function woocommerce_add_eurobank_gateway($methods)
- {
- $methods[] = 'WC_Eurobank_Gateway';
- return $methods;
- }
- add_filter('woocommerce_payment_gateways', 'woocommerce_add_eurobank_gateway');
- /**
- * Add Settings link to the plugin entry in the plugins menu for WC below 2.1
- * */
- if (version_compare(WOOCOMMERCE_VERSION, "2.1") <= 0) {
- add_filter('plugin_action_links', 'eurobank_plugin_action_links', 10, 2);
- function eurobank_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§ion=WC_eurobank_Gateway">Settings</a>';
- array_unshift($links, $settings_link);
- }
- return $links;
- }
- }
- /**
- * Add Settings link to the plugin entry in the plugins menu for WC 2.1 and above
- * */else {
- add_filter('plugin_action_links', 'eurobank_plugin_action_links', 10, 2);
- function eurobank_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§ion=WC_Eurobank_Gateway">Settings</a>';
- array_unshift($links, $settings_link);
- }
- return $links;
- }
- }
- }
Add Comment
Please, Sign In to add comment