Blog Logo
TAGS

Preventing Duplicate Payments with Idempotency Keys by Stripe, PayPal and Adyen

Payment service providers such as PayPal or Stripe use a method called Idempotency Keys to prevent duplicate payments. Idempotency ensures that transactions are executed just once, even if multiple requests are sent. This method plays a vital role in handling the complexities of online payments, preventing customers from being charged multiple times for the same purchase. At the heart of idempotency lies the use of unique idempotency keys for each transaction request. These keys, often a universally unique identifier (UUID) or a robust random string, act as transaction fingerprints. When a client initiates a transaction, the payment system requires an idempotency key to be included in the request. The system then checks if this idempotency key has been already seen by controlling Redis cache. If the key has been seen, the system retrieves the current status of the firstly executed request; otherwise, it processes the request, inserts the new payment into the database, and adds the idempotency key to the Redis cache. The payment service provider then returns the current status of the payment as HTTP 201-Created if its the first request for that payment or HTTP 422-Unprocessable Entity if its a duplicated request. By understanding and implementing idempotency keys, payment service providers can ensure smooth and secure transactions for their customers.