Bug: Filter the apple pay retryable connectors for a specific business profile



Currently while fetching the apple pay retry connector list all the enabled merchant connector accounts for that specific merchant id is included. But we need to retry the connector with only the connectors in that specific profile id with which the payment attempt was created.
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 7bc0b532239..b64e7479c4d 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -3165,11 +3165,11 @@ where
                 routing_data.business_sub_label = choice.sub_label.clone();
             }
 
-            #[cfg(feature = "retry")]
+            #[cfg(all(feature = "retry", feature = "connector_choice_mca_id"))]
             let should_do_retry =
                 retry::config_should_call_gsm(&*state.store, &merchant_account.merchant_id).await;
 
-            #[cfg(feature = "retry")]
+            #[cfg(all(feature = "retry", feature = "connector_choice_mca_id"))]
             if payment_data.payment_attempt.payment_method_type
                 == Some(storage_enums::PaymentMethodType::ApplePay)
                 && should_do_retry
@@ -3180,15 +3180,15 @@ where
                     payment_data,
                     key_store,
                     connector_data.clone(),
-                    #[cfg(feature = "connector_choice_mca_id")]
                     choice.merchant_connector_id.clone().as_ref(),
-                    #[cfg(not(feature = "connector_choice_mca_id"))]
-                    None,
                 )
                 .await?;
 
                 if let Some(connector_data_list) = retryable_connector_data {
-                    return Ok(ConnectorCallType::Retryable(connector_data_list));
+                    if connector_data_list.len() > 1 {
+                        logger::info!("Constructed apple pay retryable connector list");
+                        return Ok(ConnectorCallType::Retryable(connector_data_list));
+                    }
                 }
             }
 
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 8f7ed4da256..74bfea22f2c 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -3961,6 +3961,7 @@ pub fn get_applepay_metadata(
         })
 }
 
+#[cfg(all(feature = "retry", feature = "connector_choice_mca_id"))]
 pub async fn get_apple_pay_retryable_connectors<F>(
     state: AppState,
     merchant_account: &domain::MerchantAccount,
@@ -3986,7 +3987,7 @@ where
         merchant_account.merchant_id.as_str(),
         payment_data.creds_identifier.to_owned(),
         key_store,
-        profile_id, // need to fix this
+        profile_id,
         &decided_connector_data.connector_name.to_string(),
         merchant_connector_id,
     )
@@ -4008,9 +4009,14 @@ where
             .await
             .to_not_found_response(errors::ApiErrorResponse::InternalServerError)?;
 
+        let profile_specific_merchant_connector_account_list = filter_mca_based_on_business_profile(
+            merchant_connector_account_list,
+            Some(profile_id.to_string()),
+        );
+
         let mut connector_data_list = vec![decided_connector_data.clone()];
 
-        for merchant_connector_account in merchant_connector_account_list {
+        for merchant_connector_account in profile_specific_merchant_connector_account_list {
             if is_apple_pay_simplified_flow(
                 merchant_connector_account.metadata,
                 Some(&merchant_connector_account.connector_name),
@@ -4031,7 +4037,33 @@ where
                 }
             }
         }
-        Some(connector_data_list)
+
+        let fallback_connetors_list = crate::core::routing::helpers::get_merchant_default_config(
+            &*state.clone().store,
+            profile_id,
+            &api_enums::TransactionType::Payment,
+        )
+        .await
+        .change_context(errors::ApiErrorResponse::InternalServerError)
+        .attach_printable("Failed to get merchant default fallback connectors config")?;
+
+        // connector_data_list is the list of connectors for which Apple Pay simplified flow is configured.
+        // This list is arranged in the same order as the merchant's default fallback connectors configuration.
+        let mut ordered_connector_data_list = vec![decided_connector_data.clone()];
+        fallback_connetors_list
+            .iter()
+            .for_each(|fallback_connector| {
+                let connector_data = connector_data_list.iter().find(|connector_data| {
+                    fallback_connector.merchant_connector_id == connector_data.merchant_connector_id
+                        && fallback_connector.merchant_connector_id
+                            != decided_connector_data.merchant_connector_id
+                });
+                if let Some(connector_data_details) = connector_data {
+                    ordered_connector_data_list.push(connector_data_details.clone());
+                }
+            });
+
+        Some(ordered_connector_data_list)
     } else {
         None
     };

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [x] Bugfix
- [ ] New feature
- [ ] Enhancement
- [ ] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description
<!-- Describe your changes in detail -->
Currently while fetching the apple pay retry connector list all the enabled merchant connector accounts for that specific merchant id is included. But we need to retry the connector with only the connectors in that specific profile id with which the payment attempt was created. Also range the apple pay retry connector in the default fallback configuration sequence.

### Additional Changes

- [ ] This PR modifies the API contract
- [ ] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->


## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->
-> Create merchant connector accounts with apple pay simplified configured for some of them.
-> Use the below curl to set the default connectors
```
curl --location 'https://sandbox.hyperswitch.io/routing/default/profile/pro_SIWKUcl7KWmcoRroVz1r' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYWJjZGIxYWItYmJiMS00YmY0LTgwYjQtNmQyYmM0NzIyZGUzIiwibWVyY2hhbnRfaWQiOiJtZXJjaGFudF8xNjk5MzUwNTMzIiwicm9sZV9pZCI6Im9yZ19hZG1pbiIsImV4cCI6MTcwMDA1MjQwMCwib3JnX2lkIjoib3JnX0F3bmNqWFNUQW9ncUgwdUpVU2h2In0.WigP7IrTpBhP4LbWcimEJX0lJfnsjgEimOCOiBK1vxs' \
--data '[
   
    {
        "connector": "adyen",
        "merchant_connector_id": "mca_NASIts6PGENwK4gd39mW"
    },
     {
        "connector": "bluesnap",
        "merchant_connector_id": "mca_E6t0JCvdefLSNoPUSGWh"
    }
]'
```
<img width="1111" alt="image" src="https://github.com/juspay/hyperswitch/assets/83439957/839b8e63-910d-4178-99b0-90017668dab3">

-> Create a payment with confirm false
```
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_XoFL52XqqjWadvSrBkC4Kt91eIH5JUEgg9APgemwbWOnyxVYlT49S3kjDFRuoO65' \
--data-raw '{
    "amount": 6500,
    "currency": "USD",
    "confirm": false,
    "business_country": "US",
    "business_label": "default",
    "amount_to_capture": 6500,
    "customer_id": "sai",
    "capture_method": "automatic",
    "capture_on": "2022-09-10T10:11:12Z",
    "authentication_type": "three_ds",
    "return_url": "https://google.com",
    "email": "something@gmail.com",
    "name": "Joseph Doe",
    "phone": "999999999",
    "phone_country_code": "+65",
    "description": "Its my first payment request",
    "statement_descriptor_name": "Juspay",
    "statement_descriptor_suffix": "Router"
    
   
}'
```
<img width="1142" alt="image" src="https://github.com/juspay/hyperswitch/assets/83439957/9f0baef3-2c35-474b-9741-4d5abe6219e9">

-> Do payment method list for the merchant
```
curl --location 'http://localhost:8080/account/payment_methods?client_secret=pay_08tgyPCEZNQrMIvRBzzZ_secret_1MBVp3rgr2vqJfiH5Gyr' \
--header 'Accept: application/json' \
--header 'api-key: pk_dev_5d8a78ba4cf84af78765ff59ea249fc6'
```
<img width="1146" alt="image" src="https://github.com/juspay/hyperswitch/assets/83439957/17c0e1b6-8e35-4509-bc9f-d06109bdd72b">

-> Confirm the payment with apple pay payment data
```
curl --location 'http://localhost:8080/payments/pay_08tgyPCEZNQrMIvRBzzZ/confirm' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_XoFL52XqqjWadvSrBkC4Kt91eIH5JUEgg9APgemwbWOnyxVYlT49S3kjDFRuoO65' \
--data '{
    "payment_method": "wallet",
    "payment_method_type": "apple_pay",
    "payment_method_data": {
        "wallet": {
            "apple_pay": {
                "payment_data":"eyJkYXRhIjoiSWJKaWp1TTRXT0lpM1BZcTRWVlpIL2wwUld1Qm5JRk5vMHBsWGp1L1E2bW9uQ0JVOE9rVHFDbnkwWDlZVVBZYzVBMXRIckE1eTV0NlpsSFVkZFJyYlY3QXIzMVVEc0NSbVlDVDRweFdEQ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2ZXJzaW9uIjoiRUNfdjEifQ==",
                "payment_method": {
                    "display_name": "Visa 0326",
                    "network": "Mastercard",
                    "type": "debit"
                },
                "transaction_identifier": "55CC32D7BF7890B9064433FCF84AFD01E4E65DD8ADE306300E9D8"
            }
        }
    }
}'
```
<img width="1151" alt="image" src="https://github.com/juspay/hyperswitch/assets/83439957/eea27285-d7dc-49f6-8a81-39a0c73dd8d6">
-> I had configured apple pay simplified flow for stripe, rapyd, adyen and manual flow for cybersource. And my default fallback list is stripe, rapyd, cybersource, adyen. 
Below is the apple pay retry connector list 
<img width="1030" alt="image" src="https://github.com/juspay/hyperswitch/assets/83439957/225c4786-8175-4ce9-bf9b-9a377ef60f8e">


## Checklist
<!-- Put an `x` in the boxes that apply -->

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