在Django项目中以{{request.user.email}}将Stripe用户检出电子邮件

时间:2018-07-14 22:09:55

标签: django django-templates django-views stripe-payments

嗨,Djangonauts,                    我正在将条纹集成到我的项目中。我不希望用户在条纹付款表格中输入他们的电子邮件。相反,我希望他们在我的网站上注册的电子邮件作为结帐电子邮件。我有以下表格。当此表单呈现时。它要求用户。

名称:, 电子邮件:, 帐单地址:, 信用卡详细信息:

我可以从中更改电子邮件吗

email = request.POST['stripeEmail'] 

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

我知道通过这样做,匿名用户将无法结帐,对此我也很满意。我可以在函数之前添加@loginrequired()装饰器

我有“订单历史记录”视图

@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})

当用户使用1封电子邮件进行注册并在结帐时使用另一封电子邮件时,此代码将提取订单历史记录order_details = Order.objects.filter(emailAddress=email)中的订单,订单不会出现在其订单历史记录中。另外,必须要有一个帐户才能结帐,这就是为什么我需要以下内容

下面是我的购物车的 views.py

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))

我还在我的 cart.html模板

下方附加了
<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>

1 个答案:

答案 0 :(得分:1)

我相信您可以通过在条纹脚本中加入data-email来隐藏电子邮件:

cart.html:

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

然后您可以在视图中检索stripeEmail或直接使用request.user.email