Skip to content

fix: invalidate surcharge cache during update#6907

Merged
Gnanasundari24 merged 2 commits intomainfrom
invalidate-surcharge-cache
Feb 4, 2025
Merged

fix: invalidate surcharge cache during update#6907
Gnanasundari24 merged 2 commits intomainfrom
invalidate-surcharge-cache

Conversation

@hrithikesh026
Copy link
Contributor

@hrithikesh026 hrithikesh026 commented Dec 20, 2024

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

When surcharge rules where updated or deleted, the in memory cache was being invalidated using the wrong key. Fixed that issue in this PR.

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

How did you test it?

  1. upsert surcharge rules. If amount >500 and <1000 add a surcharge of 40%.
curl --location --request PUT 'http://localhost:8080/routing/decision/surcharge' \
--header 'Content-Type: application/json' \
--header 'api-key: dev_bLGFEtyJtHdvJqXtbnwOcnxpBuNk9lcGIHewiAgCcQQKXYdyoHw8SyAz5JcfikMl' \
--data '{
    "name": "Surcharge Conditional config",
    "algorithm": {
        "defaultSelection": {
            "surcharge_details": null
        },
        "rules": [
            {
                "name": "rule_1",
                "routingOutput": {
                    "surcharge_details": {
                        "surcharge": {
                            "type": "rate",
                            "value": {
                                "percentage": 40
                            }
                        },
                        "tax_on_surcharge": {
                            "percentage": 40
                        }
                    }
                },
                "statements": [
                    {
                        "condition": [
                            {
                                "lhs": "amount",
                                "comparison": "greater_than",
                                "value": {
                                    "type": "number",
                                    "value": 500
                                },
                                "metadata": {}
                            },
                            {
                                "lhs": "amount",
                                "comparison": "less_than",
                                "value": {
                                    "type": "number",
                                    "value": 1000
                                },
                                "metadata": {}
                            }
                        ],
                        "nested": null
                    }
                ]
            }
        ],
        "metadata": {}
    },
    "merchant_surcharge_configs": {
        "show_surcharge_breakup_screen": true
    }
}'

should get status code 200 response.

  1. Create payment of 540
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_bLGFEtyJtHdvJqXtbnwOcnxpBuNk9lcGIHewiAgCcQQKXYdyoHw8SyAz5JcfikMl' \
--data-raw '{
    "amount": 540,
    "currency": "USD",
    "confirm": false,
    "capture_method": "automatic",
    "capture_on": "2022-09-10T10:11:12Z",
    "amount_to_capture": 540,
    "customer_id": "StripeCustomer",
    "email": "guest@example.com",
    "name": "John Doe",
    "phone": "999999999",
    "phone_country_code": "+65",
    "description": "Its my first payment request",
    "authentication_type": "no_three_ds",
    "return_url": "https://duck.com",
    "billing": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "PiX"
        }
    },
    "shipping": {
        "address": {
            "line1": "1467",
            "line2": "Harrison Street",
            "line3": "Harrison Street",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "US",
            "first_name": "PiX"
        }
    },
    "statement_descriptor_name": "joseph",
    "statement_descriptor_suffix": "JS",
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    }
}'
  1. Do payment method list.
curl --location 'http://localhost:8080/account/payment_methods?client_secret=pay_rKiEoW6t1eHHzS8WukOx_secret_QvEuTgyhWcGmNhzqHrTc' \
--header 'Accept: application/json' \
--header 'api-key: pk_dev_8ea5f5ea888f43e199ae05a22b7aaae8'

Appropriate surcharge must be applied to each of the payment method.

"surcharge_details": {
                        "surcharge": {
                            "type": "rate",
                            "value": {
                                "percentage": 40.0
                            }
                        },
                        "tax_on_surcharge": {
                            "percentage": 40.0
                        },
                        "display_surcharge_amount": 2.16,
                        "display_tax_on_surcharge_amount": 0.87,
                        "display_total_surcharge_amount": 3.03
                    },
  1. Now update the surcharge rule. If amount >500 and <1000 add a surcharge of 10%.
curl --location --request PUT 'http://localhost:8080/routing/decision/surcharge' \
--header 'Content-Type: application/json' \
--header 'api-key: dev_bLGFEtyJtHdvJqXtbnwOcnxpBuNk9lcGIHewiAgCcQQKXYdyoHw8SyAz5JcfikMl' \
--data '{
    "name": "Surcharge Conditional config",
    "algorithm": {
        "defaultSelection": {
            "surcharge_details": null
        },
        "rules": [
            {
                "name": "rule_1",
                "routingOutput": {
                    "surcharge_details": {
                        "surcharge": {
                            "type": "rate",
                            "value": {
                                "percentage": 10
                            }
                        },
                        "tax_on_surcharge": {
                            "percentage": 10
                        }
                    }
                },
                "statements": [
                    {
                        "condition": [
                            {
                                "lhs": "amount",
                                "comparison": "greater_than",
                                "value": {
                                    "type": "number",
                                    "value": 500
                                },
                                "metadata": {}
                            },
                            {
                                "lhs": "amount",
                                "comparison": "less_than",
                                "value": {
                                    "type": "number",
                                    "value": 1000
                                },
                                "metadata": {}
                            }
                        ],
                        "nested": null
                    }
                ]
            }
        ],
        "metadata": {}
    },
    "merchant_surcharge_configs": {
        "show_surcharge_breakup_screen": true
    }
}'
  1. Do payment method list again.
curl --location 'http://localhost:8080/account/payment_methods?client_secret=pay_rKiEoW6t1eHHzS8WukOx_secret_QvEuTgyhWcGmNhzqHrTc' \
--header 'Accept: application/json' \
--header 'api-key: pk_dev_8ea5f5ea888f43e199ae05a22b7aaae8'

Appropriate surcharge must be applied to each of the payment method.

"surcharge_details": {
                        "surcharge": {
                            "type": "rate",
                            "value": {
                                "percentage": 10.0
                            }
                        },
                        "tax_on_surcharge": {
                            "percentage": 10.0
                        },
                        "display_surcharge_amount": 0.54,
                        "display_tax_on_surcharge_amount": 0.06,
                        "display_total_surcharge_amount": 0.6
                    },

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@hrithikesh026 hrithikesh026 added C-bug Category: Bug S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels Dec 20, 2024
@hrithikesh026 hrithikesh026 self-assigned this Dec 20, 2024
@hrithikesh026 hrithikesh026 requested a review from a team as a code owner December 20, 2024 09:07
@semanticdiff-com
Copy link

semanticdiff-com bot commented Dec 20, 2024

Review changes with  SemanticDiff

Changed Files
File Status
  crates/router/src/core/surcharge_decision_config.rs  34% smaller

@Gnanasundari24 Gnanasundari24 added this pull request to the merge queue Jan 27, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jan 27, 2025
@Gnanasundari24 Gnanasundari24 added this pull request to the merge queue Jan 31, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jan 31, 2025
@Gnanasundari24 Gnanasundari24 added this pull request to the merge queue Feb 3, 2025
Merged via the queue into main with commit 8ac1b83 Feb 4, 2025
18 of 21 checks passed
@Gnanasundari24 Gnanasundari24 deleted the invalidate-surcharge-cache branch February 4, 2025 01:18
pixincreate added a commit that referenced this pull request Feb 4, 2025
…-connectors

* 'main' of github.com:juspay/hyperswitch:
  ci: disable stripe in cypress (#7183)
  fix(router): [Cybersource] add flag to indicate final capture  (#7085)
  fix: invalidate surcharge cache during update (#6907)
  chore(version): 2025.02.04.0
  fix(samsung_pay): populate `payment_method_data` in the payment response (#7095)
  feat(router): add core changes for external authentication flow through unified_authentication_service (#7063)
  fix(connector): [NETCETERA] add `sdk-type` and `default-sdk-type` in netcetera authentication request (#7156)
@pixincreate pixincreate removed the S-waiting-on-review Status: This PR has been implemented and needs to be reviewed label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bug Category: Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] previous rules are being considered for surcharge calculation even after updating surcharge rules in database

5 participants