skip to Main Content

I am using React.JS as Front end and Node.JS as Backend. I have to integrate Stripe Payment Gateway. As per the requirement, I have to show the price based on users location.

For example, if the user is from USA then I have to show $100 but if someone is from Africa then I have to show $50. All the users will be able to use the same content no matter from where they are accessing the website.

The tricky part here is, if someone uses VPN and changes the location then the system should restrict such user.

Any help would be appreciated.

2

Answers


  1. To achieve this functionality and handle VPN users, you can follow these steps:

    • User Location Detection:

    Implement IP geolocation to determine the user’s approximate location based on their IP address. You can use third-party services like MaxMind’s GeoIP2 or IPinfo.io for this purpose.
    Store the user’s location in a user session or a cookie to persist this information.

    • Pricing Management:

    Create a pricing database or configuration in your backend that maps regions or countries to specific prices. For example, you can have a table that associates countries with their respective prices.
    Retrieve the user’s location from the session/cookie and fetch the appropriate price from the pricing database.

    • Stripe Integration:

    Integrate Stripe into your frontend and backend using Stripe’s official libraries. You’ll need to set up products and prices within Stripe that correspond to the different price points you want to offer.

    const express = require('express');
    const app = express();
    
    // Middleware for VPN detection (use a VPN detection service)
    function detectVPN(req, res, next) {
      // Implement VPN detection logic here
      if (isUsingVPN(req.ip)) {
        // Handle VPN user (e.g., restrict access)
        res.status(403).send('VPN usage is not allowed.');
      } else {
        next(); // Continue processing
      }
    }
    
    // Middleware for location-based pricing
    function setLocationBasedPrice(req, res, next) {
      const userLocation = req.cookies.userLocation; // Get user's location from cookie
      const price = getPriceForLocation(userLocation); // Fetch price based on location
    
      req.price = price; // Attach the price to the request object
      next();
    }
    
    app.use(detectVPN); // Apply VPN detection middleware globally
    app.use(setLocationBasedPrice); // Apply location-based pricing middleware globally
    
    // Route for processing payments
    app.post('/checkout', (req, res) => {
      const price = req.price; // Access the price from the request object
    
      // Create a Stripe checkout session with the appropriate price
      // Redirect the user to Stripe checkout
      // Handle payment success and failure
      // ...
    });
    
    app.listen(3000, () => {
      console.log('Server is running on port 3000');
    });
    

    Please note that this is a simplified example, and you’ll need to adapt it to your specific requirements and integrate it with your React.js frontend. Additionally, the VPN detection mechanism may require third-party services or libraries to function effectively.

    Login or Signup to reply.
  2. You can refer to this post, if you want to detect the user’s location and proxy. ipinfo.io offers you to detect location and proxy. I am not sure if this can be implemented in the backend because I couldn’t find the user’s ip from the request param, that is why I have implemented it on the frontend side.

    After accessing user’s ip and making request to the ipinfo.io, you can pass the user’s location while requesting the payment intent from the backend. To create payment intent you can refer to this doc

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search