def shipping(): form = ShippingForm() form.address.set_selects(filter_noship=True) order = Order.load(current_user) customer = order.customer if not customer: customer = Customer.get_from_session() if form.validate_on_submit(): if not customer: customer = Customer() db.session.add(customer) if customer.current_order is not order: customer.current_order = order customer.shipping_address = form.address.get_or_create_address() if not form.comments.data: form.comments.data = None if form.comments.data != order.shipping_comments: order.shipping_comments = form.comments.data db.session.commit() return redirect(url_for('shop.billing')) try: form.address.populate_from_address(customer.shipping_address) form.comments.data = order.shipping_comments except AttributeError: form.address.country.data = 'USA' return render_template('shop/shipping.html', form=form, order=order)
def checkout(): guest = request.args.get('guest') or False current_order = Order.load(current_user) form = CheckoutForm() form.billing_address.set_selects() form.shipping_address.set_selects(filter_noship=True) customer = current_order.customer if not customer: customer = Customer.get_from_session() if form.validate_on_submit(): print('Nonce: {}'.format(form.nonce.data)) if not customer: customer = Customer() if customer.current_order is not current_order: customer.current_order = current_order if not current_user.is_anonymous and not current_user.customer_data: current_user.customer_data = customer if not form.billing_address.equals_address(customer.billing_address): customer.billing_address = ( form.billing_address.get_or_create_address() ) if not form.shipping_address.equals_address(customer.shipping_address): if form.billing_address.form == form.shipping_address.form: customer.shipping_address = customer.billing_address else: customer.shipping_address = ( form.shipping_address.get_or_create_address() ) if form.shipping_comments.data: current_order.shipping_comments = form.shipping_notes.data elif current_order.shipping_comments: current_order.shipping_comments = None current_order.status = Order.PENDING_REVIEW db.session.add_all([current_order, customer]) db.session.commit() if current_user.is_anonymous: customer.save_id_to_session() flash('All fields valid.') return redirect(url_for('shop.review_order')) # Since as of writing this wtforms has a bug in which `None` is coerced # to a string in select fields, I'm using whether or not `form.submit` has # been pressed to know if the default countries need to be set or not. -N if not form.review_order.data: try: form.billing_address.populate_from_address( customer.billing_address ) form.shipping_address.populate_from_address( customer.shipping_address ) except AttributeError: form.billing_address.country.data = 'USA' form.shipping_address.country.data = 'USA' if current_order.shipping_comments: form.shipping_comments.data = current_order.shipping_notes return render_template('shop/checkout.html', current_order=current_order, guest=guest, form=form)
def billing(): # TODO: Handle returning customers. form = BillingForm() form.address.set_selects() order = Order.load(current_user) customer = order.customer if not customer: customer = Customer.get_from_session() if form.validate_on_submit(): if form.same_as_shipping.data: customer.billing_address = customer.shipping_address else: customer.shipping_address = form.address.get_or_create_address() session['stripe_token'] = form.stripeToken.data db.session.commit() return redirect(url_for('shop.review')) try: form.address.populate_from_address(customer.billing_address) except AttributeError: form.address.country.data = "USA" return render_template( 'shop/billing.html', form=form, order=order, shipping=customer.shipping_address )
def review(): if current_user.is_anonymous: customer = Customer.get_from_session() else: customer = current_user.customer_data order = customer.current_order form = ConfirmOrderForm() if form.validate_on_submit(): if not customer.stripe_id: scustomer = stripe.Customer.create( source=session['stripe_token'], description=customer.fullname ) customer.stripe_id = scustomer.id db.session.commit() else: scustomer = stripe.Customer.retrieve(customer.stripe_id) # Charge! try: charge = stripe.Charge.create( amount=order.total_cents, currency="usd", customer=customer.stripe_id, metadata={'order_number': order.number } ) except stripe.error.CardError: # TODO return 'It done broked!' flash('It worked!') return redirect(url_for('seeds.home')) try: t = session['stripe_token'] token = stripe.Token.retrieve(t) except KeyError: # TODO: Edit flash message. flash('Could not process credit card information.') return redirect(url_for('shop.billing')) return render_template( 'shop/review.html', current_order=order, card=token['card'], form=form )