Skip to content

Commit

Permalink
Express Checkout - In Context Checkout Integration, ref #199
Browse files Browse the repository at this point in the history
  • Loading branch information
kcppdevelopers committed Jul 21, 2017
1 parent d122ac1 commit 41b278b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function buy_now_button() {
break;
}
if ($this->show_paypal_credit == 'yes') {
$paypal_credit_button_markup = '<a class="paypal_checkout_button paypal_checkout_button_cc" href="' . esc_url(add_query_arg('use_paypal_credit', 'true', add_query_arg('pp_action', 'set_express_checkout', add_query_arg('wc-api', 'WC_Gateway_PayPal_Express_AngellEYE', home_url('/'))))) . '" >';
$paypal_credit_button_markup = '<a class="single_add_to_cart_button paypal_checkout_button paypal_checkout_button_cc" href="' . esc_url(add_query_arg('use_paypal_credit', 'true', add_query_arg('pp_action', 'set_express_checkout', add_query_arg('wc-api', 'WC_Gateway_PayPal_Express_AngellEYE', home_url('/'))))) . '" >';
$paypal_credit_button_markup .= '<img src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/ppcredit-logo-small.png" width="148" height="26" class="ppcreditlogo ec_checkout_page_button_type_pc" align="top" alt="' . __('Check out with PayPal Credit', 'paypal-for-woocommerce') . '" />';
$paypal_credit_button_markup .= '</a>';
$ec_html_button .= $paypal_credit_button_markup;
Expand Down Expand Up @@ -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',
Expand All @@ -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) {

}
Expand Down Expand Up @@ -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) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion assets/css/single.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
52 changes: 0 additions & 52 deletions assets/js/angelleye-express-checkout-custom.js

This file was deleted.

2 changes: 1 addition & 1 deletion assets/js/angelleye-express-checkout.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
61 changes: 32 additions & 29 deletions assets/js/angelleye-frontend.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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';
Expand All @@ -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');
});
62 changes: 30 additions & 32 deletions assets/js/angelleye-in-context-checkout.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -13,44 +15,33 @@
{
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 {

window.paypalCheckoutReady = function () {
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: {
Expand Down Expand Up @@ -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({
Expand All @@ -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;
}
}
});
}

Expand Down
Loading

0 comments on commit 41b278b

Please sign in to comment.