refactor(dynamic_routing): add logic for creating merchant account in decision engine#8191
Conversation
Changed Files
|
| // Call to DE here | ||
| // Check if creation should be based on default profile | ||
| #[cfg(all(feature = "dynamic_routing", feature = "v1"))] | ||
| { | ||
| if state.conf.open_router.enabled { | ||
| merchant_account | ||
| .default_profile | ||
| .as_ref() | ||
| .async_map(|profile_id| { | ||
| routing::helpers::create_decision_engine_merchant(&state, profile_id) | ||
| }) | ||
| .await | ||
| .transpose() | ||
| .map_err(|err| { | ||
| crate::logger::error!("Failed to create merchant in Decision Engine {err:?}"); | ||
| }) | ||
| .ok(); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
I don't think we should create merchant_account in decision engine for every profile creation on our side as we are not sure whether SR will be toggled for that profile. Instead I have moved the merchant creation call in DE to the place where SR is being toggled. From this we are taking of all the below cases -
- New profile creation: Can use SR toggle API which creates both merchant and rules in DE
- Old profile: for both the cases where SR is already toggled or not toggled,
is_merchant_created_in_decision_engineflag in profile table will indicate whether merchant is created on DE. if not, we'll create it.
| if !dynamic_routing_algo_ref.is_merchant_created_in_decision_engine { | ||
| logger::debug!( | ||
| "Creating merchant_account in decision engine for profile {}", | ||
| profile_id.get_string_repr() | ||
| ); | ||
|
|
||
| create_decision_engine_merchant(state, profile_id) | ||
| .await | ||
| .map_err(|err| { | ||
| logger::warn!("Merchant creation error in decision_engine: {err:?}"); | ||
| }) | ||
| .ok(); | ||
|
|
||
| // TODO: Update the status based on the status code or error message from the API call | ||
| dynamic_routing_algo_ref.update_merchant_creation_status_in_decision_engine(true); |
There was a problem hiding this comment.
Rare edge case but if DE response does not come through while the operation still being a success on DE, We'd again make a call for merchant creation next time around, which would fail and this status would never get updated.
A workaround would be to do a GET call on the merchant-account instead of maintaining state at Hyperswitch.
There was a problem hiding this comment.
And when does this GET call be executed?
There was a problem hiding this comment.
Instead of looking up the is_merchant_created_in_decision_engine, we can do a GET call instead. These are functionally equivalent right?
There was a problem hiding this comment.
We didn't want to do this extra call everytime something is toggled. Also your case won't even occur if we switch the is_merchant_created_in_decision_engine flag based on status from create merchant call which will be picked up by @jagan-jaya
| }; | ||
|
|
||
| // Create merchant in Decision Engine if it is not already created | ||
| create_merchant_in_decision_engine_if_not_exists(state, profile_id, dynamic_routing_algo_ref) |
There was a problem hiding this comment.
Can we move this function to a common location in the routing logic? We need to create a merchant whenever debit routing or dynamic routing is enabled.
7710c74
…-merchant-creation-in-de
5b1b775 to
409e869
Compare
…tch into cypress/fix * 'cypress/fix' of github.com:juspay/hyperswitch: chore(cypress): run formatter and address lints * 'main' of github.com:juspay/hyperswitch: feat(router): add merchantId authentication for Payments v2 (#8239) chore(version): 2025.06.06.0 Documentation edits made through Mintlify web editor Documentation edits made through Mintlify web editor feat(router): Return payment_experience in PML for payment (v2) (#8255) refactor: add infra-values in intent kafka events (#8264) fix(wasm): [Worldpayvantiv] add support for metadata.report_group (#8260) chore(version): 2025.06.05.0 revert(routing): Add connectors from current active routing algorithm before adding fallback connectors (#8207) feat(connectors): [Worldpayvantiv] add card support (#8219) feat(connectors): [Template] add Worldpayvantiv (#8226) refactor(dynamic_routing): add logic for creating merchant account in decision engine (#8191) feat(events): adding infra level components to api-events (#8214) chore(version): 2025.06.04.0 feat(core): add support for consuming eci for AuthNResponse in Authentication flow (#8225)
Type of Change
Description
Merchant account creation on decision-engine is pre-requisite for making success based routing calls to it. Since toggle SR endpoint on Hyperswitch is the place where rule creation API of DE is being called, we can check whether merchant_account exists in DE, if exists, toggle endpoint will go ahead with creating the rule. If not, it will first call DE to create a merchant_account and then create the rule.
I have created the
is_merchant_created_in_decision_engineflag inside thedynamic_routing_algorithmcolumn which is of type JSON in profile table. I have added a#[serde(default)]attribute on top ofis_merchant_created_in_decision_engine. Hence it shouldn't break for older profiles which doesn't have this field itself.Additional Changes
Motivation and Context
How did you test it?
Check logs for toggle call which should have something like
Creating merchant_account in decision engine for profile <profile_id>Create merchant in debit routing flow
-> Update business profile by setting is_debit_routing_enabled
-> Db screenshot


-> Db entry when business profile is created
Test sr
-> Update profile to enable debit routing

Checklist
cargo +nightly fmt --allcargo clippy