-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Only credit back allowed amount #1603
Only credit back allowed amount #1603
Conversation
f898e19
to
b8abebe
Compare
|
||
context "when there are offsets" do | ||
before do | ||
payment.offsets.create( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer create!
to ensure errors are raised in test setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -67,7 +67,7 @@ def credit(amount_in_cents, auth_code, gateway_options = {}) | |||
originator = gateway_options[:originator] | |||
|
|||
store_credit.credit( | |||
amount_in_cents / 100.0.to_d, | |||
::Money.new(amount_in_cents, currency).to_d, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the .to_d
still required now that we're returning a Fixnum
from amount_in_cents
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is creating a RubyMoney in order to reverse the amount_in_cents logic. Example:
[1] pry(main)> ::Money.new(1000, 'USD').to_d.to_s
=> "10.0"
[2] pry(main)> ::Money.new(1000, 'JPY').to_d.to_s
=> "1000.0"
amount_in_cents = (store_credit_event.amount * 100).round | ||
credit(amount_in_cents, auth_code) | ||
credit( | ||
store_credit_event.originator.credit_allowed_in_cents, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the originator
here ever be something other than a payment? I assume so, and cause this will break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is going through the PaymentMethod
I would hope not, but I should raise a more appropriate exception in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just assumed since a polymorphic association is used, we should expect it possible that something other than a payment method be the originator. Maybe that should be changed to not be polymorphic?
@@ -267,15 +267,17 @@ | |||
create(:store_credit_capture_event, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spacing here needs some help. Perhaps in a separate commit?
|
||
it_behaves_like "a spree payment method" | ||
|
||
it "refunds the capture amount" do | ||
expect { subject }.to change{ store_credit.reload.amount_remaining }. | ||
from(original_amount - captured_amount). | ||
to(original_amount) | ||
from(90).to(100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great improvement, thank you!
auth_code, | ||
currency, | ||
action_originator: originator | ||
) if amount_in_cents > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a regression test for this guard?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
c8314ff
to
fbc2b60
Compare
fbc2b60
to
d73f8f0
Compare
Going to do some additional work on this. There's an issue with cancelling an order containing a fully refunded store credit payment. |
d73f8f0
to
d24ca62
Compare
I have corrected the error which occurred when cancelling an order which was previously fully refunded. |
core/app/models/spree/payment.rb
Outdated
# | ||
# @return [Fixnum] The amount of this payment minus offets and refunds | ||
# in cents. | ||
def credit_allowed_in_cents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the rational here for returning the in_cents rather than the money object and letting the caller do with it as it wishes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmacdougall could you follow up here?
This will not attempt to credit store credit for zero dollar amount, which will trigger errors.
d24ca62
to
793f44e
Compare
Previously there was some unsafe math where numbers were multiplied and divided by 100 in the store credit section. This is a step towards getting rid of that.
When a payment is cancelled, previously it would attempt to return the whole amount to the store credit even if it had been entirely or partially refunded.
793f44e
to
04eccbe
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My feedback is resolved, thanks 👍
This was superseded by #2111 |
This fixes an issue with payment refunding. To reproduce:
This will result in an exception as it will attempt to give the user more
store credit than they could potentially have available.
This modifies the store credit payment cancel to give back the maximum allowed
rather than the full amount.
Additionally, this gets rid of some terrible logic which only works with currencies
that have two decimal places which multiplies and divides things by 100.