Skip to content

Commit

Permalink
Apple Pay on PDP causes issue on normal add to cart closes #195
Browse files Browse the repository at this point in the history
  • Loading branch information
roykho committed Mar 22, 2017
1 parent a13d3d9 commit 2c7ec5d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 40 deletions.
103 changes: 68 additions & 35 deletions assets/js/stripe-apple-pay-single.js
Expand Up @@ -26,16 +26,6 @@ jQuery( function( $ ) {
init: function() {
Stripe.applePay.checkAvailability( function( available ) {
if ( available ) {
$( document.body ).on( 'woocommerce_variation_has_changed', function() {
wc_stripe_apple_pay_single.generate_cart();
})

.on( 'change', '.quantity .qty', function() {
wc_stripe_apple_pay_single.generate_cart();
});

wc_stripe_apple_pay_single.generate_cart();

$( '.apple-pay-button' ).show();
}
});
Expand All @@ -61,9 +51,14 @@ jQuery( function( $ ) {
currencyCode: wc_stripe_apple_pay_single_params.currency_code,
total: {
label: wc_stripe_apple_pay_single_params.label,
amount: wc_stripe_apple_pay_single_params.total
amount: 1,
type: 'pending'
},
lineItems: {
label: wc_stripe_apple_pay_single_params.i18n.sub_total,
amount: 1,
type: 'pending'
},
lineItems: wc_stripe_apple_pay_single_params.line_items,
requiredBillingContactFields: ['postalAddress'],
requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_single_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name']
};
Expand Down Expand Up @@ -100,33 +95,40 @@ jQuery( function( $ ) {
if ( 'yes' === wc_stripe_apple_pay_single_params.needs_shipping ) {
// After the shipping contact/address has been selected
applePaySession.onshippingcontactselected = function( shipping ) {
var data = {
'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_get_shipping_methods_nonce,
'address': shipping.shippingContact
};

$.ajax({
type: 'POST',
data: data,
url: wc_stripe_apple_pay_single.getAjaxURL( 'apple_pay_get_shipping_methods' ),
success: function( response ) {
var total = {
'label': wc_stripe_apple_pay_single_params.label,
'amount': response.total
};

if ( 'true' === response.success ) {
applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items );
}

if ( 'false' === response.success ) {
applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items );
$.when( wc_stripe_apple_pay_single.generate_cart() ).then( function() {
var data = {
'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_get_shipping_methods_nonce,
'address': shipping.shippingContact
};

$.ajax({
type: 'POST',
data: data,
url: wc_stripe_apple_pay_single.getAjaxURL( 'apple_pay_get_shipping_methods' ),
success: function( response ) {
var total = {
'label': wc_stripe_apple_pay_single_params.label,
'amount': response.total
};

if ( response.total <= 0 ) {
total.amount = 1;
total.type = 'pending';
}

if ( 'true' === response.success ) {
applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items );
}

if ( 'false' === response.success ) {
applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items );
}
}
}
});
});
};

// After the shipping method has been selected
// After the shipping method has been selected.
applePaySession.onshippingmethodselected = function( event ) {
var data = {
'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_update_shipping_method_nonce,
Expand Down Expand Up @@ -155,6 +157,24 @@ jQuery( function( $ ) {
};
}

// When payment is selected, we need to fetch cart.
applePaySession.onpaymentmethodselected = function( event ) {
$.when( wc_stripe_apple_pay_single.generate_cart() ).then( function() {

var total = {
label: wc_stripe_apple_pay_single_params.label,
amount: wc_stripe_apple_pay_single_params.total
},
lineItems = wc_stripe_apple_pay_single_params.line_items;

applePaySession.completePaymentMethodSelection( total, lineItems );
});
};

applePaySession.oncancel = function( event ) {
wc_stripe_apple_pay_single.clear_cart();
};

applePaySession.begin();
});
},
Expand Down Expand Up @@ -200,6 +220,19 @@ jQuery( function( $ ) {
wc_stripe_apple_pay_single_params.line_items = response.line_items;
}
});
},

clear_cart: function() {
var data = {
'nonce': wc_stripe_apple_pay_single_params.stripe_apple_pay_cart_nonce
};

return $.ajax({
type: 'POST',
data: data,
url: wc_stripe_apple_pay_single.getAjaxURL( 'apple_pay_clear_cart' ),
success: function( response ) {}
});
}
};

Expand Down
2 changes: 1 addition & 1 deletion assets/js/stripe-apple-pay-single.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions includes/class-wc-stripe-apple-pay.php
Expand Up @@ -63,7 +63,7 @@ public function instance() {
*
* @access public
* @since 3.1.0
* @version 3.1.0
* @version 3.1.4
*/
public function init() {
// If Apple Pay is not enabled no need to proceed further.
Expand Down Expand Up @@ -91,6 +91,7 @@ public function init() {
add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 );
add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) );
add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) );
add_action( 'wc_ajax_wc_stripe_apple_pay_clear_cart', array( $this, 'clear_cart' ) );
add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) );
add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) );
add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) );
Expand Down Expand Up @@ -121,7 +122,7 @@ public function filter_gateway_title( $title, $id ) {
* Enqueue JS scripts and styles for single product page.
*
* @since 3.1.0
* @version 3.1.0
* @version 3.1.4
*/
public function single_scripts() {
if ( ! is_single() ) {
Expand Down Expand Up @@ -155,7 +156,10 @@ public function single_scripts() {
'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ),
'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ),
'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ),
'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no',
'needs_shipping' => $product->needs_shipping() ? 'yes' : 'no',
'i18n' => array(
'sub_total' => __( 'Sub-Total', 'woocommerce-gateway-stripe' ),
),
);

wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) );
Expand Down Expand Up @@ -234,7 +238,7 @@ public function display_apple_pay_button() {

$product = wc_get_product( $post->ID );

if ( ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
if ( ! is_object( $product ) || ! in_array( ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) {
return;
}
}
Expand Down Expand Up @@ -350,6 +354,17 @@ public function generate_apple_pay_cart() {
wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) );
}

/**
* Clears Apple Pay cart.
*
* @since 3.1.4
* @version 3.1.4
*/
public function clear_cart() {
WC()->cart->empty_cart();
exit;
}

/**
* Calculate and set shipping method.
*
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Expand Up @@ -98,6 +98,7 @@ If you get stuck, you can ask for help in the Plugin Forum.
= 3.1.4 =
* Tweak - If Apple Pay is not enabled, prevent Apple Pay Init.
* Fix - Update for WooCommerce 3.0 compatibility.
* Fix - Apple Pay on product detail page causes qty issue when using normal add to cart.

= 3.1.3 =
* Fix - When using Stripe Checkout, add payment method was disabled.
Expand Down

0 comments on commit 2c7ec5d

Please sign in to comment.