【发布时间】:2018-12-22 22:26:54
【问题描述】:
嗨,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>
【问题讨论】:
-
我不明白为什么你不能记住给经过身份验证的用户的电子邮件一个条带令牌以便他可以付款。
-
@NuriKatsuki 我在问题中添加了我的订单历史记录视图。这就是我想做这些改变的原因
-
你的问题太宽泛了,你问是否可以将'stripeEmail'换成另一个,答案是肯定的。你的问题到底是什么?
-
@NuriKatsuki 很抱歉,不知道
email是否是条纹表单中的必填字段。如果 stripe 坚持让用户输入他们自己的电子邮件。我只希望电子邮件成为一个预先填充的字段,其中包含在我的网站上注册的用户的电子邮件。这将帮助我将订单链接到用户资料。请参阅上面的订单历史记录代码。如果不是,我将承担用户输入与他们在我的网站上注册时使用的电子邮件不同的电子邮件的风险。在这种情况下,订单将不会显示在他们的个人资料中
标签: django django-templates django-views stripe-payments