diff --git a/assets/css/monei-icons.css b/assets/css/monei-icons-classic.css similarity index 67% rename from assets/css/monei-icons.css rename to assets/css/monei-icons-classic.css index f36c4f4..190fb35 100644 --- a/assets/css/monei-icons.css +++ b/assets/css/monei-icons-classic.css @@ -1,11 +1,16 @@ +[class*="payment_method_monei"] label { + +} label .monei-icons{ - float:right; + margin-left: 15px; + margin-right:0; + padding-right:0; max-height: 28px !important; max-width: 80px !important; } .monei-icons svg{ - float:right; + width: auto; } /* Applies to the apple/google button in checkout*/ diff --git a/assets/js/cofidis.min.js b/assets/js/cofidis.min.js deleted file mode 100644 index 8ccbd98..0000000 --- a/assets/js/cofidis.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(i){"use strict";i(document.body).on("updated_checkout",(function(i,o){"object"==typeof o&&o.fragments&&o.fragments.monei_new_total&&(e.total=o.fragments.monei_new_total,e.widget_update_props(),o.fragments.monei_new_total<7500||o.fragments.monei_new_total>1e5?e.hide_cofidis():e.show_cofidis()),e.is_cofidis_selected()&&e.on_payment_selected()}));var e={$checkout_form:i("form.woocommerce-checkout"),$codifis_widget_container:"#cofidis_widget",account_id:wc_monei_cofidis_params.account_id,total:wc_monei_cofidis_params.total,init_counter:0,init:function(){this.$checkout_form.on("change",this.on_change),e.init_cofidis_widget()},init_cofidis_widget:function(){var i=monei.CofidisWidget({accountId:e.account_id,amountInt:parseInt(e.total),language:e.lang,style:{base:{color:"#6D6D6D"},label:{fontWeight:"normal",color:"#333"}}});window.cofidisWidget=i},widget_render:function(){window.cofidisWidget.render("#cofidis_widget")},widget_update_props:function(i){window.cofidisWidget&&(window.cofidisWidget.close(),e.init_cofidis_widget(),0!==e.init_counter&&e.widget_render())},on_change:function(){i("[name='payment_method']").on("change",(function(){e.on_payment_selected()}))},on_payment_selected(){e.is_cofidis_selected()&&0===e.init_counter&&(e.widget_render(),e.init_counter++)},is_cofidis_selected:function(){return i("#payment_method_monei_cofidis").is(":checked")},hide_cofidis:function(){i("li.payment_method_monei_cofidis").hide()},show_cofidis:function(){i("li.payment_method_monei_cofidis").show()}};i((function(){e.init()}))}(jQuery) \ No newline at end of file diff --git a/assets/js/monei-bizum-classic.js b/assets/js/monei-bizum-classic.js new file mode 100644 index 0000000..a31011f --- /dev/null +++ b/assets/js/monei-bizum-classic.js @@ -0,0 +1,197 @@ +(function( $ ) { + 'use strict'; + + // Checkout form. + $( document.body ).on( + 'updated_checkout', + function(e, data) { + // Update cofidis_widget.total on every updated_checkout event. + if ( 'object' === typeof( data ) && data.fragments && data.fragments[ 'monei_new_total' ] ) { + wc_bizum_form.total = data.fragments[ 'monei_new_total' ]; + } + + if ( wc_bizum_form.is_bizum_selected() ) { + wc_bizum_form.init_checkout_bizum(); + } + } + ); + + // Add Payment Method form. + $( 'form#add_payment_method' ).on( + 'click payment_methods', + function() { + if ( wc_bizum_form.is_bizum_selected() ) { + wc_bizum_form.init_checkout_bizum(); + } + } + ); + + // On Pay for order form. + $( 'form#order_review' ).on( + 'click', + function() { + if ( wc_bizum_form.is_bizum_selected() ) { + wc_bizum_form.init_checkout_bizum(); + } + } + ); + + var targetNode = document.getElementById('order_review'); + + if (targetNode) { + var observer = new MutationObserver(function(mutationsList, observer) { + for (var mutation of mutationsList) { + if (mutation.type === 'childList') { + if ( wc_bizum_form.is_bizum_selected() ) { + wc_bizum_form.on_payment_selected(); + } + } + } + }); + + observer.observe(targetNode, { childList: true, subtree: true }); + } + + var wc_bizum_form = { + $checkout_form: $( 'form.woocommerce-checkout' ), + $add_payment_form: $( 'form#add_payment_method' ), + $order_pay_form: $( 'form#order_review' ), + $container: null, + $paymentForm: null, + is_checkout: false, + is_add_payment_method: false, + is_order_pay: false, + form: null, + submitted: false, + init_counter: 0, + total: wc_bizum_params.total, + init: function() { + // Checkout Page + if ( this.$checkout_form.length ) { + this.is_checkout = true; + this.form = this.$checkout_form; + } + + // Add payment method Page + if ( this.$add_payment_form.length ) { + this.is_add_payment_method = true; + this.form = this.$add_payment_form; + } + + // Pay for order ( change_payment_method for subscriptions) + if ( this.$order_pay_form.length ) { + if ( wc_bizum_form.is_bizum_selected() ) { + wc_bizum_form.init_checkout_bizum(); + } + this.is_order_pay = true; + this.form = this.$order_pay_form; + console.log('TOTAL',this.form) + } + + if ( this.form ) { + this.form.on( 'change', this.on_change ); + } + }, + on_change: function() { + // Triggers on payment method selection. + $( "[name='payment_method']" ).on( + 'change', + function() { + wc_bizum_form.on_payment_selected(); + } + ); + }, + on_payment_selected() { + if ( wc_bizum_form.is_bizum_selected() ) { + wc_bizum_form.init_checkout_bizum(); + if ( wc_bizum_form.is_checkout ) { + $( "[name='woocommerce_checkout_place_order']" ).attr( 'bizum-data-monei', 'submit' ); + } + $('#place_order').prop('disabled', true); + } else { + if ( wc_bizum_form.is_checkout ) { + $( "[name='woocommerce_checkout_place_order']" ).removeAttr( 'bizum-data-monei' ); + } + //todo central state. If Apple is selected we dont want to mess with the disable after it + if(!wc_bizum_form.is_apple_selected()){ + $('#place_order').prop('disabled', false); + } + } + }, + is_bizum_selected: function() { + return $( '#payment_method_monei_bizum' ).is( ':checked' ); + }, + is_apple_selected: function() { + return $( '#payment_method_monei_apple_google' ).is( ':checked' ); + }, + init_bizum_component: function() { + if ( window.bizumRequest ) { + window.bizumRequest.close(); + } + wc_bizum_form.instantiate_payment_request(); + }, + instantiate_payment_request: function() { + var paymentRequest = monei.Bizum({ + accountId: wc_bizum_params.account_id, + sessionId: wc_bizum_params.session_id, + amount: parseInt( wc_bizum_form.total ), + currency: wc_bizum_params.currency, + onSubmit(result) { + $('#place_order').prop('disabled', false); + wc_bizum_form.request_token_handler( result.token ); + }, + onError(error) { + console.error(error); + }, + }); + // Render an instance of the Payment Request Component into the `payment_request_container`
. + paymentRequest.render('#bizum-container'); + // Assign a global variable to paymentRequest so it's accessible. + window.bizumRequest = paymentRequest; + }, + init_checkout_bizum: function() { + // If checkout is updated (and monei was initiated already), ex, selecting new shipping methods, checkout is re-render by the ajax call. + // and we need to reset the counter in order to initiate again the monei component. + if ( wc_bizum_form.$container && 0 === wc_bizum_form.$container.childElementCount ) { + wc_bizum_form.init_counter = 0; + } + + // init monei just once, despite how many times this may be triggered. + if ( 0 !== this.init_counter ) { + return; + } + + if ( wc_bizum_form.is_checkout ) { + $( "[name='woocommerce_checkout_place_order']" ).attr( 'bizum-data-monei', 'submit' ); + } + + wc_bizum_form.init_bizum_component(); + wc_bizum_form.$container = document.getElementById( 'bizum-container' ); + + // We already init Bizum. + this.init_counter++; + }, + request_token_handler: function (token ) { + wc_bizum_form.create_hidden_input( 'monei_payment_request_token', token ); + // Once Token is created, submit form. + $('#place_order').prop('disabled', false); + wc_bizum_form.form.submit(); + }, + create_hidden_input: function( id, token ) { + var hiddenInput = document.createElement( 'input' ); + hiddenInput.setAttribute( 'type', 'hidden' ); + hiddenInput.setAttribute( 'name', id ); + hiddenInput.setAttribute( 'id', id ); + hiddenInput.setAttribute( 'value', token ); + wc_bizum_form.$paymentForm = document.getElementById( 'monei-bizum-form' ); + wc_bizum_form.$paymentForm.appendChild( hiddenInput ); + } + }; + + $( + function() { + wc_bizum_form.init(); + } + ); + +})( jQuery ); diff --git a/assets/js/block-checkout-bizum.js b/assets/js/monei-block-checkout-bizum.js similarity index 93% rename from assets/js/block-checkout-bizum.js rename to assets/js/monei-block-checkout-bizum.js index 83cde15..6de8555 100644 --- a/assets/js/block-checkout-bizum.js +++ b/assets/js/monei-block-checkout-bizum.js @@ -41,7 +41,6 @@ * Initialize MONEI card input and handle token creation. */ const initMoneiCard = () => { - console.log( parseInt( bizumData.total * 100 ) ); const bizum = monei.Bizum( { accountId: bizumData.accountId, sessionId: bizumData.sessionId, @@ -65,7 +64,7 @@ } }, onError( error ) { - console.log( error ); + console.error( error ); }, } ); @@ -90,7 +89,7 @@ type: responseTypes.SUCCESS, meta: { paymentMethodData: { - monei_payment_token: requestToken, + monei_payment_request_token: requestToken, monei_is_block_checkout: 'yes', }, }, @@ -105,19 +104,17 @@ const unsubscribeSuccess = onCheckoutSuccess( ( { processingResponse } ) => { const { paymentDetails } = processingResponse; - // Ensure we have the paymentId from the server if ( paymentDetails && paymentDetails.paymentId ) { const paymentId = paymentDetails.paymentId; - const tokenValue = paymentDetails.token; - // Call monei.confirmPayment to complete the payment (with 3D Secure) - monei - .confirmPayment( { + console.log(typeof paymentId) + console.log({ + paymentId, + paymentToken: tokenValue}) + monei.confirmPayment( { paymentId, - tokenValue, - } ) + paymentToken: tokenValue} ) .then( ( result ) => { - console.log( 'confirmed', result ); if ( result.nextAction && result.nextAction.mustRedirect @@ -134,7 +131,7 @@ } } ) .catch( ( error ) => { - console.log( + console.error( 'Error during payment confirmation:', error ); @@ -197,5 +194,6 @@ }, supports: bizumData.supports, }; + console.log('hola bizum 6') registerPaymentMethod( MoneiBizumPaymentMethod ); } )(); diff --git a/assets/js/checkout-cc.js b/assets/js/monei-block-checkout-cc.js similarity index 99% rename from assets/js/checkout-cc.js rename to assets/js/monei-block-checkout-cc.js index 9ea6123..03b2d7b 100644 --- a/assets/js/checkout-cc.js +++ b/assets/js/monei-block-checkout-cc.js @@ -295,7 +295,7 @@ } } ) .catch( ( error ) => { - console.log( + console.error( 'Error during payment confirmation:', error ); @@ -412,7 +412,8 @@ } }, onError( error ) { - console.log( error ); + console.error( error ); + console.error( error ); }, } ); diff --git a/assets/js/monei.js b/assets/js/monei-cc-classic.js similarity index 50% rename from assets/js/monei.js rename to assets/js/monei-cc-classic.js index cad7391..72ca65e 100644 --- a/assets/js/monei.js +++ b/assets/js/monei-cc-classic.js @@ -5,16 +5,18 @@ $( document.body ).on( 'updated_checkout', function(e, data) { + wc_monei_form.update_apple_google_label(); // Update cofidis_widget.total on every updated_checkout event. if ( 'object' === typeof( data ) && data.fragments && data.fragments[ 'monei_new_total' ] ) { wc_monei_form.total = data.fragments[ 'monei_new_total' ]; } - if ( wc_monei_form.is_monei_selected() ) { - wc_monei_form.init_checkout_monei(); - // We need to re-init payment request with the new price. + if (wc_monei_form.is_apple_selected()) { wc_monei_form.init_apple_google_pay(); + } + if ( wc_monei_form.is_monei_selected() ) { + wc_monei_form.init_checkout_monei(); } } ); @@ -33,35 +35,22 @@ $( 'form#order_review' ).on( 'click', function() { + if (wc_monei_form.is_apple_selected()) { + wc_monei_form.init_apple_google_pay(); + } if ( wc_monei_form.is_monei_selected() ) { wc_monei_form.init_checkout_monei(); } } ); - var targetNode = document.getElementById('order_review'); - - if (targetNode) { - var observer = new MutationObserver(function(mutationsList, observer) { - for (var mutation of mutationsList) { - if (mutation.type === 'childList') { - if ( wc_monei_form.is_monei_selected() ) { - wc_monei_form.init_checkout_monei(); - } - } - } - }); - - observer.observe(targetNode, { childList: true, subtree: true }); - } - var wc_monei_form = { $checkout_form: $( 'form.woocommerce-checkout' ), $add_payment_form: $( 'form#add_payment_method' ), $order_pay_form: $( 'form#order_review' ), $cardInput: null, $container: null, - $payment_request_container: '#payment_request_container', + $payment_request_container: null, $errorContainer: null, $paymentForm: null, is_checkout: false, @@ -70,7 +59,9 @@ form: null, submitted: false, init_counter: 0, + init_apple_counter: 0, total: wc_monei_params.total, + cardholderNameRegex: /^[A-Za-zÀ-ú- ]{5,50}$/, init: function() { // Checkout Page if ( this.$checkout_form.length ) { @@ -89,21 +80,33 @@ // Pay for order ( change_payment_method for subscriptions) if ( this.$order_pay_form.length ) { if ( wc_monei_form.is_monei_selected() ) { - wc_monei_form.init_checkout_monei(); + wc_monei_form.on_payment_selected() + } + if(wc_monei_form.is_apple_selected()) { + wc_monei_form.init_apple_google_pay() } this.is_order_pay = true; this.form = this.$order_pay_form; - this.form.on( 'submit', this.place_order ); + this.form.on( 'submit', this.place_order_page ); + + $('input[name="payment_method"]').on('change', function() { + console.log('radio changed') + // Check if the apple google pay method is selected + if (wc_monei_form.is_apple_selected()) { + wc_monei_form.init_apple_google_pay(); + } + // Check if the monei method is selected + if (wc_monei_form.is_monei_selected()) { + wc_monei_form.init_checkout_monei(); + } + }); } if ( this.form ) { this.form.on( 'change', this.on_change ); } }, - submit_form: function() { - wc_monei_form.form.submit(); - }, on_change: function() { // Triggers on payment method selection. $( "[name='payment_method']" ).on( @@ -121,29 +124,49 @@ ); }, on_payment_selected() { - if ( wc_monei_form.is_monei_selected() ) { + if ( wc_monei_form.is_apple_selected()) { + wc_monei_form.init_apple_google_pay(); + if ( wc_monei_form.is_checkout ) { + $("[name='woocommerce_checkout_place_order']").attr('data-monei', 'submit'); + } + $('#place_order').prop('disabled', true); + return false; + } else if ( wc_monei_form.is_monei_selected() ) { wc_monei_form.init_checkout_monei(); + $('#place_order').prop('disabled', false); if ( wc_monei_form.is_checkout ) { $( "[name='woocommerce_checkout_place_order']" ).attr( 'data-monei', 'submit' ); } - - // If a tokenised card is checked, we hide google/apple request button. - if ( wc_monei_form.is_checkout && wc_monei_params.apple_google_pay ) { - if ( wc_monei_form.is_tokenized_cc_selected() ) { - wc_monei_form.hide_payment_request_container(); - } else { - wc_monei_form.show_payment_request_container(); - } + if ( wc_monei_form.is_tokenized_cc_selected() ) { + $('.monei-input-container, .monei-card-input').hide(); + } else { + $('.monei-input-container, .monei-card-input').show(); } } else { if ( wc_monei_form.is_checkout ) { + $('#place_order').prop('disabled', false); $( "[name='woocommerce_checkout_place_order']" ).removeAttr( 'data-monei' ); } } }, + validate_cardholder_name: function() { + var value = $('#monei_cardholder_name').val(); + if (!wc_monei_form.cardholderNameRegex.test(value)) { + // Show error + wc_monei_form.print_errors('Invalid cardholder name', '#monei-cardholder-name-error'); + return false; + } else { + // Clear error + wc_monei_form.clear_errors('#monei-cardholder-name-error'); + return true; + } + }, is_monei_selected: function() { return $( '#payment_method_monei' ).is( ':checked' ); }, + is_apple_selected: function() { + return $( '#payment_method_monei_apple_google' ).is( ':checked' ); + }, is_tokenized_cc_selected: function() { return ( $( 'input[name="wc-monei-payment-token"]' ).is( ':checked' ) && 'new' !== $( 'input[name="wc-monei-payment-token"]:checked' ).val() ); }, @@ -151,13 +174,32 @@ return ( wc_monei_form.is_monei_selected() && wc_monei_form.is_tokenized_cc_selected() ); }, init_apple_google_pay: function() { - if ( ! wc_monei_params.apple_google_pay ) { + // If checkout is updated (and monei was initiated already), ex, selecting new shipping methods, checkout is re-render by the ajax call. + // and we need to reset the counter in order to initiate again the monei component. + if ( wc_monei_form.$payment_request_container && 0 === wc_monei_form.$payment_request_container.childElementCount ) { + wc_monei_form.init_apple_counter = 0; + } + + // init monei just once, despite how many times this may be triggered. + if ( 0 !== this.init_apple_counter ) { return; } - if ( window.paymentRequest ) { - window.paymentRequest.close(); + + if ( wc_monei_form.is_checkout ) { + $( "[name='woocommerce_checkout_place_order']" ).attr( 'data-monei', 'submit' ); + } + + // Init Apple/Google Pay. + if ( ! wc_monei_params.apple_google_pay ) { + return; } + wc_monei_form.instantiate_payment_request(); + wc_monei_form.$payment_request_container = document.getElementById('payment-request-container') + + // We already init the button. + this.init_apple_counter++; + }, instantiate_payment_request: function() { // Create an instance of the Apple/Google Pay component. @@ -170,21 +212,20 @@ wc_monei_form.apple_google_token_handler( result.token ); }, onError(error) { - console.log(error); + console.error(error); }, }); // Render an instance of the Payment Request Component into the `payment_request_container`
. + console.log('rendering') paymentRequest.render('#payment-request-container'); // Assign a global variable to paymentRequest so it's accessible. window.paymentRequest = paymentRequest; }, - hide_payment_request_container: function() { - $('#payment-request-container').hide(); - }, - show_payment_request_container: function() { - $('#payment-request-container').show(); - }, init_checkout_monei: function() { + let container = document.getElementById('monei-card-input') + if(container === null) { + return; + } // If checkout is updated (and monei was initiated already), ex, selecting new shipping methods, checkout is re-render by the ajax call. // and we need to reset the counter in order to initiate again the monei component. if ( wc_monei_form.$container && 0 === wc_monei_form.$container.childElementCount ) { @@ -205,10 +246,11 @@ $( "[name='woocommerce_checkout_place_order']" ).attr( 'data-monei', 'submit' ); } - // Init Apple/Google Pay. - wc_monei_form.init_apple_google_pay(); + $('#monei_cardholder_name').on('blur', function() { + wc_monei_form.validate_cardholder_name(); + }); - wc_monei_form.$container = document.getElementById( 'card-input' ); + wc_monei_form.$container = document.getElementById( 'monei-card-input' ); wc_monei_form.$errorContainer = document.getElementById( 'monei-card-error' ); var style = { @@ -255,97 +297,151 @@ this.init_counter++; }, place_order: function( e ) { - // If MONEI token already created, submit form. - if ( $( '#monei_payment_token' ).length ) { - return true; - } - // If MONEI payment request token already created (apple/google), submit form. - if ( $( '#monei_payment_request_token' ).length ) { + const token = document.getElementById('monei_payment_token') + if(token) { return true; } - if ( ! wc_monei_form.is_monei_selected() ) { - return true; + if ( wc_monei_form.is_monei_selected() && ! wc_monei_form.is_monei_saved_cc_selected()) { + if (!wc_monei_form.validate_cardholder_name()) { + return false; + } + //e.preventDefault(); + // This will be trigger, when CC component is used and "Place order" has been clicked. + monei.createToken( wc_monei_form.$cardInput ) + .then( + function ( result ) { + if ( result.error ) { + console.error('error', result.error); + // Inform the user if there was an error. + wc_monei_form.print_errors( result.error ); + } else { + console.log('token') + // Create monei token and append it to Dconsole.logOM + wc_monei_form.monei_token_handler( result.token ); + } + } + ) + .catch( + function (error) { + console.error( error ); + wc_monei_form.print_errors( error.message ); + } + ); + return false; } - // If user has selected any tokenized CC, we just submit the form normally. - if ( wc_monei_form.is_monei_saved_cc_selected() ) { + }, + place_order_page: function( e ) { + const token = document.getElementById('monei_payment_token') + if(token) { return true; } - e.preventDefault(); - // This will be trigger, when CC component is used and "Place order" has been clicked. - monei.createToken( wc_monei_form.$cardInput ) - .then( - function ( result ) { - if ( result.error ) { - console.log('error', result.error); - // Inform the user if there was an error. - wc_monei_form.print_errors( result.error ); - } else { - // Create monei token and append it to Dconsole.logOM - wc_monei_form.monei_token_handler( result.token ); + if ( wc_monei_form.is_monei_selected() && ! wc_monei_form.is_monei_saved_cc_selected()) { + if (!wc_monei_form.validate_cardholder_name()) { + return false; + } + e.preventDefault(); + // This will be trigger, when CC component is used and "Place order" has been clicked. + monei.createToken( wc_monei_form.$cardInput ) + .then( + function ( result ) { + if ( result.error ) { + console.error('error', result.error); + // Inform the user if there was an error. + wc_monei_form.print_errors( result.error ); + } else { + console.log('token', result.token) + // Create monei token and append it to Dconsole.logOM + wc_monei_form.monei_token_handler( result.token ); + } } - } - ) - .catch( - function (error) { - console.log( error ); - wc_monei_form.print_errors( error.message ); - } - ); - return false; + ) + .catch( + function (error) { + console.error( error ); + wc_monei_form.print_errors( error.message ); + } + ); + return false; + } }, /** * Printing errors into checkout form. * @param error_string + * @param errorContainer */ - print_errors: function (error_string ) { - $( wc_monei_form.$errorContainer ).html( '
' ); - $( wc_monei_form.$errorContainer ).find( 'li' ).text( error_string ); - /** - * Scroll to Monei Errors. - */ - if ( $( '.monei-error' ).length ) { - $( 'html, body' ).animate( - { - scrollTop: ( $( '.monei-error' ).offset().top - 200 ) - }, - 200 - ); + print_errors: function(error_string, errorContainer) { + if (!errorContainer) { + errorContainer = wc_monei_form.$errorContainer; + } + $(errorContainer).html('
'); + $(errorContainer).find('li').text(error_string); + // Scroll to error + if ($(errorContainer).find('.monei-error').length) { + $('html, body').animate({ + scrollTop: ($(errorContainer).offset().top - 200) + }, 200); } }, /** * Clearing form errors. */ - clear_errors: function() { - $( '.monei-error' ).remove(); + clear_errors: function(errorContainer) { + if (!errorContainer) { + errorContainer = wc_monei_form.$errorContainer; + } + // Clear all content from the error container + $(errorContainer).html(''); }, monei_token_handler: function( token ) { - wc_monei_form.create_hidden_input( 'monei_payment_token', token ); + wc_monei_form.create_hidden_input( 'monei_payment_token', 'payment-form' , token ); // Once Token is created, submit form. wc_monei_form.form.submit(); }, apple_google_token_handler: function (token ) { - wc_monei_form.create_hidden_input( 'monei_payment_request_token', token ); + $('#place_order').prop('disabled', false); + wc_monei_form.create_hidden_input( 'monei_payment_request_token', 'payment-request-form', token ); // Once Token is created, submit form. wc_monei_form.form.submit(); }, - create_hidden_input: function( id, token ) { - console.log( 'token', token ); + create_hidden_input: function( id, form, token ) { var hiddenInput = document.createElement( 'input' ); hiddenInput.setAttribute( 'type', 'hidden' ); hiddenInput.setAttribute( 'name', id ); hiddenInput.setAttribute( 'id', id ); hiddenInput.setAttribute( 'value', token ); - wc_monei_form.$paymentForm = document.getElementById( 'payment-form' ); + wc_monei_form.$paymentForm = document.getElementById( form ); wc_monei_form.$paymentForm.appendChild( hiddenInput ); }, - get_form: function() { - return this.form; + /** + * If Apple can make payments then we need to show the apple logo and title instead of Google + */ + update_apple_google_label: function () { + //if apple google is selected and Apple can make payment + if ( ! wc_monei_params.apple_google_pay ) { + return; + } + const isApple = window.ApplePaySession?.canMakePayments(); + if (isApple) { + const label = document.querySelector('label[for="payment_method_monei_apple_google"]'); + if (label) { + // Change the label text to "Apple Pay" + label.childNodes[0].nodeValue = "Apple Pay "; + + // Select the image within the label and change its source + const icon = label.querySelector('img'); + if (icon) { + icon.src = "https://mollie-payments-for-woocommerce.ddev.site/wp-content/plugins/monei-woocommerce-do-not-delete/assets/images/apple-logo.svg"; + icon.alt = "Apple Pay"; // Optional: update alt text as well + } + } + } } }; $( function() { wc_monei_form.init(); + wc_monei_form.update_apple_google_label(); } ); diff --git a/assets/js/cofidis.js b/assets/js/monei-cofidis.js similarity index 100% rename from assets/js/cofidis.js rename to assets/js/monei-cofidis.js diff --git a/assets/js/monei.min.js b/assets/js/monei.min.js deleted file mode 100644 index c84a925..0000000 --- a/assets/js/monei.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(e){"use strict";e(document.body).on("updated_checkout",(function(e,n){"object"==typeof n&&n.fragments&&n.fragments.monei_new_total&&(t.total=n.fragments.monei_new_total),t.is_monei_selected()&&(t.init_checkout_monei(),t.init_apple_google_pay())})),e("form#add_payment_method").on("click payment_methods",(function(){t.is_monei_selected()&&t.init_checkout_monei()})),e("form#order_review").on("click",(function(){t.is_monei_selected()&&t.init_checkout_monei()}));var n=document.getElementById("order_review");n&&new MutationObserver((function(e,n){for(var o of e)"childList"===o.type&&t.is_monei_selected()&&t.init_checkout_monei()})).observe(n,{childList:!0,subtree:!0});var t={$checkout_form:e("form.woocommerce-checkout"),$add_payment_form:e("form#add_payment_method"),$order_pay_form:e("form#order_review"),$cardInput:null,$container:null,$payment_request_container:"#payment_request_container",$errorContainer:null,$paymentForm:null,is_checkout:!1,is_add_payment_method:!1,is_order_pay:!1,form:null,submitted:!1,init_counter:0,total:wc_monei_params.total,init:function(){this.$checkout_form.length&&(this.is_checkout=!0,this.form=this.$checkout_form,this.form.on("checkout_place_order",this.place_order)),this.$add_payment_form.length&&(this.is_add_payment_method=!0,this.form=this.$add_payment_form,this.form.on("submit",this.place_order)),this.$order_pay_form.length&&(t.is_monei_selected()&&t.init_checkout_monei(),this.is_order_pay=!0,this.form=this.$order_pay_form,this.form.on("submit",this.place_order)),this.form&&this.form.on("change",this.on_change)},submit_form:function(){t.form.submit()},on_change:function(){e("[name='payment_method']").on("change",(function(){t.on_payment_selected()})),e("[name='wc-monei-payment-token']").on("change",(function(){t.on_payment_selected()}))},on_payment_selected(){t.is_monei_selected()?(t.init_checkout_monei(),t.is_checkout&&e("[name='woocommerce_checkout_place_order']").attr("data-monei","submit"),t.is_checkout&&wc_monei_params.apple_google_pay&&(t.is_tokenized_cc_selected()?t.hide_payment_request_container():t.show_payment_request_container())):t.is_checkout&&e("[name='woocommerce_checkout_place_order']").removeAttr("data-monei")},is_monei_selected:function(){return e("#payment_method_monei").is(":checked")},is_tokenized_cc_selected:function(){return e('input[name="wc-monei-payment-token"]').is(":checked")&&"new"!==e('input[name="wc-monei-payment-token"]:checked').val()},is_monei_saved_cc_selected:function(){return t.is_monei_selected()&&t.is_tokenized_cc_selected()},init_apple_google_pay:function(){wc_monei_params.apple_google_pay&&(window.paymentRequest&&window.paymentRequest.close(),t.instantiate_payment_request())},instantiate_payment_request:function(){var e=monei.PaymentRequest({accountId:wc_monei_params.account_id,sessionId:wc_monei_params.session_id,amount:parseInt(t.total),currency:wc_monei_params.currency,onSubmit(e){t.apple_google_token_handler(e.token)},onError(e){console.log(e)}});e.render("#payment-request-container"),window.paymentRequest=e},hide_payment_request_container:function(){e("#payment-request-container").hide()},show_payment_request_container:function(){e("#payment-request-container").show()},init_checkout_monei:function(){if(t.$container&&0===t.$container.childElementCount&&(t.init_counter=0),0===this.init_counter&&!t.is_monei_saved_cc_selected()){t.is_checkout&&e("[name='woocommerce_checkout_place_order']").attr("data-monei","submit"),t.init_apple_google_pay(),t.$container=document.getElementById("card-input"),t.$errorContainer=document.getElementById("monei-card-error");t.$cardInput=monei.CardInput({accountId:wc_monei_params.account_id,sessionId:wc_monei_params.session_id,style:{input:{fontFamily:'"Helvetica Neue", Helvetica, sans-serif',fontSmoothing:"antialiased",fontSize:"15px"},invalid:{color:"#fa755a"},icon:{marginRight:"0.4em"}},onChange:function(e){e.isTouched&&e.error?t.print_errors(e.error):t.clear_errors()},onEnter:function(){t.form.submit()},onFocus:function(){t.$container.classList.add("is-focused")},onBlur:function(){t.$container.classList.remove("is-focused")}}),t.$cardInput.render(t.$container),this.init_counter++}},place_order:function(n){return!!e("#monei_payment_token").length||(!!e("#monei_payment_request_token").length||(!t.is_monei_selected()||(!!t.is_monei_saved_cc_selected()||(n.preventDefault(),monei.createToken(t.$cardInput).then((function(e){e.error?(console.log("error",e.error),t.print_errors(e.error)):t.monei_token_handler(e.token)})).catch((function(e){console.log(e),t.print_errors(e.message)})),!1))))},print_errors:function(n){e(t.$errorContainer).html('
'),e(t.$errorContainer).find("li").text(n),e(".monei-error").length&&e("html, body").animate({scrollTop:e(".monei-error").offset().top-200},200)},clear_errors:function(){e(".monei-error").remove()},monei_token_handler:function(e){t.create_hidden_input("monei_payment_token",e),t.form.submit()},apple_google_token_handler:function(e){t.create_hidden_input("monei_payment_request_token",e),t.form.submit()},create_hidden_input:function(e,n){console.log("token",n);var o=document.createElement("input");o.setAttribute("type","hidden"),o.setAttribute("name",e),o.setAttribute("id",e),o.setAttribute("value",n),t.$paymentForm=document.getElementById("payment-form"),t.$paymentForm.appendChild(o)},get_form:function(){return this.form}};e((function(){t.init()}))}(jQuery); \ No newline at end of file diff --git a/build/package.json b/build/package.json index 490e78a..b2e5e2b 100644 --- a/build/package.json +++ b/build/package.json @@ -7,7 +7,7 @@ "license": "MIT", "private": true, "scripts": { - "build": "terser ../assets/js/monei.js --compress --mangle --output ../assets/js/monei.min.js", + "build": "terser ../assets/js/monei-cc-classic.js --compress --mangle --output ../public/js/monei-cc-classic.min.js", "release": "release-it" }, "release-it": { diff --git a/class-woocommerce-gateway-monei.php b/class-woocommerce-gateway-monei.php index 04a3b92..d1a7c71 100644 --- a/class-woocommerce-gateway-monei.php +++ b/class-woocommerce-gateway-monei.php @@ -245,15 +245,15 @@ public function init() { do_action( 'woocommerce_gateway_monei_init' ); wp_register_style( 'monei-icons', - $this->plugin_url() . '/assets/css/monei-icons.css', + $this->plugin_url() . '/public/css/monei-icons-classic.css', [], - filemtime( $this->plugin_path() . '/assets/css/monei-icons.css' ), + filemtime( $this->plugin_path() . '/public/css/monei-icons-classic.css' ), 'screen' ); wp_enqueue_style( 'monei-icons' ); wp_register_style( 'monei-blocks-checkout-cc', - WC_Monei()->plugin_url(). '/public/css/monei-blocks-checkout-cc.css', + WC_Monei()->plugin_url(). '/public/css/monei-blocks-checkout.css', array(), WC_Monei()->version, 'all' @@ -265,17 +265,17 @@ public function copyKeysToCentralSettings($default_params) { $centralApiKey = get_option('monei_apikey'); $centralAccountId = get_option('monei_accountid'); - $ccApiKey = $default_params['apikey']; - $ccAccountId = $default_params['accountid']; + $ccApiKey = $default_params['apikey'] ?? false; + $ccAccountId = $default_params['accountid'] ?? false; // Update API key if centralApiKey is empty - if ( empty( $centralApiKey ) ) { - update_option( 'monei_apikey', !empty( $ccApiKey ) ? $ccApiKey : $centralApiKey ); + if ( empty( $centralApiKey ) && !empty( $ccApiKey ) ) { + update_option( 'monei_apikey', $ccApiKey ); } // Update Account ID if centralAccountId is empty - if ( empty( $centralAccountId ) ) { - update_option( 'monei_accountid', !empty( $ccAccountId ) ? $ccAccountId : $centralAccountId ); + if ( empty( $centralAccountId ) && !empty( $ccAccountId ) ) { + update_option( 'monei_accountid', $ccAccountId ); } return $default_params; @@ -289,31 +289,32 @@ public function copyKeysToCentralSettings($default_params) * * @return array */ - public function monei_settings_by_default( $default_params ) { - $default_params['testmode'] = empty( $default_params['testmode'] ) - ? ( !empty( get_option( 'monei_testmode' ) ) - ? get_option( 'monei_testmode' ) - : ( !empty( monei_get_settings( 'testmode' ) ) - ? monei_get_settings( 'testmode' ) - : $default_params['testmode'] ) ) - : $default_params['testmode']; - $default_params['apikey'] = empty( $default_params['apikey'] ) - ? ( !empty( get_option( 'monei_apikey' ) ) - ? get_option( 'monei_apikey' ) - : ( !empty( monei_get_settings( 'apikey' ) ) - ? monei_get_settings( 'apikey' ) - : $default_params['apikey'] ) ) - : $default_params['apikey']; - $default_params['debug'] = empty( $default_params['debug'] ) - ? ( !empty( get_option( 'monei_debug' ) ) - ? get_option( 'monei_debug' ) - : ( !empty( monei_get_settings( 'debug' ) ) - ? monei_get_settings( 'debug' ) - : $default_params['debug'] ) ) - : $default_params['apikey']; - $default_params['orderdo'] = ( empty( $default_params['orderdo'] ) ) ? monei_get_settings( 'orderdo' ) : $default_params['orderdo']; - return $default_params; - } + public function monei_settings_by_default( $default_params ) { + $default_params['testmode'] = $this->get_setting_with_default( 'testmode', $default_params ); + $default_params['apikey'] = $this->get_setting_with_default( 'apikey', $default_params ); + $default_params['debug'] = $this->get_setting_with_default( 'debug', $default_params ); + $default_params['orderdo'] = ( empty( $default_params['orderdo'] ) ) ? monei_get_settings( 'orderdo' ) : $default_params['orderdo']; + + return $default_params; + } + + private function get_setting_with_default( $key, $params ) { + if ( ! empty( $params[ $key ] ) ) { + return $params[ $key ]; + } + + $option_value = get_option( "monei_$key" ); + if ( ! empty( $option_value ) ) { + return $option_value; + } + + $monei_setting_value = monei_get_settings( $key ); + if ( ! empty( $monei_setting_value ) ) { + return $monei_setting_value; + } + + return ''; + } /** * Hooks when plugin_loaded @@ -350,7 +351,7 @@ private function include_payment_methods() { */ public function add_gateways( $methods ) { $methods[] = 'WC_Gateway_Monei_CC'; - if (has_block('woocommerce/checkout') && !is_wc_endpoint_url( 'order-pay' )) { + if (!is_admin()) { $methods[] = 'MoneiAppleGoogleGateway'; } $methods[] = 'WC_Gateway_Monei_Cofidis'; diff --git a/includes/MoneiBizumBlocksSupport.php b/includes/MoneiBizumBlocksSupport.php index 6b7f16b..0e2fab1 100644 --- a/includes/MoneiBizumBlocksSupport.php +++ b/includes/MoneiBizumBlocksSupport.php @@ -17,7 +17,7 @@ public function get_payment_method_script_handles() { wp_register_script( $script_name, - WC_Monei()->plugin_url(). '/public/js/block-checkout-bizum.min.js', + WC_Monei()->plugin_url(). '/public/js/monei-block-checkout-bizum.min.js', array( 'wc-blocks-checkout', 'wc-blocks-registry', diff --git a/includes/abstracts/abstract-wc-monei-payment-gateway-component.php b/includes/abstracts/abstract-wc-monei-payment-gateway-component.php index 448c539..dd4e379 100644 --- a/includes/abstracts/abstract-wc-monei-payment-gateway-component.php +++ b/includes/abstracts/abstract-wc-monei-payment-gateway-component.php @@ -13,7 +13,7 @@ * @since 5.0 */ abstract class WC_Monei_Payment_Gateway_Component extends WC_Monei_Payment_Gateway { - + const APPLE_GOOGLE_ID = 'monei_apple_google'; /** * Process the payment and return the result * @@ -203,9 +203,9 @@ public function create_payload( $order, $allowed_payment_method = null ) { if ( $this->tokenization && $this->get_save_payment_card_checkbox() ) { $payload['generatePaymentToken'] = true; } - + $componentGateways = [MONEI_GATEWAY_ID, self::APPLE_GOOGLE_ID]; // If merchant is not using redirect flow (means component CC or apple/google pay), there is a generated frontend token paymentToken and we need to add session ID to the request. - if ( MONEI_GATEWAY_ID === $this->id && ! $this->redirect_flow && ( $this->get_frontend_generated_monei_token() || $this->get_frontend_generated_monei_apple_google_token() ) ) { + if ( in_array($this->id, $componentGateways) && ! $this->redirect_flow && ( $this->get_frontend_generated_monei_token() || $this->get_frontend_generated_monei_apple_google_token() ) ) { $payload['sessionId'] = (string) WC()->session->get_customer_id(); } diff --git a/includes/abstracts/abstract-wc-monei-payment-gateway-hosted.php b/includes/abstracts/abstract-wc-monei-payment-gateway-hosted.php index 5ab95b8..1390fc2 100644 --- a/includes/abstracts/abstract-wc-monei-payment-gateway-hosted.php +++ b/includes/abstracts/abstract-wc-monei-payment-gateway-hosted.php @@ -106,6 +106,13 @@ public function process_payment( $order_id, $allowed_payment_method = null ) { if ( $this->tokenization && $this->get_save_payment_card_checkbox() ) { $payload['generatePaymentToken'] = true; } + + if ( $token_id = $this->get_frontend_generated_bizum_token() ) { + if(! $this->isBlockCheckout()) { + $payload['paymentToken'] = $token_id; + } + $payload['sessionId'] = (string) WC()->session->get_customer_id(); + } // Filter to enable external changes on payload. $payload = apply_filters( 'wc_gateway_monei_create_payload', $payload ); @@ -125,7 +132,7 @@ public function process_payment( $order_id, $allowed_payment_method = null ) { 'result' => 'success', 'redirect' => false, 'paymentId' => $payment->getId(),// Send the paymentId back to the client - 'token' => $this->get_frontend_generated_monei_token(),// Send the token back to the client + 'token' => $this->get_frontend_generated_bizum_token(),// Send the token back to the client 'completeUrl' => $payload['completeUrl'], 'failUrl'=> $payload['failUrl'], 'orderId'=> $order_id @@ -144,5 +151,18 @@ public function process_payment( $order_id, $allowed_payment_method = null ) { ); } } + + /** + * Frontend MONEI payment-request token generated when Bizum. + * + * @return false|string + */ + protected function get_frontend_generated_bizum_token() + { + if ($this->id !== 'monei_bizum'){ + return false; + } + return ( isset( $_POST[ 'monei_payment_request_token' ] ) ) ? filter_var( $_POST[ 'monei_payment_request_token' ], FILTER_SANITIZE_STRING ) : false; // WPCS: CSRF ok. + } } diff --git a/includes/abstracts/abstract-wc-monei-payment-gateway.php b/includes/abstracts/abstract-wc-monei-payment-gateway.php index 542ebca..9f01d3f 100644 --- a/includes/abstracts/abstract-wc-monei-payment-gateway.php +++ b/includes/abstracts/abstract-wc-monei-payment-gateway.php @@ -309,5 +309,24 @@ public function get_frontend_generated_monei_token() { return ( isset( $_POST['monei_payment_token'] ) ) ? filter_var( $_POST['monei_payment_token'], FILTER_SANITIZE_STRING ) : false; // WPCS: CSRF ok. } + /** + * @return float|int|string|null + */ + public function determineTheTotalAmountToBePassed() + { + $total = null; + if (is_wc_endpoint_url('order-pay') && isset($_GET['key'])) { + // If on the pay for order page, get the order total + $order_id = wc_get_order_id_by_order_key(sanitize_text_field($_GET['key'])); + if ($order_id) { + $order = wc_get_order($order_id); + $total = $order ? $order->get_total() : 0; + } + } else { + // Otherwise, use the cart total + $total = WC()->cart->get_total(false); + } + return $total; + } } diff --git a/includes/class-monei-cc-blocks.php b/includes/class-monei-cc-blocks.php index f3e33db..ba14656 100644 --- a/includes/class-monei-cc-blocks.php +++ b/includes/class-monei-cc-blocks.php @@ -54,7 +54,7 @@ public function get_payment_method_script_handles() { wp_register_script( $script_name, - WC_Monei()->plugin_url(). '/public/js/checkout-cc.min.js', + WC_Monei()->plugin_url(). '/public/js/monei-block-checkout-cc.min.js', array( 'wc-blocks-checkout', 'wc-blocks-registry', @@ -89,7 +89,7 @@ public function get_payment_method_data() { $total = isset(WC()->cart) ? WC()->cart->get_total( false ) : 0; $data = array( 'title' => $this->gateway->title, - 'description' => $this->gateway->description, + 'description' => $this->gateway->description === ' ' ? '' : $this->gateway->description, 'logo' => WC_Monei()->plugin_url() . '/assets/images/monei-cards.svg', 'logo_apple' => WC_Monei()->plugin_url() . '/assets/images/apple-logo.svg', 'logo_google' => WC_Monei()->plugin_url() . '/assets/images/google-logo.svg', diff --git a/includes/payment-methods/MoneiAppleGoogleGateway.php b/includes/payment-methods/MoneiAppleGoogleGateway.php index 798b8e6..f7b289e 100644 --- a/includes/payment-methods/MoneiAppleGoogleGateway.php +++ b/includes/payment-methods/MoneiAppleGoogleGateway.php @@ -34,6 +34,12 @@ public function __construct() { parent::__construct(); $this->id = 'monei_apple_google'; $this->method_title = __( 'MONEI - Apple/Google', 'monei' ); + $this->title = __( 'Google Pay', 'monei' ); + $this->description = __(' ', 'monei'); + $iconUrl = apply_filters( 'woocommerce_monei_icon', WC_Monei()->image_url( 'google-logo.svg' )); + $iconMarkup = 'MONEI'; + + $this->icon = ( $this->hide_logo ) ? '' : $iconMarkup; $this->settings = get_option( 'woocommerce_monei_settings', array() ); $this->enabled = ( ! empty( isset($this->settings['apple_google_pay']) && 'yes' ===$this->settings['apple_google_pay'] ) ) ? 'yes' : 'no'; @@ -50,10 +56,6 @@ public function __construct() { */ public function hideAppleGoogleInCheckout($available_gateways) { - if (!has_block('woocommerce/checkout')) { - unset($available_gateways['monei_apple_google']); - } - return $available_gateways; } public function isBlockCheckout(): bool @@ -87,5 +89,36 @@ public function isBlockCheckout(): bool public function process_payment($order_id, $allowed_payment_method = null) { return parent::process_payment($order_id, self::PAYMENT_METHOD); } + + /** + * Payments fields, shown on checkout or payment method page (add payment method). + */ + function payment_fields() { + ob_start(); + + // Checkout screen. + // We show description, if tokenization available, we show saved cards and checkbox to save. + echo esc_html( $this->description ); + if ( $this->apple_google_pay ) { + $this->render_google_pay_form(); + } + + ob_end_flush(); + } + + /** + * Form where Google or Apple Pay button will be rendered. + * https://docs.monei.com/docs/monei-js/payment-request/#2-add-payment-request-component-to-your-payment-page-client-side + */ + protected function render_google_pay_form() { + ?> +
+
+
+
+
+
+ id = MONEI_GATEWAY_ID; $this->method_title = __( 'MONEI - Credit Card', 'monei' ); - $this->method_description = __( 'Accept Credit Card payments.', 'monei' ); + //$this->method_description = __( 'Accept Credit Card payments.', 'monei' ); $this->enabled = ( ! empty( $this->get_option( 'enabled' ) && 'yes' === $this->get_option( 'enabled' ) ) && $this->is_valid_for_use() ) ? 'yes' : false; // Load the form fields. @@ -54,7 +54,12 @@ public function __construct() { // Load the settings. $this->init_settings(); - // Hosted payment with redirect. + $description = ! empty( $this->get_option( 'description' ) ) + ? $this->get_option( 'description' ) + : ' '; // Non-breaking space if description is empty + + + // Hosted payment with redirect. $this->has_fields = false; $iconUrl = apply_filters( 'woocommerce_monei_icon', WC_Monei()->image_url( 'monei-cards.svg' )); $iconMarkup = 'MONEI'; @@ -65,7 +70,7 @@ public function __construct() { $this->apple_google_pay = ( ! empty( $this->get_option( 'apple_google_pay' ) && 'yes' === $this->get_option( 'apple_google_pay' ) ) ) ? true : false; $this->testmode = ( ! empty( $this->getTestmode() && 'yes' === $this->get_option( 'testmode' ) ) ) ? true : false; $this->title = ( ! empty( $this->get_option( 'title' ) ) ) ? $this->get_option( 'title' ) : ''; - $this->description = ( ! empty( $this->get_option( 'description' ) ) ) ? $this->get_option( 'description' ) : ''; + $this->description = ( ! empty( $this->get_option( 'description' ) ) ) ? $this->get_option( 'description' ) : ' '; $this->status_after_payment = ( ! empty( $this->get_option( 'orderdo' ) ) ) ? $this->get_option( 'orderdo' ) : ''; $this->account_id = $this->getAccountId(); $this->api_key = $this->getApiKey(); @@ -269,67 +274,44 @@ function payment_fields() { $this->render_monei_form(); } } - if ( $this->apple_google_pay ) { - $this->render_google_pay_form(); - } } ob_end_flush(); } - /** - * Form where Google or Apple Pay button will be rendered. - * https://docs.monei.com/docs/monei-js/payment-request/#2-add-payment-request-component-to-your-payment-page-client-side - */ - protected function render_google_pay_form() { - ?> -
-
-
- -
-
-
- - -
-
-
-
- -
- -
-
-
-
- id); ?>-cc-form"> + +
+ +
+
+ +
+
+
+
+
+ + + testmode ) ? 'monei.js' : 'monei.min.js'; - wp_register_script( 'monei', 'https://js.monei.com/v1/monei.js', '', '1.0', true ); - wp_register_script( 'woocommerce_monei', plugins_url( 'assets/js/' . $script_version_name, MONEI_MAIN_FILE ), [ + if(!wp_script_is('monei', 'registered')){ + wp_register_script( 'monei', 'https://js.monei.com/v1/monei.js', '', '1.0', true ); + + } + wp_register_script( 'woocommerce_monei', plugins_url( 'public/js/monei-cc-classic.min.js', MONEI_MAIN_FILE ), [ 'jquery', 'monei' ], MONEI_VERSION, true ); wp_enqueue_script( 'monei' ); - + // Determine the total amount to be passed + $total = $this->determineTheTotalAmountToBePassed(); wp_localize_script( 'woocommerce_monei', 'wc_monei_params', @@ -360,8 +345,9 @@ public function monei_scripts() { 'account_id' => monei_get_settings( false, 'monei_accountid' ), 'session_id' => WC()->session->get_customer_id(), 'apple_google_pay' => $this->apple_google_pay, - 'total' => monei_price_format( WC()->cart->get_total( false ) ), + 'total' => monei_price_format( $total ), 'currency' => get_woocommerce_currency(), + 'apple_logo' => WC_Monei()->image_url( 'apple-logo.svg' ) ] ); diff --git a/includes/payment-methods/class-wc-gateway-monei-hosted-bizum.php b/includes/payment-methods/class-wc-gateway-monei-hosted-bizum.php index b173e20..19fbfb3 100644 --- a/includes/payment-methods/class-wc-gateway-monei-hosted-bizum.php +++ b/includes/payment-methods/class-wc-gateway-monei-hosted-bizum.php @@ -38,7 +38,7 @@ public function __construct() { $this->hide_logo = ( ! empty( $this->get_option( 'hide_logo' ) && 'yes' === $this->get_option( 'hide_logo' ) ) ) ? true : false; $this->icon = ( $this->hide_logo ) ? '' : $iconMarkup; $this->title = ( ! empty( $this->get_option( 'title' ) ) ) ? $this->get_option( 'title' ) : ''; - $this->description = ( ! empty( $this->get_option( 'description' ) ) ) ? $this->get_option( 'description' ) : ''; + $this->description = ( ! empty( $this->get_option( 'description' ) ) ) ? $this->get_option( 'description' ) : ' '; $this->status_after_payment = ( ! empty( $this->get_option( 'orderdo' ) ) ) ? $this->get_option( 'orderdo' ) : ''; $this->api_key = $this->getApiKey(); $this->account_id = $this->getAccountId(); @@ -61,8 +61,26 @@ function ($is_post) { return $this->checks_before_save($is_post, 'woocommerce_monei_bizum_enabled'); } ); + + add_action('wp_enqueue_scripts', [$this, 'bizum_scripts']); } + /** + * Return whether or not this gateway still requires setup to function. + * + * When this gateway is toggled on via AJAX, if this returns true a + * redirect will occur to the settings page instead. + * + * @since 3.4.0 + * @return bool + */ + public function needs_setup() { + + if ( ! $this->account_id || ! $this->api_key ) { + return true; + } + return false; + } /** * Initialise Gateway Settings Form Fields * @@ -85,5 +103,50 @@ public function init_form_fields() { public function process_payment( $order_id, $allowed_payment_method = null ) { return parent::process_payment( $order_id, self::PAYMENT_METHOD ); } + + public function payment_fields() { + echo '
+
+
+
'; + } + + public function bizum_scripts() { + if (! is_checkout()) { + return; + } + if ( 'no' === $this->enabled ) { + return; + } + if(!wp_script_is('monei', 'registered')){ + wp_register_script( 'monei', 'https://js.monei.com/v1/monei.js', '', '1.0', true ); + } + if(!wp_script_is('monei', 'enqueued')) { + wp_enqueue_script( 'monei' ); + } + wp_register_script( 'woocommerce_monei-bizum', plugins_url( 'public/js/monei-bizum-classic.min.js', MONEI_MAIN_FILE ), [ + 'jquery', + 'monei' + ], MONEI_VERSION, true ); + wp_enqueue_script('woocommerce_monei-bizum'); + + // Determine the total amount to be passed + $total = $this->determineTheTotalAmountToBePassed(); + + wp_localize_script( + 'woocommerce_monei-bizum', + 'wc_bizum_params', + [ + 'account_id' => monei_get_settings( false, 'monei_accountid' ), + 'session_id' => WC()->session->get_customer_id(), + 'total' => monei_price_format( $total ), + 'currency' => get_woocommerce_currency(), + 'language' => locale_iso_639_1_code(), + ] + ); + } } diff --git a/includes/payment-methods/class-wc-gateway-monei-hosted-cofidis.php b/includes/payment-methods/class-wc-gateway-monei-hosted-cofidis.php index c0d5ad6..c7bc267 100644 --- a/includes/payment-methods/class-wc-gateway-monei-hosted-cofidis.php +++ b/includes/payment-methods/class-wc-gateway-monei-hosted-cofidis.php @@ -71,6 +71,23 @@ function ($is_post) { } ); } + /** + * Return whether or not this gateway still requires setup to function. + * + * When this gateway is toggled on via AJAX, if this returns true a + * redirect will occur to the settings page instead. + * + * @since 3.4.0 + * @return bool + */ + public function needs_setup() { + + if ( ! $this->account_id || ! $this->api_key ) { + return true; + } + + return false; + } /** * Initialise Gateway Settings Form Fields @@ -139,8 +156,7 @@ public function cofidis_scripts() { wp_enqueue_script( 'monei' ); } - $script_version_name = ( $this->testmode ) ? 'cofidis.js' : 'cofidis.min.js'; - wp_register_script( 'woocommerce_monei_cofidis', plugins_url( 'assets/js/' . $script_version_name, MONEI_MAIN_FILE ), [ 'jquery', 'monei' ], MONEI_VERSION, true ); + wp_register_script( 'woocommerce_monei_cofidis', plugins_url( 'public/js/monei-cofidis.min.js', MONEI_MAIN_FILE ), [ 'jquery', 'monei' ], MONEI_VERSION, true ); wp_localize_script( 'woocommerce_monei_cofidis', 'wc_monei_cofidis_params', diff --git a/includes/payment-methods/class-wc-gateway-monei-hosted-paypal.php b/includes/payment-methods/class-wc-gateway-monei-hosted-paypal.php index aba2a39..96c0826 100644 --- a/includes/payment-methods/class-wc-gateway-monei-hosted-paypal.php +++ b/includes/payment-methods/class-wc-gateway-monei-hosted-paypal.php @@ -63,6 +63,24 @@ function ($is_post) { ); } + /** + * Return whether or not this gateway still requires setup to function. + * + * When this gateway is toggled on via AJAX, if this returns true a + * redirect will occur to the settings page instead. + * + * @since 3.4.0 + * @return bool + */ + public function needs_setup() { + + if ( ! $this->account_id || ! $this->api_key ) { + return true; + } + + return false; + } + /** * Initialise Gateway Settings Form Fields * diff --git a/templates/notice-admin-gateway-not-available-api.php b/templates/notice-admin-gateway-not-available-api.php index ef13632..28c6ee7 100644 --- a/templates/notice-admin-gateway-not-available-api.php +++ b/templates/notice-admin-gateway-not-available-api.php @@ -1,8 +1,13 @@ 'wc-settings', + 'tab' => 'monei_settings', +), 'admin.php' ) ) ); ?>
-

- : -

+

+ : + +

\ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index da01a03..d21af9e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,52 +1,36 @@ -const path = require( 'path' ); -const defaultConfig = require( '@wordpress/scripts/config/webpack.config' ); +const path = require('path'); +const fs = require('fs'); +const defaultConfig = require('@wordpress/scripts/config/webpack.config'); -module.exports = () => { - const configs = [ - { - ...defaultConfig, - entry: { - 'checkout-cc': path.resolve( - __dirname, - 'assets/js/checkout-cc.js' - ), - 'block-checkout-bizum': path.resolve( - __dirname, - 'assets/js/block-checkout-bizum.js' - ), - }, - output: { - path: path.resolve( __dirname, 'public/js/' ), // Output directory - filename: '[name].min.js', // Use the entry name for the output file - }, - }, - { - ...defaultConfig, - entry: { - 'monei-blocks-checkout-cc': path.resolve( - __dirname, - 'assets/css/monei-blocks-checkout.css' - ), - }, - output: { - path: path.resolve( __dirname, 'public/css/' ), - filename: '[name].js', - }, +// Helper function to dynamically generate entries for a specific file type +const getEntries = (dir, extension) => { + const entries = {}; + fs.readdirSync(dir).forEach(file => { + if (path.extname(file) === extension) { + const name = path.parse(file).name; // Get file name without extension + entries[name] = path.resolve(dir, file); + } + }); + return entries; +}; + +const configs = [ + { + ...defaultConfig, + entry: getEntries(path.resolve(__dirname, 'assets/js'), '.js'), + output: { + path: path.resolve(__dirname, 'public/js/'), + filename: '[name].min.js', }, - { - ...defaultConfig, - entry: { - 'monei-admin': path.resolve( - __dirname, - 'assets/css/monei-admin.css' - ), - }, - output: { - path: path.resolve( __dirname, 'public/css/' ), - filename: '[name].js', - }, + }, + { + ...defaultConfig, + entry: getEntries(path.resolve(__dirname, 'assets/css'), '.css'), + output: { + path: path.resolve(__dirname, 'public/css/'), + filename: '[name].js' }, - ]; + }, +]; - return configs; -}; +module.exports = configs; \ No newline at end of file