'how do I make a page only to be accessed through redirection from a stripe checkout session page, in django?

I want a page (view) to be only accessed through redirection from stripe checkout session page, and not accessible in any other way.

Here is the page that I want to only be accessed through redirection from the stripe checkout session page:

   def successView(request):
       data = cartData(request)
       cartItems = data['cartItems']

       context = {'cartItems':cartItems}

       return render(request, 'store/success.html', context)

Here is the stripe checkout session page that redirects to the page:

class CreateCheckoutSessionView(View):
def post(self, request, *args, **kwargs):
    data = cartData(request)
    cartItems = data['cartItems']
    order = data['order']
    items = data['items']

    line_items_list=[]

    for item in items:
        quantity=item['quantity']
        price_stripe=item['product']['stripe-price']
        line_items_list.append(
            {
                'price': price_stripe,
                'quantity': quantity,
                
            }
            )
    
    checkout_session = stripe.checkout.Session.create(
        payment_method_types=['card'],
        shipping_address_collection={'allowed_countries': ['DK']},
        line_items=line_items_list,
        automatic_tax={
            'enabled': True,
        },
        mode="payment",
        success_url=YOUR_DOMAIN + 'success/',
        cancel_url=YOUR_DOMAIN + 'kurv/'
    )

    return redirect(checkout_session.url)


Solution 1:[1]

Of course, there can be many solutions and you can find them yourself. But to find a solution, you need to understand what your server knows about the visitor redirected from the Stripe checkout page. You can use two parameters to determine if a user has returned to your site after redirecting from the Stripe checkout page:

  • URL parameter session_id of the GET request;
  • HTTP header Referer.

With these hints, you can turn on your creativity and implement, for example:

  • checking if a session with session_id exists;
  • write into .htaccess file some allow / deny / redirecting rules based on Referrer value.

Good luck!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 seeker