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

Conversation

@haszari
Copy link
Contributor

@haszari haszari commented Nov 15, 2020

Fixes #3064

This PR updates the two APIs for registering payment methods so they are simpler. They now both receive an options JS object. Previously was a callback which exposed internal details of how payment methods are handled.

The new interfaces are:

  • registerPaymentMethod( options )
  • registerExpressPaymentMethod( options )

The existing callback API is still supported if a function is passed.

All payment methods in this code base have been updated to the new simpler API. Docs have also been updated.

This PR also includes JSDoc typedefs for the options objects. This should help us catch any payment methods passing incorrect info, and when we support new fields to options.

Deprecating the old API - help!

In the issue it's suggested that we deprecate the old callback-style API. I don't know how we should do this – how do we deprecate JS APIs?

Perhaps we can add a warn_deprecated_api() routine that logs to console, possibly with an extra conditional (e.g. SCRIPT_DEBUG ?), and call this in the deprecated/legacy code path.

Update: using @wordpress/deprecated.

API docs

I've manually updated the markdown doc for now. Long term it might be a good idea to start documenting our JS APIs using code docs, for example using JSDoc or some other tech. Then we can use this to automatically generate consistently formatted API docs, and also keep track of changes over different releases.

@nerrad - keen for your thoughts on this. If there's interest I'm happy to take a look in the next cooldown.

I haven't looked to see what other projects are doing (e.g. Gutenberg, WooCommerce, wc-admin, Jetpack), maybe there's a convention we can adopt.

Update: using a JSDoc typedef now.

How to test the changes in this Pull Request:

  1. Check out this branch and build.
  2. Test all payment methods work correctly - e.g. complete an order with each payment method.

Note I haven't tested the following payment methods myself, would appreciate extra testing of the following. Please comment in your review which payment methods you tested – thanks!

  • Stripe Payment Request - Chrome/Google Pay
  • Stripe Payment Request - Apple Pay
  • PayPal

Bonus points:

  • Test a 3rd-party payment method from another extension (e.g. WC Pay) - using new or old API.
  • Hack a payment method in blocks code base to use callback-style registration and ensure fallback works correctly.

Changelog

Dev: Payment method registration APIs now receive a plain JavaScript object argument (previously expected a callback function).

Developer Note

Payment method registration APIs receive JS options arg (was callback)

In WooCommerce Blocks {version} the APIs for registering a payment method have changed.

The affected JavaScript functions are:

  • registerPaymentMethod( options )
  • registerExpressPaymentMethod( options )

Previously these APIs received a callback function, for example:

const bankTransferPaymentMethod = {
	name: PAYMENT_METHOD_NAME,
	label: <Label />,
	content: <Content />,
	edit: <Content />,
	icons: null,
	canMakePayment: () => true,
	ariaLabel: label,
};
// OLD API
registerPaymentMethod( ( Config ) => new Config( bankTransferPaymentMethod ) );

Now you can simply pass the options object directly:

// New API
registerPaymentMethod( bankTransferPaymentMethod );

All payment methods (gateway extensions) should update to the new API as soon as possible. The previous callback-based API is supported as a fallback, and will be deprecated in a future release.

- no need for a callback dance
- support the previous API: if a function is passed, call it as before
@haszari haszari self-assigned this Nov 15, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Nov 15, 2020

Size Change: +2.09 kB (0%)

Total Size: 1.11 MB

Filename Size Change
build/active-filters-frontend.js 8.89 kB +6 B (0%)
build/all-products-frontend.js 31.3 kB +11 B (0%)
build/all-products.js 36.1 kB +1 B
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 3.18 kB +2 B (0%)
build/atomic-block-components/add-to-cart.js 7.52 kB -1 B
build/atomic-block-components/button-frontend.js 2.02 kB +3 B (0%)
build/atomic-block-components/button.js 839 B +1 B
build/atomic-block-components/category-list.js 476 B -2 B (0%)
build/atomic-block-components/image.js 1.15 kB -1 B
build/atomic-block-components/price-frontend.js 2.29 kB -1 B
build/atomic-block-components/price.js 2.32 kB +1 B
build/atomic-block-components/rating-frontend.js 523 B -1 B
build/atomic-block-components/sale-badge-frontend.js 858 B +1 B
build/atomic-block-components/sale-badge.js 863 B -1 B
build/atomic-block-components/stock-indicator.js 572 B -1 B
build/atomic-block-components/tag-list.js 474 B +1 B
build/atomic-block-components/title-frontend.js 1.23 kB +1 B
build/atomic-block-components/title.js 1.06 kB -1 B
build/attribute-filter-frontend.js 18.2 kB -7 B (0%)
build/cart-frontend.js 71.2 kB +1.09 kB (1%)
build/cart.js 39.9 kB +1.09 kB (2%)
build/checkout-frontend.js 85.7 kB -192 B (0%)
build/checkout.js 42.2 kB +70 B (0%)
build/featured-category.js 7.73 kB +1 B
build/featured-product.js 9.98 kB +2 B (0%)
build/handpicked-products.js 7.37 kB +3 B (0%)
build/price-filter-frontend.js 14.9 kB +12 B (0%)
build/price-filter.js 10.4 kB -3 B (0%)
build/product-categories.js 3.23 kB -3 B (0%)
build/product-category.js 8.39 kB -2 B (0%)
build/product-new.js 7.61 kB -1 B
build/product-on-sale.js 7.99 kB -1 B
build/product-search.js 3.56 kB -2 B (0%)
build/product-tag.js 6.53 kB +2 B (0%)
build/product-top-rated.js 7.58 kB -2 B (0%)
build/products-by-attribute.js 8.35 kB +2 B (0%)
build/reviews-by-category.js 11.8 kB +2 B (0%)
build/reviews-by-product.js 13.4 kB +1 B
build/reviews-frontend.js 9.38 kB +8 B (0%)
build/single-product-frontend.js 33.8 kB +2 B (0%)
build/single-product.js 10.1 kB +1 B
build/vendors.js 407 kB -10 B (0%)
build/wc-blocks-data.js 7.1 kB +2 B (0%)
build/wc-blocks-middleware.js 932 B +1 B
build/wc-blocks-registry.js 2.39 kB +65 B (2%)
build/wc-payment-method-bacs.js 775 B -15 B (1%)
build/wc-payment-method-cheque.js 771 B -16 B (2%)
build/wc-payment-method-cod.js 866 B -13 B (1%)
build/wc-payment-method-paypal.js 813 B -18 B (2%)
build/wc-payment-method-stripe.js 12 kB -7 B (0%)
build/wc-settings.js 2.35 kB +1 B
build/wc-shared-hocs.js 1.66 kB -2 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/active-filters.js 8.95 kB 0 B
build/all-reviews.js 9.78 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/add-to-cart-frontend.js 8.94 kB 0 B
build/atomic-block-components/category-list-frontend.js 469 B 0 B
build/atomic-block-components/image-frontend.js 1.7 kB 0 B
build/atomic-block-components/rating.js 527 B 0 B
build/atomic-block-components/sku-frontend.js 386 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/summary-frontend.js 917 B 0 B
build/atomic-block-components/summary.js 926 B 0 B
build/atomic-block-components/tag-list-frontend.js 467 B 0 B
build/attribute-filter.js 12.5 kB 0 B
build/blocks.js 3.54 kB 0 B
build/editor-rtl.css 13.8 kB 0 B
build/editor.css 13.9 kB 0 B
build/product-best-sellers.js 7.45 kB 0 B
build/style-rtl.css 17.9 kB 0 B
build/style.css 17.9 kB 0 B
build/vendors--atomic-block-components/price-frontend.js 5.65 kB 0 B
build/vendors-style-rtl.css 1.03 kB 0 B
build/vendors-style.css 1.03 kB 0 B
build/wc-shared-context.js 1.53 kB 0 B

compressed-size-action

- add legacy fallback if passed a function
- update stripe express payment method
- update docs
- remove unused `assertValidPaymentMethodCreator` util
@haszari haszari changed the title handle plain options passed to registerPaymentMethod Support a plain js config argument to payment method registration APIs Nov 16, 2020
@haszari haszari marked this pull request as ready for review November 16, 2020 01:22
@haszari haszari requested a review from a team as a code owner November 16, 2020 01:22
@haszari haszari requested review from nerrad and senadir and removed request for a team November 16, 2020 01:22
@senadir
Copy link
Member

senadir commented Nov 16, 2020

I'm going to do a functionality review for this soon.
However, I don't think we need to create any false sense of urgency. This is a low priority update that doesn't break anything, it's a simple code refactor and we still have total control on both cases because we're exposing the Config class and therefor control the implementation of it.

I'd also like us to follow up on this comment to explain that this refactor is not shipped yet and implementing it will break the integration.

Also shipping it now would require people to have both latest versions at the same time, while a phase period is better.

We should hold the urgency card until we actually need it (security, something blocking feature work, performance).

@RadoslavGeorgiev
Copy link

How should the consumers of the registerPaymentMethod API know whether to pass a callback or a configuration object as a parameter?

Reading this comment made me think I'd be able to use the new argument, or the old one as fallback. The only way to do this right now would be to check woocommerce-gutenberg-products-block's version in PHP and send data to JavaScript.

The good thing is that blocks support in WCPay is still an experimental feature, so the risks of updating it to the new API without a fallback are very low. Still, is this change really necessary? The only benefit I, as some who's not really involved in the repo, see is that the registration of the PM looks a bit cleaner.

@nerrad
Copy link
Contributor

nerrad commented Nov 16, 2020

@RadoslavGeorgiev I think Rua jumped the gun a bit in potentially attributing a sense of urgency to this that isn't necessary. While we will emphasize the new argument used for registration going forward, it's trivial for us to support both argument shapes for some time so WCPay can keep using the older signature until you are comfortably able to switch to the new signature (which may be what Rua meant by "as soon as practical" in his comment in the WC Pay repo).

@haszari
Copy link
Contributor Author

haszari commented Nov 16, 2020

attributing a sense of urgency to this that isn't necessary

Totally agree, I just wanted to give WCPay a heads up. No need to make the change any time soon, both signatures will be supported for the time being.

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.

Looks great Rua! I've indicated a couple minor doc fixes. Also before merging I think it'd be good to add a typedef for the payment method config option (so the passed in object shape is described by the typedef).

I didn't test this - this is a code review only.

Pre-approving - the doc fixes should be applied before merge but the typedef is up to you.

@senadir
Copy link
Member

senadir commented Nov 16, 2020

It seems I had a comment that got lost

Deprecating the old API - help!
In the issue it's suggested that we deprecate the old callback-style API. I don't know how we should do this – how do we deprecate JS APIs?
Perhaps we can add a warn_deprecated_api() routine that logs to console, possibly with an extra conditional (e.g. SCRIPT_DEBUG ?), and call this in the deprecated/legacy code path.

You can use @wordpress/deprecated, this is how we deprecated setSetting and I think you should use the same.
Just call the function when you detect that the user passed a callback.

https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/2bcda21e3a03cb501e57287024f29a42d8914021/assets/js/settings/shared/set-setting.js#L27-L37

@haszari
Copy link
Contributor Author

haszari commented Nov 17, 2020

Thanks for the reviews @nerrad @senadir - great tips!

This is ready to go (though feel free to review/feedback the changes) but needs testing before merge. I've tested all regular payment methods but still need a volunteer to test express payment (I can't test this).

add a typedef for the payment method config option

Great idea, I've implemented this for both types, and also mentioned it in the docs. This will help us notice when we add new fields 🧑‍🍳. Note the type I used for the react elements is a bit fuzzy (Object) but is better than nothing, we can improve in future.

@wordpress/deprecated

TIL, thanks for this. I'm now calling it if the callback is passed. It's a slightly different use case - we're deprecating an arg type, not a whole function, but it does the job. Here's the console message you get if you pass a callback:

Screen Shot 2020-11-17 at 2 24 12 PM

@haszari
Copy link
Contributor Author

haszari commented Nov 18, 2020

I've got Chrome Pay working on my ephemeral test site now. I've tested the "pay now" button shows up in cart and checkout, and clicking it triggers the modal. This test confirms that express payment method registration is working correctly.

Note I can't complete a successful payment with Chrome Pay - fails every time. I see this with the shortcode checkout, so I'm confident this isn't a bug with this PR :)

Screen Shot 2020-11-18 at 2 33 09 PM

We can merge this when someone else has given this a quick test, ideally with a successful express payment.

@nerrad
Copy link
Contributor

nerrad commented Nov 18, 2020

Tested chrome pay with incognito and it worked fine. I'm not seeing any errors related to the changes in this pr.

I did reproduce what you are reporting with failed chrome pay under certain conditions for a logged in user but was also able to reproduce on trunk so it's unrelated to those changes. It's the end of my day so I'll report the issue tomorrow.

@haszari
Copy link
Contributor Author

haszari commented Nov 18, 2020

Thanks @nerrad! Merging 🚢

I did reproduce what you are reporting with failed chrome pay under certain conditions for a logged in user but was also able to reproduce on trunk so it's unrelated to those changes. It's the end of my day so I'll report the issue tomorrow.

I saw that with shortcode checkout too, so possibly unrelated to blocks.

@haszari haszari merged commit a489ca6 into trunk Nov 18, 2020
@haszari haszari deleted the fix/simplify-payment-method-api branch November 18, 2020 22:06
@nerrad nerrad added category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. block: checkout Issues related to the checkout block. labels Nov 23, 2020
@nerrad nerrad added this to the 3.9.0 milestone Nov 23, 2020
@nerrad nerrad added the type: refactor The issue/PR is related to refactoring. label Nov 23, 2020
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. category: extensibility Work involving adding or updating extensibility. Useful to combine with other scopes impacted. type: refactor The issue/PR is related to refactoring.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change registerPaymentMethod to accept a config object instead of a PaymentMethodConfig instance

5 participants