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

feat: add unique constraint restriction for KV #3723

Merged
merged 6 commits into from
Feb 26, 2024
Merged

Conversation

dracarys18
Copy link
Member

@dracarys18 dracarys18 commented Feb 20, 2024

Type of Change

  • Enhancement

Description

Add constraint per table for KV.

Tables Affected:

  • PaymentIntent
  • PaymentAttempt
  • Address
  • Refund
  • ReverseLookup

Motivation and Context

Currently we are storing the data in redis to give a buffer time for drainer to drain the data in the database. So the problem is after drainer drains the database and the data in redis is already expired, When you insert the data with the same ID as the last it will succeed from application side but fail in drainer, So all constraints will be forgotten.

This PR fixes the above issue by using Redis Sets to save the constraint id's in place. This will check Sets everytime before inserting the data and throw the Error if the id exists.

Limits of Redis Sets:

  • Size limit of redis instance.
  • 2^64 -1 number of keys, Since Redis uses 64-bit unsigned integer for keys.

How did you test it?

  • Make ROUTER__KV_CONFIG__TTL=2 (Default is 15 in min)

Payments

  • Create a payment. With an payment_id. Should Succeed
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_nzwdA9jmg8sXNCma3rp1t5T67lypzliPAYZdXK8dy6OsSmAVdjdRzPoJqP6MMkGO' \
--data-raw '{
  "payment_id":"pay_sm3WbVmQxQ09gL1Eo5kx",
  "amount": 6540,
  "currency": "USD",
  "confirm": true,
  "capture_method": "automatic",
  "capture_on": "2022-09-10T10:11:12Z",
  "amount_to_capture": 6540,
  "customer_id": "StripeCustomer",
  "email": "guest@example.com",
  "name": "John Doe",
  "phone": "999999999",
  "connector": ["adyen_test"],
  "phone_country_code": "+1",
  "description": "Its my first payment request",
  "authentication_type": "no_three_ds",
  "return_url": "https://google.com",
  "payment_method": "card",
  "payment_method_type": "credit",
  "payment_method_data": {
    "card": {
      "card_number": "4242424242424242",
      "card_exp_month": "10",
      "card_exp_year": "25",
      "card_holder_name": "joseph Doe",
      "card_cvc": "123"
    }
  },
  "billing": {
    "address": {
      "line1": "1467",
      "line2": "Harrison Street",
      "line3": "Harrison Street",
      "city": "San Fransico",
      "state": "California",
      "zip": "94122",
      "country": "US",
      "first_name": "joseph",
      "last_name": "Doe"
    },
    "phone": {
      "number": "8056594427",
      "country_code": "+91"
    }
  },
  "shipping": {
    "address": {
      "line1": "1467",
      "line2": "Harrison Street",
      "line3": "Harrison Street",
      "city": "San Fransico",
      "state": "California",
      "zip": "94122",
      "country": "US",
      "first_name": "joseph",
      "last_name": "Doe"
    },
    "phone": {
      "number": "8056594427",
      "country_code": "+91"
    }
  },
  "statement_descriptor_name": "joseph",
  "statement_descriptor_suffix": "JS",
  "metadata": {
    "udf1": "value1",
    "new_customer": "true",
    "login_date": "2019-09-10T10:11:12Z"
  }
}'
  • Wait for ROUTER__KV_CONFIG__TTL in mins and create payment with same id again. It should give Unique constraint error

Refunds

  • Create a refund
curl --location 'http://localhost:8080/refunds' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_XA9H0K3C4JbBRhXYfjAhArJP6wfiXxSnpKT4H4Ip8ld8VT9ipRFKk62qjyY4W2GI' \
--data '{
    "payment_id": "pay_sm3WbVmQxQ09gL1Eo5kx",
    "amount": 600,
    "reason": "Customer returned product",
    "refund_type": "instant",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    }
}'
  • Get the refund_id and wait for ROUTER__KV_CONFIG__TTL in mins and create a refund with same refund_id it should return error with bad request

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code

@dracarys18 dracarys18 added A-core Area: Core flows S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels Feb 20, 2024
@dracarys18 dracarys18 added this to the February 2024 Release milestone Feb 20, 2024
@dracarys18 dracarys18 self-assigned this Feb 20, 2024
@dracarys18 dracarys18 requested a review from a team as a code owner February 20, 2024 10:48
@likhinbopanna likhinbopanna added this pull request to the merge queue Feb 26, 2024
Merged via the queue into main with commit c117f8e Feb 26, 2024
25 of 27 checks passed
@likhinbopanna likhinbopanna deleted the kv_constraints branch February 26, 2024 13:57
pixincreate added a commit that referenced this pull request Feb 28, 2024
…stman-runner

* 'main' of github.com:juspay/hyperswitch: (22 commits)
  chore(version): 2024.02.28.0
  chore(postman): update Postman collection files
  fix(connector): [AUTHORIZEDOTNET] Fix status mapping (#3845)
  refactor(router): added logs health and deep health (#3780)
  feat(roles): Change list roles, get role and authorization info api to respond with groups (#3837)
  fix(core): validate amount_to_capture in payment update (#3830)
  refactor(connector): [Square] change error message from NotSupported to NotImplemented (#2875)
  feat(router): add connector mit related columns to the payment methods table (#3764)
  ci(postman): refactor NMI postman collection (#3805)
  refactor(connector): [Braintree] Mask PII data (#3759)
  refactor(connector): [Forte] Mask PII data (#3824)
  refactor(compatibility): added compatibility layer request logs (#3774)
  refactor(payment_methods): introduce `locker_id` column in `payment_methods` table (#3760)
  feat(connector): mask pii information in connector request and response for stripe, aci, adyen, airwallex  and authorizedotnet (#3678)
  chore(version): 2024.02.27.0
  fix(core): do not construct request if it is already available (#3826)
  refactor: incorporate `hyperswitch_interface` into router (#3669)
  feat: add unique constraint restriction for KV (#3723)
  feat(connector): [Payme] Add Void flow to Payme (#3817)
  refactor(connector): [Cybersource] Mask PII data  (#3786)
  ...
@SanchithHegde SanchithHegde removed the S-waiting-on-review Status: This PR has been implemented and needs to be reviewed label Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core flows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants