Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Hide saved payment methods if their gateway is disabled #2975

Merged
merged 7 commits into from Aug 11, 2020

Conversation

Aljullu
Copy link
Contributor

@Aljullu Aljullu commented Aug 6, 2020

Fix #2850.

How to test the changes in this Pull Request:

Steps from #2850.

  1. Set up checkout block, Stripe CC payment method, check Enable Payment via Saved Cards.
  2. Complete a purchase with Stripe test card and check the Save payment information to my account for future purchases. checkbox on checkout.
  3. Go to WooCommerce > Settings > Payments and disable Stripe CC payment method.
  4. Add new stuff to cart, proceed to checkout.
  5. Scroll down and verify saved card (e.g. Visa ending in 4242 (expires 02/22)) is not there.

Also, test there are no regressions with the testing steps introduced in #2934.

Changelog

Saved payment methods are not shown to the user if their payment gateway has been disabled.

@Aljullu Aljullu added status: needs review type: bug The issue/PR concerns a confirmed bug. block: checkout Issues related to the checkout block. labels Aug 6, 2020
@Aljullu Aljullu added this to the 3.2.0 milestone Aug 6, 2020
@Aljullu Aljullu requested a review from a team as a code owner August 6, 2020 03:05
@Aljullu Aljullu self-assigned this Aug 6, 2020
@Aljullu Aljullu requested review from nerrad and removed request for a team August 6, 2020 03:05
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2020

Size Change: +348 B (0%)

Total Size: 1.66 MB

Filename Size Change
build/cart-frontend.js 66.5 kB +82 B (0%)
build/cart.js 34.9 kB +77 B (0%)
build/checkout-frontend.js 83.5 kB +95 B (0%)
build/checkout.js 40.2 kB +94 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters-frontend.js 8.76 kB 0 B
build/active-filters.js 8.8 kB 0 B
build/all-products-frontend.js 31.2 kB 0 B
build/all-products.js 35.6 kB 0 B
build/all-reviews-legacy.js 9.4 kB 0 B
build/all-reviews.js 9.73 kB 0 B
build/atomic-block-components/add-to-cart-frontend.js 8.88 kB 0 B
build/atomic-block-components/add-to-cart.js 7.46 kB 0 B
build/atomic-block-components/add-to-cart~atomic-block-components/button.js 3.17 kB 0 B
build/atomic-block-components/add-to-cart~atomic-block-components/image~atomic-block-components/title.js 335 B 0 B
build/atomic-block-components/button-frontend.js 2.02 kB 0 B
build/atomic-block-components/button.js 838 B 0 B
build/atomic-block-components/category-list-frontend.js 468 B 0 B
build/atomic-block-components/category-list.js 476 B 0 B
build/atomic-block-components/image-frontend.js 1.71 kB 0 B
build/atomic-block-components/image.js 1.15 kB 0 B
build/atomic-block-components/price-frontend.js 2.08 kB 0 B
build/atomic-block-components/price.js 2.11 kB 0 B
build/atomic-block-components/rating-frontend.js 523 B 0 B
build/atomic-block-components/rating.js 527 B 0 B
build/atomic-block-components/sale-badge-frontend.js 862 B 0 B
build/atomic-block-components/sale-badge.js 864 B 0 B
build/atomic-block-components/sku-frontend.js 389 B 0 B
build/atomic-block-components/sku.js 394 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 568 B 0 B
build/atomic-block-components/stock-indicator.js 571 B 0 B
build/atomic-block-components/summary-frontend.js 918 B 0 B
build/atomic-block-components/summary.js 926 B 0 B
build/atomic-block-components/tag-list-frontend.js 465 B 0 B
build/atomic-block-components/tag-list.js 473 B 0 B
build/atomic-block-components/title-frontend.js 1.22 kB 0 B
build/atomic-block-components/title.js 1.06 kB 0 B
build/attribute-filter-frontend.js 18.1 kB 0 B
build/attribute-filter.js 12.4 kB 0 B
build/blocks-legacy.js 3.54 kB 0 B
build/blocks.js 3.54 kB 0 B
build/editor-legacy-rtl.css 13.8 kB 0 B
build/editor-legacy.css 13.8 kB 0 B
build/editor-rtl.css 14 kB 0 B
build/editor.css 14 kB 0 B
build/featured-category-legacy.js 7.29 kB 0 B
build/featured-category.js 7.67 kB 0 B
build/featured-product-legacy.js 9.55 kB 0 B
build/featured-product.js 9.92 kB 0 B
build/handpicked-products-legacy.js 6.95 kB 0 B
build/handpicked-products.js 7.32 kB 0 B
build/price-filter-frontend.js 14.4 kB 0 B
build/price-filter.js 10.2 kB 0 B
build/product-best-sellers-legacy.js 7.03 kB 0 B
build/product-best-sellers.js 7.39 kB 0 B
build/product-categories-legacy.js 3.23 kB 0 B
build/product-categories.js 3.23 kB 0 B
build/product-category-legacy.js 7.94 kB 0 B
build/product-category.js 8.34 kB 0 B
build/product-new-legacy.js 7.19 kB 0 B
build/product-new.js 7.56 kB 0 B
build/product-on-sale-legacy.js 7.56 kB 0 B
build/product-on-sale.js 7.95 kB 0 B
build/product-search-legacy.js 3.17 kB 0 B
build/product-search.js 3.51 kB 0 B
build/product-tag-legacy.js 6.12 kB 0 B
build/product-tag.js 6.47 kB 0 B
build/product-top-rated-legacy.js 7.16 kB 0 B
build/product-top-rated.js 7.53 kB 0 B
build/products-by-attribute-legacy.js 7.92 kB 0 B
build/products-by-attribute.js 8.26 kB 0 B
build/reviews-by-category-legacy.js 11.4 kB 0 B
build/reviews-by-category.js 11.8 kB 0 B
build/reviews-by-product-legacy.js 12.9 kB 0 B
build/reviews-by-product.js 13.3 kB 0 B
build/reviews-frontend-legacy.js 8.43 kB 0 B
build/reviews-frontend.js 9.33 kB 0 B
build/single-product-frontend.js 34 kB 0 B
build/single-product.js 10.1 kB 0 B
build/style-legacy-rtl.css 16.8 kB 0 B
build/style-legacy.css 16.8 kB 0 B
build/style-rtl.css 17.5 kB 0 B
build/style.css 17.5 kB 0 B
build/vendors-legacy.js 367 kB 0 B
build/vendors-style-legacy-rtl.css 1.03 kB 0 B
build/vendors-style-legacy.css 1.03 kB 0 B
build/vendors-style-rtl.css 1.03 kB 0 B
build/vendors-style.css 1.03 kB 0 B
build/vendors.js 416 kB 0 B
build/vendors~atomic-block-components/price-frontend.js 5.65 kB 0 B
build/wc-blocks-data.js 7.09 kB 0 B
build/wc-blocks-middleware.js 931 B 0 B
build/wc-blocks-registry.js 2.28 kB 0 B
build/wc-payment-method-bacs.js 790 B 0 B
build/wc-payment-method-cheque.js 796 B 0 B
build/wc-payment-method-cod.js 875 B 0 B
build/wc-payment-method-paypal.js 831 B 0 B
build/wc-payment-method-stripe.js 11.9 kB 0 B
build/wc-settings.js 2.14 kB 0 B
build/wc-shared-context.js 1.53 kB 0 B
build/wc-shared-hocs.js 1.66 kB 0 B

compressed-size-action

@Aljullu Aljullu force-pushed the fix/2850-hide-saved-card-if-stripe-disabled branch from e8cdb66 to 9bf9b36 Compare August 6, 2020 16:31
Copy link
Contributor

@nerrad nerrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this Albert! I don't think we can use it as is though for the reasons outlined in my feedback.

@@ -8,6 +8,7 @@ import {
usePaymentMethodDataContext,
} from '@woocommerce/base-context';
import RadioControl from '@woocommerce/base-components/radio-control';
import { PAYMENT_GATEWAY_SORT_ORDER } from '@woocommerce/block-settings';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SavedPaymentMethodOptions is already consuming the Payment Method data context via usePaymentMethodDataContext. So you can grab the paymentMethods and paymentMethodsInitialized properties from there and use those to match against whether saved payment methods should display or not.

I even wonder if this should be handled upstream in the payment method data context and filter the available customerPaymentMethod by what is actually enabled in the paymentMethods object (which could be done via a effect in the context provider). That way this logic wouldn't need to exist in this component.

PAYMENT_GATEWAY_SORT_ORDER creates a coupling to server provided values that I think we should try to limit as much as possible in our components so down the road if this data is provided over REST or a transport other than via a WordPress host there's less refactoring involved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 good idea. Should be fixed now.

Comment on lines +79 to 98
/**
* Returns true if the payment gateway is enabled.
*
* @param object $gateway Payment gateway.
* @return boolean
*/
private function is_payment_gateway_enabled( $gateway ) {
return filter_var( $gateway->enabled, FILTER_VALIDATE_BOOLEAN );
}

/**
* Add payment method data to Asset Registry.
*/
public function add_payment_method_script_data() {
// Enqueue the order of enabled gateways as `paymentGatewaySortOrder`.
if ( ! $this->asset_registry->exists( 'paymentGatewaySortOrder' ) ) {
$available_gateways = WC()->payment_gateways->payment_gateways();
$this->asset_registry->add( 'paymentGatewaySortOrder', array_keys( $available_gateways ) );
$payment_gateways = WC()->payment_gateways->payment_gateways();
$enabled_gateways = array_filter( $payment_gateways, array( $this, 'is_payment_gateway_enabled' ) );
$this->asset_registry->add( 'paymentGatewaySortOrder', array_keys( $enabled_gateways ) );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break the logic that was implemented as a result of this comment?

If you implement the changes I suggested in an earlier comment, there won't be need for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really: enabled and available are different concepts. For example, COD might be enabled but not available depending on the shipping method.

While this code is no longer needed for this PR, what do you think about keeping it? While I see you discussed that was not an information leak, at the same time I don't see any benefit on exposing disabled payment methods in the frontend, so I think we could keep the filter. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, COD might be enabled but not available depending on the shipping method.

Ahh, right that makes sense. I'm fine with keeping this but you should make sure testing steps for the work done in #2157 are done to cover any unexpected regressions (don't expect any but still...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider testing steps in #2934 already cover any potential regression introduced in this PR. I updated the testing steps in this PR to also point to testing steps in #2934. Do you think we need to add anything else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, if you're happy with that, it works for me!

@Aljullu Aljullu force-pushed the fix/2850-hide-saved-card-if-stripe-disabled branch from 40db3ee to a6281d1 Compare August 10, 2020 13:48
@Aljullu
Copy link
Contributor Author

Aljullu commented Aug 10, 2020

Thanks for the review @nerrad. This is ready for another look. I see e2e tests are failing but I think it's not related to this pull.

@Aljullu Aljullu requested a review from nerrad August 10, 2020 14:18
Copy link
Contributor

@nerrad nerrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: checkout Issues related to the checkout block. type: bug The issue/PR concerns a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Checkout block shows saved Stripe card when Stripe payment method is disabled
3 participants