Skip to content

Webhook development support and generic payment method slug#271

Merged
elia merged 5 commits into
masterfrom
elia/webhook-dev-support
Apr 5, 2023
Merged

Webhook development support and generic payment method slug#271
elia merged 5 commits into
masterfrom
elia/webhook-dev-support

Conversation

@elia

@elia elia commented Apr 3, 2023

Copy link
Copy Markdown
Member

Summary

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@elia elia self-assigned this Apr 3, 2023
@elia elia force-pushed the elia/webhook-dev-support branch 8 times, most recently from 3844979 to dad6dd1 Compare April 5, 2023 12:20
@codecov

codecov Bot commented Apr 5, 2023

Copy link
Copy Markdown

Codecov Report

Merging #271 (dad6dd1) into master (7cfff91) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head dad6dd1 differs from pull request most recent head 731629d. Consider uploading reports for the commit 731629d to get more accurate results

@@            Coverage Diff             @@
##           master     #271      +/-   ##
==========================================
- Coverage   99.56%   99.56%   -0.01%     
==========================================
  Files          26       26              
  Lines         462      460       -2     
==========================================
- Hits          460      458       -2     
  Misses          2        2              
Impacted Files Coverage Δ
...p/controllers/solidus_stripe/intents_controller.rb 100.00% <ø> (ø)
app/models/solidus_stripe/payment_method.rb 100.00% <100.00%> (ø)
app/models/solidus_stripe/slug_entry.rb 100.00% <100.00%> (ø)
config/routes.rb 100.00% <100.00%> (ø)
...tes/app/views/orders/payment_info/_stripe.html.erb 100.00% <100.00%> (ø)
lib/solidus_stripe/testing_support/factories.rb 100.00% <100.00%> (ø)
lib/solidus_stripe/webhook/event.rb 96.96% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@elia elia marked this pull request as ready for review April 5, 2023 12:46
@elia elia requested a review from rainerdema April 5, 2023 12:46

@rainerdema rainerdema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I left a comment, but overall looks great! 👏

Comment thread app/models/solidus_stripe/slug_entry.rb
@elia elia force-pushed the elia/webhook-dev-support branch from dad6dd1 to 731629d Compare April 5, 2023 14:02
@elia elia requested a review from rainerdema April 5, 2023 14:18

@rainerdema rainerdema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💯

@elia elia merged commit be652e6 into master Apr 5, 2023
@elia elia deleted the elia/webhook-dev-support branch April 5, 2023 14:45

@waiting-for-dev waiting-for-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for working on that.

I left a posthumous review. No need to take action, but leaving my thoughts for future reference.

class << self
def from_request(payload:, signature_header:, slug:, tolerance: default_tolerance)
payment_method = SolidusStripe::WebhookEndpoint.payment_method(slug)
payment_method = SolidusStripe::PaymentMethod.with_slug(slug).first!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure to get why we unwrapped the procedure to get the payment method. IMHO, that opens gates to duplication and copy-paste programming.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@waiting-for-dev that's a valid concern. The reason it was moved to a slightly lower-level construct like a scope is that it's more versatile and can be mixed and matched with other scopes without generating extra queries more easily, e.g. in this case

@payment_method = current_order(create_order_if_necessary: true)
.available_payment_methods
.merge(SolidusStripe::PaymentMethod.with_slug(params[:slug]))
.first!

end

private
def assign_slug

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think responsibilities were fairer when that method was part of the SlugEntry class, especially considering that the payment method already deals with a lot of stuff (via inheritance from upstream Solidus).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The main reason was (I think) having access to preferred_test_mode (see the other comment), personally I'm fine with both arrangements, with a preference for SlugEntry not needing to know too much about the payment method.

def assign_slug
# If there's only one payment method, we can use a default slug.
slug = preferred_test_mode ? 'test' : 'live' if self.class.count == 1
slug = SecureRandom.hex(16) while SlugEntry.exists?(slug: slug) || slug.nil?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For sure opinionated, but IMHO using recursion made it simpler (for instance, we didn't need to check for slug.nil?) 🙂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it was originally moved in order to check the .count and .preferred_test_mode on the payment method, but could be simplified for sure, e.g. the following is probably equivalent:

  return if slug_entry.present?

  slug = preferred_test_mode ? 'test' : 'live'
  slug = SecureRandom.hex(16) while SlugEntry.exists?(slug: slug)

Although given how rare it is I agree we should do it for readability (vs. performance).

@elia elia left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@waiting-for-dev I hope I provided some context on the code changes you highlighted, please feel free to send PRs for refining rough edges if there's anything you particularly care about 🙌

class << self
def from_request(payload:, signature_header:, slug:, tolerance: default_tolerance)
payment_method = SolidusStripe::WebhookEndpoint.payment_method(slug)
payment_method = SolidusStripe::PaymentMethod.with_slug(slug).first!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@waiting-for-dev that's a valid concern. The reason it was moved to a slightly lower-level construct like a scope is that it's more versatile and can be mixed and matched with other scopes without generating extra queries more easily, e.g. in this case

@payment_method = current_order(create_order_if_necessary: true)
.available_payment_methods
.merge(SolidusStripe::PaymentMethod.with_slug(params[:slug]))
.first!

def assign_slug
# If there's only one payment method, we can use a default slug.
slug = preferred_test_mode ? 'test' : 'live' if self.class.count == 1
slug = SecureRandom.hex(16) while SlugEntry.exists?(slug: slug) || slug.nil?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it was originally moved in order to check the .count and .preferred_test_mode on the payment method, but could be simplified for sure, e.g. the following is probably equivalent:

  return if slug_entry.present?

  slug = preferred_test_mode ? 'test' : 'live'
  slug = SecureRandom.hex(16) while SlugEntry.exists?(slug: slug)

Although given how rare it is I agree we should do it for readability (vs. performance).

end

private
def assign_slug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The main reason was (I think) having access to preferred_test_mode (see the other comment), personally I'm fine with both arrangements, with a preference for SlugEntry not needing to know too much about the payment method.

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.

Consider scoping all payment method routes to its slug

3 participants