Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from DarkSorrow/fix/payment-location-detection
Fix payment selection for non HK user
- Loading branch information
Showing
7 changed files
with
76 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,37 @@ | ||
/** | ||
* Parse the accepted language header and check if the first languages are from HK | ||
* @param {string|undefined} acceptedLanguage Browser accepted language header | ||
* @returns {string|undefined} | ||
*/ | ||
function detectHKBrowserLang(acceptedLanguage) { | ||
if (acceptedLanguage) { | ||
const langs = acceptedLanguage.split(','); | ||
const len = langs.length > 3 ? 3 : langs.length; // only three first browser language are important | ||
for (let i = 0; i < len; i += 1) { | ||
const pair = langs[i].split(';'); | ||
if (pair[0].endsWith('-HK')) { | ||
// This check if the language is for HK region | ||
return 'HK'; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
export default ({ app, store, req, res, query }) => { | ||
// gcloud magic geoip headers | ||
// https://cloud.google.com/appengine/docs/standard/go/reference/request-response-headers#app_engine-specific_headers | ||
const ipCountry = req.headers['x-appengine-country']; | ||
const ipCity = req.headers['x-appengine-city']; | ||
const cacheServerName = req.headers['x-forwarded-server'] || ''; | ||
if ((ipCountry && ipCountry !== 'ZZ') || ipCity || cacheServerName) { | ||
const isHK = | ||
ipCountry === 'HK' || | ||
ipCity === 'hong kong' || | ||
cacheServerName.includes('HKG'); | ||
store.dispatch('setIsHK', isHK); | ||
// const cacheServerName = req.headers['x-forwarded-server'] || ''; | ||
const browserLanguage = detectHKBrowserLang(req.headers['accept-language']); // browser language workaround | ||
// Check if country is not unknown otherwise fallback to language only check if it's from HK and last is webcall | ||
if (ipCountry !== 'ZZ') { | ||
store.dispatch( | ||
'setIsHK', | ||
ipCountry === 'HK' || browserLanguage === 'HK' || ipCity === 'hong kong' | ||
); | ||
} else if (browserLanguage === 'HK') { | ||
store.dispatch('setIsHK', true); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters