If this is your first visit, check our documentation. To post, register or login.
Forums / Code Matters / Bug Reports / [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

[Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Locked

Started by lat9 on 17 Oct 2010, 8:51 PM · Views: 40,440

Results 1 to 20 of 25
This thread is locked. New replies are disabled.
17 Oct 2010, 8:51 PM
#1
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

[Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Fresh install of v1.3.9f. Enabled PayPal Express Payment module with settings for my sandbox account and

Express Checkout: Require Confirmed Address:  No 
Express Checkout: Select Cheapest Shipping Automatically: Yes
Express Checkout: Skip Payment Page: Yes
Express Checkout: Automatic Account Creation:  No

This is important to me because the majority of my products are (via attributes) either downloadable (virtual) or shippable (physical). If the customer clicks "Add to Cart" and doesn't realize until later in the checkout process that they've chosen the wrong 'flavor', it's not until the checkout_confirmation page that they can change it.

Steps to reproduce the problem:

  1. Choose "Mixed Product Types" category. Add 1 "Sample of Product Free Shipping Type" to cart.

  2. From shopping_cart page, click the PayPal Express Checkout button.

  3. On the PayPal website, login to your test account, choose payment type, press Continue.

  4. When ZC checkout_confirmation page is displayed, click the Edit button to edit the shopping cart contents; from the shopping_cart page, delete the free-shipping product.

  5. Add a physical product to your cart (like DVD - A Bug's Life Linked. From the shopping cart page, click (again) the PayPal button. Login (again) to your PayPal account, verify the payment information and click "continue".

  6. You're taken to checkout_success (bypassing checkout_confirmation) and when viewing the order details via "My Account" it shows that no shipping was charged (still free shipping), although the physical product does require a shipping charge.

I've got the PayPal and paypalwpp logs available if needed.

18 Oct 2010, 8:15 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

It's not changing the cart from virtual to physical, it's changing the cart from "free shipping" to "not free shipping" (or vice versa, I'm guessing). **Note **that in my first post, step 5, the button from PayPal (after logging in) is "Pay Now" instead of "Continue".

The first pass through EC (with the free-shipping product) sets $_SESSION['shipping']; coming back the second pass (with a shipping-required product), the variable is already set, so paypalwpp.php's setShippingMethod function doesn't mess with it ... resulting in free shipping for a product that otherwise would have a shipping cost. During the second pass, the $_SESSION['paypal_ec_token'] is also set, so that ipn_main_handler.php starts off at ec_step2().

It feels like the paypal payment process needs to be notified of cart content changes so that the shipping costs (and possibly taxes) can be appropriately adjusted.

Any enlightenment on this would be truly appreciated as I slog through this code!

19 Oct 2010, 1:16 AM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

As a quick fix, if you're using v1.3.9g there's a new option in your EC module settings to allow you to turn off the EC Shortcut button on the Shopping Cart page, which then causes your customer to go through checkout normally (where they can of course select PayPal from your list of presented payment choices).

Doing that will save you the challenges of "slogging through code", as you call it.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

19 Oct 2010, 3:39 AM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Thanks, DrByte. I'll give the 1.3.9g implementation a look, but I thought that it was a PayPal requirement that the EC button appear on the checkout page.

Are there plans to correct the underlying problem?

19 Oct 2010, 3:41 AM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

We've had extensive discussions with PayPal over the last couple years trying to convince them to allow the removal of the shortcut button from the Shopping Cart page. They've finally conceded, and thus the option was added to v1.3.9g.
The button must be enabled by default, but if you the merchant wish to disable it, you are no longer prohibited from having the option to make that choice yourself.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

19 Oct 2010, 3:45 AM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

NOTE: REVISED HERE: http://www.zen-cart.com/forum/showthread.php?p=949910#post949910

As to the underlying problem, try the following:

In ipn_main_handler.php find the line that says:```
require(DIR_WS_CLASSES . 'payment.php');

and add the following ABOVE it:

if (!defined('TEXT_RESELECT_SHIPPING')) define('TEXT_RESELECT_SHIPPING', 'You have changed your order. Please re-select your shipping preference.');
// if cart contents has changed since last pass, reset
if (isset($_SESSION['cart']->cartID) && $_SESSION['cartID']) {
if ($_SESSION['cart']->cartID != $_SESSION['cartID']) {
if (isset($_SESSION['shipping'])) {
unset($_SESSION['shipping']);
$messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
}
}
}

// if shipping has been selected as free but the order does not qualify for free shipping, then reset
$selectedShipping = (isset($_SESSION['shipping'])) ? $_SESSION['shipping'] : '';
if (is_array($selectedShipping) && isset($selectedShipping['id'])) $selectedShipping = $selectedShipping['id'];
if (is_array($selectedShipping) && isset($selectedShipping['title'])) $selectedShipping = $selectedShipping['title'];
if ( ($selectedShipping == 'free_free' && (defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER') && $_SESSION['cart']->show_total() < MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER))
|| (substr($selectedShipping, 0, 4) == 'free' && isset($_SESSION['cart']) && $_SESSION['cart']->get_content_type != 'virtual') ) {
unset($_SESSION['shipping']);
$messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
}

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

19 Oct 2010, 4:22 AM
#7
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Thanks, DrByte! That works like a charm.

19 Oct 2010, 2:14 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

My apologies, but my testing last night of DrByte's fix for (http://www.zen-cart.com/forum/showthread.php?t=166215) was necessary but insufficient. The fix works great when a PPEC cart starts out with only free-shipping items and the customer changes the cart to contain NON free-shipping items.

Unfortunately, when the PPEC cart starts out with a NON free-shipping item and the cart is changed to have only free-shipping items, the original cart's shipping charge is applied to the free-shipping item.

To reproduce (ZC 1.3.9f with the above fix, demo products):

  1. Add product "Test 2 - $20" to cart; click PPEC button, log into PayPal account, press 'Continue' from PayPal, returning to checkout_payment.

  2. Click Continue Checkout to checkout_confirmation, click Edit for the shipping cart; from shopping_cart page delete the item.

  3. Add "New for v1.2->Downloads->Single Download" to cart. Press PPEC button from shopping cart and log into PayPal account.

You see that the $39.00 download is accompanied by a $2.50 shipping charge.

19 Oct 2010, 5:23 PM
#9
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

OK, I'm getting close to a solution. Here's my current modification of DrByte's change:

  if (!defined('TEXT_RESELECT_SHIPPING')) define('TEXT_RESELECT_SHIPPING', 'You have changed your order. Please re-select your shipping preference.');
  // if cart contents has changed since last pass, reset
  if (isset($_SESSION['cart']->cartID) && $_SESSION['cartID']) {
    if ($_SESSION['cart']->cartID != $_SESSION['cartID']) {
      if (isset($_SESSION['shipping'])) {
        unset($_SESSION['shipping']);
        $messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
        zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
      }
    }
  }  

  // if shipping has been selected as free but the order does not qualify for free shipping, then reset
  $selectedShipping = (isset($_SESSION['shipping'])) ? $_SESSION['shipping'] : '';
  $cartContentType = (isset($_SESSION['cart'])) ? $_SESSION['cart']->get_content_type() : '';
  if (is_array($selectedShipping) && isset($selectedShipping['id'])) $selectedShipping = $selectedShipping['id'];
  if (is_array($selectedShipping) && isset($selectedShipping['title'])) $selectedShipping = $selectedShipping['title'];
  
  if ( $cartContentType != 'virtual' && ( ($selectedShipping == 'free_free' && (defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER') && $_SESSION['cart']->show_total() < MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER))
       || (substr($selectedShipping, 0, 4) == 'free') ) ) {
    unset($_SESSION['shipping']);
    $messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
    zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  }
  
  // if the selected shipping is not 'free_free' (or unselected) and the cart is virtual, then reset
  $virtualNotFreeShipping = false;
  if ($cartContentType == 'virtual' && $selectedShipping != 'free_free' && $selectedShipping != '') {
    unset($_SESSION['shipping']);
    $virtualNotFreeShipping = true;
  }

  require(DIR_WS_CLASSES . 'payment.php');
  // See if we were sent a request to clear the session for PayPal.
  if (isset($_GET['clearSess']) || isset($_GET['amp;clearSess']) || isset($_GET['ec_cancel']) || isset($_GET['amp;ec_cancel']) || $virtualNotFreeShipping) {
    // Unset the PayPal EC information.
    unset($_SESSION['paypal_ec_temp']);
    unset($_SESSION['paypal_ec_token']);
    unset($_SESSION['paypal_ec_payer_id']);
    unset($_SESSION['paypal_ec_payer_info']);
  }
  //- eof bug fix

The stuff in green I've checked out pretty thoroughly; it covers the condition where the cart was changed from physical/mixed to virtual and results in the free_free shipping type being applied to the virtual order.

The stuff in red I'm still not sure about.

  1. I modified the first clause of the 'big' if statement so that it applies only if the cart contents are not virtual; otherwise a virtual cart with value less than the free-shipping limit winds up in an infinite checkout_payment->checkout_confirmation->checkout_payment loop due to ipn_main_handler.php's redirection to checkout_shipping. This change appears to work.

  2. I think that more processing needs to be applied to this clause, so that it mimics the ot_shipping.php's processing relative to the free-shipping limit, including testing of MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING == 'true' and whether MODULE_ORDER_TOTAL_SHIPPING_DESTINATION applies based on the current destination.

Also, the physical product now in the cart could be defined as 'always free shipping', which would also result in a checkout-related loop here.

I'm continuing to find the full solution ...

19 Oct 2010, 5:28 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

In reality, the earlier part about the CartID should be the part that actually catches all changes in cart contents.

We could copy all the code from all the shipping and order total and checkout modules into the ipn handler ... but that's dumb. Keeping it lean is prudent. Duplicating code needlessly is bad.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

19 Oct 2010, 7:43 PM
#11
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

DrByte:

In reality, the earlier part about the CartID should be the part that actually catches all changes in cart contents.

Agreed, but it doesn't appear to!

DrByte:

We could copy all the code from all the shipping and order total and checkout modules into the ipn handler ... but that's dumb. Keeping it lean is prudent. Duplicating code needlessly is bad.

I was not suggesting that code be copied, but noting that there are additional conditions where shipping changes that also need to be accounted for.

19 Oct 2010, 8:26 PM
#12
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Here's my latest update that appears to work when the cart goes from free-shipping to shipping and vice-versa. Debug via notifier information is currently included. We're still got the mysterious bug where sometimes free shipping comes up as 'free_free' and sometimes as 'f', so I've included that test in this modules processing.

Tested:

  1. Physical (with shipping) -> Virtual (free shipping)
  2. Virtual (free shipping) -> Physical (with shipping)

Todo:

  1. Enable per-item shipping and see what transpires.
  2. Verify tested scenarios with a product that has 'always free shipping' set
//- bof bug fix lat9 from DrByte
  if (!defined('TEXT_RESELECT_SHIPPING')) define('TEXT_RESELECT_SHIPPING', 'You have changed your order. Please re-select your shipping preference.');
  // if cart contents has changed since last pass, reset
  if (isset($_SESSION['cart']->cartID) && $_SESSION['cartID']) {
    if ($_SESSION['cart']->cartID != $_SESSION['cartID']) {
      if (isset($_SESSION['shipping'])) {
        unset($_SESSION['shipping']);
        $zco_notifier->notify('IPN_MAIN_HANDLER_REDIRECT_SHIPPING_1', $_session['cart']->cartID . '/' . $_SESSION['cartID']);
        $messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
        zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
      }
    }
  }  

  // if shipping has been selected as free but the order does not qualify for free shipping, then reset
  $selectedShipping = (isset($_SESSION['shipping'])) ? $_SESSION['shipping'] : '';
  $selectedShippingCost = '';
  $cartContentType = (isset($_SESSION['cart'])) ? $_SESSION['cart']->get_content_type() : '';
  if (is_array($selectedShipping)) {
    if (isset($selectedShipping['cost'])) $selectedShippingCost = $selectedShipping['cost'];
    if (isset($selectedShipping['id'])) $selectedShipping = $selectedShipping['id'];
    if (isset($selectedShipping['title'])) $selectedShipping = $selectedShipping['title'];
  }

  $cartIsAllFreeShipping = (isset($_SESSION['cart']) && $_SESSION['cart']->count_contents() == $_SESSION['cart']->free_shipping_items()) ? true : false;
  
  $zco_notifier->notify('IPN_MAIN_HANDLER_CHECK_SHIPPING', $selectedShipping . '/' . $cartContentType . '/' . $_SESSION['cart']->show_total() . '/' . $selectedShippingCost . '/' . $cartIsAllFreeShipping);
  
  if ($cartContentType != 'virtual' && ($selectedShipping == 'free_free' || $selectedShipping == 'f') && !$cartIsAllFreeShipping) {
    unset($_SESSION['shipping']);
    $zco_notifier->notify('IPN_MAIN_HANDLER_REDIRECT_SHIPPING_2');
    $messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
    zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  }
  
  // if the cart contains only free-shipping items and the shipping cost is not 0, reset shipping
  $shouldBeFreeShipping = false;
  if ($cartIsAllFreeShipping && $selectedShippingCost != '' && $selectedShippingCost != 0.0) {
    unset($_SESSION['shipping']);
    $shouldBeFreeShipping = true;
  }

  require(DIR_WS_CLASSES . 'payment.php');
  // See if we were sent a request to clear the session for PayPal.
  if (isset($_GET['clearSess']) || isset($_GET['amp;clearSess']) || isset($_GET['ec_cancel']) || isset($_GET['amp;ec_cancel']) || $shouldBeFreeShipping) {
    // Unset the PayPal EC information.
    unset($_SESSION['paypal_ec_temp']);
    unset($_SESSION['paypal_ec_token']);
    unset($_SESSION['paypal_ec_payer_id']);
    unset($_SESSION['paypal_ec_payer_info']);
  }
  //- eof bug fix
20 Oct 2010, 5:04 AM
#13
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

OK, I think I've got it!

I took DrByte's observation to heart: "In reality, the earlier part about the CartID should be the part that actually catches all changes in cart contents."

The way I understand $_SESSION['cartID'], it should be the means through which the shopping_cart class and the checkout_shipping page act together to identify whether $_SESSION['shipping'] reflects the current $_SESSION['cart'] contents. With that assumption,

  1. The shopping_cart class should always unset($_SESSION['cartID']) when the shopping cart contents are changed.

  2. The checkout_shipping page should set $_SESSION['cartID'] to $_SESSION['cart']->cartID when it has set $_SESSION['shipping'] based on the current cart's contents.

Looking through these two modules, I found:

  1. The current (1.3.9f) shopping_cart class only manipulates $_SESSION['cartID'] in its reset() function, setting the value to ''.

  2. The current /includes/modules/pages/checkout_shipping/header_php.php unconditionally sets $_SESSION['cartID'] to $_SESSION['cart']->cartID if it doesn't redirect to the time_out, login, or shopping_cart pages.


My solution to the problem involves minor edits to these two modules as well as /ipn_main_handler.php:

  1. /includes/classes/shopping_cart.php

--> towards the bottom of function restore_contents

    unset($this->cartID);
 //   $_SESSION['cartID'] = '';
    unset($_SESSION['cartID']); 
    $this->notify('NOTIFIER_CART_RESET_END');

--> towards the bottom of function update_quantity

    // although we're not regenerating the cart ID, clear the session variable so PayPal EC knows the contents have changed
    unset ($_SESSION['cartID']); 
    $this->notify('NOTIFIER_CART_UPDATE_QUANTITY_END');

--> and function generate_cart_id (which covers cart add and remove requests)

  function generate_cart_id($length = 5) {
    // reset the session-based cart ID, to let PayPal EC know that the contents have changed
    unset($_SESSION['cartID']);  
    return zen_create_random_value($length, 'digits');
  }
  1. /includes/modules/pages/checkout_shipping/header_php.php

--> Code fragment starting at line 70:

  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;

// register a random ID in the session to check throughout the checkout procedure
// against alterations in the shopping cart contents
//  $_SESSION['cartID'] = $_SESSION['cart']->cartID;

// if the order contains only virtual products, forward the customer to the billing page as
// a shipping address is not needed
  if ($order->content_type == 'virtual') {
    $_SESSION['shipping'] = 'free_free';
    $_SESSION['shipping']['title'] = 'free_free';
    $_SESSION['sendto'] = false;
    // register a random ID in the session to check throughout the checkout procedure
    // against alterations in the shopping cart contents
    $_SESSION['cartID'] = $_SESSION['cart']->cartID; 
    zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
  }

--> towards the bottom of the file:

    if ( (zen_count_shipping_modules() > 0) || ($free_shipping == true) ) {
      if ( (isset($_POST['shipping'])) && (strpos($_POST['shipping'], '_')) ) {
        /**
         * check to be sure submitted data hasn't been tampered with
         */
        if ($_POST['shipping'] == 'free_free' && ($order->content_type != 'virtual' && !$pass)) {
          $quote['error'] = 'Invalid input. Please make another selection.';
        } else {
          $_SESSION['shipping'] = $_POST['shipping'];
        }

        list($module, $method) = explode('_', $_SESSION['shipping']);
        if ( is_object($$module) || ($_SESSION['shipping'] == 'free_free') ) {
          if ($_SESSION['shipping'] == 'free_free') {
            $quote[0]['methods'][0]['title'] = FREE_SHIPPING_TITLE;
            $quote[0]['methods'][0]['cost'] = '0';
          } else {
            $quote = $shipping_modules->quote($method, $module);
          }
          if (isset($quote['error'])) {
            $_SESSION['shipping'] = '';
          } else {
            if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) {
              $_SESSION['shipping'] = array('id' => $_SESSION['shipping'],
                                'title' => (($free_shipping == true) ?  $quote[0]['methods'][0]['title'] : $quote[0]['module'] . ' (' . $quote[0]['methods'][0]['title'] . ')'),
                                'cost' => $quote[0]['methods'][0]['cost']);
              // register a random ID in the session to check throughout the checkout procedure
              // against alterations in the shopping cart contents
              $_SESSION['cartID'] = $_SESSION['cart']->cartID; 
              zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
            }
          }
        } else {
          $_SESSION['shipping'] = false;
        }
      }
    } else {
      $_SESSION['shipping'] = false;
      // register a random ID in the session to check throughout the checkout procedure
      // against alterations in the shopping cart contents
      $_SESSION['cartID'] = $_SESSION['cart']->cartID;
      zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
    }
  1. /ipn_main_handler.php, towards the top of the file:
//- bof bug fix
  if (!defined('TEXT_RESELECT_SHIPPING')) define('TEXT_RESELECT_SHIPPING', 'You have changed your order. Please re-select your shipping preference.');

  // if cart contents has changed since last pass, reset
  if (isset($_SESSION['cart']->cartID)) {
    if (!isset($_SESSION['cartID']) || $_SESSION['cart']->cartID != $_SESSION['cartID']) {
      if (isset($_SESSION['shipping'])) {
        unset($_SESSION['shipping']);
        $messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
        zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
      }
    }
  }  
//- eof bug fix
21 Oct 2010, 1:24 AM
#14
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

I'm not sure I agree with unsetting the $_SESSION['cartID'] all over the place, and I don't agree with making changes that are only focused around relevance to PayPal EC. There's a lot more involved than just fixing this for your specific store requirements.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

21 Oct 2010, 6:50 AM
#15
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

I think I'd approach it like this instead:

checkout_shipping/header_php.php:
Replace the one-line $_SESSION['cartID'] = $_SESSION['cart']->cartID;with:```
if (isset($_SESSION['cart']->cartID)) {
if (!isset($_SESSION['cartID']) || $_SESSION['cart']->cartID != $_SESSION['cartID']) {
$_SESSION['cartID'] = $_SESSION['cart']->cartID;
}
} else {
zen_redirect(zen_href_link(FILENAME_TIME_OUT));
}



With /ipn_main_handler.php, use this instead:

if (!defined('TEXT_RESELECT_SHIPPING')) define('TEXT_RESELECT_SHIPPING', 'You have changed your order. Please re-select your shipping preference.');
// if cart contents has changed since last pass, reset
if (isset($_SESSION['cart']->cartID)) {
if (isset($_SESSION['cartID'])) { // This will only be set if customer has been to the checkout_shipping page. Will not be set if starting via EC Shortcut button, so don't want to redirect in that case.
if ($_SESSION['cart']->cartID != $_SESSION['cartID']) {
if (isset($_SESSION['shipping'])) {
unset($_SESSION['shipping']);
$messageStack->add_session('checkout_shipping', TEXT_RESELECT_SHIPPING, 'error');
zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
}
}
}
// } else {
// zen_redirect(zen_href_link(FILENAME_TIME_OUT));
}



And shopping_cart.php class:
add     ```
$this->cartID = $this->generate_cart_id();

on line 180 above: $this->notify('NOTIFIER_CART_RESTORE_CONTENTS_END');
and
on line 425 above: $this->notify('NOTIFIER_CART_UPDATE_QUANTITY_END');

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

21 Oct 2010, 12:29 PM
#16
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Thanks, DrByte. I'll give your solution a go.

Were my assumptions on the 'meaning' of $_SESSION['cartID'] incorrect, i.e. if it's equal to the current shopping cart's is that the shipping reflects the current cart?

21 Oct 2010, 1:53 PM
#17
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

Ok, gave it a go ... but it's a no-go. All 'shopping' is done as a guest, i.e. not logged in when the initial product is added to the cart.

  1. Add a physical (shipping required) product to the cart. Press PPEC button, sign into PayPal, press Continue. Back in ZC, press 'Continue Checkout' to checkout_confirmation; click Edit to edit the shopping cart contents. Remove physical item, add Single Download and press PPEC button from shopping_cart page.

Sign into PayPal, shipping from previous item still shows. PayPal button shows Confirm instead of Continue.

  1. Add a Single Download to the cart, press PPEC button from shopping_cart page, sign into PayPal, press Continue. Back in ZC, press Continue Checkout to checkout_confirmation, click Edit to edit the shopping cart contents. Remove download, add physical (shipping required) item and press PPEC button from shopping_cart page.

Sign into PayPal, no shipping is applied. PayPal button shows Confirm instead of Continue.

21 Oct 2010, 3:00 PM
#18
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

I think that part of the problem with the most recent code is that the following statements in header_php.php of checkout_payment and checkout_confirmation never 'fire'

// avoid hack attempts during the checkout procedure by checking the internal cartID
if (isset($_SESSION['cart']->cartID) && $_SESSION['cartID']) {
  if ($_SESSION['cart']->cartID != $_SESSION['cartID']) {
    zen_redirect(zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  }
}

I'm not familiar with the construct if ($_SESSION['cartID'])), but I'm guessing that it's the reason that the if statement does not fire since the variable is not yet set.

21 Oct 2010, 3:12 PM
#19
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

lat9:

Were my assumptions on the 'meaning' of $_SESSION['cartID'] incorrect, i.e. if it's equal to the current shopping cart's is that the shipping reflects the current cart?It's not necessarily focused on shipping. It simply indicates whether the cart has been changed ... which could mean a need to recalculate shipping, or anything else like taxes and discounts. It simply goes back to checkout_shipping because that's the beginning of checkout steps where all those validations take place.

lat9:

I think that part of the problem with the most recent code is that the following statements in header_php.php of checkout_payment and checkout_confirmation never 'fire'
Those don't "fire" if you don't visit those pages.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

21 Oct 2010, 4:17 PM
#20
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: [Done v1.3.9h]PayPal EC Shipping not changed if cart changed from virtual to physical

OK, understood (and thank you for setting me straight)!

By undoing the change on line 221 of the shipping_cart.php module

    unset($_SESSION['cartID']);

and reverting back to

$_SESSION['cartID'] = '';

the process works in all (hopefully!) conditions:

  1. Cart changed from product with shipping required to product with shipping free.
  2. Cart changed from product with free shipping to product with shipping required.
  3. Using per_item shipping, change the quantity of a single product (shipping required) in the cart and get shipping charge recalculated.
  4. Enabling ot_shipping's free-shipping-over-this-amount, change the cart from a total less than the shipping-over amount to a total greater than the amount.

Are there other conditions that require checking?