def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create( order=order, product=item['product'], price=item['price'], quantity=item['quantity'] ) # clear the cart logger.info("Clearing cart after successful post") cart.clear() logger.info(f"Launching asynchronous email task for order {order.id}") order_created.delay(order.id) # set order in the session request.session['order_id'] = order.id # redirect for payment return redirect(reverse('payment:process')) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def checkout(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) # clear the cart cart.clear() # launch asynchronous task order_created.delay(order.id) # set the order in the session request.session['order_id'] = order.id # redirect for payment return redirect(reverse('payment:process')) else: form = OrderCreateForm() context = {'cart': cart, 'form': form} return render(request, 'orders/checkout.html', context)
def charge(request, order_id): order = get_object_or_404(Order, id=order_id) if request.method == "POST": form = SalePaymentForm(request.POST) if form.is_valid(): # charges the card try: customer = stripe.Charge.create( amount=int(order.get_total_cost() * 100), currency="USD", card=form.cleaned_data['stripe_id']) order.paid = True order.save() order_created.delay(order.id) return render(request, 'orders/order/created.html', {'order': order}) except stripe.CardError as ce: messages.error(request, "Your card was declined!") else: form = SalePaymentForm() args = { 'form': form, 'publishable': settings.STRIPE_PUBLISHABLE, 'order': order } args.update(csrf(request)) return render(request, 'payment/payment.html', args)
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() # launch asynchronous task order_created.delay(order.id) # set the order in the session request.session['order_id'] = order.id # redirect for payment return redirect(reverse('payment:process')) form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def payment_done(request): """ 处理支付成功的view :param request: :return: """ from cart.cart import Cart from orders.tasks import order_created # 后台自动发送邮件 order_created.delay(request.session['order_id']) return render(request, 'payment/done.html')
def form_valid(self, form): self.order = form.save() cart = Cart(self.request) for item in cart: OrderItem.objects.create(order=self.order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() # launch asynchronous task, only work if worker is running order_created.delay(self.order.id) # set the order in the session # return render(self.request, 'orders/order/created.html', {'order': self.order}) # redirect to the payment (paypal) self.request.session['order_id'] = self.order.id return redirect(reverse('payment:process'))
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() order_created.delay(order.id) request.session['order_id'] = order.id return redirect(reverse('payment:process')) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def order_create(request): """ """ cart = Cart(request) if request.method == "POST": form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) # the last attr are fields in each databases # so we're just trying to assign store 'fields' :D if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() # The docs says this # "Apply tasks asynchronously by sending a message." order_created.delay(order.id) # Hmm, interesting request.session['order_id'] = order.id # The old is just rendering a (fake, sort-of) page return redirect(reverse('payment:process')) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', { 'cart': cart, 'form': form })
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) # Clear the cart cart.clear() # launch asynchronous task order_created.delay(order.id) return render(request, 'orders/order/created.html', {'order': order}) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def payment_process(request): order_id = request.session.get('order_id') order = get_object_or_404(Order, id=order_id) total_cost = order.get_total_cost() print("musterinin odedigi miktari gosteriyor dogru mu diye kontrol et") print(total_cost) print("order delivery fee") print(order.delivery_fee()) if request.method == 'POST': print(request.POST) token = request.POST.get('stripeToken') print("token burda") print(token) result = stripe.Charge.create( #amount=100, amount=int(total_cost * 100), currency="usd", source=token, description="Payment for Sam&M Trade", ) print("result burda") print(result) if result.status == "succeeded": print("yeaaaaaah!!!!") # mark the order as paid order.payment = True # store the unique transaction id order.braintree_id = result.id order.save() total_price = [] for i in request.session["cart"].keys(): for z in request.session["cart"][i]: if z == "total_price": total_price.append(request.session["cart"][i][z]) product = [] for i in request.session["cart"].keys(): for z in request.session["cart"][i]: if z == "product": product.append(request.session["cart"][i][z]) quantity = [] for i in request.session["cart"].keys(): for z in request.session["cart"][i]: if z == "quantity": quantity.append(request.session["cart"][i][z]) #cart'in icindekileri yularidaki for loop'lar sayaesinde zip yaptim. sonrada bu zipi template'de kullandim #super_list'in icine cartta ne varsa koyabilirsin ve goresellestirebilirsin super_list = zip(product, quantity, total_price) html_message_for_customer = loader.render_to_string( 'order_confirmation_email_template.html', { 'user_name': request.user.company_name, 'subject': "Order Confirmation #" + str(order.id), 'content': "Thank you for your order! Your order number is #" + str(order.id) + ".", "order_no": order.id, "order_total": order.order_total, 'super_list': order.items.all(), "address": order.address, "postal_code": order.postal_code, "city": order.city, "delivery_fee": order.delivery_fees, 'delivery_method': order.delivery_method, 'time': order.created, 'discounted_amount': order.discounted_amount, 'off': order.campaign.campaign_discount }) total_price = [] for i in request.session["cart"].keys(): for z in request.session["cart"][i]: if z == "total_price": total_price.append(request.session["cart"][i][z]) product = [] for i in request.session["cart"].keys(): for z in request.session["cart"][i]: if z == "product": product.append(request.session["cart"][i][z]) quantity = [] for i in request.session["cart"].keys(): for z in request.session["cart"][i]: if z == "quantity": quantity.append(request.session["cart"][i][z]) #cart'in icindekileri yularidaki for loop'lar sayaesinde zip yaptim. sonrada bu zipi template'de kullandim #super_list'in icine cartta ne varsa koyabilirsin ve goresellestirebilirsin super_list = zip(product, quantity, total_price) html_message_for_admins = loader.render_to_string( 'new_order.html', { 'user_name': order.company_name, 'subject': "New Order by #" + str(order.company_name), 'content': "New Order", "order_no": order.id, "order_total": order.order_total, 'time': order.created, 'super_list': order.items.all(), "address": order.address, "postal_code": order.postal_code, "city": order.city, 'delivery_method': order.delivery_method, "delivery_fee": order.delivery_fees, 'discounted_amount': order.discounted_amount, 'off': order.campaign.campaign_discount }) # launch asynchronous task order_created.delay(order.id, html_message_for_customer) inform_admins.delay(order.id, html_message_for_admins) return redirect('payment:done') else: return redirect('payment:canceled') else: # generate token #client_token = gateway.client_token.generate() return render(request, 'process.html', { 'order': order, 'publish_key': STRIPE_PUBLIC_KEY })