square / connect-api-examples

Code samples demonstrating the functionality of the Square Connect API
https://developer.squareup.com/docs/sample-apps
389 stars 792 forks source link

Fix Payment SDK sample apps idempotency #373

Closed barakj closed 1 year ago

barakj commented 1 year ago

Previously, we would generate a new idempotency key for each payment. This is bad practice, since the reason we use idempotency key in the first place is to prevent issues like multiple clicks or accidentally submitting multiple payment requests. I fixed the sample apps so that we generate the idempotency key on page load.

Previous behaviour: loading the app and paying multiple times would "charge" the user multiple times since different idempotency keys are used for every request.

New behaviour: loading the app and paying multiple times would still generate multiple requests, but with the same idempotency key. Only the first payment will be successful.

Additionally, I fixed an issue with the Java payment app which wasn't working properly, and added some additional error handling to the rails payment sample.