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

refactor(payment_methods): refactor customer payment methods list v2 code to follow better code practices #6433

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

SanchithHegde
Copy link
Member

Type of Change

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

Description

This PR includes refactors around the customer payment methods list v2 code to follow some of the practices we've started enforcing in the recent few months. Additionally, I've picked up some minor refactors along the way.

This PR contains the following changes:

  • Adds a should_collect_cvv_during_payment column to the business profile v2 table to replace the {merchant_id}_requires_cvv config we used to read previously (and still do in v1) from the configs table.
  • Renames the business_profile field to profile in the SavedPMLPaymentsInfo struct. (This follows the changes done in refactor: Rename business profile to profiles in api, diesel, domain, interface and error types #5877.)
  • Renames the requires_cvv field to collect_cvv_during_payment in SavedPMLPaymentsInfo struct, to follow the new column added in the business profile v2 table.
  • Passes profile to the form_payments_info() method instead of fetching it within the method. Eventually, we'd want to obtain the profile from the authentication layer instead, which would be passed forward.
  • Moves the PaymentsMandateReference from the router crate to the diesel_models crate, and updates the connector_mandate_details field in the payment methods table to use PaymentsMandateReference instead of serde_json::Value in v2 code. (This should help reduce / prevent invalid data being stored in this column of the database.)
  • Refactors the get_mca_status() function to avoid a possible database call (to fetch merchant connector accounts associated with a merchant / profile) happening within an iterator.
  • Drops the metadata column from the payment methods table and API models in v2 code, since it isn't being actively used in v1 code anyway. Neither do any of our hosted environments have any useful data stored in that column.

The other minor refactors I took up along the way include:

  • Improves the doc comment for the is_network_tokenization_enabled field in profile API models.
  • Update the ExtendedCardInfoUpdate, ConnectorAgnosticMitUpdate and NetworkTokenizationUpdate enum variants of the ProfileUpdate domain model enum to have required fields instead of optional ones. (This is because these variants either enable something or not, and we always have the value to be updated in all cases.)
  • Uses serialize_and_set_key_with_expiry() when storing payment method token in Redis instead of explicit JSON serialization happening in business logic.
  • Replace usages of create_payment_method() (which only seemed to be updating the metadata field, and was thus confusing) with a correct apply_changeset() implementation. This change only affects MockDb implementation.

The PR can be reviewed one commit at a time, if that helps ease reviews.

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables
  • This PR adds a new column in the business profile v2 database table and corresponding field in the business profile v2 API models.
  • This PR drops the metadata column from the payment methods v2 database table and corresponding payment methods v2 API models.

Motivation and Context

This PR contains only a subset of the changes / improvements that can help us in arriving at the final implementation of the customer payment methods list v2 API. There are still a few more refactors pending, which would be taken up in subsequent PRs.

How did you test it?

Haven't been able to test these changes, there are still a couple of panics remaining in the customer payment methods list v2 code.

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

…ng_payment` in `SavedPMLPaymentsInfo` and populate it from profile
…Value` for `connector_mandate_details` field
…when storing payment method token in Redis
…` with a correct `apply_changeset()` implementation
… not actively being used in v1

There does not seem to be any useful information in this column in any of our hosted environments either.
@SanchithHegde SanchithHegde added S-waiting-on-review Status: This PR has been implemented and needs to be reviewed M-database-changes Metadata: This PR involves database schema changes A-payment-methods Area: Payment Methods C-refactor Category: Refactor M-api-contract-changes Metadata: This PR involves API contract changes api-v2 labels Oct 24, 2024
@SanchithHegde SanchithHegde added this to the October 2024 Release milestone Oct 24, 2024
@SanchithHegde SanchithHegde self-assigned this Oct 24, 2024
Copy link

semanticdiff-com bot commented Oct 24, 2024

Review changes with SemanticDiff.

Analyzed 21 of 25 files.

Overall, the semantic diff is 18% smaller than the GitHub diff.

File Information
Filename Status
v2_migrations/2024-08-28-081721_add_v2_columns/down.sql Unsupported file format
v2_migrations/2024-08-28-081721_add_v2_columns/up.sql Unsupported file format
v2_migrations/2024-08-23-112510_payment_methods_v2_db_changes/down.sql Unsupported file format
v2_migrations/2024-08-23-112510_payment_methods_v2_db_changes/up.sql Unsupported file format
✔️ crates/router/src/types/payment_methods.rs Analyzed
✔️ crates/router/src/types/storage/payment_method.rs Analyzed
✔️ crates/router/src/types/api/admin.rs Analyzed
✔️ crates/router/src/routes/payment_methods.rs Analyzed
✔️ crates/router/src/db/payment_method.rs 37.74% smaller
✔️ crates/router/src/core/admin.rs Analyzed
✔️ crates/router/src/core/payment_methods.rs 19.09% smaller
✔️ crates/router/src/core/payments.rs 93.26% smaller
✔️ crates/router/src/core/payments/tokenization.rs 1.04% smaller
✔️ crates/router/src/core/payments/operations/payment_response.rs Analyzed
✔️ crates/router/src/core/payment_methods/cards.rs 19.05% smaller
✔️ crates/router/src/core/payment_methods/transformers.rs Analyzed
✔️ crates/hyperswitch_domain_models/src/business_profile.rs 6.81% smaller
✔️ crates/hyperswitch_domain_models/src/payment_methods.rs Analyzed
✔️ crates/diesel_models/src/business_profile.rs Analyzed
✔️ crates/diesel_models/src/payment_method.rs 12.13% smaller
✔️ crates/diesel_models/src/schema_v2.rs Analyzed
✔️ crates/api_models/src/admin.rs 45.91% smaller
✔️ crates/api_models/src/payment_methods.rs Analyzed
✔️ api-reference-v2/openapi_spec.json 33.42% smaller
✔️ api-reference/openapi_spec.json 47.42% smaller

@SanchithHegde SanchithHegde marked this pull request as ready for review October 25, 2024 06:00
@SanchithHegde SanchithHegde requested review from a team as code owners October 25, 2024 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-payment-methods Area: Payment Methods api-v2 C-refactor Category: Refactor M-api-contract-changes Metadata: This PR involves API contract changes M-database-changes Metadata: This PR involves database schema changes S-waiting-on-review Status: This PR has been implemented and needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant