From 5ea31e1d969a32530934f02cf9f78401f10ea13e Mon Sep 17 00:00:00 2001 From: Aashish Date: Wed, 2 Oct 2024 17:36:32 +0700 Subject: [PATCH 1/2] Removed code related to the option to enable secure form. --- .../omise-myaccount-card-handler.js | 103 +++--------------- .../javascripts/omise-payment-credit-card.js | 14 ++- .../javascripts/omise-payment-form-handler.js | 62 +---------- includes/class-omise-wc-myaccount.php | 2 - .../abstract-omise-payment-base-card.php | 13 --- .../class-omise-payment-creditcard.php | 22 +--- omise-woocommerce.php | 9 +- templates/myaccount/my-card.php | 26 ++--- templates/payment/form.php | 49 +++------ .../blocks/gateways/traits/mock-gateways.php | 6 +- .../class-omise-payment-creditcard-test.php | 1 - 11 files changed, 56 insertions(+), 251 deletions(-) diff --git a/assets/javascripts/omise-myaccount-card-handler.js b/assets/javascripts/omise-myaccount-card-handler.js index da0072dd..edcee31b 100755 --- a/assets/javascripts/omise-myaccount-card-handler.js +++ b/assets/javascripts/omise-myaccount-card-handler.js @@ -60,12 +60,8 @@ } }); - if(Boolean(parseInt(omise_params.secure_form_enabled))) { - hideError(); - OmiseCard.requestCardToken() - } else { - saveCardTraditional() - } + hideError(); + OmiseCard.requestCardToken() } $(".delete_card").click(function(event){ @@ -87,71 +83,6 @@ create_card(); }); - function saveCardTraditional() { - let errors = [], - omise_card = {}, - omise_card_number_field = 'number', - omise_card_fields = { - 'name' : $( '#omise_card_name' ), - 'number' : $( '#omise_card_number' ), - 'expiration_month' : $( '#omise_card_expiration_month' ), - 'expiration_year' : $( '#omise_card_expiration_year' ), - 'security_code' : $( '#omise_card_security_code' ) - }; - - $.each( omise_card_fields, function( index, field ) { - omise_card[ index ] = (index === omise_card_number_field) ? field.val().replace(/\s/g, '') : field.val(); - if ( "" === omise_card[ index ] ) { - errors.push( omise_params[ 'required_card_' + index ] ); - } - } ); - - if ( errors.length > 0 ) { - showError(errors, $form); - return false; - }else{ - hideError(); - if(Omise){ - Omise.setPublicKey(omise_params.key); - Omise.createToken("card", omise_card, function (statusCode, response) { - if (statusCode == 200) { - $.each( omise_card_fields, function( index, field ) { - field.val( '' ); - } ); - - const data = { - action: "omise_create_card", - omise_token: response.id, - omise_nonce: $("#omise_add_card_nonce").val() - }; - - $.post(omise_params.ajax_url, data, - function(wp_response){ - if(wp_response.id){ - window.location.reload(); - }else{ - showError(wp_response.message, $form); - } - }, "json" - ); - } else { - if(response.message){ - showError( omise_params.cannot_create_card + "
" + response.message, $form ); - }else if(response.responseJSON && response.responseJSON.message){ - showError( omise_params.cannot_create_card + "
" + response.responseJSON.message, $form ); - }else if(response.status==0){ - showError( omise_params.cannot_create_card + "
" + omise_params.cannot_connect_api, $form ); - }else { - showError( omise_params.retry_or_contact_support, $form ); - } - } - }); - }else{ - showError( omise_params.cannot_load_omisejs + '
' + omise_params.check_internet_connection, $form ); - } - } - } - function saveCardEmbedded(payload) { const data = { action: "omise_create_card", @@ -169,21 +100,19 @@ ); } - if(Boolean(parseInt(omise_params.secure_form_enabled))) { - showOmiseEmbeddedCardForm({ - element: document.getElementById('omise-card'), - publicKey: omise_params.key, - locale: LOCALE, - theme: CARD_FORM_THEME ?? 'light', - design: FORM_DESIGN, - brandIcons: CARD_BRAND_ICONS, - hideRememberCard: true, - onSuccess: saveCardEmbedded, - onError: (error) => { - showError(error) - $form.unblock() - } - }) - } + showOmiseEmbeddedCardForm({ + element: document.getElementById('omise-card'), + publicKey: omise_params.key, + locale: LOCALE, + theme: CARD_FORM_THEME ?? 'light', + design: FORM_DESIGN, + brandIcons: CARD_BRAND_ICONS, + hideRememberCard: true, + onSuccess: saveCardEmbedded, + onError: (error) => { + showError(error) + $form.unblock() + } + }) } )(jQuery); diff --git a/assets/javascripts/omise-payment-credit-card.js b/assets/javascripts/omise-payment-credit-card.js index e4b7d0fc..d77545c1 100644 --- a/assets/javascripts/omise-payment-credit-card.js +++ b/assets/javascripts/omise-payment-credit-card.js @@ -3,14 +3,16 @@ const cardFormTheme = $('#woocommerce_omise_card_form_theme'); const cardFormThemeParent = cardFormTheme.closest("tr[valign='top']"); - Boolean(parseInt(creditCardFormType.val())) - ? cardFormThemeParent.show() - : cardFormThemeParent.hide(); + cardFormThemeParent.show(); + // Boolean(parseInt(creditCardFormType.val())) + // ? cardFormThemeParent.show() + // : cardFormThemeParent.hide(); // Add an event listener to the Product Type field creditCardFormType.on('change', function(e) { - Boolean(parseInt($(this).val())) - ? cardFormThemeParent.show() - : cardFormThemeParent.hide(); + cardFormThemeParent.show(); + // Boolean(parseInt($(this).val())) + // ? cardFormThemeParent.show() + // : cardFormThemeParent.hide(); }); })(jQuery); diff --git a/assets/javascripts/omise-payment-form-handler.js b/assets/javascripts/omise-payment-form-handler.js index 25457834..f20d222a 100755 --- a/assets/javascripts/omise-payment-form-handler.js +++ b/assets/javascripts/omise-payment-form-handler.js @@ -58,70 +58,12 @@ } if (0 === $('input.omise_token').length) { - (Boolean(omise_params.secure_form_enabled)) - ? requestCardToken() - : traditionalForm(); + requestCardToken(); return false; } } } - function traditionalForm() { - $form.block({ - message: null, - overlayCSS: { - background: '#fff url(' + wc_checkout_params.ajax_loader_url + ') no-repeat center', - backgroundSize: '16px 16px', - opacity: 0.6 - } - }); - - let errors = [], - omise_card = {}, - omise_card_number_field = 'number', - omise_card_fields = { - 'name' : $('#omise_card_name'), - 'number' : $('#omise_card_number'), - 'expiration_month' : $('#omise_card_expiration_month'), - 'expiration_year' : $('#omise_card_expiration_year'), - 'security_code' : $('#omise_card_security_code') - }; - - $.each(omise_card_fields, function(index, field) { - omise_card[index] = (index === omise_card_number_field) ? field.val().replace(/\s/g, '') : field.val(); - if ("" === omise_card[index]) { - errors.push(omise_params['required_card_' + index]); - } - }); - - if (errors.length > 0) { - showError(errors); - $form.unblock(); - return false; - } - - hideError(); - - if(Omise) { - Omise.setPublicKey(omise_params.key); - Omise.createToken("card", omise_card, function (statusCode, response) { - if (statusCode == 200) { - $.each(omise_card_fields, function(index, field) { - field.val(''); - }); - - $form.append(''); - $form.submit(); - } else { - handleTokensApiError(response); - }; - }); - } else { - showError(omise_params.cannot_load_omisejs + '
' + omise_params.check_internet_connection); - $form.unblock(); - } - } - function googlePay() { window.addEventListener('loadpaymentdata', event => { document.getElementById('place_order').style.display = 'inline-block'; @@ -258,7 +200,7 @@ // This will be undefined when WC block is enabled in Checkout page if (typeof HIDE_REMEMBER_CARD !== 'undefined') { const omiseCardElement = document.getElementById('omise-card'); - if (omiseCardElement && Boolean(omise_params.secure_form_enabled)) { + if (omiseCardElement) { showOmiseEmbeddedCardForm({ element: omiseCardElement, publicKey: omise_params.key, diff --git a/includes/class-omise-wc-myaccount.php b/includes/class-omise-wc-myaccount.php index 7e1fe8b0..7fbc1cff 100644 --- a/includes/class-omise-wc-myaccount.php +++ b/includes/class-omise-wc-myaccount.php @@ -45,7 +45,6 @@ public function init_panel() { try { $viewData['existing_cards'] = $this->customerCard->get($this->omise_customer_id)['data']; $viewData['cardFormTheme'] = $this->omiseCardGateway->get_option('card_form_theme'); - $viewData['secure_form_enabled'] = (boolean)$this->omiseCardGateway->get_option('secure_form_enabled'); $viewData['formDesign'] = Omise_Page_Card_From_Customization::get_instance()->get_design_setting(); $viewData['cardIcons'] = $this->omiseCardGateway->get_card_icons(); $this->register_omise_my_account_scripts(); @@ -120,7 +119,6 @@ public function getParamsForJS() 'number is invalid and brand not supported (unknown)' => __( 'number is invalid and brand not supported (unknown)', 'omise' ), 'expiration year is invalid, expiration date cannot be in the past, number is invalid, and brand not supported (unknown)' => __( 'expiration year is invalid, expiration date cannot be in the past, number is invalid, and brand not supported (unknown)', 'omise' ), 'expiration month is not between 1 and 12, expiration date is invalid, number is invalid, and brand not supported (unknown)' => __('expiration month is not between 1 and 12, expiration date is invalid, number is invalid, and brand not supported (unknown)', 'omise'), - 'secure_form_enabled' => (boolean)$this->omiseCardGateway->get_option('secure_form_enabled') ]; } diff --git a/includes/gateway/abstract-omise-payment-base-card.php b/includes/gateway/abstract-omise-payment-base-card.php index 2707d17d..f9e0e3cd 100644 --- a/includes/gateway/abstract-omise-payment-base-card.php +++ b/includes/gateway/abstract-omise-payment-base-card.php @@ -69,7 +69,6 @@ private function prepareChargeData($order_id, $order, $omise_customer_id, $card_ 'return_uri' => $this->get_redirect_url('omise_callback', $order_id, $order), 'metadata' => $this->get_metadata( $order_id, - [ 'secure_form_enabled' => $this->getSecureFormState()] ), ]; @@ -98,17 +97,6 @@ private function prepareChargeData($order_id, $order, $omise_customer_id, $card_ return $data; } - /** - * Returns the the secure form state in yes/not format - */ - private function getSecureFormState() - { - // tracking the embedded form adoption - $omiseCardGateway = new Omise_Payment_Creditcard(); - $secureFormEnabled = $omiseCardGateway->get_option('secure_form_enabled'); - return (boolean)$secureFormEnabled ? 'yes' : 'no'; - } - /** * Saving card * @@ -378,7 +366,6 @@ public function getParamsForJS() 'expiration month is not between 1 and 12, expiration date is invalid, number is invalid, and brand not supported (unknown)', 'omise' ), - 'secure_form_enabled' => (boolean)$omiseCardGateway->get_option('secure_form_enabled') ]; } } diff --git a/includes/gateway/class-omise-payment-creditcard.php b/includes/gateway/class-omise-payment-creditcard.php index 277bfed7..f808a4a5 100644 --- a/includes/gateway/class-omise-payment-creditcard.php +++ b/includes/gateway/class-omise-payment-creditcard.php @@ -2,8 +2,6 @@ defined( 'ABSPATH' ) or die( 'No direct script access allowed.' ); class Omise_Payment_Creditcard extends Omise_Payment_Base_Card { - const SECURE_FORM_ENABLED = true; - public function __construct() { parent::__construct(); @@ -86,16 +84,6 @@ function init_form_fields() { ), 'desc_tip' => true ), - 'secure_form_enabled' => [ - 'title' => __( 'Secure form', 'omise' ), - 'type' => 'select', - 'description' => __( 'Try the new secure form to accept card payments. The new form features additional controls to ensure PCI-DSS compliance.
Using this form will be mandatory in a future release.', 'omise' ), - 'default' => !self::SECURE_FORM_ENABLED, - 'options' => array( - !self::SECURE_FORM_ENABLED => __( 'No', 'omise' ), - self::SECURE_FORM_ENABLED => __( 'Yes', 'omise' ) - ) - ], 'card_form_theme' => [ 'title' => __( 'Secure form theme', 'omise' ), @@ -200,13 +188,9 @@ public function get_existing_cards() { } public function get_secure_form_config() { - $data['secure_form_enabled'] = (boolean)$this->get_option('secure_form_enabled'); - - if ($data['secure_form_enabled'] === self::SECURE_FORM_ENABLED) { - $data['card_form_theme'] = $this->get_option('card_form_theme'); - $data['card_icons'] = $this->get_card_icons(); - $data['form_design'] = Omise_Page_Card_From_Customization::get_instance()->get_design_setting(); - } + $data['card_form_theme'] = $this->get_option('card_form_theme'); + $data['card_icons'] = $this->get_card_icons(); + $data['form_design'] = Omise_Page_Card_From_Customization::get_instance()->get_design_setting(); return $data; } diff --git a/omise-woocommerce.php b/omise-woocommerce.php index 346355bd..6ca22bf6 100644 --- a/omise-woocommerce.php +++ b/omise-woocommerce.php @@ -80,13 +80,8 @@ public function enable_hpos() { public function embedded_form_notice() { $this->omiseCardGateway = new Omise_Payment_Creditcard(); - $secure_form_enabled = $this->omiseCardGateway->get_option('secure_form_enabled'); - - // hide if user enables the embedded form. - if (!(bool)$secure_form_enabled) { - $translation = __('Critical plugin update released: Now compatible with WooCommerce block, and enforces mandatory secure form checkout. Upgrade immediately and re-customize your credit card form to ensure compliance and enhanced customer data protection.', 'omise'); - echo "

Opn Payments: $translation

"; - } + $translation = __('Critical plugin update released: Now compatible with WooCommerce block, and enforces mandatory secure form checkout. Upgrade immediately and re-customize your credit card form to ensure compliance and enhanced customer data protection.', 'omise'); + echo "

Opn Payments: $translation

"; } /** diff --git a/templates/myaccount/my-card.php b/templates/myaccount/my-card.php index 26cd33b1..ed4d1c6d 100644 --- a/templates/myaccount/my-card.php +++ b/templates/myaccount/my-card.php @@ -36,26 +36,16 @@ class='button delete_card'

- - -
- -
- -
-
- +
- - - + diff --git a/templates/payment/form.php b/templates/payment/form.php index 76404d14..71faa7cd 100644 --- a/templates/payment/form.php +++ b/templates/payment/form.php @@ -26,40 +26,19 @@ - -
-
- -
-
- -
- - - -
- - -
- - -
- - -
-
- +
+
+ +
+
- - - + + diff --git a/tests/unit/includes/blocks/gateways/traits/mock-gateways.php b/tests/unit/includes/blocks/gateways/traits/mock-gateways.php index 29e5068e..39bc5af7 100644 --- a/tests/unit/includes/blocks/gateways/traits/mock-gateways.php +++ b/tests/unit/includes/blocks/gateways/traits/mock-gateways.php @@ -38,9 +38,9 @@ public function get_existing_cards() { return ['user_logged_in' => false]; } - public function get_secure_form_config() { - return ['secure_form_enabled' => false]; - } + // public function get_secure_form_config() { + // return ['secure_form_enabled' => false]; + // } public function get_view_data() { return [ diff --git a/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php b/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php index 25ddb0d4..27d4576a 100644 --- a/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php @@ -94,7 +94,6 @@ public function get_secure_form_config() { $config = $creditCard->get_secure_form_config(); - $this->assertArrayHasKey('secure_form_enabled', $config); $this->assertArrayHasKey('card_form_theme', $config); $this->assertArrayHasKey('card_icons', $config); $this->assertArrayHasKey('form_design', $config); From 2629c5391824e60c6dce8025a379c9f2e175c5cf Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 3 Oct 2024 09:42:59 +0700 Subject: [PATCH 2/2] Fix tests. --- .../javascripts/omise-payment-credit-card.js | 18 ------------------ .../gateway/class-omise-payment-creditcard.php | 14 +------------- .../blocks/gateways/traits/mock-gateways.php | 6 +++--- .../class-omise-payment-creditcard-test.php | 6 ------ 4 files changed, 4 insertions(+), 40 deletions(-) delete mode 100644 assets/javascripts/omise-payment-credit-card.js diff --git a/assets/javascripts/omise-payment-credit-card.js b/assets/javascripts/omise-payment-credit-card.js deleted file mode 100644 index d77545c1..00000000 --- a/assets/javascripts/omise-payment-credit-card.js +++ /dev/null @@ -1,18 +0,0 @@ -(function ($) { - const creditCardFormType = $('#woocommerce_omise_secure_form_enabled'); - const cardFormTheme = $('#woocommerce_omise_card_form_theme'); - const cardFormThemeParent = cardFormTheme.closest("tr[valign='top']"); - - cardFormThemeParent.show(); - // Boolean(parseInt(creditCardFormType.val())) - // ? cardFormThemeParent.show() - // : cardFormThemeParent.hide(); - - // Add an event listener to the Product Type field - creditCardFormType.on('change', function(e) { - cardFormThemeParent.show(); - // Boolean(parseInt($(this).val())) - // ? cardFormThemeParent.show() - // : cardFormThemeParent.hide(); - }); -})(jQuery); diff --git a/includes/gateway/class-omise-payment-creditcard.php b/includes/gateway/class-omise-payment-creditcard.php index f808a4a5..3db5b698 100644 --- a/includes/gateway/class-omise-payment-creditcard.php +++ b/includes/gateway/class-omise-payment-creditcard.php @@ -2,6 +2,7 @@ defined( 'ABSPATH' ) or die( 'No direct script access allowed.' ); class Omise_Payment_Creditcard extends Omise_Payment_Base_Card { + public function __construct() { parent::__construct(); @@ -28,8 +29,6 @@ public function __construct() $this->payment_action = $this->get_option( 'payment_action' ); $this->restricted_countries = array( 'TH', 'JP', 'SG', 'MY' ); - $this->register_omise_credit_card_scripts(); - add_action( 'woocommerce_api_' . $this->id . '_callback', 'Omise_Callback::execute' ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'omise_scripts' ) ); @@ -84,7 +83,6 @@ function init_form_fields() { ), 'desc_tip' => true ), - 'card_form_theme' => [ 'title' => __( 'Secure form theme', 'omise' ), 'type' => 'select', @@ -144,16 +142,6 @@ function init_form_fields() { ); } - private function register_omise_credit_card_scripts() { - wp_enqueue_script( - 'omise-credit-card', - plugins_url( '../assets/javascripts/omise-payment-credit-card.js', dirname( __FILE__ ) ), - array( 'jquery' ), - WC_VERSION, - true - ); - } - /** * @see WC_Payment_Gateway::payment_fields() * @see woocommerce/includes/abstracts/abstract-wc-payment-gateway.php diff --git a/tests/unit/includes/blocks/gateways/traits/mock-gateways.php b/tests/unit/includes/blocks/gateways/traits/mock-gateways.php index 39bc5af7..181ab5d6 100644 --- a/tests/unit/includes/blocks/gateways/traits/mock-gateways.php +++ b/tests/unit/includes/blocks/gateways/traits/mock-gateways.php @@ -38,9 +38,9 @@ public function get_existing_cards() { return ['user_logged_in' => false]; } - // public function get_secure_form_config() { - // return ['secure_form_enabled' => false]; - // } + public function get_secure_form_config() { + return []; + } public function get_view_data() { return [ diff --git a/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php b/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php index 27d4576a..55bb5628 100644 --- a/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-creditcard-test.php @@ -39,7 +39,6 @@ protected function setUp(): void } Monkey\Functions\expect('add_action')->andReturn(null); - Monkey\Functions\expect('wp_enqueue_script'); Monkey\Functions\expect('wp_kses') ->times(3) ->andReturn( @@ -64,8 +63,6 @@ protected function tearDown(): void */ public function test_class_is_initialized_properly() { - Monkey\Functions\expect('plugins_url'); - $creditCard = new Omise_Payment_Creditcard; $this->assertEquals('credit_card', $creditCard->source_type); @@ -88,8 +85,6 @@ public function get_secure_form_config() { $cardFormCustomization->shouldReceive('get_instance')->andReturn($cardFormCustomization); $cardFormCustomization->shouldReceive('get_design_setting')->andReturn(['abc' => ['xyz']]); - Monkey\Functions\expect('plugins_url'); - $creditCard = new Omise_Payment_Creditcard; $config = $creditCard->get_secure_form_config(); @@ -105,7 +100,6 @@ public function get_secure_form_config() { public function get_existing_cards_for_user_logged_in() { Monkey\Functions\expect('is_user_logged_in')->andReturn(true); Monkey\Functions\expect('wp_get_current_user')->andReturn((object)['test_omise_customer_id' => 1]); - Monkey\Functions\expect('plugins_url'); $customerCard = Mockery::mock('alias:OmiseCustomerCard'); $customerCard->shouldReceive('get')->andReturn(1);