Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage Stripe V3 JS code via Sprokets #32

Merged
merged 5 commits into from
Mar 11, 2020

Conversation

spaghetticode
Copy link
Member

Stripe V3 JS code (Elements, Payment Intents, and cart checkout button)
has been completely removed from HTML views and is now loaded via
Sprockets in Solidus frontend/all.js file.

The right API (Elements, Intents...) is now selected by looking for
an HTML element with data-v3-api attribute set with the name of
one of Stripe V3 APIs.

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

Love this.

I left a question and I also have some doubt about how to customize some part of the provided JS now that it is in the assets pipeline. I think it's pretty easy but maybe adding a couple of lines in the README will help new users. What do you think?

@@ -1,4 +1,4 @@
<div id="payment-request-button" data-stripe-config="<%= payment_method.stripe_config(current_order).to_json %>"></div>
<div id="payment-request-button" data-stripe-config="<%= payment_method.stripe_config(current_order).to_json %>" data-v3-api="<%= stripe_v3_api %>"></div>
Copy link
Member

Choose a reason for hiding this comment

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

Do we always show this button in v3, or it should be wrapped in an if somehow? Or is it already done via JS?

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, there's one flag to disable the button:

def payment_request?
  v3_intents? && preferred_stripe_country.present?
end

when this method returns false, the button is not shown. So, the preference stripe_country controls the button visibility, as it's a requirement for the payment request to work.

When the preference is not present, the JS code eventually will not initialize the button:

    setUpPaymentRequest: function(opts) {
      var opts = opts || {};
      var config = this.config.payment_request;

      if (config) {
       // ....

It's probably worth documenting a bit this as well.

@spaghetticode
Copy link
Member Author

@kennyadsl I agree adding some documentation will help, will do that soon, thanks for the suggestion!

The code has been encapsulated in proper constructor objects by
separating common code as necessary.

Code shared among all the Stripe V3 API checkout procedures is
now contained in the `Spree.StripePayment` constructor.

`SolidusStripe.Elements` inherits directly from `SolidusStripe.Payment`,
same goes for `SolidusStripe.CartPageCheckout`.

`SolidusStripe.PaymentIntents` inherits from `SolidusStripe.Elements`,
as it basically extends the Elements functionality by adding the 3D
secure feature.

Both `SolidusStripe.CartPageCheckout` and `SolidusStripe.PaymentIntents`
allow to pay with Apple Pay/Google Pay, so they include shared methods
from `PaymentRequestButtonShared`.

All functions are defined on the different classes prototype, this will
allow to easily customize those functions according to needs by just
overriding them on the proper prototype object.
@spaghetticode spaghetticode force-pushed the v3-sprockets branch 2 times, most recently from 085a8f6 to 3ea07f1 Compare March 10, 2020 11:44
Since solidusio/solidus#3524 was merged,
we need to verify if we're using the single "Name" field or the
previous first/last name combination.
Stripe V3 JS code (Elements, Payment Intents and cart checkout button)
has been completely removed from HTML views and is now loaded via
Sprockets in Solidus `frontend/all.js` file.

The right API (Elements, Intents...) is now selected by looking for
an HTML element with `data-v3-api` attribute set with the name of
one of Stripe V3 APIs.
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

👍

@spaghetticode spaghetticode merged commit 43c2643 into solidusio:master Mar 11, 2020
@spaghetticode spaghetticode deleted the v3-sprockets branch March 11, 2020 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants