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

Mercado Pago: add idempotency key field #5229

Merged
merged 1 commit into from
Aug 26, 2024

Conversation

yunnydang
Copy link
Contributor

Adds the idempotency key to the headers of a request. Decided to use an instance variable to store the idempotency key (if provided) so that there is less wrangling of the options object and changes to the endpoint methods.

Local:
5995 tests, 80210 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Unit:
45 tests, 209 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Remote:
45 tests, 107 assertions, 17 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
62.2222% passed

Note: the tests failing are msotly due to payer email related issues.

@yunnydang yunnydang requested a review from a team August 22, 2024 21:47
@yunnydang yunnydang self-assigned this Aug 22, 2024
Copy link
Contributor

@jcreiff jcreiff left a comment

Choose a reason for hiding this comment

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

Hey @yunnydang I think the instance variable is one possible clever way to solve this without having to update the params sent to commit and headers, but I also notice that we used a different convention to achieve this for the other header that gets added, by the way that we handle options[:device_id] - it gets added to the post for the purpose of getting sent to commit, but then is omitted by the logic in post_data

Do you think we should stick with that type of convention, for the sake of consistency?

@jcreiff
Copy link
Contributor

jcreiff commented Aug 23, 2024

Although now that I look at it again, I see that the add_additional_data method that references the device_id is only getting called for purchase, and we need to be able to send the idempotency key on other txn types as well 🤔 So maybe it's not a simpler/cleaner way to accomplish this after all. Maybe there's some kind of hybrid approach that makes sense?

@yunnydang
Copy link
Contributor Author

Hey @yunnydang I think the instance variable is one possible clever way to solve this without having to update the params sent to commit and headers, but I also notice that we used a different convention to achieve this for the other header that gets added, by the way that we handle options[:device_id] - it gets added to the post for the purpose of getting sent to commit, but then is omitted by the logic in post_data

Do you think we should stick with that type of convention, for the sake of consistency?

Hey @jcreiff I actually tried this initially and wehen we add x_idempotency_key to the post object and send it along, the gateway returns an error that states that the field name (idempotency_key) is invalid. For some reason its accepting the device_id but it refuses the idempotency key field in the post object

@jcreiff
Copy link
Contributor

jcreiff commented Aug 23, 2024

@yunnydang Could you update the post_data method to also delete the idempotency key? I think that's why there's no issue with the device_id - it's getting pulled out of the final request body

@yunnydang
Copy link
Contributor Author

Although now that I look at it again, I see that the add_additional_data method that references the device_id is only getting called for purchase, and we need to be able to send the idempotency key on other txn types as well 🤔 So maybe it's not a simpler/cleaner way to accomplish this after all. Maybe there's some kind of hybrid approach that makes sense?

Yes, in line with my other reply, this would then be needed for the other endpoints. I believe that we could try to do a hybrid approach but the issue is that I think we would still run into to the error in my other comment. Im inclined to believe that the gateway does not accept this field unless its only in the header

@yunnydang
Copy link
Contributor Author

@yunnydang Could you update the post_data method to also delete the idempotency key? I think that's why there's no issue with the device_id - it's getting pulled out of the final request body

Hmm i think this could work! Let me see what tinkering I can do

@yunnydang yunnydang force-pushed the mercadopago_add_idempotency_key branch from 92537cb to 5671acc Compare August 23, 2024 18:05
@yunnydang yunnydang requested a review from jcreiff August 23, 2024 18:05
@yunnydang
Copy link
Contributor Author

@yunnydang Could you update the post_data method to also delete the idempotency key? I think that's why there's no issue with the device_id - it's getting pulled out of the final request body

Thanks for this suggestion! I was able to make a hybridish way for this to work and definitely prefer keeping conventions! I removed the refund remote test because i noticed it would sometimes work and sometimes not. It would fail and state that the refund was not found

commit('void', "payments/#{authorization}", post)
end

def verify(credit_card, options = {})
post = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this ultimately calls the authorize method, I don't think you actually need to touch anything within the verify block - the changes you made to purchase_request should do the trick 😁

Copy link
Contributor

Choose a reason for hiding this comment

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

Just making sure you saw this comment as well @yunnydang - I think we can remove these additions here - you can test it yourself if you change your unit test to be a verify (you'll have to change the # of arguments passed in) - it should still pass even without these updates within this method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh gotcha I was unsure if we needed to send it on the auth and verify as well since they bot go to the payments endpoint!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even though its hitting the auth endpoint first, we dont need to send the key on the subsequent post to verify?

lib/active_merchant/billing/gateways/mercado_pago.rb Outdated Show resolved Hide resolved
lib/active_merchant/billing/gateways/mercado_pago.rb Outdated Show resolved Hide resolved
@yunnydang yunnydang force-pushed the mercadopago_add_idempotency_key branch from 5671acc to cc200e0 Compare August 23, 2024 20:14
@yunnydang yunnydang requested a review from jcreiff August 23, 2024 20:15
@yunnydang yunnydang force-pushed the mercadopago_add_idempotency_key branch from cc200e0 to 0620e09 Compare August 23, 2024 21:24
Copy link
Contributor

@jcreiff jcreiff left a comment

Choose a reason for hiding this comment

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

Looks good!

@yunnydang yunnydang force-pushed the mercadopago_add_idempotency_key branch from 0620e09 to f999c5a Compare August 26, 2024 18:36
@yunnydang yunnydang force-pushed the mercadopago_add_idempotency_key branch from f999c5a to 1549bec Compare August 26, 2024 18:37
@yunnydang yunnydang merged commit 1549bec into master Aug 26, 2024
5 checks passed
@yunnydang yunnydang deleted the mercadopago_add_idempotency_key branch August 26, 2024 18:38
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