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

feat(core): add localization support for unified error messages #5624

Merged
merged 17 commits into from
Aug 21, 2024

Conversation

srujanchikke
Copy link
Contributor

@srujanchikke srujanchikke commented Aug 14, 2024

Type of Change

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

Description

Add localization support for unifed error messages.

Implementation:

  • Added a table with columns unified_code, unified_message, locale and translation.
    • primary key - unified_code, unified_message, locale
    • all fields are strings
  • consume locale and update the payment_attempt.unified_message with translation by fetching it from unified_translation table.
  • Pass unified_message to payment links and display it in status page incase of error.
  • Data migrations need to be run for translations table.

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?

step1 : Run data migrations after deployment . Make sure all unified codes and unified messages have their translations in all available languages.
step2: create a payment link with adyen as connector. Can vary the locale in headers .

curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Accept-Language: es' \
--header 'api-key: dev_JtGwVigtQWXswRY3cQa23eopizFmipPJuoF07EnJptXA98ZyY3TKGDHxFBxoA2ij' \
--data-raw '{
    "amount":5000,
    "currency": "USD",
    "confirm": false,
    "payment_link" : true,
    "capture_on": "2029-09-10T10:11:12Z",
    "amount_to_capture": 5000,
    "email": "[email protected]",
    "name": "John Doe",
    "phone": "999999999",
    "phone_country_code": "+1",
    "description": "Its my first payment request",
    "authentication_type": "three_ds",
    "return_url": "https://google.com",
    "payment_method_data": {
        "card": {
            "card_number": "4242424242424242",
            "card_exp_month": "03",
            "card_exp_year": "25",
            "card_holder_name": "[email protected]",
            "card_cvc": "737",
            "card_network": "Visa",
            "card_type": null,
            "card_issuing_country": null,
            "bank_code": null,
            "nick_name" : "srujan"
        },
        "billing": {
            "address": {
                "line1": "1467",
                "line2": "CA",
                "line3": "CA",
                "city": "San Fransico",
                "state": "California",
                "zip": "94122",
                "country": "NL",
                "first_name": "joseph",
                "last_name": "Doe"
            },
            "phone": {
                "number": "8056594427",
                "country_code": "+91"
            }
        }
    },
    "billing": {
            "address": {
                "line1": "1467",
                "line2": "CA",
                "line3": "CA",
                "city": "San Fransico",
                "state": "California",
                "zip": "94122",
                "country": "US",
                "first_name": "joseph",
                "last_name": "Doe"
            },
            "phone": {
                "number": "8056594427",
                "country_code": "+91"
            }
        },
    "payment_method": "card",
    "payment_method_type" :"credit",
    
    "customer_acceptance": {
        "acceptance_type": "online",
        "accepted_at": "2026-07-04T10:59:42.232Z",
        "online": {
            "ip_address": null,
            "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"
        }
    },
    "browser_info": {
        "color_depth": 24,
        "java_enabled": true,
        "java_script_enabled": true,
        "language": "en-GB",
        "screen_height": 720,
        "screen_width": 1280,
        "time_zone": -330,
        "ip_address": "208.127.127.193",
        "accept_header": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"
    },
    "shipping": {
        "address": {
            "line1": "1467",
            "line2": "CA",
            "line3": "CA",
            "city": "San Fransico",
            "state": "California",
            "zip": "94122",
            "country": "NL",
            "first_name": "joseph",
            "last_name": "Doe"
        },
        "phone": {
            "number": "8056594427",
            "country_code": "+91"
        }
    },
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    },
    
    "order_details": [
        {
            "product_name": "Tea",
            "quantity": 1,
            "amount": 110,
            "product_img_link": "https://thumbs.dreamstime.com/b/indian-tea-spices-masala-chai-33827904.jpg"
        },
        {
            "product_name": "Tea",
            "quantity": 1,
            "amount": 110,
            "product_img_link": "https://thumbs.dreamstime.com/b/indian-tea-spices-masala-chai-33827904.jpg"
        },
        {
            "product_name": "Tea",
            "quantity": 1,
            "amount": 110,
            "product_img_link": "https://thumbs.dreamstime.com/b/indian-tea-spices-masala-chai-33827904.jpg"
        }
    ]
}'

step3 : create failed payment with random card and expiry. error code and error message should display in preferred locale.
Screenshot 2024-08-20 at 12 43 31 PM

step4 : payments retrieve (pass locale in headers)

curl --location 'http://localhost:8080/payments' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Accept-Language: zh' \
--header 'api-key: snd_efbc4c35649f45b2ac283bb2c1609730' \
--data-raw '{
    "amount":8100,
    "currency": "USD",
    "confirm": true,
    "capture_method": "automatic",
    "capture_on": "2022-09-10T10:11:12Z",
    "amount_to_capture": 8100,
    "customer_id": "123sdfadf",
    "email": "[email protected]",
    "name": "John Doe",
    "phone": "999999999",
    "phone_country_code": "+1",
    "description": "Its my first payment request",
    "authentication_type": "no_three_ds",
    "return_url": "https://google.com",
    "payment_method": "card",
    "payment_method_type": "credit",
    "payment_method_data": {
        "card": {
            "card_number": "424242424242",
            "card_exp_month": "02",
            "card_exp_year": "2026",
            "card_holder_name": "joseph",
            "card_cvc": "3723"
        }
    },
    "billing": {
        "address": {
            "line1": "1467",
            "line2": "CA",
            "line3": "CA",
            "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": "CA",
            "line3": "CA",
            "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,
        "ip_address": "127.2.2.0",
        "screen_height": 723,
        "screen_width": 1536,
        "time_zone": 0,
        "java_enabled": true,
        "java_script_enabled": true
    },
    "statement_descriptor_name": "joseph",
    "statement_descriptor_suffix": "JS",
    
    "metadata": {
        "udf1": "value1",
        "new_customer": "true",
        "login_date": "2019-09-10T10:11:12Z"
    }
}'
Screenshot 2024-08-21 at 11 39 13 AM Screenshot 2024-08-21 at 11 38 27 AM

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

@hyperswitch-bot hyperswitch-bot bot added the M-database-changes Metadata: This PR involves database schema changes label Aug 14, 2024
@srujanchikke srujanchikke self-assigned this Aug 14, 2024
@srujanchikke srujanchikke marked this pull request as ready for review August 20, 2024 06:07
@srujanchikke srujanchikke requested review from a team as code owners August 20, 2024 06:07
@srujanchikke srujanchikke added the A-core Area: Core flows label Aug 20, 2024
kashif-m
kashif-m previously approved these changes Aug 20, 2024
}

impl UnifiedTranslations {
pub async fn find(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub async fn find(
pub async fn find_by_unified_code_unified_message_locale(

)
.await
}
pub async fn retrieve_translation(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer is this was moved to the db module, you can only keep the unique database queries here.

.map(|item| item.translation)
}

pub async fn update(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indicate what fields are used to update / delete: update_by_field1_field2 etc.

You also don't need translation to search for the record, right?

Default,
)]
#[diesel(table_name = unified_translations)]
pub struct UnifiedTranslationsUpdateInternal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add modified_at as a mandatory field in UnifiedTranslationsUpdateInternal.

Comment on lines 4720 to 4723
"Translation missing for unified_code - {}, unified_message - {:?}, locale - {:?}",
unified_code,
unified_message,
locale
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either use Debug impl for all or Display impl for all.

&self,
_translation: storage::UnifiedTranslationsNew,
) -> CustomResult<storage::UnifiedTranslations, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a task item in #172.

Comment on lines 1 to 3
-- Your SQL goes here
-- Your SQL goes here
-- Tables
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove these comments.

CREATE TABLE IF NOT EXISTS unified_translations (
unified_code VARCHAR(255) NOT NULL,
unified_message VARCHAR(1024) NOT NULL,
locale VARCHAR(255) NOT NULL DEFAULT 'en',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure providing a default from database side is a good idea, better to populate the default value from the application, if we absolutely need to.

@srujanchikke srujanchikke requested a review from a team as a code owner August 20, 2024 15:01
@srujanchikke srujanchikke added the S-waiting-on-review Status: This PR has been implemented and needs to be reviewed label Aug 20, 2024
SanchithHegde
SanchithHegde previously approved these changes Aug 20, 2024
#[max_length = 1024]
translation -> Varchar,
created_at -> Timestamp,
last_modified -> Timestamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
last_modified -> Timestamp,
last_modified_at -> Timestamp,


#[derive(Debug)]
pub struct UnifiedTranslationsUpdate {
pub unified_code: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need not update unified_code, unified_message and locale. Can we remove it from here is not required?

@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE unified_translations;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DROP TABLE unified_translations;
DROP TABLE IF EXISTS unified_translations;

@likhinbopanna likhinbopanna added this pull request to the merge queue Aug 21, 2024
Merged via the queue into main with commit 1f0ee3c Aug 21, 2024
13 of 14 checks passed
@likhinbopanna likhinbopanna deleted the error_message_localization branch August 21, 2024 13:00
@pixincreate pixincreate removed the S-waiting-on-review Status: This PR has been implemented and needs to be reviewed label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core flows M-database-changes Metadata: This PR involves database schema changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants