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

Improve Bogus (test) Credit Card voiding #4861

Merged
merged 3 commits into from
Jan 23, 2023

Conversation

kennyadsl
Copy link
Member

Follow up for #4843 and #4859.

⚠️ This doesn't change any behavior of real credit card processing. This change has been made to improve DX, when someone is trying to simulate a realistic order flow (create order, capture payment, cancel order) with the provided BogusCreditCard placeholder, which we use in our demo and sandbox.

Summary

Before this change, all the attempts to void a payment in the admin panel would be succeeding. This is not realistic because, in real life, you can't void captured credit card payments.

With this change, we can simulate a failure (by trying to cancel an order with captured payment). This will leave the payment in the complete state and will create a refund for that payment.

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

  • I have written a thorough PR description.
  • I have kept my commits small and atomic.
  • I have used clear, explanatory commit messages.

The following are not always needed (cross them out if they are not):

  • I have added automated tests to cover my changes.
  • I have attached screenshots to demo visual changes.
  • I have opened a PR to update the guides.
  • I have updated the README to account for my changes.

@kennyadsl kennyadsl added type:enhancement Proposed or newly added feature changelog:solidus_core Changes to the solidus_core gem labels Jan 19, 2023
@kennyadsl kennyadsl self-assigned this Jan 19, 2023
@kennyadsl kennyadsl requested a review from a team as a code owner January 19, 2023 09:04
@chrean chrean changed the title Improve Bogus (test) Credit Card voding Improve Bogus (test) Credit Card voiding Jan 19, 2023
@kennyadsl kennyadsl force-pushed the kennyadsl/better-bogus-try-void branch from eeef264 to a8d2f06 Compare January 19, 2023 09:18
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Thanks, @kennyadsl. Leaving some comments. It looks like the tests don't reflect the current responsibilities. Feel free to merge if I'm missing something or if you see it as something that we can't address at this point (this PR is already an improvement on its own).

end
end

it "should not change the payment state whe the payment is completed" do
expect(order).to be_allow_cancel
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this expectation fail now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mhh, no. The completed payment state doesn't change. It stays completed but a refund will be issued.

Copy link
Member Author

Choose a reason for hiding this comment

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

Anyway, there's a typo in the spec description which I'm going to fix.

@@ -113,9 +113,18 @@
end
end

it "should automatically refund all payments" do
context "with a pending payment" do
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, the tested behavior depends on the underlying payment method. Do these tests make sense here?

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right, this should probably go in the specific payment method specs. And we should just test that the outer interface method is called here. Let me see if I can do the change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, you were right. This behavior is already tested in order specs and payment cancellation specs. This change gives us the opportunity to remove the duplication.

In payment method specs instead, we now have the opportunity to just use the bogus credit card instead of the anonymous classes. The downside is that we'll connect these bogus implementations to the tests, but I think it also has an advantage: if we make some changes that make these specs fail, it most likely will be the same for other payment gateways with the same interface, and at least we will notice immediately.

Please re-review now, commit-by-commit should be easier.

# @see Spree::PaymentMethod#try_void
def try_void(_payment)
ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE)
def void(_response_code, _credit_card, options = {})
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add/update tests for that in the bogus credit card specs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

This is tested now, but in the payment method specs, see the second commit. We could move these tests into the bogus own spec files, but I tend to think it has more value if we leave them together in the payment_method specs. Still open to changing my mind, though.

@kennyadsl
Copy link
Member Author

@waiting-for-dev I think your suggestions make sense, I'll take a look. To be honest, I didn't expect any specs to fail after this change but apparently we have a lot of specs relying on the BogusCreditCard behavior, being that the parent class of the factories we are using in specs for credit card payments.

@kennyadsl kennyadsl marked this pull request as draft January 20, 2023 08:26
It now simulates what really happens on most credit cards
payment gateway. If the payment has been already captured,
it's not possible to void it, and the operation will fail.
@kennyadsl kennyadsl force-pushed the kennyadsl/better-bogus-try-void branch 3 times, most recently from 014fc4f to cb22d29 Compare January 21, 2023 08:16
It will fall back to Spree::PaymentMethod#try_void
via class inheritance.

Now that the parent class' try_void has an implementation,
we can rely on that for the Simple/BogusCreditCard.

Before this change, all the attempts to voiding a payment in
the admin panel would be succeding. Now we can simulate a
failure (by trying to cancel an order with captured payment)
and this will create a refund for that payment instead.

Some specs have been updated as well:

- order state machine: refactored and added some scenarios.
  Now the specs are only testing that cancel! on payment is
  actually called, leaving to Spree::Payment::Cancellation
  and its specs the responsibility to test what happens when
  it's called.
- payment method: to simulate what happens with real payment
  methods, we used to create an anonymus class with the expected
  behavior. This behavior is now identical to the default
  implementation, used by simple bogus credit card and bogus
  credit card. We can just use the factories.
We had a lot of duplication between specs in order_spec
and order/state_machine_spec.

I tried to make some order, moving all the cancel! logic
specs into order_spec and leaving to order/state_machine
the only responsibily to test the value of can_cancel?
because that's the only thing that is done by the state
machine, other than calling Spree::Order#after_cancel
when the transition happens.
@kennyadsl kennyadsl force-pushed the kennyadsl/better-bogus-try-void branch from cb22d29 to 2eab6a4 Compare January 21, 2023 08:24
Copy link
Contributor

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

Choose a reason for hiding this comment

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

Thanks, @kennyadsl, for improving this 🙏

@kennyadsl kennyadsl marked this pull request as ready for review January 23, 2023 08:50
@kennyadsl kennyadsl merged commit ad5c86b into solidusio:master Jan 23, 2023
@kennyadsl kennyadsl deleted the kennyadsl/better-bogus-try-void branch January 23, 2023 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants