Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make secure form mandatory #480

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 16 additions & 87 deletions assets/javascripts/omise-myaccount-card-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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 + "<br/>" + response.message, $form );
}else if(response.responseJSON && response.responseJSON.message){
showError( omise_params.cannot_create_card + "<br/>" + response.responseJSON.message, $form );
}else if(response.status==0){
showError( omise_params.cannot_create_card + "<br/>" + omise_params.cannot_connect_api, $form );
}else {
showError( omise_params.retry_or_contact_support, $form );
}
}
});
}else{
showError( omise_params.cannot_load_omisejs + '<br/>' + omise_params.check_internet_connection, $form );
}
}
}

function saveCardEmbedded(payload) {
const data = {
action: "omise_create_card",
Expand All @@ -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);
16 changes: 0 additions & 16 deletions assets/javascripts/omise-payment-credit-card.js

This file was deleted.

62 changes: 2 additions & 60 deletions assets/javascripts/omise-payment-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input type="hidden" class="omise_token" name="omise_token" value="' + response.id + '"/>');
$form.submit();
} else {
handleTokensApiError(response);
};
});
} else {
showError(omise_params.cannot_load_omisejs + '<br/>' + omise_params.check_internet_connection);
$form.unblock();
}
}

function googlePay() {
window.addEventListener('loadpaymentdata', event => {
document.getElementById('place_order').style.display = 'inline-block';
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions includes/class-omise-wc-myaccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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')
];
}

Expand Down
13 changes: 0 additions & 13 deletions includes/gateway/abstract-omise-payment-base-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
),
];

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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')
];
}
}
34 changes: 3 additions & 31 deletions includes/gateway/class-omise-payment-creditcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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()
{
Expand Down Expand Up @@ -30,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' ) );
Expand Down Expand Up @@ -86,17 +83,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.<br /><strong><em>Using this form will be mandatory in a future release</em></strong>.', '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' ),
'type' => 'select',
Expand Down Expand Up @@ -156,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
Expand Down Expand Up @@ -200,13 +176,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;
}
Expand Down
9 changes: 2 additions & 7 deletions omise-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<div class='notice notice-warning is-dismissible'><p><strong>Opn Payments:</strong> $translation</p></div>";
}
$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 "<div class='notice notice-warning is-dismissible'><p><strong>Opn Payments:</strong> $translation</p></div>";
}

/**
Expand Down
26 changes: 8 additions & 18 deletions templates/myaccount/my-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,16 @@ class='button delete_card'
<h4><?php _e( 'Add new card', 'omise' ); ?></h4>
<form name="omise_cc_form" id="omise_cc_form">
<?php wp_nonce_field('omise_add_card','omise_add_card_nonce'); ?>

<?php if($viewData['secure_form_enabled']): ?>
<div id="omise-card" style="width:100%; max-width: 400px;"></div>
<?php else: ?>
<fieldset>
<?php require_once( __DIR__ . '/../payment/form-creditcard.php' ); ?>
<div class="clear"></div>
</fieldset>
<?php endif; ?>
<div id="omise-card" style="width:100%; max-width: 400px;"></div>

</form>
<button id="omise_add_new_card" class="button"><?php _e( 'Save card', 'omise' ); ?></button>
</div>

<?php if($viewData['secure_form_enabled']): ?>
<script>
window.CARD_FORM_THEME = "<?php echo $viewData['cardFormTheme'] ?>";
window.FORM_DESIGN = JSON.parse(`<?php echo json_encode($viewData['formDesign']) ?>`);
window.CARD_BRAND_ICONS = JSON.parse(`<?php echo json_encode($viewData['cardIcons']) ?>`);
window.LOCALE = `<?php echo get_locale(); ?>`;
window.OMISE_CUSTOM_FONT_OTHER = 'Other';
</script>
<?php endif; ?>
<script>
window.CARD_FORM_THEME = "<?php echo $viewData['cardFormTheme'] ?>";
window.FORM_DESIGN = JSON.parse(`<?php echo json_encode($viewData['formDesign']) ?>`);
window.CARD_BRAND_ICONS = JSON.parse(`<?php echo json_encode($viewData['cardIcons']) ?>`);
window.LOCALE = `<?php echo get_locale(); ?>`;
window.OMISE_CUSTOM_FONT_OTHER = 'Other';
</script>
Loading
Loading