Webhook development support and generic payment method slug#271
Conversation
3844979 to
dad6dd1
Compare
Codecov Report
@@ 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
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
rainerdema
left a comment
There was a problem hiding this comment.
I left a comment, but overall looks great! 👏
Also remove the prefix when delegating `#slug` to it.
Add a scope that can be combined with `.first!` to find a payment method by slug.
dad6dd1 to
731629d
Compare
waiting-for-dev
left a comment
There was a problem hiding this comment.
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! |
There was a problem hiding this comment.
Not sure to get why we unwrapped the procedure to get the payment method. IMHO, that opens gates to duplication and copy-paste programming.
There was a problem hiding this comment.
@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
solidus_stripe/app/controllers/solidus_stripe/intents_controller.rb
Lines 45 to 48 in 731629d
| end | ||
|
|
||
| private | ||
| def assign_slug |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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? |
There was a problem hiding this comment.
For sure opinionated, but IMHO using recursion made it simpler (for instance, we didn't need to check for slug.nil?) 🙂
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
@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! |
There was a problem hiding this comment.
@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
solidus_stripe/app/controllers/solidus_stripe/intents_controller.rb
Lines 45 to 48 in 731629d
| 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? |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Summary
Checklist
Check out our PR guidelines for more details.
The following are mandatory for all PRs:
The following are not always needed: