How to Get your Visitor's Location from their IP address

Use JavaScript to detect the country and location of your website visitors from their IP address and serve different payment options or offer purchasing power parity.

The PayPal website mentions a list of 200 countries where the PayPal service is officially available. There are about 46 countries and regions left where buyers cannot transact using PayPal.

Countries where PayPal is not available

As highlighted in the Google Map above, the regions where PayPal is not available includes Afghanistan, Bangladesh, Cuba, Ghana, Iraq, Iran, North Korea, Lebanon, Liberia, Libya, Pakistan, Palestine, Sudan, Syria, Turkey and Uzbekistan.

If you have a digital goods store that relies exclusively on the PayPal service for processing payments, you could be losing business as customers from countries like Bangladesh, Turkey or Pakistan would not be able to make payments.

As an alternative, you can sign-up for a non-US payment processing service - Paddle and FastSpring are good alternatives - and offer these as payment options on the checkout screen to customers who land on your website from countries where PayPal is unavailable.

Detect the Country of your Website Visitors

I have implemented a similar technique for my Google add-ons website and it seems to work well. The website uses PayPal and Stripe as the default payment handler but if someone lands from a non-supported country, the PayPal buttons are hidden and they are offered an option to checkout with Paddle.

To get the website visitor’s location, I use the ip2c.org service that quickly resolves the visitor’s IP address to their country. If you fetch the ip2c.org/self service, it returns the ISO code of the country of the computer that made the HTTP request.

const getVisitorCountry = () => {
  return new Promise((resolve, reject) => {
    window
      .fetch('https://ip2c.org/self')
      .then((response) => response.text())
      .then((data) => {
        const [status, country] = String(data).split(';');
        if (status !== '1') {
          throw new Error('Unable to fetch country');
        }
        resolve(country);
      })
      .catch(() => {
        resolve('US');
      });
  });
};

getVisitorCountry().then((country) => {
  if (['PK', 'BD', 'TR', 'AF'].indexOf(country) !== -1) {
    // show Paddle Buttons
  } else {
    // show PayPal buttons
  }
});

Some online stores follow the “Purchasing Power Parity” theory (learn more) where non-tangible goods like video courses and software licenses are priced dynamically depending on the country of customers. The above client-side approach for detecting the visitor’s location can be help in such scenarios as well.

Purchasing Power Parity

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻