Bug: [FEATURE] List Pm Based on Profile



### Feature Description

 List Pm Based on Profile for off_session payments

### Possible Implementation

 List Pm Based on Profile for off_session payments

### Have you spent some time checking if this feature request has been raised before?

- [X] I checked and didn't find a similar issue

### Have you read the Contributing Guidelines?

- [X] I have read the [Contributing Guidelines](https://github.com/juspay/hyperswitch/blob/main/docs/CONTRIBUTING.md)

### Are you willing to submit a PR?

Yes, I am willing to submit a PR!
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index fa01437e5ed..e5c8ef57b29 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -3892,6 +3892,7 @@ pub async fn list_customer_payment_method(
         let mca_enabled = get_mca_status(
             state,
             &key_store,
+            profile_id.clone(),
             merchant_account.get_id(),
             is_connector_agnostic_mit_enabled,
             connector_mandate_details,
@@ -3932,7 +3933,9 @@ pub async fn list_customer_payment_method(
                 && customer.default_payment_method_id == Some(pm.payment_method_id),
             billing: payment_method_billing,
         };
-        customer_pms.push(pma.to_owned());
+        if requires_cvv || mca_enabled {
+            customer_pms.push(pma.to_owned());
+        }
 
         let redis_conn = state
             .store
@@ -4410,19 +4413,22 @@ async fn generate_saved_pm_response(
         .change_context(errors::ApiErrorResponse::InternalServerError)
         .attach_printable("Failed to deserialize to Payment Mandate Reference ")?;
 
-    let (is_connector_agnostic_mit_enabled, requires_cvv, off_session_payment_flag) = payment_info
-        .map(|pi| {
-            (
-                pi.is_connector_agnostic_mit_enabled,
-                pi.requires_cvv,
-                pi.off_session_payment_flag,
-            )
-        })
-        .unwrap_or((false, false, false));
+    let (is_connector_agnostic_mit_enabled, requires_cvv, off_session_payment_flag, profile_id) =
+        payment_info
+            .map(|pi| {
+                (
+                    pi.is_connector_agnostic_mit_enabled,
+                    pi.requires_cvv,
+                    pi.off_session_payment_flag,
+                    pi.business_profile.map(|profile| profile.profile_id),
+                )
+            })
+            .unwrap_or((false, false, false, Default::default()));
 
     let mca_enabled = get_mca_status(
         state,
         key_store,
+        profile_id,
         merchant_account.get_id(),
         is_connector_agnostic_mit_enabled,
         connector_mandate_details,
@@ -4486,6 +4492,7 @@ async fn generate_saved_pm_response(
 pub async fn get_mca_status(
     state: &routes::SessionState,
     key_store: &domain::MerchantKeyStore,
+    profile_id: Option<String>,
     merchant_id: &id_type::MerchantId,
     is_connector_agnostic_mit_enabled: bool,
     connector_mandate_details: Option<storage::PaymentsMandateReference>,
@@ -4507,19 +4514,17 @@ pub async fn get_mca_status(
             .change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
                 id: merchant_id.get_string_repr().to_owned(),
             })?;
-
         let mut mca_ids = HashSet::new();
         let mcas = mcas
             .into_iter()
-            .filter(|mca| mca.disabled == Some(true))
+            .filter(|mca| mca.disabled == Some(false) && profile_id.clone() == mca.profile_id)
             .collect::<Vec<_>>();
 
         for mca in mcas {
             mca_ids.insert(mca.get_id());
         }
-
         for mca_id in connector_mandate_details.keys() {
-            if !mca_ids.contains(mca_id) {
+            if mca_ids.contains(mca_id) {
                 return Ok(true);
             }
         }

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

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

## Description
List cards on the basis of profiles, if it has a mandate details.The precedence order for off_session payments should be if network_txn_id is present , then show the pm irrespective of  the profile, but if its not there then check for mandate_details for that profile and list it on that basis.


### 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?

- Create a MA and 2 Profiles
- Create a MCA for Profile 1
- Create a `off_session` payment 
```
Create
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_jsssWAg0XyxGK0ycPunkFjlItYNs30X1IVOr1MIF7lwrDQgBBk7npwdkZB4t7EQd' \
--data-raw '{
    "amount": 10000,
    "currency": "USD",
    "confirm": false,
    
    "customer_id": "CustomerX7777",
    "capture_method": "automatic",
    "capture_on": "2022-09-10T10:11:12Z",
    "authentication_type": "no_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",
       
"setup_future_usage":"off_session",
    "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"
        }
    },
    "connector_metadata": {
        "noon": {
            "order_category": "pay"
        }
    }
    
   
    , "profile_id": "pro_6RgaXF6EvoYBYoGpOISQ"
}'
```


```
Confirm

{
    "confirm": true,
    "client_secret":"{{client_secret}}",
    "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"
        }
    },
    "browser_info": {
        "user_agent": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/70.0.3538.110 Safari\/537.36",
        "accept_header": "text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8",
        "language": "nl-NL",
        "color_depth": 24,
        "screen_height": 723,
        "screen_width": 1536,
        "time_zone": 0,
        "java_enabled": true,
        "java_script_enabled": true,
        "ip_address": "125.0.0.1"
    },
  
    "payment_method": "card",
    // //  "payment_token": "token_olUiqngeoftZLox6awiz"
    //  "payment_method_type": "debit",
    "payment_method_data": {
        "card": {
             "card_number": "4242424242424242",
            "card_exp_month": "06",
            "card_exp_year": "2030",
            "card_holder_name": "John T",
            //  "card_network": "Visa",
            // "card_issuing_country": "UNITEDKINGDOM",
            // "card_issuer": "STRIPE PAYMENTS UK LIMITED",
            "card_cvc": "737"
        }
    }
}
```

- Do a PML it will show the PM
```
{
    "customer_payment_methods": [
        {
            "payment_token": "token_UWLBwJH7yena1nKMTF3G",
            "payment_method_id": "pm_EbbMg190tRVUKn9sLVcr",
            "customer_id": "CustomerX7777",
            "payment_method": "card",
            "payment_method_type": null,
            "payment_method_issuer": null,
            "payment_method_issuer_code": null,
            "recurring_enabled": true,
            "installment_payment_enabled": false,
            "payment_experience": [
                "redirect_to_url"
            ],
            "card": {
                "scheme": null,
                "issuer_country": null,
                "last4_digits": "4242",
                "expiry_month": "06",
                "expiry_year": "2030",
                "card_token": null,
                "card_holder_name": "John T",
                "card_fingerprint": null,
                "nick_name": null,
                "card_network": null,
                "card_isin": "424242",
                "card_issuer": null,
                "card_type": null,
                "saved_to_locker": true
            },
            "metadata": null,
            "created": "2024-08-09T14:55:58.379Z",
            "bank": null,
            "surcharge_details": null,
            "requires_cvv": false,
            "last_used_at": "2024-08-09T14:55:58.379Z",
            "default_payment_method_set": true,
            "billing": {
                "address": {
                    "city": "San Fransico",
                    "country": "US",
                    "line1": "1467",
                    "line2": "Harrison Street",
                    "line3": "Harrison Street",
                    "zip": "94122",
                    "state": "California",
                    "first_name": "joseph",
                    "last_name": "Doe"
                },
                "phone": {
                    "number": "8056594427",
                    "country_code": "+91"
                },
                "email": null
            }
        }
    ],
    "is_guest_customer": false
}
```

- Create a intent for `off_session` with the Profile 2 
```
Create

curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_jsssWAg0XyxGK0ycPunkFjlItYNs30X1IVOr1MIF7lwrDQgBBk7npwdkZB4t7EQd' \
--data-raw '{
    "amount": 6777,
    "currency": "USD",
    "confirm": false,
    
    "customer_id": "CustomerX7777",
    "capture_method": "automatic",
    "capture_on": "2022-09-10T10:11:12Z",
    "authentication_type": "no_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",
        "profile_id": "pro_NP3IjxxHEUdqVoYi6K4c",
"setup_future_usage":"off_session",
    "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"
        }
    }

  
}'
```

- Do a PML it will not show the PM

```
curl --location 'http://localhost:8080/customers/payment_methods?client_secret=pay_Nk9H9hj64BiGm63EPJT9_secret_Ida6aZ1FR5LhAeO7l3HH' \
--header 'Accept: application/json' \
--header 'api-key: pk_dev_5c27d077a0b94ddb8a3869e4acbe8fe6'
```
```
{
    "customer_payment_methods": [],
    "is_guest_customer": false
}
```
- - Create a intent for `on_session` with the Profile 2
```
curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'api-key: dev_jsssWAg0XyxGK0ycPunkFjlItYNs30X1IVOr1MIF7lwrDQgBBk7npwdkZB4t7EQd' \
--data-raw '{
    "amount": 6777,
    "currency": "USD",
    "confirm": false,
    
    "customer_id": "CustomerX7777",
    "capture_method": "automatic",
    "capture_on": "2022-09-10T10:11:12Z",
    "authentication_type": "no_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",
        "profile_id": "pro_NP3IjxxHEUdqVoYi6K4c",
"setup_future_usage":"on_session",
    "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"
        }
    }
    
    
}'
```
- Do a PML it will  show the PM
```
{
    "customer_payment_methods": [
        {
            "payment_token": "token_rHJg2C54VdYsdF5ln4SQ",
            "payment_method_id": "pm_EbbMg190tRVUKn9sLVcr",
            "customer_id": "CustomerX7777",
            "payment_method": "card",
            "payment_method_type": null,
            "payment_method_issuer": null,
            "payment_method_issuer_code": null,
            "recurring_enabled": false,
            "installment_payment_enabled": false,
            "payment_experience": [
                "redirect_to_url"
            ],
            "card": {
                "scheme": null,
                "issuer_country": null,
                "last4_digits": "4242",
                "expiry_month": "06",
                "expiry_year": "2030",
                "card_token": null,
                "card_holder_name": "John T",
                "card_fingerprint": null,
                "nick_name": null,
                "card_network": null,
                "card_isin": "424242",
                "card_issuer": null,
                "card_type": null,
                "saved_to_locker": true
            },
            "metadata": null,
            "created": "2024-08-09T14:55:58.379Z",
            "bank": null,
            "surcharge_details": null,
            "requires_cvv": true,
            "last_used_at": "2024-08-09T14:55:58.379Z",
            "default_payment_method_set": true,
            "billing": {
                "address": {
                    "city": "San Fransico",
                    "country": "US",
                    "line1": "1467",
                    "line2": "Harrison Street",
                    "line3": "Harrison Street",
                    "zip": "94122",
                    "state": "California",
                    "first_name": "joseph",
                    "last_name": "Doe"
                },
                "phone": {
                    "number": "8056594427",
                    "country_code": "+91"
                },
                "email": null
            }
        }
    ],
    "is_guest_customer": false
}
```
## Checklist
<!-- Put an `x` in the boxes that apply -->

- [ ] 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
