Beta

Gateway payments redirect - Refactoring the code

Description:

You started working for a new company, the company mainly sells merchandise offline but they have an e-commerce website too for their domestic market.

The e-commerce platform is full of legacy, ancient code and it relies on a third party gateway for online payments, when an user clicks on the "Buy" button it redirects him/her to the payment system with a different URL.

The function accepts 2 string arguments, the first one is the domain of the server, while the second is the ID of the item. To simplify things back then the developer merged the item price with the item id separated by a dash -.

The URL is structured by different pieces, it will always be on a https protocol, it should contain an username and password separated by a colon :, the domain name followed by a trailing slash and as subsequently 3 query parameters itemid, amount and codecurrency. Remember the query parameters are separated by an ampersand & and the query values are separated from the query keys by an equal =.

So given the following details:

Gateway payment Username: codewars
Gateway payment Password: myfancypassword
Gateway payment Domain: paymentserver.codewars.com
Item ID: 123456
Amount to pay: $100

The user should be redirected to:

https://codewars:myfancypassword@paymentserver.codewars.com/?itemid=123456&amount=100&codecurrency=USD

You had an interesting meeting about the e-commerce website, seems the marketing team wants to open up for the overseas market and start trading in different countries.

In conjuction to this the e-commerce core developer suggest some changes that might affect the functionality of the redirect function as well as the fact currently the redirects on the server3 does not work at all (you might want to take a look and fix that).

He suggested it might be better to pass to the function two different objects with all the details of the server and of the item instead of simple strings as he feels the current solution is horribly wrong and you agree with him after looking at the code on the file and the general mess the previous developer did before leaving the job.

The objective of this kata is to teach you how to understand other's people code, their mistakes and how to make it more small, elegant and efficient. Remember about readability of your code as well as a consistent naming convention between variables and functions, you are coding for other developers, not for the machine only.

Also future-proofing is a must as well as backward compatibility, as you might want the code to be ready when the new e-commerce backend will be deployed.

Performance
Refactoring

Stats:

CreatedFeb 10, 2016
PublishedFeb 10, 2016
Warriors Trained52
Total Skips0
Total Code Submissions218
Total Times Completed11
JavaScript Completions11
Total Stars5
% of votes with a positive feedback rating83% of 6
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes1
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • luigimannoni Avatar
Ad