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(cypress): verify payment status after payment redirection #6187

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

Conversation

Ankesh2004
Copy link
Contributor

Type of Change

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

This pull request Resolve #5915

Description

This PR refactors the verifyReturnUrl function to validate the redirection_url and ensure that the payment status is either succeeded or processing. If the status is invalid, the test will now fail, preventing false positives in redirection tests like 3DS or bank redirects.

###Key Changes:

  • Parse the redirection_url query string to extract the status parameter.
  • Validate the status against the allowed values (succeeded or processing).
  • Fail the test if the status is not one of the expected values.
  • Maintain the existing redirection validation process (e.g., origin matching).

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

This change improves the reliability of Cypress redirection tests by ensuring that the final redirection URL reflects a valid payment status, preventing false positives and ensuring tests behave as expected for flows like 3DS and bank redirects.

How did you test it?

  • Manually tested redirection flows with both successful and failed statuses.
  • Ran existing Cypress tests to ensure no regression in functionality.
    hyperswitch_cypress_test

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

@Ankesh2004 Ankesh2004 requested a review from a team as a code owner October 1, 2024 10:33
Copy link

semanticdiff-com bot commented Oct 1, 2024

Review changes with SemanticDiff.

Analyzed 1 of 1 files.

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

Filename Status
✔️ cypress-tests/cypress/support/redirectionHandler.js 10.81% smaller

Copy link
Member

@pixincreate pixincreate left a comment

Choose a reason for hiding this comment

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

other than that, looks good to me!

cypress-tests/cypress/support/redirectionHandler.js Outdated Show resolved Hide resolved
cypress-tests/cypress/support/redirectionHandler.js Outdated Show resolved Hide resolved
@pixincreate pixincreate added A-CI-CD Area: Continuous Integration/Deployment S-waiting-on-review Status: This PR has been implemented and needs to be reviewed labels Oct 1, 2024
@pixincreate pixincreate changed the title fix[refactor]: verifyurl in redirection handler refactor(cypress): verify payment status after payment redirection Oct 1, 2024
@Ankesh2004
Copy link
Contributor Author

@pixincreate I have made all the suggested changes.

Copy link
Member

@pixincreate pixincreate left a comment

Choose a reason for hiding this comment

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

other than that, looks good to me!

cypress-tests/cypress/support/redirectionHandler.js Outdated Show resolved Hide resolved
Co-authored-by: Pa1NarK  <69745008+pixincreate@users.noreply.github.com>
pixincreate
pixincreate previously approved these changes Oct 2, 2024
Gnanasundari24
Gnanasundari24 previously approved these changes Oct 3, 2024
@Gnanasundari24 Gnanasundari24 self-requested a review October 3, 2024 07:35
@Ankesh2004
Copy link
Contributor Author

Hey @Gnanasundari24 @Sakilmostak , please review the PR now

@pixincreate pixincreate added the hacktoberfest Issues that are up for grabs for Hacktoberfest participants label Oct 7, 2024
Comment on lines 416 to 424
if (statusCode >= 400 && statusCode < 500) {
console.error(`Client Error: ${statusCode} - ${paymentStatus || "Payment status not available"}`);
throw new ClientError(`Client error occurred. Status code: ${statusCode}`);
} else if (statusCode >= 500) {
console.error(`Server Error: ${statusCode} - ${paymentStatus || "Payment status not available"}`);
throw new ServerError(`Server error occurred. Status code: ${statusCode}`);
} else if (!paymentStatus) {
console.error(`Error: Payment status is undefined. Unexpected error.`);
throw new Error(`Payment status is undefined. Unexpected error.`);
Copy link
Member

@pixincreate pixincreate Oct 7, 2024

Choose a reason for hiding this comment

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

From what I understand, this would not work as expected.
The reason being, you will get a 2xx response with a url in next_action and the paymentStatus would be requires_customer_action.
The possibility that @Gnanasundari24 mentioned was about getting a 4xx during redirection. You would not get a status_code over there. For example:

image

But you can definitely see the 4xx or a 5xx when you open inspect tab.

So, you would have either have to throw an error directly like what is done in commit e1f1e44 (#6187) or read what is mentioned in the screen which I do not think is possible in a line or 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check if latest commit resolve it

@gorakhnathy7
Copy link
Collaborator

Hey @Ankesh2004 Kindly look into the comments!

Comment on lines 416 to 417
console.error(`Error: Payment status is undefined. This might indicate a 4xx or 5xx error.`);
throw new Error(`Payment status is undefined. There may have been a client or server error (4xx/5xx). Check network logs or UI.`);
Copy link
Member

Choose a reason for hiding this comment

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

would be great if you could make it take a screenshot when redirection fails.

to do so, you can use cy.screenshot() command.

Comment on lines 420 to 422
if (paymentStatus === 'requires_customer_action') {
console.log(`Payment requires customer action. Proceeding to next step.`);
return;
Copy link
Member

Choose a reason for hiding this comment

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

this is redundant. we get requires_customer_action as the status in the same way we get for any other status. so it will not matter.

}

Copy link
Member

Choose a reason for hiding this comment

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

this and below made changes in the latest commit is also not necessary. can be reverted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pixincreate Done with all reviews corrections. look now

Copy link
Member

@pixincreate pixincreate left a comment

Choose a reason for hiding this comment

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

almost there!

Comment on lines 8 to 21
class ClientError extends Error {
constructor(message) {
super(message);
this.name = "ClientError"; // 4xx errors
}
}

class ServerError extends Error {
constructor(message) {
super(message);
this.name = "ServerError"; // 5xx errors
}
}

Copy link
Member

Choose a reason for hiding this comment

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

this can be deleted as it is redundant now

Comment on lines +443 to +445



Copy link
Member

Choose a reason for hiding this comment

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

you can run prettier . --write once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pixincreate Have a look now

@Ankesh2004
Copy link
Contributor Author

Ankesh2004 commented Oct 14, 2024

@pixincreate please review my comments up there

Copy link
Member

@pixincreate pixincreate left a comment

Choose a reason for hiding this comment

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

you should move your changes to L416. and it should look something like below:

if (redirection_url.host.endsWith(expected_url.host)) {
  cy.wait(WAIT_TIME);
  cy.window()
    .its("location") // after 10 sec wait, the end url will be `hyperswitch.io/status=<payment_status>`
    .then((location) => {
      const url_params = new URLSearchParams(location.search);
      const payment_status = url_params.get("status");
  // now, do the validations
    });
} else {
...

Comment on lines 398 to 399
const urlParams = new URLSearchParams(redirection_url.search);
const paymentStatus = urlParams.get('status');
Copy link
Member

Choose a reason for hiding this comment

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

i tested your changes and it does not work. the reason being, redirection_url is not hyperswitch.io at this moment. a wait of at least 10 sec is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have the wait time and necessary changes . @pixincreate Review it

@pixincreate
Copy link
Member

Hey @Ankesh2004, I request you to revert the latest commit by executing below command in the terminal:

git revert 2429fef

Then, refer to this comment: #6187 (review)

In short,

you'll have to move the validations that is done outside of the conditional statements to inside it i.e., after:

if (redirection_url.host.endsWith(expected_url.host)) {
// add them here

@gorakhnathy7
Copy link
Collaborator

Hey @Ankesh2004 Any update on the changes!

@gorakhnathy7 gorakhnathy7 added the hacktoberfest-accepted Pull requests accepted as Hacktoberfest contributions label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-CI-CD Area: Continuous Integration/Deployment hacktoberfest Issues that are up for grabs for Hacktoberfest participants hacktoberfest-accepted Pull requests accepted as Hacktoberfest contributions 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.

[CYPRESS_FRAMEWORK] Verify URL after redirection
4 participants