From f4020dee189d42ea903dd74d8b95a9ba9997ea3c Mon Sep 17 00:00:00 2001 From: jineshpv Date: Tue, 1 Oct 2024 14:49:24 +0530 Subject: [PATCH] Implemented a feature to enable or disable debug logging. All communication data is encrypted and stored in a log file, Implemented the ability to process void transactions, Implemented the ability to add mandatory or optional extra fees on the checkout page & Fixed the compatibility issue with the WooCommerce Subscription plugin --- CHANGELOG.md | 24 +- Makefile | 4 +- README.md | 46 +--- assets/js/mastercard-admin.js | 43 ++++ changelog.txt | 7 + composer.json | 2 +- includes/class-payment-gateway.php | 274 ++++++++++++++++++++--- includes/class-simplify-api-logger.php | 134 +++++++++++ includes/class-subscription-addon.php | 7 +- includes/embedded-template.php | 55 +++-- woocommerce-simplify-payment-gateway.php | 12 +- 11 files changed, 480 insertions(+), 128 deletions(-) create mode 100755 includes/class-simplify-api-logger.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cf1218..e41ea8b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # Changelog All notable changes to this project will be documented in this file. +## [2.4.3] - 2024-10-01 +### Added +- Implemented a feature to enable or disable debug logging. All communication data is encrypted and stored in a log file. +- Implemented the ability to process void transactions. +- Implemented the ability to add mandatory or optional extra fees on the checkout page. +### Fixed +- Fixed the compatibility issue with the WooCommerce Subscription plugin. + ## [2.4.2] - 2024-05-06 ### Added - Implemented a notification feature to alert the WordPress administrator whenever a new version is launched on GitHub. @@ -26,12 +34,10 @@ All notable changes to this project will be documented in this file. ### Fixed - “Amount mismatch” error is triggered when a user purchases a product on recent versions of WooCommerce (WordPress 5.9.3 / Woocommerce 6.4.1) - ## [2.3.1] - 2021-12-01 ### Fixed - Fixed a "false" Transaction Description issue that happened for some types of transactions - ## [2.3.0] - 2021-10-19 ### Changed - Add Embedded Payment Option @@ -43,67 +49,55 @@ All notable changes to this project will be documented in this file. - It's impossible to Refund the Payment if the plugin works in the Authorization Mode - Fix the issue with text transformation while save the settings - ## [2.2.0] - 2021-01-29 ## [2.1.2] - 2021-01-14 ### Fixed - Fixing an issue where performing a partial refund through woocommerce will instead refund the full amount. - ## [2.1.1] ### Fixed - Patch to allow for non-latin (e.g. Greek or Arabic) characters in the checkout - ## [2.1.0] ### Changed - Added support for transaction modes, Payment and Authorization - Added capture and reverse capability - ## [2.0.0] ### Changed - Standard integration has been removed - Compatibility with Wordpress 5.2 and WooCommerce 3.6 - ## [1.4.3] ### Fixed - Fix issue with amounts off by 1c. - ## [1.4.2] ### Changed - Adding Qatar to the supported countries list - ## [1.4.1] ### Fixed - Using hash_equals in return_handler helps prevent timing attacks. - ## [1.4.0] ### Changed - Display supported card types set by merchant - ## [1.3.0] ### Changed - Save hosted payment for subscription - ## [1.2.0] ### Changed - Adding "Australia" to the supported countries list - Pass currency code in Hosted Payments args - ## [1.1.0] ### Fixed - Fix the card on file not working for subscription payment - ## [1.0.0] ### Changed -- First version +- First version \ No newline at end of file diff --git a/Makefile b/Makefile index a41be08..62d6c85 100755 --- a/Makefile +++ b/Makefile @@ -1,3 +1,3 @@ all : - git archive HEAD --prefix=simplify-woocommerce-mastercard/ -o ./simplify-woocommerce-mastercard.zip &&\ - echo "\nCreated simplify-woocommerce-mastercard.zip\n" + git archive HEAD --prefix=simplifycommerce/ -o ./woocommerce-simplifycommerce.zip &&\ + echo "\nCreated woocommerce-simplifycommerce.zip\n" diff --git a/README.md b/README.md index 5d12693..e0505e0 100755 --- a/README.md +++ b/README.md @@ -1,14 +1,8 @@ # Simplify Commerce Payment Gateway for WooCommerce -The Simplify Commerce payment gateway plugin for WooCommerce 7.6+ lets you to take card payments directly on your WooCommerce store. -

-Fingent logo  MC logo -

+The Simplify Commerce payment gateway plugin for WooCommerce 7.3+ lets you to take card payments directly on your WooCommerce store. -## Overview Section -Mastercard Inc. is the second-largest payment-processing corporation worldwide. It offers a range of payment transaction processing and other related payment services. By connecting individuals, businesses, and organizations in more than 210 countries and territories today, we're unlocking opportunities for more people in more places for generations. This WooCommerce module adds different payment methods to the WooCommerce checkout, enabling credit and debit card payments to be accepted securely on your WooCommerce-powered website. - -Payments through this module are processed securely via the Mastercard Payment Gateway. This ensures that card data is managed in compliance with all legal requirements. The gateway monitors every transaction and handles sensitive payment data on PCI Level 1 certified servers, simplifying PCI compliance for your business. +The Standard payment form in some of the previous releases is now deprecated and out of support. ## Support @@ -24,42 +18,12 @@ For any issues or enhancement requests you have with this plugin, please raise t **Minimum Requirements** -* WooCommerce 7.6 or greater +* WooCommerce 7.3 or greater * PHP version 7.4 or greater * cURL -## Compatibility -The module has been tested with the WooCommerce versions: - -- 7.6.1 -- 7.7.2 -- 7.8.0 -- 7.9.0 -- 8.0.1 -- 8.1.1 -- 8.2.1 -- 8.3.0 -- 8.4.1 -- 8.5.2 -- 8.7.1 -- 8.0.0 -- 9.0.2 -- 9.1.4 - -The module has been tested with the WordPress versions: - -- 5.2.4 -- 5.3.2 -- 5.6.0 -- 5.7.2 -- 5.9.1 -- 6.0.3 -- 6.1.2 -- 6.2.2 -- 6.3.1 -- 6.4.2 -- 6.5.2 -- 6.6.1 +The module has been tested and is supported with the following versions of WooCommerce: +7.3 - 7.7.1 ## Documentation The official documentation for this module is available on: [https://mpgs.fingent.wiki/simplify-commerce/simplify-commerce-payment-gateway-for-woocommerce/getting-started/](https://mpgs.fingent.wiki/simplify-commerce/simplify-commerce-payment-gateway-for-woocommerce/getting-started/) diff --git a/assets/js/mastercard-admin.js b/assets/js/mastercard-admin.js index 1195aca..d0aaf5d 100755 --- a/assets/js/mastercard-admin.js +++ b/assets/js/mastercard-admin.js @@ -27,6 +27,49 @@ jQuery(function ($) { gateway_url.hide(); } }).change(); + + $( '#woocommerce_simplify_commerce_handling_fee_amount' ).before( '' ); + $( '#handling_fee_amount_label' ).css({ "width": "35px", "height": "31px", "line-height": "30px", "background-color": "#eaeaea", "text-align": "center", "position": "absolute", "left": "1px", "top": "1px", "border-radius": "3px 0 0 3px" }).parent().css( "position", "relative" ); + $( '#woocommerce_simplify_commerce_handling_fee_amount' ).css( "padding-left", "45px" ); + if( $( '#woocommerce_simplify_commerce_hf_amount_type' ).val() == 'fixed' ) { + $( '#handling_fee_amount_label' ).html( wcSettings.currency.symbol ); + } else { + $( '#handling_fee_amount_label' ).html( '%' ); + } + + $('#woocommerce_simplify_commerce_hf_amount_type').on('change', function () { + if( $( this ).val() == 'fixed' ) { + $( '#handling_fee_amount_label' ).html( wcSettings.currency.symbol ); + } else { + $( '#handling_fee_amount_label' ).html( '%' ); + } + }).change(); + $( '#woocommerce_simplify_commerce_handling_fee_amount' ).on( 'keypress', function(e) { + var charCode = ( e.which ) ? e.which : e.keyCode; + if ( charCode == 46 || charCode == 8 || charCode == 9 || charCode == 27 || charCode == 13 || + ( charCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || + ( charCode == 67 && ( e.ctrlKey === true || e.metaKey === true ) ) || + ( charCode == 86 && ( e.ctrlKey === true || e.metaKey === true ) ) || + ( charCode == 88 && ( e.ctrlKey === true || e.metaKey === true ) ) || + // Allow: home, end, left, right + ( charCode >= 35 && charCode <= 39 ) ) { + return; + } + + if ( ( charCode < 48 || charCode > 57 ) && charCode !== 46 ) { + e.preventDefault(); + } + + if ( charCode === 46 && $( this ).val().indexOf( '.' ) !== -1 ) { + e.preventDefault(); + } + }); + $( '#woocommerce_simplify_commerce_handling_fee_amount' ).on( 'input', function() { + var value = this.value; + if ( !/^\d*\.?\d*$/.test( value ) ) { + this.value = value.substring( 0, value.length - 1 ); + } + }); } }; wc_mastercard_admin.init(); diff --git a/changelog.txt b/changelog.txt index f9a6ef8..cccbc8d 100755 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,12 @@ == Simplify - Mastercard Payment Gateway Services Changelog == +2024-10-01 - version 2.4.3 + +* Add - Implemented a feature to enable or disable debug logging. All communication data is encrypted and stored in a log file. +* Add - Implemented the ability to process void transactions. +* Add - Implemented the ability to add mandatory or optional extra fees on the checkout page. +* Fix - Fixed the compatibility issue with the WooCommerce Subscription plugin. + 2024-05-06 - version 2.4.2 * Add - Implemented a notification feature to alert the WordPress administrator whenever a new version is launched on GitHub. diff --git a/composer.json b/composer.json index 6aee7c2..c42af7d 100755 --- a/composer.json +++ b/composer.json @@ -1,4 +1,4 @@ { - "version": "2.4.2", + "version": "2.4.1", "type": "woocommerce-plugin" } diff --git a/includes/class-payment-gateway.php b/includes/class-payment-gateway.php index e49eaf1..87384f0 100755 --- a/includes/class-payment-gateway.php +++ b/includes/class-payment-gateway.php @@ -19,20 +19,24 @@ exit; // Exit if accessed directly } -define( 'MPGS_SIMPLIFY_MODULE_VERSION', '2.4.2' ); +define( 'MPGS_SIMPLIFY_MODULE_VERSION', '2.4.3' ); +define( 'MPGS_SIMPLIFY_SDK_VERSION', '1.7.0' ); class WC_Gateway_Simplify_Commerce extends WC_Payment_Gateway_CC { const ID = 'simplify_commerce'; - const TXN_MODE_PURCHASE = 'purchase'; + const TXN_MODE_PURCHASE = 'purchase'; const TXN_MODE_AUTHORIZE = 'authorize'; - const INTEGRATION_MODE_MODAL = 'modal'; + const INTEGRATION_MODE_MODAL = 'modal'; const INTEGRATION_MODE_EMBEDDED = 'embedded'; - const SIP_HOST = 'www.simplify.com'; + const SIP_HOST = 'www.simplify.com'; const SIP_CUSTOM = 'custom'; + const HF_FIXED = 'fixed'; + const HF_PERCENTAGE = 'percentage'; + /** * @var string */ @@ -63,6 +67,23 @@ class WC_Gateway_Simplify_Commerce extends WC_Payment_Gateway_CC { */ protected $is_modal_integration_model; + /** + * @var string + */ + protected $logging_level; + + /** + * @var string + */ + protected $hash; + + /** + * Handling fees + * + * @var bool + */ + protected $hf_enabled = null; + /** * Constructor. */ @@ -114,6 +135,9 @@ public function __construct() { $this->public_key = $this->sandbox === 'no' ? $this->get_option( 'public_key' ) : $this->get_option( 'sandbox_public_key' ); $this->private_key = $this->sandbox === 'no' ? $this->get_option( 'private_key' ) : $this->get_option( 'sandbox_private_key' ); $this->is_modal_integration_model = $this->get_option( 'integration_mode' ) === self::INTEGRATION_MODE_MODAL; + $this->logging_level = $this->get_debug_logging_enabled() ? true : false; + $this->hash = hash( 'sha256', $this->public_key . $this->private_key ); + $this->hf_enabled = $this->get_option( 'hf_enabled', false ); $this->init_simplify_sdk(); @@ -143,6 +167,20 @@ public function __construct() { 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); + + add_action( + 'wp_ajax_download_log', + array( $this, 'download_decrypted_log' ) + ); + + add_filter( + 'woocommerce_admin_order_should_render_refunds', + array( $this, 'admin_order_should_render_refunds' ), 10, 3 + ); + add_action( + 'woocommerce_cart_calculate_fees', + array( $this, 'add_handling_fee' ) + ); } /** @@ -150,10 +188,6 @@ public function __construct() { */ public function admin_scripts() { - if( 'woocommerce_page_wc-orders' === get_current_screen()->id || 'shop_order' === get_current_screen()->id ) { - add_action( 'admin_head', array( $this, 'woocommerce_simplify_custom_styles' ) ); - } - if ( 'woocommerce_page_wc-settings' !== get_current_screen()->id ) { return; } @@ -163,16 +197,52 @@ public function admin_scripts() { } /** - * @return void + * Check if debug logging is enabled. + * + * @return bool True if debug logging is enabled, false otherwise. */ + protected function get_debug_logging_enabled() { + if ( 'yes' === $this->get_option( 'debug', false ) ) { + + $filename = WP_CONTENT_DIR . '/mastercard_simplify.log'; + if ( !is_writable( $filename ) ) { + @chmod( $filename, 0644 ); + } - public function woocommerce_simplify_custom_styles() { - $order_id = WC_Gateway_Simplify_Commerce_Loader::get_order_id(); - $order = new WC_Order( $order_id ); + if ( ! file_exists( $filename ) ) { + file_put_contents( $filename, '' ); + chmod( $filename, 0644 ); + } + + return true; + } + + return false; + } - if ( 'refunded' === $order->get_status() ) { - echo ''; + /** + * Determines whether the admin order page should render refunds. + * + * This function is used to check if refunds should be displayed on the admin order page. + * It can be used to add custom logic or conditions for rendering refunds in the order details view. + * + * @param bool $render_refunds Indicates whether refunds should be rendered. + * @param int $order_id The ID of the order being viewed. + * @param WC_Order $order The order object for which the refunds are being checked. + * @return bool Updated value of $render_refunds indicating whether refunds should be rendered. + */ + public function admin_order_should_render_refunds( $render_refunds, $order_id, $order ) { + $auth_code = $order->get_meta( '_simplify_authorization' ); + if ( $auth_code ) { + if( + ( 'simplify_commerce' === $order->get_payment_method() && 'refunded' === $order->get_status() ) || + ( 'simplify_commerce' === $order->get_payment_method() && empty( get_post_meta( $order_id, '_simplify_order_captured', true ) ) ) + ) { + return false; + } } + + return $render_refunds; } /** @@ -196,7 +266,7 @@ public function capture_authorized_order() { throw new Exception( 'Invalid or missing authorization code' ); } - $payment = Simplify_Payment::createPayment( array( + $data = array( 'authorization' => $authCode, 'reference' => $order->get_id(), 'currency' => strtoupper( $order->get_currency() ), @@ -205,7 +275,10 @@ public function capture_authorized_order() { __( 'Order #%s', 'woocommerce-gateway-simplify-commerce' ), $order->get_order_number() ), - ) ); + ); + $this->log( 'Capture Request', json_encode( $data ) ); + $payment = Simplify_Payment::createPayment( $data ); + $this->log( 'Capture Response', $payment ); if ( $payment->paymentStatus === 'APPROVED' ) { $this->process_capture_order_status( $order, $payment->id ); @@ -330,18 +403,9 @@ public function void_authorized_order() { $authTxn = Simplify_Authorization::findAuthorization( $authCode ); $authTxn->deleteAuthorization(); - - $order->add_order_note( sprintf( __( 'Gateway reverse authorization (ID: %s)', - 'woocommerce-gateway-simplify-commerce' ), - $authCode ) ); - - wc_create_refund( array( - 'order_id' => $order->get_id(), - 'reason' => 'Reverse', - 'refund_payment' => false, - 'restock_items' => true, - 'amount' => $order->get_remaining_refund_amount(), - ) ); + $order->update_status( 'cancelled', sprintf( __( 'Gateway reverse authorization (ID: %s)', + 'woocommerce-gateway-simplify-commerce' ), + $authCode ) ); if ( wp_get_referer() || 'yes' !== WC_Gateway_Simplify_Commerce_Loader::is_hpos() ) { wp_safe_redirect( wp_get_referer() ); @@ -483,7 +547,23 @@ public function is_available() { * Initialise Gateway Settings Form Fields. */ public function init_form_fields() { + $download_url = add_query_arg( + array( + 'action' => 'download_log', + '_wpnonce' => wp_create_nonce( 'mpgs_download_log' ) + ), admin_url( 'admin-ajax.php' ) ); + $this->form_fields = array( + 'heading' => array( + 'title' => null, + 'type' => 'title', + 'description' => sprintf( + /* translators: 1. MPGS Simplify module vesion, 2. MPGS Simplify SDK version. */ + __( 'Plugin version: %1$s
SDK version: %2$s', 'woocommerce-gateway-simplify-commerce' ), + MPGS_SIMPLIFY_MODULE_VERSION, + MPGS_SIMPLIFY_SDK_VERSION + ), + ), 'enabled' => array( 'title' => __( 'Enable/Disable', 'woocommerce-gateway-simplify-commerce' ), 'label' => __( @@ -565,6 +645,18 @@ public function init_form_fields() { 'woocommerce-gateway-simplify-commerce' ), ), + 'debug' => array( + 'title' => __( 'Debug Logging', 'woocommerce-gateway-simplify-commerce' ), + 'label' => __( 'Enable Debug Logging', 'woocommerce-gateway-simplify-commerce' ), + 'type' => 'checkbox', + 'description' => sprintf( + /* translators: Gateway API Credentials */ + __( 'All communications with the Simplify Mastercard Gateway are encrypted and logged in the ./wp-content/mastercard_simplify.log. The decrypted log file can be downloaded here.', 'woocommerce-gateway-simplify-commerce' ), + $download_url + ), + 'default' => 'no', + + ), 'sandbox' => array( 'title' => __( 'Sandbox', 'woocommerce-gateway-simplify-commerce' ), 'label' => __( 'Enable Sandbox Mode', 'woocommerce-gateway-simplify-commerce' ), @@ -615,6 +707,41 @@ public function init_form_fields() { 'default' => '', 'desc_tip' => true ), + 'handling_fee' => array( + 'title' => __( 'Handling Fee', 'mastercard' ), + 'type' => 'title', + 'description' => __( 'The handling amount for the order, including taxes on the handling.', 'mastercard' ), + ), + 'hf_enabled' => array( + 'title' => __( 'Enable/Disable', 'mastercard' ), + 'label' => __( 'Enable', 'mastercard' ), + 'type' => 'checkbox', + 'description' => '', + 'default' => 'no', + ), + 'handling_text' => array( + 'title' => __( 'Handling Fee Text', 'mastercard' ), + 'type' => 'text', + 'description' => __( 'Display text for handling fee.', 'mastercard' ), + 'default' => '', + 'css' => 'min-height: 33px;' + ), + 'hf_amount_type' => array( + 'title' => __( 'Applicable Amount Type', 'mastercard' ), + 'type' => 'select', + 'options' => array( + self::HF_FIXED => __( 'Fixed', 'mastercard' ), + self::HF_PERCENTAGE => __( 'Percentage', 'mastercard' ), + ), + 'default' => self::HF_FIXED, + ), + 'handling_fee_amount' => array( + 'title' => __( 'Amount', 'mastercard' ), + 'type' => 'text', + 'description' => __( 'The total amount for handling fee; Eg: 10.00 or 10%.', 'mastercard' ), + 'default' => '', + 'css' => 'min-height: 33px;' + ) ); } @@ -764,7 +891,6 @@ public function do_payment( $order, $amount = 0, $token = array() ) { } try { - // Create customer $customer = Simplify_Customer::createCustomer( array( 'email' => $order->get_billing_email(), @@ -790,7 +916,9 @@ public function do_payment( $order, $amount = 0, $token = array() ) { } $data = array_merge( $data, $token ); + $this->log( 'Payment Request', json_encode( $data ) ); $payment = Simplify_Payment::createPayment( $data ); + $this->log( 'Payment Response', $payment ); } catch ( Exception $e ) { $error_message = $e->getMessage(); @@ -881,7 +1009,6 @@ protected function process_hosted_payments( $order ) { */ public function process_payment( $order_id ) { $order = wc_get_order( $order_id ); - return $this->process_hosted_payments( $order ); } @@ -960,6 +1087,29 @@ protected function get_total( $order ) { return $this->get_total_amount( $order->get_total() ); } + /** + * This function processes the admin options. + * + * @return array $saved Admin Options. + */ + public function process_admin_options() { + $saved = parent::process_admin_options(); + if( 'simplify_commerce' === $this->id ) { + static $error_added = false; + if( isset( $this->settings['hf_amount_type'] ) && 'percentage' === $this->settings['hf_amount_type'] ) { + if ( absint( $this->settings['handling_fee_amount'] ) > 100 ) { + if ( ! $error_added ) { + WC_Admin_Settings::add_error( __( 'The maximum allowable percentage is 100.', 'woocommerce-gateway-simplify-commerce' ) ); + $error_added = true; + } + $this->update_option( 'handling_fee_amount', 100 ); + } + } + } + + return $saved; + } + /** * Receipt page. * @@ -1150,6 +1300,7 @@ protected function authorize( $order, $card_token, $amount ) { ) ); $order_builder = new Mastercard_Simplify_CheckoutBuilder( $order ); + $data = array( 'amount' => $amount, 'token' => $card_token, @@ -1161,12 +1312,13 @@ protected function authorize( $order, $card_token, $amount ) { ), 'order' => $order_builder->getOrder(), ); - + $this->log( 'Authorize Request', json_encode( $data ) ); if ( is_object( $customer ) && '' != $customer->id ) { $data['customer'] = wc_clean( $customer->id ); } $authorization = Simplify_Authorization::createAuthorization( $data ); + $this->log( 'Authorize Response', $authorization ); return $this->process_authorization_order_status( $order, @@ -1286,8 +1438,9 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { 'reason' => $reason ?: $defaultRefundReason, 'reference' => $order_id, ); - + $this->log( 'Refund Request', json_encode( $refund_data ) ); $refund = Simplify_Refund::createRefund( $refund_data ); + $this->log( 'Refund Response', $refund ); if ( 'APPROVED' === $refund->paymentStatus ) { $order->add_order_note( @@ -1318,4 +1471,61 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { return false; } + + /** + * Logs a message with a specific text. + * + * @param string $text The text to include in the log. + * @param mixed $message The message or data to be logged. + */ + public function log( $text, $message ) { + if( $this->logging_level ) { + $logger = new Mastercard_Simplify_Api_Logger( $this->hash ); + $message = date( 'Y-m-d g:i a' ) . ' : ' . $text . ' :- ' . $message; + $logger->write_encrypted_log( $message ); + } + } + + /** + * Handles the download of the decrypted log file. + * + * This function initiates the process to allow users to download + * a decrypted log file. It ensures proper file access and + * sets appropriate headers for file download. + */ + public function download_decrypted_log() { + if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'mpgs_download_log' ) ) { + $logger = new Mastercard_Simplify_Api_Logger( $this->hash ); + $logger->read_decrypted_log(); + wp_die(); + } else { + wp_safe_redirect( admin_url( 'page=wc-settings&tab=checkout§ion=simplify_commerce' ) ); + wp_die(); + } + } + + /** + * Adds a handling fee to the WooCommerce cart calculation. + * + * This ensures that the handling fee is added during the cart calculation process. + */ + public function add_handling_fee() { + if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { + return; + } + + if ( isset( $this->hf_enabled ) && 'yes' === $this->hf_enabled ){ + $handling_text = $this->get_option( 'handling_text' ); + $amount_type = $this->get_option( 'hf_amount_type' ); + $handling_fee = $this->get_option( 'handling_fee_amount' ) ? $this->get_option( 'handling_fee_amount' ) : 0; + + if ( self::HF_PERCENTAGE === $amount_type ) { + $surcharge = (float)( WC()->cart->cart_contents_total ) * ( (float) $handling_fee / 100 ); + } else { + $surcharge = $handling_fee; + } + + WC()->cart->add_fee( $handling_text, $surcharge, true, '' ); + } + } } diff --git a/includes/class-simplify-api-logger.php b/includes/class-simplify-api-logger.php new file mode 100755 index 0000000..c37ae12 --- /dev/null +++ b/includes/class-simplify-api-logger.php @@ -0,0 +1,134 @@ +cipher_algo = "AES-256-CBC"; + $this->cipher = $hash; + $this->filename = WP_CONTENT_DIR . '/mastercard_simplify.log'; + } + + /** + * Encrypts a given log entry. + * + * This function takes a plain text log entry as input and returns the encrypted version of the log. + * The encryption method and key should be securely managed to ensure the confidentiality of the log data. + * + * @param string $plain_text The log entry in plain text format. + * @return string The encrypted log entry. + */ + public function encrypt_log( $plain_text ) { + $iv_len = openssl_cipher_iv_length( $this->cipher_algo ); + $iv = openssl_random_pseudo_bytes( $iv_len ); + $cipher_text = openssl_encrypt( $plain_text, $this->cipher_algo, $this->cipher, $options = 0, $iv ); + $cipher_text_iv = base64_encode( $iv . $cipher_text ); + + return $cipher_text_iv; + } + + /** + * Encrypts and writes a log message to a secure log file. + * + * @param string $message The log message to be encrypted and stored. + */ + public function write_encrypted_log( $message ) { + $encrypted_message = $this->encrypt_log( $message, $this->cipher ); + file_put_contents( $this->filename, rtrim( $encrypted_message ) . PHP_EOL, FILE_APPEND ); + } + + /** + * Decrypts a log entry using the provided cipher text and initialization vector (IV). + * + * @param string $cipher_text_iv The encrypted log entry, including the initialization vector. + * @return string The decrypted log entry. + */ + public function decrypt_log( $cipher_text_iv ) { + $iv_len = openssl_cipher_iv_length( $this->cipher_algo ); + $cipher_text_iv = base64_decode( $cipher_text_iv ); + $iv = substr( $cipher_text_iv, 0, $iv_len ); + $cipher_text = substr( $cipher_text_iv, $iv_len ); + $original_plaintext = openssl_decrypt( $cipher_text, $this->cipher_algo, $this->cipher, $options = 0, $iv ); + + return $original_plaintext; + } + + /** + * Reads and processes the decrypted log file. + * + * This function handles the reading of the log file after it has been decrypted. + * It assumes the file is in a readable format and will process its contents accordingly. + * Make sure the file has been decrypted before calling this function. + * + * @return void + */ + public function read_decrypted_log() { + $decrypted_log_data = []; + $log_entries = file( $this->filename, FILE_IGNORE_NEW_LINES ); + + if( $log_entries ) { + foreach ( $log_entries as $cipher_text ) { + if ( ! empty( $cipher_text ) ) { + $decrypted_message = $this->decrypt_log( $cipher_text ); + $decrypted_log_data[] = rtrim( $decrypted_message ) . PHP_EOL; + } + } + + if( $decrypted_log_data ) { + $decrypted_log_data = implode( '', $decrypted_log_data ); + } + } + + header( 'Content-Type: text/plain' ); + header( 'Content-Disposition: attachment; filename="mastercard_simplify.log"' ); + header( 'Content-Length: ' . strlen( $decrypted_log_data ) ); + echo $decrypted_log_data; + } +} diff --git a/includes/class-subscription-addon.php b/includes/class-subscription-addon.php index a87913c..769aca3 100755 --- a/includes/class-subscription-addon.php +++ b/includes/class-subscription-addon.php @@ -386,7 +386,10 @@ public function process_subscription_payment( $order, $amount = 0, $token = arra // Charge the customer $data = array( 'amount' => $this->get_total_amount( $amount ), // In cents. - 'description' => sprintf( __( '%s - Order #%s', 'woocommerce-gateway-simplify-commerce' ), $order->get_order_number() ), + 'description' => sprintf( + __( 'Order #%s', 'woocommerce-gateway-simplify-commerce' ), + $order->get_order_number() + ), 'currency' => strtoupper( get_woocommerce_currency() ), 'reference' => $order->get_id() ); @@ -440,7 +443,7 @@ public function process_subscription_payment( $order, $amount = 0, $token = arra * @param WC_Order $renewal_order A WC_Order object created to record the renewal payment. */ public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) { - $result = $this->process_subscription_payment( $renewal_order, $amount_to_charge ); + $result = $this->process_subscription_payment( $renewal_order, $amount_to_charge, array() ); if ( is_wp_error( $result ) ) { $renewal_order->update_status( 'failed', diff --git a/includes/embedded-template.php b/includes/embedded-template.php index 2675d68..b1cd028 100755 --- a/includes/embedded-template.php +++ b/includes/embedded-template.php @@ -23,23 +23,23 @@ $url_query = parse_url( $redirect_url, PHP_URL_QUERY ); $url_query_parts = $url_query ? explode( '&', $url_query ) : []; - ?> - - - - - -
diff --git a/woocommerce-simplify-payment-gateway.php b/woocommerce-simplify-payment-gateway.php index 0e9bed7..6dc6727 100755 --- a/woocommerce-simplify-payment-gateway.php +++ b/woocommerce-simplify-payment-gateway.php @@ -6,15 +6,14 @@ * Author: Fingent Global Solutions Pvt. Ltd. * Author URI: https://www.fingent.com/ * Text Domain: woocommerce-gateway-simplify-commerce - * Version: 2.4.2 - * - * Requires at least: 6.0 - * Tested up to: 6.4.3 + * Version: 2.4.3 + * Requires at least: 5.6.0 + * Tested up to: 6.6.1 * Requires PHP: 7.4 * php version 8.1 * * WC requires at least: 7.6 - * WC tested up to: 8.5.2 + * WC tested up to: 9.1.4 * * Copyright (c) 2019-2026 Mastercard * @@ -117,6 +116,7 @@ public function init() { require_once plugin_basename( '/includes/class-simplify-checkout-builder.php' ); require_once plugin_basename( '/includes/class-gateway-notification.php' ); + require_once plugin_basename( '/includes/class-simplify-api-logger.php' ); // Don't hook anything else in the plugin if we're in an incompatible environment if ( self::get_environment_warning() ) { @@ -142,7 +142,7 @@ public function init() { 'woocommerce-gateway-simplify-commerce' ); $actions[ WC_Gateway_Simplify_Commerce::ID . '_void_payment' ] = __( - 'Reverse Authorization', + 'Void', 'woocommerce-gateway-simplify-commerce' ); }