def add_to_cart(request): cart = get_cart_session(request) (item_id, name, size, quantity) = test_post_item(request) try: item = Item.objects.get(id=item_id) price = item.markdownPrice if item.markdownPrice != 0 else item.price desc = item.description image_url = item.front().url existsInCart = False for cart_item in cart: if cart_item["id"] == item_id and cart_item["size"] == size: cart_item["quantity"] = int( cart_item["quantity"]) + int(quantity) existsInCart = True if not existsInCart: cart.append({ "id": item_id, "price": str(price), "quantity": quantity, "size": size, "name": name, "description": desc, "image_url": image_url }) request.session["cart"] = cart return HttpResponse(status=200, content=json.dumps(cart)) except Exception, e: logger.error(e) return HttpResponse(status=500, content="Internal Server Failure, Check the logs!")
def get_cart_session(request): try: #Return cart OR an empty list return request.session.get("cart",[]) except Exception, e: logger.error("Session failure") logger.error(e) return HttpResponse(status=500, content="Internal Server Failure")
def get_cart_session(request): try: #Return cart OR an empty list return request.session.get("cart", []) except Exception, e: logger.error("Session failure") logger.error(e) return HttpResponse(status=500, content="Internal Server Failure")
def test_post_item(request): try: #Add the new item to cart item_id = request.POST["id"] name = request.POST["name"] size = request.POST["size"] quantity = request.POST["quantity"] return (item_id, name, size, quantity) except Exception, e: logger.error(e) return HttpResponse(status=400, content="Bad params passed, expects id, name, size & quantity")
def test_post_item(request): try: #Add the new item to cart item_id = request.POST["id"] name = request.POST["name"] size = request.POST["size"] quantity = request.POST["quantity"] return (item_id, name, size, quantity) except Exception, e: logger.error(e) return HttpResponse( status=400, content="Bad params passed, expects id, name, size & quantity")
def post(self, request): """ Send an email and return 200 """ data = request.DATA email_addr = data.get('email') subject = "Contact Form Submission" message = "From:%s\nE-mail:%s\nMessage:%s\n---\nForm submission time:%s \nForm submitted from IP:%s" % (data.get('full-name','no-name'),email_addr if email_addr else 'no-email', data.get('message','no-message'), datetime.now(), request.META['REMOTE_ADDR']) try: send_email(subject, message, email_addr) return Response('') except Exception, e: logger.error(e) return Response('Email Failed: %s' % e, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def ajax_item_selected(request, item_id): try: item = Item.objects.get(id=item_id) size_list = item.quantity.order_by("id") jsonDict = { "item":item.json(), "size_list": size_list, "quantity_list": range(1,11), "paypal_debug" : settings.PAYPAL_DEBUG, } jsonDict.update(csrf(request)) logger.debug(jsonDict) return render_to_response("website/ajax_item_form.html", jsonDict) except Exception, e: logger.error(e) return HttpResponseRedirect("/shop/")
def ajax_item_selected(request, item_id): try: item = Item.objects.get(id=item_id) size_list = item.quantity.order_by("id") jsonDict = { "item": item.json(), "size_list": size_list, "quantity_list": range(1, 11), "paypal_debug": settings.PAYPAL_DEBUG, } jsonDict.update(csrf(request)) logger.debug(jsonDict) return render_to_response("website/ajax_item_form.html", jsonDict) except Exception, e: logger.error(e) return HttpResponseRedirect("/shop/")
def update_cart(request): cart = get_cart_session(request) (item_id, name, size, quantity) = test_post_item(request) try: item = Item.objects.get(id=item_id) logger.debug(cart) for cart_item in cart: #If item in cart matches ID and size if cart_item["id"] == item_id and cart_item["size"] == size: cart_item["quantity"] = quantity cart = [item for item in cart if item["quantity"] != "0"] request.session["cart"] = cart logger.debug(cart) return HttpResponse(status=200, content=json.dumps(cart)) except Exception, e: logger.error(e) return HttpResponse(status=500, content="Internal Server Failure, Check the logs!")
def send_email(subject, body, from_email, to, cc=None, fail_silently=False): """ Use django.core.mail.EmailMessage to send and log an Atmosphere email. """ try: msg = EmailMessage(subject=subject, body=body, from_email=from_email, to=to, cc=cc) msg.send(fail_silently=fail_silently) logging.info( "Email Sent. To: %s\nFrom: %sCc: %s\nSubject: %s\nBody:\n%s" % (from_email, to, cc, subject, body)) return True except Exception as e: logger.error(e) return False
def send_email(subject, body, from_email, to, cc=None, fail_silently=False): """ Use django.core.mail.EmailMessage to send and log an Atmosphere email. """ try: msg = EmailMessage(subject=subject, body=body, from_email=from_email, to=to, cc=cc) msg.send(fail_silently=fail_silently) logging.info("Email Sent. To: %s\nFrom: %sCc: %s\nSubject: %s\nBody:\n%s" % (from_email, to, cc, subject, body)) return True except Exception as e: logger.error(e) return False
def add_to_cart(request): cart = get_cart_session(request) (item_id, name, size, quantity) = test_post_item(request) try: item = Item.objects.get(id=item_id) price = item.markdownPrice if item.markdownPrice != 0 else item.price desc = item.description image_url = item.front().url existsInCart = False for cart_item in cart: if cart_item["id"] == item_id and cart_item["size"] == size: cart_item["quantity"] = int(cart_item["quantity"]) + int(quantity) existsInCart = True if not existsInCart: cart.append({"id":item_id, "price":str(price), "quantity":quantity, "size":size, "name":name, "description":desc, "image_url":image_url}) request.session["cart"] = cart return HttpResponse(status=200, content=json.dumps(cart)) except Exception, e: logger.error(e) return HttpResponse(status=500, content="Internal Server Failure, Check the logs!")
def make_purchase(request): (cart_list,pretax_total, tax, tax_amount, shipping_total, total) = get_cart_details(request) card_args = getCardArgs(request.POST) billing_args = getBillingArgs(request.POST) shipping_args = getShippingArgs(request.POST, billing_args) error_list = [] full_name = "%s %s" % (shipping_args["first_name"], shipping_args["last_name"]) full_name = "%s %s" % (shipping_args["first_name"], shipping_args["last_name"]) billing_addr = "%s\n%s,%s %s" % (billing_args["line1"], billing_args["city"], billing_args["state"], billing_args["postal_code"]) shipping_addr = "%s\n%s,%s %s" % (shipping_args["line1"], shipping_args["city"], shipping_args["state"], shipping_args["postal_code"]) email = request.POST["email-billing"] if not email: error_list.append("Please enter an e-mail address") if error_list: return show_review_page(request, errors=error_list) payment = paypalrestsdk.Payment({ "intent": "sale", "payer": { "payment_method": "credit_card", "funding_instruments": [{ "credit_card": card_args }] }, "transactions": [{ "amount": { "total" : "%.2f" % round(total,2), "currency": "USD", "details" : { "subtotal": "%.2f" % round(pretax_total,2), "tax" : "%.2f" % round(tax_amount,2), "shipping" : "%.2f" % round(shipping_total,2), } }, "description": "Your purchase at Rev-Ink.com." }]}) if not payment.create(): error_list.append("Your Credit Card was not accepted! Please review the error(s) and try again:%s" % payment.error) logger.error("ERROR ACCEPTING PAYMENT! errors included: %s" % payment.__dict__) email_to_admin("ERROR ACCEPTING PAYMENT!", "errors included: %s" % payment.__dict__) return show_review_page(request, errors=error_list) confirmation = payment.id billing_args["ipaddress"] = request.META["REMOTE_ADDR"] logger.info("Payment created successfully. ID=%s" % confirmation) logger.info(full_name) card_args["last4"] = card_args["number"][-4:] if len(card_args["number"]) > 4 else "" logger.info(card_args["last4"]) logger.info(email) confirm_id = make_transaction(full_name, email, shipping_addr, billing_addr, cart_list, confirmation_id=confirmation) del request.session["cart"] #send_confirmation_email(full_name, email) params = { "cart_list" : cart_list, "transaction_id" : confirmation, "cart_pretax" : "%.2f" % round(pretax_total,2), "cart_tax" : "%.1f" % round(tax*100,1), "cart_tax_total" : "%.2f" % round(tax_amount, 2), "shipping_total" : "%.2f" % round(shipping_total, 2), "cart_total" : "%.2f" % round(total,2), "server_url": settings.SERVER_URL, "billing_info":billing_args, "card_info":card_args, "shipping_info":shipping_args, } logger.info(params) c = RequestContext(request, params) return render_to_response("website/thank_you_transaction.html",c)
def make_purchase(request): (cart_list, pretax_total, tax, tax_amount, shipping_total, total) = get_cart_details(request) card_args = getCardArgs(request.POST) billing_args = getBillingArgs(request.POST) shipping_args = getShippingArgs(request.POST, billing_args) error_list = [] full_name = "%s %s" % (shipping_args["first_name"], shipping_args["last_name"]) full_name = "%s %s" % (shipping_args["first_name"], shipping_args["last_name"]) billing_addr = "%s\n%s,%s %s" % ( billing_args["line1"], billing_args["city"], billing_args["state"], billing_args["postal_code"]) shipping_addr = "%s\n%s,%s %s" % ( shipping_args["line1"], shipping_args["city"], shipping_args["state"], shipping_args["postal_code"]) email = request.POST["email-billing"] if not email: error_list.append("Please enter an e-mail address") if error_list: return show_review_page(request, errors=error_list) payment = paypalrestsdk.Payment({ "intent": "sale", "payer": { "payment_method": "credit_card", "funding_instruments": [{ "credit_card": card_args }] }, "transactions": [{ "amount": { "total": "%.2f" % round(total, 2), "currency": "USD", "details": { "subtotal": "%.2f" % round(pretax_total, 2), "tax": "%.2f" % round(tax_amount, 2), "shipping": "%.2f" % round(shipping_total, 2), } }, "description": "Your purchase at Rev-Ink.com." }] }) if not payment.create(): error_list.append( "Your Credit Card was not accepted! Please review the error(s) and try again:%s" % payment.error) logger.error("ERROR ACCEPTING PAYMENT! errors included: %s" % payment.__dict__) email_to_admin("ERROR ACCEPTING PAYMENT!", "errors included: %s" % payment.__dict__) return show_review_page(request, errors=error_list) confirmation = payment.id billing_args["ipaddress"] = request.META["REMOTE_ADDR"] logger.info("Payment created successfully. ID=%s" % confirmation) logger.info(full_name) card_args["last4"] = card_args["number"][-4:] if len( card_args["number"]) > 4 else "" logger.info(card_args["last4"]) logger.info(email) confirm_id = make_transaction(full_name, email, shipping_addr, billing_addr, cart_list, confirmation_id=confirmation) del request.session["cart"] #send_confirmation_email(full_name, email) params = { "cart_list": cart_list, "transaction_id": confirmation, "cart_pretax": "%.2f" % round(pretax_total, 2), "cart_tax": "%.1f" % round(tax * 100, 1), "cart_tax_total": "%.2f" % round(tax_amount, 2), "shipping_total": "%.2f" % round(shipping_total, 2), "cart_total": "%.2f" % round(total, 2), "server_url": settings.SERVER_URL, "billing_info": billing_args, "card_info": card_args, "shipping_info": shipping_args, } logger.info(params) c = RequestContext(request, params) return render_to_response("website/thank_you_transaction.html", c)