Samir Tendulkar
Samir Tendulkar

Reputation: 1191

Making Stripe User Checkout Email as {{ request.user.email }} in Django project

Hi Djangonauts, I am integrating stripe in my in my project. I do not want users to enter their email in their stripe payment form. Instead I want the email they registered on my site as their checkout email. I have the below form. When this form renders. It asks for users.

Name:, Email:, Billing Address:, Credit Card Details:,

Can I change email from

email = request.POST['stripeEmail'] 

to

If user.is_authenticated:
     email = request.user.email

I am aware that by doing this anonymous users will not be able to checkout and I am ok with that. I can add @loginrequired() decorator before the function

I have Order history view

@login_required()
def orderHistory(request):
    if request.user.is_authenticated:
        email = str(request.user.email)
        order_details = Order.objects.filter(emailAddress=email)        
    return render(request, 'order/order_list.html', {'order_details': order_details})

This code fetches Orders in Order History order_details = Order.objects.filter(emailAddress=email) when users sign up with 1 email and use another email at checkout the orders don't appear in their Order history. Plus it is mandatory to have a account to checkout that's why I needed the below

Below are the views.py of my shopping cart

def cart_detail(request, total=0, counter=0, cart_items=None):
    try:
        cart = Cart.objects.get(cart_id=_cart_id(request))
        cart_items = CartItem.objects.filter(cart=cart, active=True)
        for cart_item in cart_items:
            total += (cart_item.tasting.price * cart_item.quantity)
            counter += cart_item.quantity
    except ObjectDoesNotExist:
        pass

    stripe.api_key = settings.STRIPE_SECRET_KEY
    stripe_total = int(total * 100)
    description = 'Khal: Share your recipes - New tasting request'
    data_key = settings.STRIPE_PUBLISHABLE_KEY
    if request.method == 'POST':
        # print(request.POST)
        try:
            token = request.POST['stripeToken']
            email = request.POST['stripeEmail']
            billingName = request.POST['stripeBillingName']
            billingAddress1 = request.POST['stripeBillingAddressLine1']
            billingCity = request.POST['stripeBillingAddressCity']
            billingZipcode = request.POST['stripeBillingAddressZip']
            billingCountry = request.POST['stripeBillingAddressCountryCode']
            customer = stripe.Customer.create(
                email=email,
                source=token
            )
            charge = stripe.Charge.create(
                amount=stripe_total,
                currency='usd',
                description=description,
                customer=customer.id,
            )
            '''Creating the Order'''
            try:
                order_details = Order.objects.create(
                    token=token,
                    total=total,
                    emailAddress=email,
                    billingName=billingName,
                    billingAddress1=billingAddress1,
                    billingCity=billingCity,
                    billingZipcode=billingZipcode,
                    billingCountry=billingCountry,

                )
                order_details.save()
                for order_item in cart_items:
                    oi = OrderItem.objects.create(
                        tasting=order_item.tasting.post.title,
                        quantity=order_item.quantity,
                        price=order_item.tasting.price,
                        order=order_details
                    )
                    oi.save()
                    '''Reduce stock when Order is placed or saved'''
                    tastings = Tasting.objects.get(id=order_item.tasting.id)
                    tastings.stock = int(order_item.tasting.stock - order_item.quantity)
                    tastings.save()
                    order_item.delete()
                    '''The terminal will print this message when the order is saved'''
                    print('The order has been created')
                    try:
                        '''*********************Calling the sendEmail function************************************'''
                        sendEmail(order_details.id)
                        print('The order email has been sent to the customer.')
                    except IOError as e:
                        return e
                return redirect('order:thanks', order_details.id)
            except ObjectDoesNotExist:
                pass
        except stripe.error.CardError as e:
            return False,e
    return render(request, 'cart/cart.html', dict(cart_items=cart_items, total=total, counter=counter, data_key=data_key,
                                             stripe_total=stripe_total, description=description))

I have also attached below my cart.html template

<form action="" method="POST">
    {% csrf_token %}
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="{{ data_key }}"
    data-amount="{{ stripe_total }}"
    data-name="Perfect Cushion Store"
    data-description="{{ description }}"
    data-image="{% static 'images/logo.png' %}"
    data-locale="auto"
    data-currency="usd"
    data-shipping-address="true"
    data-billing-address="true"
    data-zip-code="true">
  </script>
</form>

Upvotes: 2

Views: 742

Answers (1)

Horai Nuri
Horai Nuri

Reputation: 5578

I believe you can hide email by including data-email into the stripe script :

cart.html:

<form action="" method="POST">
    {% csrf_token %}
    <script ... data-email="{{request.user.email}}"></script>
</form>

You could then retrieve stripeEmail or directly use request.user.email in your view.

Upvotes: 2

Related Questions