From 41b278b693e59784ac928593968deb86a5b66f62 Mon Sep 17 00:00:00 2001 From: Kcppdevelopers Date: Fri, 21 Jul 2017 18:16:18 +0530 Subject: [PATCH] Express Checkout - In Context Checkout Integration, ref #199 --- ...ateway-paypal-express-helper-angelleye.php | 38 ++-- ...teway-paypal-express-request-angelleye.php | 1 + assets/css/single.css | 6 +- .../js/angelleye-express-checkout-custom.js | 52 ------ assets/js/angelleye-express-checkout.js | 2 +- assets/js/angelleye-frontend.js | 61 ++++--- assets/js/angelleye-in-context-checkout.js | 62 ++++--- assets/js/checkout.js | 170 ------------------ paypal-for-woocommerce.php | 7 +- 9 files changed, 93 insertions(+), 306 deletions(-) delete mode 100644 assets/js/angelleye-express-checkout-custom.js delete mode 100644 assets/js/checkout.js diff --git a/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-helper-angelleye.php b/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-helper-angelleye.php index 3fe175c5d..b56e7c6d6 100644 --- a/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-helper-angelleye.php +++ b/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-helper-angelleye.php @@ -151,7 +151,7 @@ public function buy_now_button() { break; } if ($this->show_paypal_credit == 'yes') { - $paypal_credit_button_markup = ''; + $paypal_credit_button_markup = ''; $paypal_credit_button_markup .= ''; $paypal_credit_button_markup .= ''; $ec_html_button .= $paypal_credit_button_markup; @@ -415,7 +415,9 @@ public function ec_order_received_text($text, $order) { public function ec_enqueue_scripts_product_page() { try { - if($this->enable_in_context_checkout_flow === 'yes' ) { + + $js_value = array('is_page_name' => '', 'enable_in_context_checkout_flow' => ( $this->enable_in_context_checkout_flow == 'yes' ? 'yes' : 'no')); + if($this->enable_in_context_checkout_flow === 'yes' && (is_checkout() || is_product() || is_cart())) { wp_enqueue_script( 'angelleye-in-context-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true ); wp_enqueue_script( 'angelleye-in-context-checkout-js-frontend', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/js/angelleye-in-context-checkout.js', array( 'jquery' ), $this->version, true ); wp_localize_script( 'angelleye-in-context-checkout-js-frontend', 'angelleye_in_content_param', @@ -433,23 +435,15 @@ public function ec_enqueue_scripts_product_page() { ) ); - } - wp_enqueue_style('angelleye-express-checkout-css', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/css/angelleye-express-checkout.css', array(), $this->version, 'all'); + } + if (is_checkout()) { - wp_enqueue_script('angelleye-express-checkout-js', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/js/angelleye-express-checkout.js', array(), $this->version, 'all'); - wp_enqueue_script('angelleye-express-checkout-custom', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/js/angelleye-express-checkout-custom.js', array(), $this->version, 'all'); - wp_localize_script('angelleye-express-checkout-js', 'is_page_name', 'checkout_page'); - wp_localize_script('angelleye-express-checkout-custom', 'is_page_name', 'checkout_page'); - } - if (is_product()) { - wp_enqueue_script('angelleye-express-checkout-custom', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/js/angelleye-express-checkout-custom.js', array(), $this->version, 'all'); - wp_localize_script('angelleye-express-checkout-custom', 'is_page_name', 'single_product_page'); - } - if (is_cart()) { - wp_enqueue_script('angelleye-express-checkout-custom', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/js/angelleye-express-checkout-custom.js', array(), $this->version, 'all'); - wp_localize_script('angelleye-express-checkout-custom', 'is_page_name', 'cart_page'); - } - return true; + $js_value['is_page_name'] = 'checkout_page'; + wp_enqueue_script('angelleye-express-checkout-js', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/js/angelleye-express-checkout.js', array(), $this->version, 'all'); + wp_localize_script('angelleye-express-checkout-js', 'angelleye_js_value', $js_value); + } + + wp_enqueue_style('angelleye-express-checkout-css', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/css/angelleye-express-checkout.css', array(), $this->version, 'all'); } catch (Exception $ex) { } @@ -745,10 +739,16 @@ public function angelleye_ajax_generate_cart() { WC()->cart->calculate_totals(); } $url = esc_url_raw(add_query_arg('pp_action', 'set_express_checkout', add_query_arg('wc-api', 'WC_Gateway_PayPal_Express_AngellEYE', home_url('/')))); - if( !empty($_POST['wc-paypal_express-new-payment-method']) && $_POST['wc-paypal_express-new-payment-method'] == true ) { + if( !empty($_POST['wc-paypal_express-new-payment-method']) && $_POST['wc-paypal_express-new-payment-method'] == 'true' ) { $url = add_query_arg( 'ec_save_to_account', 'true', $url ); } + if( !empty($_POST['is_cc']) && $_POST['is_cc'] == 'true' ) { + $url = add_query_arg( 'use_paypal_credit', 'true', $url ); + } + + + wp_send_json( array('url' => $url) ); } diff --git a/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-request-angelleye.php b/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-request-angelleye.php index 6a34fedc2..a89e07966 100644 --- a/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-request-angelleye.php +++ b/angelleye-includes/express-checkout/class-wc-gateway-paypal-express-request-angelleye.php @@ -77,6 +77,7 @@ public function angelleye_redirect() { $paypal_express_checkout = WC()->session->get('paypal_express_checkout'); if( !empty($paypal_express_checkout['token'] ) ) { $payPalURL = $this->PAYPAL_URL . $paypal_express_checkout['token']; + wc_clear_notices(); wp_redirect($payPalURL, 302); exit; } diff --git a/assets/css/single.css b/assets/css/single.css index f1ea5fcce..6d1c21886 100644 --- a/assets/css/single.css +++ b/assets/css/single.css @@ -9,7 +9,11 @@ #paypal_ec_paypal_credit_button, #paypal_ec_button, .paypal_ec_textbutton { position: relative; } -.single_variation_wrap_angelleye:disabled { +.single_add_to_cart_button.disabled { + opacity: 0.4; + cursor: not-allowed; +} +.single_variation_wrap_angelleye:disabled, .single_add_to_cart_button:disabled { opacity: 0.4; cursor: not-allowed; } diff --git a/assets/js/angelleye-express-checkout-custom.js b/assets/js/angelleye-express-checkout-custom.js deleted file mode 100644 index d4eb1fa4d..000000000 --- a/assets/js/angelleye-express-checkout-custom.js +++ /dev/null @@ -1,52 +0,0 @@ -jQuery(function($) { - if( is_page_name == "single_product_page" ){ - jQuery("a.single_add_to_cart_button.paypal_checkout_button").click(function() { - if (!$('.woocommerce-variation-add-to-cart').hasClass('woocommerce-variation-add-to-cart-disabled')) { - block_div_to_payment(); - var express_checkout_action = $(this).attr('href'); - $(this).append(" "); - $('form.variations_form').attr('action', express_checkout_action); - $(this).attr('disabled', 'disabled'); - $('form.variations_form').submit(); - return true; - } - }); - - } else if( is_page_name == "checkout_page" ){ - jQuery("a.single_add_to_cart_button").click(function () { - block_div_to_payment(); - }); -var is_set_class = $("div").is(".express-provided-address"); -if( is_set_class ){ - jQuery('#express_checkout_button_chekout_page').hide(); - jQuery('#express_checkout_button_text').hide(); - jQuery('.woocommerce-message').hide(); - jQuery('.checkout_paypal_message').hide(); - } - - } else if(is_page_name == "cart_page"){ - jQuery("a.single_add_to_cart_button.paypal_checkout_button").click(function () { - block_div_to_payment(); - }); - } - - function block_div_to_payment() { - var ec_class = ""; - if (is_page_name == 'cart_page') { - ec_class = "div.wc-proceed-to-checkout"; - } else if (is_page_name == 'single_product_page') { - ec_class = "form.cart"; - } else if (is_page_name == 'checkout_page') { - ec_class = "div.woocommerce"; - } - if (ec_class.toString().length > 0) { - $(ec_class).block({ - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 - } - }); - } - } -}); \ No newline at end of file diff --git a/assets/js/angelleye-express-checkout.js b/assets/js/angelleye-express-checkout.js index 3c18a7f4c..19c4b8e5c 100644 --- a/assets/js/angelleye-express-checkout.js +++ b/assets/js/angelleye-express-checkout.js @@ -1,5 +1,5 @@ (function() { - if( is_page_name == "checkout_page" ){ + if( angelleye_js_value.is_page_name == "checkout_page"){ jQuery(document).ready(function(e) { return e("form.checkout").length && e("body").hasClass("express-checkout") ? (e("ul.payment_methods").hide(), e(".ex-show-address-fields").on("click", function(b) { var c; diff --git a/assets/js/angelleye-frontend.js b/assets/js/angelleye-frontend.js index a43fb1a6c..0b4bd4ae0 100644 --- a/assets/js/angelleye-frontend.js +++ b/assets/js/angelleye-frontend.js @@ -1,24 +1,27 @@ -jQuery(document).ready(function ($){ - if (angelleye_frontend.is_product == "yes" && angelleye_frontend.enable_in_context_checkout_flow == 'no'){ - jQuery("#paypal_ec_button_product input, input.single_variation_wrap_angelleye").click(function(){ - if (!$('.woocommerce-variation-add-to-cart').hasClass('woocommerce-variation-add-to-cart-disabled')) { - $( '.cart' ).block({ - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 - } - }); - var angelleye_action = $(this).data('action'); - $('form.cart').attr( 'action', angelleye_action ); - $(this).attr('disabled', 'disabled'); - $('form.cart').submit(); - return false; - } +jQuery(document).ready(function ($) { + if (angelleye_frontend.is_product == "yes") { + jQuery(".single_add_to_cart_button.paypal_checkout_button").click(function () { + if (!$('.woocommerce-variation-add-to-cart').hasClass('woocommerce-variation-add-to-cart-disabled')) { + $('.cart').block({ + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }); + var angelleye_action = $(this).data('action'); + if( typeof angelleye_action === 'undefined' || angelleye_action === null ){ + angelleye_action = $(this).attr("href"); + } + $('form.cart').attr('action', angelleye_action); + $(this).attr('disabled', 'disabled'); + $('form.cart').submit(); + return false; + } }); } - if (angelleye_frontend.is_cart == "yes"){ - $(".paypal_checkout_button").click(function(){ + if (angelleye_frontend.is_cart == "yes") { + $(".paypal_checkout_button").click(function () { $('.woocommerce').block({ message: null, overlayCSS: { @@ -36,23 +39,23 @@ jQuery(document).ready(function ($){ $(this).attr("href", angelleye_action); }); } - - if (angelleye_frontend.is_checkout == "yes"){ + + if (angelleye_frontend.is_checkout == "yes") { var is_set_class = $("div").is(".express-provided-address"); if (is_set_class) { jQuery('#express_checkout_button_chekout_page').hide(); jQuery('#express_checkout_button_text').hide(); jQuery('.woocommerce-message').hide(); jQuery('#checkout_paypal_message').hide(); - } + } jQuery(".paypal_checkout_button").click(function () { $('.woocommerce').block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 - } - }); + } + }); if ($("#wc-paypal_express-new-payment-method").is(':checked')) { angelleye_action = $(this).attr("href"); angelleye_action = angelleye_action + '&ec_save_to_account=true'; @@ -63,10 +66,10 @@ jQuery(document).ready(function ($){ return true; }); } - // Let themes/plugins override our event handlers - // NOTE: The jQuery .on() function attached to listen to the event below MUST - // be included somewhere on the page itself...so it gets registered before we load - // asyncronous JavaScript resources. Otherwise your function listening might not fire. + // Let themes/plugins override our event handlers + // NOTE: The jQuery .on() function attached to listen to the event below MUST + // be included somewhere on the page itself...so it gets registered before we load + // asyncronous JavaScript resources. Otherwise your function listening might not fire. - $(".paypal_checkout_button").trigger('angelleye_paypal_checkout_button_js_loaded'); + $(".paypal_checkout_button").trigger('angelleye_paypal_checkout_button_js_loaded'); }); diff --git a/assets/js/angelleye-in-context-checkout.js b/assets/js/angelleye-in-context-checkout.js index 0c9400f64..8b454e222 100644 --- a/assets/js/angelleye-in-context-checkout.js +++ b/assets/js/angelleye-in-context-checkout.js @@ -1,9 +1,11 @@ ; (function ($, window, document) { if (angelleye_in_content_param.is_product == 'no') { - $angelleye_ec_in_content = { - init: function () { + + var target_url = ''; + window.paypalCheckoutReady = function () { + setInterval(function () { $('.woocommerce').unblock(); }, 3000); @@ -13,37 +15,24 @@ { environment: angelleye_in_content_param.environment, button: selector, - locale: angelleye_in_content_param.locale + locale: angelleye_in_content_param.locale, + click: function (event) { + event.preventDefault(); + console.log($(event.target).parent().attr("href")); + paypal.checkout.initXO(); + target_url = $(event.target).parent().attr("href"); + if( $("#wc-paypal_express-new-payment-method").is(':checked') ) { + target_url = target_url + "&ec_save_to_account=true"; + } + paypal.checkout.startFlow(target_url); + } } ); }); } - } - } - var costs_updated = false; - $('a.paypal_checkout_button', 'a.paypal_checkout_button_cc').click(function (event) { - if (costs_updated) { - costs_updated = false; - return; - } - event.stopPropagation(); - var data = { - 'nonce': angelleye_in_content_param.update_shipping_costs_nonce - }; - $.ajax({ - type: 'POST', - data: data, - url: angelleye_in_content_param.ajaxurl, - success: function (response) { - costs_updated = true; - $( this ).click(); - } - }); - }); + + - if (angelleye_in_content_param.show_modal) { - $angelleye_ec_in_content.init(); - } } else { @@ -51,6 +40,8 @@ paypal.checkout.setup(angelleye_in_content_param.payer_id, { environment: angelleye_in_content_param.environment, click: function (event) { + event.preventDefault(); + paypal.checkout.initXO(); $( '.cart' ).block({ message: null, overlayCSS: { @@ -81,13 +72,13 @@ 'data': data }; }; - event.preventDefault(); - + var data = { 'nonce': angelleye_in_content_param.generate_cart_nonce, 'qty': $( '.quantity .qty' ).val(), 'attributes': $( '.variations_form' ).length ? get_attributes().data : [], - 'wc-paypal_express-new-payment-method' : $("#wc-paypal_express-new-payment-method").is(':checked') + 'wc-paypal_express-new-payment-method' : $("#wc-paypal_express-new-payment-method").is(':checked'), + 'is_cc': $(event.target).hasClass('ec_checkout_page_button_type_pc') }; $.ajax({ @@ -105,7 +96,14 @@ } }); }, - button: ['.paypal_checkout_button', '.paypal_checkout_button_cc'] + button: ['.paypal_checkout_button', '.paypal_checkout_button_cc'], + condition: function () { + if( $('.paypal_checkout_button').hasClass( "disabled" ) ) { + return false; + } else { + return true; + } + } }); } diff --git a/assets/js/checkout.js b/assets/js/checkout.js deleted file mode 100644 index f3d13d895..000000000 --- a/assets/js/checkout.js +++ /dev/null @@ -1,170 +0,0 @@ -jQuery(function($) { - - // wc_checkout_params is required to continue, ensure the object exists - if (typeof wc_checkout_params === "undefined") - return false; - - var updateTimer; - var dirtyInput = false; - var xhr; - - function update_checkout() { - - if (xhr) xhr.abort(); - - var shipping_methods = []; - - $('select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]').each( function( index, input ) { - shipping_methods[ $(this).data( 'index' ) ] = $(this).val(); - } ); - - var payment_method = $('#order_review input[name=payment_method]:checked').val(); - var country = $('#billing_country').val(); - var state = $('#billing_state').val(); - var postcode = $('input#billing_postcode').val(); - var city = $('input#billing_city').val(); - var address = $('input#billing_address_1').val(); - var address_2 = $('input#billing_address_2').val(); - - if ( $('#ship-to-different-address input').is(':checked') || $('#ship-to-different-address input').size() == 0 ) { - var s_country = $('#shipping_country').val(); - var s_state = $('#shipping_state').val(); - var s_postcode = $('input#shipping_postcode').val(); - var s_city = $('input#shipping_city').val(); - var s_address = $('input#shipping_address_1').val(); - var s_address_2 = $('input#shipping_address_2').val(); - } else { - var s_country = country; - var s_state = state; - var s_postcode = postcode; - var s_city = city; - var s_address = address; - var s_address_2 = address_2; - } - - $('#order_methods, #paypalexpress_order_review').block({message: null,overlayCSS: {background: '#fff',opacity: 0.6}}); - - var data = { - action: 'woocommerce_update_order_review', - security: wc_checkout_params.update_order_review_nonce, - shipping_method: shipping_methods, - payment_method: payment_method, - country: country, - state: state, - postcode: postcode, - city: city, - address: address, - address_2: address_2, - s_country: s_country, - s_state: s_state, - s_postcode: s_postcode, - s_city: s_city, - s_address: s_address, - s_address_2: s_address_2, - pp_action: 'revieworder', - post_data: $('form.angelleye_checkout').serialize() - }; - - xhr = $.ajax({ - type: 'POST', - url: wc_checkout_params.ajax_url, - data: data, - success: function( data ) { - - // Always update the fragments - if ( data && data.fragments ) { - $.each( data.fragments, function ( key, value ) { - $( key ).replaceWith( value ); - $( key ).unblock(); - } ); - } - - // Check for error - if ( 'failure' == data.result ) { - - if ( 'true' === data.reload ) { - window.location.reload(); - return; - } - - $( '.woocommerce-error, .woocommerce-message' ).remove(); - - // Form object - var $form = $( 'form.angelleye_checkout' ); - - // Add new errors - if ( data.messages ) { - $form.prepend( data.messages ); - } else { - $form.prepend( data ); - } - - // Lose focus for all fields - $form.find( '.input-text, select' ).blur(); - - // Scroll to top - $( 'html, body' ).animate( { - scrollTop: ( $( 'form.angelleye_checkout' ).offset().top - 100 ) - }, 1000 ); - - } - - //$( '#order_review' ).html( $.trim( response.html ) ); - $( '#order_review' ).find( 'input[name=payment_method]:checked' ).trigger( 'click' ); - $( 'body' ).trigger( 'updated_checkout' ); - $('#order_methods, #paypalexpress_order_review').unblock(); - } - - }); - - } - - // Event for updating the checkout - $('body').bind('update_checkout', function() { - clearTimeout(updateTimer); - update_checkout(); - }); - - - - // Used for input change events below - function input_changed() { - var update_totals = true; - - if ( $(dirtyInput).size() ) { - - $required_siblings = $(dirtyInput).closest('.form-row').siblings('.address-field.validate-required'); - - if ( $required_siblings.size() ) { - $required_siblings.each(function(){ - if ( $(this).find('input.input-text').val() == '' || $(this).find('input.input-text').val() == 'undefined' ) { - update_totals = false; - } - }); - } - - } - - if ( update_totals ) { - dirtyInput = false; - $('body').trigger('update_checkout'); - } - } - - $('form.angelleye_checkout') - - /* Update totals/taxes/shipping */ - - // Inputs/selects which update totals instantly - .on( 'input change', 'select.shipping_method, input[name^=shipping_method], #ship-to-different-address input, .update_totals_on_change select', function(){ - clearTimeout( updateTimer ); - dirtyInput = false; - $('body').trigger('update_checkout'); - }) - - // Let themes/plugins override our event handlers - // NOTE: The jQuery .on() function attached to listen to the event below MUST - // be included somewhere on the page itself...so it gets registered before we load - // asyncronous JavaScript resources. Otherwise your function listening might not fire. - $('form.angelleye_checkout').trigger('angelleye_paypal_form_checkout_js_loaded'); -}); diff --git a/paypal-for-woocommerce.php b/paypal-for-woocommerce.php index c72980d68..90fb2e88f 100644 --- a/paypal-for-woocommerce.php +++ b/paypal-for-woocommerce.php @@ -350,8 +350,11 @@ function frontend_scripts() { 'four_digits' => __('4 digits usually found on the front of the card.', 'paypal-for-woocommerce'), 'enable_in_context_checkout_flow' => $enable_in_context_checkout_flow ); - wp_localize_script( 'angelleye_frontend', 'angelleye_frontend', $translation_array ); - wp_enqueue_script('angelleye_frontend'); + + if($enable_in_context_checkout_flow == 'no') { + wp_localize_script( 'angelleye_frontend', 'angelleye_frontend', $translation_array ); + wp_enqueue_script('angelleye_frontend'); + } if ( ! is_admin() && is_cart()){ wp_enqueue_style( 'ppe_cart', plugins_url( 'assets/css/cart.css' , __FILE__ ) );