def checkout(request): stripe_public_key = settings.STRIPE_PUBLIC_KEY stripe_secret_key = settings.STRIPE_SECRET_KEY if request.method == 'POST': cart = request.session.get('cart', {}) form_data = { 'full_name': request.POST['full_name'], 'email': request.POST['email'], 'phone_number': request.POST['phone_number'], 'country': request.POST['country'], 'postcode': request.POST['postcode'], 'town_or_city': request.POST['town_or_city'], 'street_address1': request.POST['street_address1'], 'street_address2': request.POST['street_address2'], 'county': request.POST['county'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save(commit=False) pid = request.POST.get('client_secret').split('_secret')[0] order.stripe_pid = pid order.original_cart = json.dumps(cart) order.save() for item_id, item_data in cart.items(): try: service = Services.objects.get(id=item_id) for date, quantity in item_data['items_by_date'].items(): order_line_item = OrderLineItem( order=order, service=service, quantity=quantity, service_date=date, ) order_line_item.save() except Services.DoesNotExist: messages.error(request, ("One of the services in your cart\ wasn't found in our database." "Please call us for assistance!")) order.delete() return redirect(reverse('view_cart')) request.session['save_info'] = 'save-info' in request.POST return redirect( reverse('checkout_success', args=[order.order_number])) else: messages.error( request, 'There was an error with your form. \ Please double check your information.') else: cart = request.session.get('cart', {}) if not cart: messages.error(request, "There's nothing in your cart at the moment") return redirect(reverse('services')) current_cart = cart_contents(request) total = current_cart['grand_total'] stripe_total = round(total * 100) stripe.api_key = stripe_secret_key intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURRENCY, ) if request.user.is_authenticated: try: profile = UserProfile.objects.get(user=request.user) order_form = OrderForm( initial={ 'full_name': profile.default_full_name, 'email': profile.user.email, 'phone_number': profile.default_phone_number, 'country': profile.default_country, 'postcode': profile.default_postcode, 'town_or_city': profile.default_town_or_city, 'street_address1': profile.default_street_address1, 'street_address2': profile.default_street_address2, 'county': profile.default_county, }) except UserProfile.DoesNotExist: order_form = OrderForm() else: order_form = OrderForm() if not stripe_public_key: messages.warning( request, 'Stripe public key is missing. \ Did you forget to set it in your environment?') template = 'checkout/checkout.html' context = { 'order_form': order_form, 'stripe_public_key': stripe_public_key, 'client_secret': intent.client_secret, } return render(request, template, context)
def checkout(request): """ This code was highly inspired from Code Institute: Boutique Ado. It renders checkout page with form to fill in. When user are posting the information. Informationg from form in the page are fetched and stripe is contacted for payment. Input: request (object): The HttpRequest object """ stripe_public_key = settings.STRIPE_PUBLIC_KEY stripe_secret_key = settings.STRIPE_SECRET_KEY if request.method == 'POST': cart = request.session.get('cart', {}) cart_rental = request.session.get('cart_rental', {}) form_data = { 'full_name': request.POST['full_name'], 'email': request.POST['email'], 'phone_number': request.POST['phone_number'], 'country': request.POST['country'], 'postcode': request.POST['postcode'], 'town_or_city': request.POST['town_or_city'], 'street_address1': request.POST['street_address1'], 'street_address2': request.POST['street_address2'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save(commit=False) pid = request.POST.get('client_secret').split('_secret')[0] order.stripe_pid = pid order.original_cart = json.dumps(cart) order.original_cart_rental = json.dumps(cart_rental) order.save() for item_id, item_data in cart.items(): try: product = Product.objects.get(id=item_id) order_buy_item = OrderBuyItem( order=order, product=product, quantity=item_data, ) order_buy_item.save() except Exception as e: messages.error( request, ("Something went wrong when trying to save the order.\ Please call us for assistance!", e)) order.delete() return redirect('cart') for months, info in cart_rental.items(): for item_id, quantity in info.items(): try: product = Product.objects.get(id=item_id) order_rental_item = OrderRentalItem( order=order, product=product, quantity=quantity, months=int(months), ) order_rental_item.save() except Exception as e: messages.error(request, ( "Something went wrong when trying to save the order.\ Please call us for assistance!", e)) order.delete() return redirect('cart') request.session['save_info'] = 'save_info' in request.POST return redirect( reverse('checkout_success', args=[order.order_number])) else: messages.error( request, 'There was an error with your form\ Please double check your information') else: cart = request.session.get('cart', {}) cart_rental = request.session.get('cart_rental', {}) if not cart and not cart_rental: messages.error(request, "There is nothing in your cart at the moment") return redirect('home') current_cart = cart_contents(request) total = current_cart["total"] current_cart_rental = cart_rental_contents(request) total_rental = current_cart_rental["total_rental"] # Stripe only accepts integer as amount to charge stripe_total = round((total + total_rental) * 100) stripe.api_key = stripe_secret_key intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURRENCY, ) if request.user.is_authenticated: try: profile = UserProfile.objects.get(user=request.user) order_form = OrderForm( initial={ 'full_name': profile.user.get_full_name(), 'email': profile.user.email, 'phone_number': profile.default_phone_number, 'country': profile.default_country, 'postcode': profile.default_postcode, 'town_or_city': profile.default_town_or_city, 'street_address1': profile.default_street_address1, 'street_address2': profile.default_street_address2, }) except Exception: order_form = OrderForm() else: order_form = OrderForm() if not stripe_public_key: messages.warning( request, 'Stripe public key is missing. \ To the developer: must be set in the environment.') context = { 'order_form': order_form, 'stripe_public_key': stripe_public_key, 'client_secret': intent.client_secret, } return render(request, 'checkout/checkout.html', context)
def view_cart(request): """A View that renders the cart contents page""" contexts = media(request) contexts.update(cart_contents(request)) return render(request, "cart.html", contexts)
def checkout(request): stripe_public_key = settings.STRIPE_PUBLIC_KEY stripe_secret_key = settings.STRIPE_SECRET_KEY if request.method == 'POST': cart = request.session.get('cart', {}) for item_id, quantity in cart.items(): plan = get_object_or_404(Pricing, pk=item_id) frequency = plan.frequency start_date = make_aware(datetime.now()) if frequency == 'annually': end_date = start_date + relativedelta(years=1) else: end_date = start_date + relativedelta(months=1) form_data = { 'full_name': request.POST['full_name'], 'email': request.POST['email'], 'phone_number': request.POST['phone_number'], 'country': request.POST['country'], 'post_code': request.POST['post_code'], 'town_or_city': request.POST['town_or_city'], 'street_address1': request.POST['street_address1'], 'street_address2': request.POST['street_address2'], 'county': request.POST['county'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save(commit=False) order.start_date = start_date order.end_date = end_date pid = request.POST.get('client_secret').split('_secret')[0] order.stripe_pid = pid order.original_cart = json.dumps(cart) order.save() cart = request.session.get('cart', {}) total = 0 for item_id, quantity in cart.items(): plan = get_object_or_404(Pricing, pk=item_id) total += quantity * plan.price total = plan.price order_line_item = OrderLineItem( order=order, plan=plan, quantity=quantity, ) order_line_item.save() request.session['save_info'] = 'save-info' in request.POST return redirect( reverse('checkout_success', args=[order.order_number])) else: messages.error( request, 'There was an error with your form. \ Please double check your information.') else: cart = request.session.get('cart', {}) if not cart: messages.error(request, "There's nothing in your cart at the moment") return redirect(reverse('pricing')) current_cart = cart_contents(request) total = current_cart['total'] stripe_total = round(total * 100) stripe.api_key = stripe_secret_key intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURRENCY, ) if request.user.is_authenticated: try: profile = UserProfile.objects.get(user=request.user) order_form = OrderForm( initial={ 'full_name': profile.user.get_full_name(), 'email': profile.user.email, 'phone_number': profile.default_phone_number, 'country': profile.default_country, 'post_code': profile.default_post_code, 'town_or_city': profile.default_town_or_city, 'street_address1': profile.default_street_address1, 'street_address2': profile.default_street_address2, 'county': profile.default_county, }) except UserProfile.DoesNotExist: order_form = OrderForm() else: order_form = OrderForm() if not stripe_public_key: messages.warning(request, 'Stripe public key is missing.') template = 'checkout/checkout.html' context = { 'order_form': order_form, 'stripe_public_key': stripe_public_key, 'client_secret': intent.client_secret, } return render(request, template, context)
def checkout(request): stripe.api_key = settings.STRIPE_SECRET_KEY cart = request.session.get('cart', {}) if request.method == 'POST': form_data = { 'full_name': request.POST['full_name'], 'email': request.POST['email'], 'phone': request.POST['phone'], 'address_line1': request.POST['address_line1'], 'address_line2': request.POST['address_line2'], 'city': request.POST['city'], 'postcode': request.POST['postcode'], 'country': request.POST['country'], 'county': request.POST['county'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save(commit=False) if request.user.is_authenticated: order.user = request.user order.stripe_pid = request.POST.get('client_secret').split( '_secret')[0] order.save() for item_id, item_data in cart.items(): item = get_object_or_404(Item, pk=item_id) order_item = OrderItem(order=order, item=item, quantity=item_data) order_item.save() request.session['save_info'] = 'save-info' in request.POST return redirect(reverse('success', args=[order.order_no])) else: if not cart: messages.error( request, "There's nothing in your \ cart at the moment") return redirect(reverse('search')) current_cart = cart_contents(request) stripe_total = round(current_cart['grand_total'] * 100) intent = stripe.PaymentIntent.create(amount=stripe_total, currency=settings.STRIPE_CURRENCY) profile = get_object_or_404(UserProfile, user=request.user) form_data = { 'email': request.user.email, 'phone': profile.phone, 'address_line1': profile.address_line1, 'address_line2': profile.address_line2, 'city': profile.city, 'county': profile.county, 'postcode': profile.postcode, 'country': profile.country, } order_form = OrderForm(form_data) template = "checkout/checkout.html" context = { 'order_form': order_form, 'stripe_public_key': settings.STRIPE_PUBLIC_KEY, 'client_secret': intent.client_secret } return render(request, template, context)
def checkout(request): STRIPE_PUBLIC_KEY = settings.STRIPE_PUBLIC_KEY STRIPE_SECRET_KEY = settings.STRIPE_SECRET_KEY if request.method == 'POST': cart = request.session.get('cart', {}) form_data = { 'full_name': request.POST['full_name'], 'email': request.POST['email'], 'phone_number': request.POST['phone_number'], 'address': request.POST['address'], 'postcode': request.POST['postcode'], 'city': request.POST['city'], 'country': request.POST['country'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save(commit=False) pid = request.POST.get('client_secret').split('_secret')[0] order.stripe_pid = pid order.original_cart = json.dumps(cart) order.save() # iterate through cart items for item_id, item_data in cart.items(): try: # IF the item exists: # get product ID from cart # create one object per line product = Product.objects.get(id=item_id) order_line_item = OrderLineItem( order=order, product=product, quantity=item_data, ) order_line_item.save() except Product.DoesNotExist: # IF the item does NOT exist: messages.error(request, ("One of the products in your cart wasn't " "found in our database. " "Please contact us for assistance!")) order.delete() return redirect(reverse('view_cart')) # Save the information to the User's profile request.session['save_info'] = 'save-info' in request.POST return redirect(reverse('checkout_success'), args=[order.order_number]) else: messages.error( request, 'There was an error processing your form. \ Please review the information you entered.') else: cart = request.session.get('cart', {}) if not cart: messages.error( request, "Hold your horses, there is nothing on your cart \ right now. Let's go do some shopping..") return redirect(reverse('products')) current_cart = cart_contents(request) grand_total = current_cart['grand_total'] stripe_total = round(grand_total * 100) stripe.api_key = STRIPE_SECRET_KEY intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURR, ) if request.user.is_authenticated: try: profile = UserProfile.objects.get(user=request.user) order_form = OrderForm( initial={ 'full_name': profile.user.get_full_name(), 'email': profile.user.email, 'phone_number': profile.default_phone_number, 'country': profile.default_country, 'postcode': profile.default_postcode, 'city': profile.default_city, 'address': profile.default_address, }) except UserProfile.DoesNotExist: order_form = OrderForm() else: order_form = OrderForm() template = 'checkout/checkout.html' context = { 'order_form': order_form, 'stripe_public_key': STRIPE_PUBLIC_KEY, 'client_secret': intent.client_secret, } return render(request, template, context)
def all_products(request): products = Product.objects.all() contexts = media(request) contexts.update(cart_contents(request)) contexts.update({"products": products}) return render(request, "products.html", contexts)
def checkout(request): """ A view to get the items from the cart and pass it into the checkout form. Code used from CI checkout and Stripe lessons """ stripe_public_key = settings.STRIPE_PUBLIC_KEY stripe_secret_key = settings.STRIPE_SECRET_KEY if request.method == 'POST': cart = request.session.get('cart', {}) form_data = { 'first_name': request.POST['first_name'], 'last_name': request.POST['last_name'], 'email_address': request.POST['email_address'], 'address': request.POST['address'], 'postcode': request.POST['postcode'], 'town': request.POST['town'], 'country': request.POST['country'], 'comments': request.POST['comments'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save() for item_id, item_data in cart.items(): toy = Toy.objects.get(id=item_id) order_line_item = OrderLineItem( order=order, toy=toy, quantity=item_data, ) order_line_item.save() return redirect( reverse('checkout_success', args=[order.order_number])) else: messages.error( request, 'There was an error with your form. \ Please double check your information.') return redirect(reverse('checkout_success')) else: cart = request.session.get('cart', {}) if not cart: messages.error(request, 'Your cart is empty') return redirect(reverse('toys')) current_cart = cart_contents(request) total = current_cart['total'] stripe_total = round(total * 100) stripe.api_key = stripe_secret_key intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURRENCY, ) order_form = OrderForm() if not stripe_public_key: messages.warning( request, 'Please fill in the correct Stripe \ Public key') template = 'checkout/checkout.html' context = { 'order_form': order_form, 'stripe_public_key': stripe_public_key, 'client_secret': intent.client_secret, } return render(request, template, context)
def checkout(request): stripe_public_key = settings.STRIPE_PUBLIC_KEY stripe_secret_key = settings.STRIPE_SECRET_KEY if request.method == 'POST': cart = request.session.get('cart', {}) form_data = { 'full_name': request.POST['full_name'], 'email': request.POST['email'], 'phone_number': request.POST['phone_number'], 'country': request.POST['country'], 'postcode': request.POST['postcode'], 'town_or_city': request.POST['town_or_city'], 'street_address1': request.POST['street_address1'], 'street_address2': request.POST['street_address2'], 'county': request.POST['county'], } order_form = OrderForm(form_data) if order_form.is_valid(): order = order_form.save(commit=False) pid = request.POST.get('client_secret').split('_secret')[0] order.stripe_pid = pid order.original_cart = json.dumps(cart) order.save() for item_id, item_data in cart.items(): try: plan = Plans.objects.get(id=item_id) if isinstance(item_data, int): order_line_item = OrderLineItem( order=order, plan=plan, quantity=item_data, ) order_line_item.save() else: for size, quantity in item_data['items_by_size'].items(): order_line_item = OrderLineItem( order=order, plan=plan, quantity=quantity, ) order_line_item.save() except Plans.DoesNotExist: messages.error(request, ( "The plan in your cart wasn't " "found in our database. " "Please contact us for assistance!") ) order.delete() return redirect(reverse('view_cart')) request.session['save_info'] = 'save-info' in request.POST return redirect(reverse('checkout_success', args=[order.order_number])) else: messages.error(request, ('There was an error with your form. ' 'Please double check your information.')) else: cart = request.session.get('cart', {}) if not cart: return redirect(reverse('plans')) current_cart = cart_contents(request) total = current_cart['grand_total'] stripe_total = round(total * 100) stripe.api_key = stripe_secret_key intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURRENCY, ) order_form = OrderForm() template = 'checkout/checkout.html' context = { 'order_form': order_form, 'stripe_public_key': stripe_public_key, 'client_secret': intent.client_secret, } return render(request, template, context)