| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255 |
- import logging
- import os
- from contextlib import asynccontextmanager
- from pathlib import Path
- from typing import Literal
- from urllib.parse import urljoin, urlparse
- import stripe
- from asgi_csrf import asgi_csrf
- from dotenv import load_dotenv
- from fastapi import BackgroundTasks, FastAPI, Form, HTTPException, Request
- from fastapi.encoders import jsonable_encoder
- from fastapi.exceptions import RequestValidationError
- from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
- from fastapi.staticfiles import StaticFiles
- from fastapi.templating import Jinja2Templates
- from starlette.exceptions import HTTPException as StarletteHTTPException
- from starsessions import CookieStore, SessionAutoloadMiddleware, SessionMiddleware
- from pydantic import ValidationError, EmailStr
- import pycountry
- from app.cart import (
- calculate_quantity_requested_by_user,
- clear_cart,
- initialize_cart,
- product_exist_in_cart,
- update_cart,
- update_cart_meta,
- create_product_list,
- )
- from app.db import (
- check_product_availability,
- export_order_to_xlsx,
- fetch_order_product_list,
- get_products_by_category,
- is_order_id_open,
- prepare_customer_order_data,
- save_shipping_info,
- update_product_inventory,
- update_status_order,
- write_variation_id,
- get_variation_id,
- write_product_id_db,
- get_products_id_db,
- )
- from app.email import send_email
- from app.parser import md, read_dir, read_file
- from app.payment import (
- calculate_shipping_cost,
- prepare_shipping_rate,
- generate_payment_link,
- ipn_signature_check,
- prepare_stripe_data,
- )
- from app.read_settings import read_settings
- from app.schema import CartSession, CartUpdate, NowPaymentsInvoice, ShippingInfo, ProductShippingMeta
- from app.template import (
- make_product_selected,
- prepare_checkout,
- prepare_email_order,
- prepare_product,
- prepare_related_products,
- prepare_sets_for_menu,
- to_srcset,
- assets_hashing,
- make_slugify
- )
- from app.form_validation import form_validation_checkout_session
- load_dotenv(".env")
- logging_level = logging.INFO
- main_logger = logging.getLogger()
- main_logger.setLevel(logging_level)
- formatter = logging.Formatter("%(asctime)s %(levelname)s: %(name)s => %(message)s")
- # Set up a stream handler to log to the console
- stream_handler = logging.StreamHandler()
- stream_handler.setLevel(logging_level)
- stream_handler.setFormatter(formatter)
- file_handler = logging.FileHandler("logs.log")
- file_handler.setLevel(logging_level)
- file_handler.setFormatter(formatter)
- # Add handlers to logger
- main_logger.addHandler(stream_handler)
- main_logger.addHandler(file_handler)
- @asynccontextmanager
- async def lifespan(app: FastAPI):
- """
- On startup of application we check that all the product variations
- have their unique IDs
- """
- products_list = read_dir(
- settings.git_repo,
- settings.document_match,
- settings.document_exclude,
- settings.block_types,
- exclude_doc="products",
- tree="products",
- )
- products_data = get_products_id_db(settings)
- for single_product in products_list:
- write_variation_id(single_product, settings)
- write_product_id_db(single_product, settings, products_data)
- yield
- app = FastAPI(docs_url=None, redoc_url=None, lifespan=lifespan)
- # -- read settings
- settings = read_settings("settings.toml")
- # -- mount static files
- app.mount(
- "/static",
- StaticFiles(directory=Path(__file__).parent.parent / "static"),
- name="static",
- )
- # mount media folder from git content repo
- app.mount("/media", StaticFiles(directory=settings.git_repo), name="media")
- # -- setup templates
- templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
- templates.env.filters["md"] = md
- templates.env.filters["to_srcset"] = to_srcset
- templates.env.filters["assets_hashing"] = assets_hashing
- templates.env.filters["slugify"] = make_slugify
- templates.env.globals.update(CACHE=os.getenv('CACHE'))
- # -- add middlewares for CSRF and Session
- app.add_middleware(
- asgi_csrf,
- signing_secret=os.getenv("CSRF_SECRET_KEY"),
- always_protect={"/", "/products", "/checkout"},
- )
- session_store = CookieStore(secret_key=os.getenv("SESSION_SECRET_KEY"))
- app.add_middleware(SessionAutoloadMiddleware)
- app.add_middleware(
- SessionMiddleware,
- store=session_store,
- lifetime=3600 * 24 * 14,
- cookie_https_only=os.getenv("ENV") == "dev",
- )
- # -- load Stripe and set API key
- if os.getenv("ENV") == "production":
- stripe.api_key = os.getenv("STRIPE_SECRET_KEY")
- else:
- stripe.api_key = os.getenv("STRIPE_TEST_SECRET_KEY")
- @app.get("/", response_class=HTMLResponse)
- async def root(request: Request):
- """The Main view of the website. It gives access to all the pages."""
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- cart = CartSession(**request.session["cart"])
- doc = read_file(
- settings.git_repo,
- "index",
- settings.document_match,
- settings.block_types,
- "",
- )
- categories = get_products_by_category(settings)
- sets = prepare_sets_for_menu(settings)
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- response = templates.TemplateResponse(
- request,
- "index.html",
- {
- "request": request,
- "nav": None,
- "doc": doc,
- "categories": categories,
- "sets": sets,
- "site": site,
- "cart": cart,
- },
- )
- return response
- @app.get("/products", response_class=RedirectResponse, status_code=301)
- async def products():
- """
- The products view does not exist, so we return the user to the main view.
- """
- return "/"
-
- @app.get("/products/{product_id}", response_class=HTMLResponse)
- async def product(
- request: Request,
- product_id: str,
- js: bool = False,
- size: str | None = None,
- style: str | None = None,
- quantity: int = 1,
- ):
- """The product view displaying the content of the selected product PRODUCT ID.
- By default we check if the product exists in quantity = 1, unless
- a different value is explicitly pass to the GET request.
- """
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- cart = CartSession(**request.session["cart"])
- tree = "products"
- doc = read_file(
- settings.git_repo,
- product_id,
- settings.document_match,
- settings.block_types,
- tree,
- )
- # we set quantity=0 because we are in a GET view and are not
- # adding anything to the cart yet, just want to see if we could
- # add 1 more item to it.
- quantity_requested_by_user = calculate_quantity_requested_by_user(
- session_data["cart"]["items"], doc.meta.product_id, quantity=0,
- )
- triplet = check_product_availability(
- doc, product_id, quantity_requested_by_user, size, style, settings
- )
- is_product_available, is_product_selectable, inventory_amount = triplet
- if js:
- product_selected = make_product_selected(size, style, quantity)
- json_response = {
- "is_product_available": is_product_available,
- "is_product_selectable": is_product_selectable,
- "product_selected": product_selected,
- }
- return JSONResponse(content=json_response, status_code=200)
- else:
- doc.blocks = prepare_product(doc)
- size_guide = None
- if doc.meta.options:
- size_guide = read_file(
- settings.git_repo,
- "products",
- settings.document_match,
- settings.block_types,
- None,
- )
- products = read_dir(
- settings.git_repo,
- settings.document_match,
- settings.document_exclude,
- settings.block_types,
- exclude_doc=doc.meta.path,
- tree="products",
- )
- related_products = prepare_related_products(products, product_id)
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- response = templates.TemplateResponse(
- request,
- "product.html",
- {
- "request": request,
- "nav": None,
- "doc": doc,
- "size_guide": size_guide,
- "related_products": related_products,
- "is_product_available": is_product_available,
- "is_product_selectable": is_product_selectable,
- "product_form": {
- "size": size,
- "style": style,
- "quantity": quantity,
- },
- "site": site,
- "cart": cart,
- },
- )
- return response
- @app.get("/pages", response_class=RedirectResponse, status_code=301)
- async def products():
- """
- The pages view does not exist, so we return the user to the main view.
- """
- return "/"
-
- @app.get("/pages/{page_id}", response_class=HTMLResponse)
- async def page(request: Request, page_id: str):
- """The Page view displaying the content of the selected PAGE ID."""
- tree = "pages"
- doc = read_file(
- settings.git_repo,
- page_id,
- settings.document_match,
- settings.block_types,
- tree,
- )
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- response = templates.TemplateResponse(
- request,
- "page.html",
- {
- "request": request,
- "nav": None,
- "doc": doc,
- "site": site,
- },
- )
- return response
- @app.get("/checkout", response_class=HTMLResponse)
- async def checkout(request: Request):
- """The Checkout view (GET, read-only)."""
- filename = request.url.path[1:]
- doc = read_file(
- settings.git_repo, filename, settings.document_match, settings.block_types
- )
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- update_cart_meta(
- session_data["cart"], session_data["cart"]["meta"]["shipping"], settings
- )
- cart = CartSession(**request.session["cart"])
-
- checkout_data = prepare_checkout(cart, settings, doc)
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- response = templates.TemplateResponse(
- request,
- "checkout.html",
- {
- "request": request,
- "nav": None,
- "data": checkout_data,
- "site": site,
- "cart": cart,
- },
- )
- return response
- @app.post("/checkout")
- async def checkout_update(
- request: Request,
- js: bool = False,
- page: Literal["product", "checkout"] = Form(...),
- redirect_with_items: bool | None = Form(None),
- operation: Literal["add", "remove", "delete", "manual"] = Form(...),
- product_id: str = Form(...),
- product_path: str = Form(...),
- price: int = Form(...),
- quantity: int = Form(...),
- size: Literal["s", "m", "l", "xl", "xxl"] | None = Form(None),
- style: str | None = Form(None),
- ):
- """The Checkout view, (POST, read-write). This view updates (add /
- remove) the user's session cart. If the item is already present in
- the cart, it increases or decreases the item quantity. Else, it
- will be created or removed accordingly. It will also check if any
- selected item in the cart is still available or it has sold out
- meanwhile. Morever, it displays the Checkout view.
- """
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- shipping_amount = session_data["cart"]["meta"]["shipping"]
- if operation == "manual":
- selected_product_id = session_data["cart"]["items"][product_id]
- price = quantity * selected_product_id["price"]
- tree = "products"
- product_filename = Path(product_path).stem
- selected_product = read_file(
- settings.git_repo,
- product_filename,
- settings.document_match,
- settings.block_types,
- tree,
- )
- product_variation_id = get_variation_id(selected_product, size, style)
- form = CartUpdate(
- operation=operation,
- product_id=product_id,
- options={"size": size,
- "style": style,
- "product_variation_id": product_variation_id,
- "weight": selected_product.meta.weight,
- },
- price=price,
- )
- quantity_requested_by_user = calculate_quantity_requested_by_user(
- session_data["cart"]["items"], product_id, quantity
- )
- triplet = check_product_availability(
- selected_product,
- product_path,
- quantity_requested_by_user,
- size,
- style,
- settings,
- )
- is_product_available, is_product_selectable, inventory_amount = triplet
- product_exists = product_exist_in_cart(
- product_id, size, style, product_variation_id, session_data["cart"]["items"]
- )
- update_cart(
- session_data["cart"],
- form,
- product_exists,
- shipping_amount,
- quantity,
- inventory_amount,
- settings,
- )
- cart = CartSession(**request.session["cart"])
- if js:
- # -- return JSON res for js-handled form update
- if page == "product":
- cart_breakdown = jsonable_encoder(cart.meta)
- product_selected = make_product_selected(size, style, quantity)
- json_response = {
- "cart_breakdown": cart_breakdown,
- "is_product_available": is_product_available,
- "is_product_selectable": is_product_selectable,
- "product_selected": product_selected,
- }
- elif page == "checkout":
- filename = request.url.path[1:]
- doc = read_file(
- settings.git_repo,
- filename,
- settings.document_match,
- settings.block_types,
- )
- checkout_data = prepare_checkout(
- cart, settings, doc, js_update=False
- )
- cart_items = [
- jsonable_encoder(item) for item in checkout_data["checkout_items"]
- ]
- cart_breakdown = jsonable_encoder(cart.meta)
- json_response = {
- "cart_items": cart_items,
- "cart_breakdown": cart_breakdown,
- }
- return JSONResponse(content=json_response, status_code=200)
- else:
- redirect_URL = urljoin(
- request.headers["referer"], urlparse(request.headers["referer"]).path
- )
- redirect_URL = f"{redirect_URL}"
- if redirect_with_items:
- URI_options = []
- param_options = {"size": size, "style": style, "quantity": quantity}
- for idx, option in enumerate(param_options.keys()):
- if param_options[option]:
- param = f"{option}={param_options[option]}"
- URI_options.append(param)
- if len(URI_options) > 0:
- URI_params = "&".join(URI_options)
- redirect_URL = f"{redirect_URL}?{URI_params}"
- else:
- redirect_URL = f"{redirect_URL}"
- return RedirectResponse(redirect_URL, status_code=303)
- @app.post("/checkout/shipping")
- async def checkout_shipping(request: Request,
- js: bool = False,
- country: str = Form(...),
- weight: int = Form(...)):
- """
- This view returns the region to which the given submitted country belongs to.
- This is used in /checkout to update shipping costs in real time.
- """
- countries = [country.name for country in pycountry.countries]
- if (
- country not in countries
- or weight <= 0
- ):
- raise HTTPException(status_code=403, detail="Shipping info are incorrect.")
- shipping_rate = calculate_shipping_cost(country, weight, settings.currency.default)
- if shipping_rate:
- # update cart with new shipping costs
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- update_cart_meta(session_data["cart"], shipping_rate.price, settings)
- cart = CartSession(**request.session['cart'])
- if js:
- return cart.meta
- else:
- return RedirectResponse(f"{request.base_url}checkout", status_code=303)
- raise HTTPException(status_code=403, detail="Shipping info are missing.")
- @app.post("/stripe-checkout-session", response_class=RedirectResponse, status_code=303)
- async def create_checkout_session(
- request: Request,
- order_id: str = Form(...),
- weight: int = Form(...),
- email: EmailStr = Form(...),
- first_name: str | None = Form(None),
- last_name: str = Form(...),
- address: str = Form(...),
- address_no: str = Form(...),
- address_extra: str | None = Form(None),
- postal_code: str = Form(...),
- city: str = Form(...),
- country: str = Form(...),
- phone_number: str = Form(None),
- note: str | None = Form(None),
- ):
- """Stripe-based checkout session. Prepare order data and create a new
- Stripe Checkout session. Handle gracefully in case of errors, or
- if order exists already in local-db and has not `status: Open`.
- """
- # -- initial form-data validation
- form_data = await request.form()
- try:
- form = form_validation_checkout_session(form_data)
-
- except ValidationError as e:
- for error in e.errors():
- main_logger.error(f"form-validation-stripe-checkout-session => {error}")
- # -- check if order-id is unique
- orders_settings = {
- "git_repo": settings.git_repo,
- "document_match": settings.document_match,
- "block_types": settings.block_types,
- }
- if not is_order_id_open(orders_settings, form.order_id):
- main_logger.error("stripe-checkout error: Order ID is wrong.")
- raise HTTPException(status_code=422, detail="Order ID is wrong.")
- shipping_info = {
- "email": form.email,
- "first_name": form.first_name,
- "last_name": form.last_name,
- "address": form.address,
- "address_no": form.address_no,
- "address_extra": form.address_extra,
- "postal_code": form.postal_code,
- "city": form.city,
- "country": form.country,
- "phone_number": form.phone_number,
- "note": form.note,
- }
- shipping_info = ShippingInfo(**shipping_info)
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- if "cart" not in session_data and len(session_data["cart"].keys()) == 0:
- main_logger.error("stripe-checkout error: Cart is empty.")
- raise HTTPException(status_code=403, detail="Cart is empty.")
- cart = CartSession(**request.session["cart"])
- try:
- line_items = prepare_stripe_data(cart)
- shipping_rate = prepare_shipping_rate(shipping_info.country, form.weight, settings.currency.default)
- if shipping_rate is None:
- main_logger.error("stripe-checkout error: Shipping info are missing.")
- raise HTTPException(status_code=403, detail="Shipping info are missing.")
- checkout_session = stripe.checkout.Session.create(
- mode="payment",
- line_items=line_items,
- shipping_options=[shipping_rate],
- customer_email=shipping_info.email,
- client_reference_id=form.order_id,
- success_url=f"{request.base_url}checkout/success",
- cancel_url=f"{request.base_url}checkout",
- )
- product_list = create_product_list(settings, cart.items)
- # -- save transaction to local-db
- customer_order = prepare_customer_order_data(
- settings.git_repo,
- settings.document_match,
- settings.document_exclude,
- settings.block_types,
- checkout_session["id"],
- product_list,
- "Stripe",
- form.order_id,
- cart,
- shipping_info,
- )
- save_shipping_info(
- customer_order, settings.git_repo, settings.local_db.filepath
- )
- # -- redirect to Stripe Checkout page
- return checkout_session.url
- except stripe.error.CardError as e:
- main_logger.error(
- f"stripe-checkout error: A payment error occurred: {e.user_message}"
- )
- raise HTTPException(status_code=400, detail="Stripe payment error.")
- except stripe.error.InvalidRequestError as e:
- main_logger.error(f"stripe-checkout error: An invalid request occurred. => {e}")
- raise HTTPException(
- status_code=400, detail="Stripe payment error (invalid request)."
- )
- except Exception as e:
- main_logger.error(
- f"stripe-checkout error: Another problem occurred, maybe unrelated to Stripe. {e}"
- )
- raise HTTPException(status_code=400, detail="Stripe payment error.")
- @app.post("/stripe-webhook", response_class=JSONResponse)
- async def stripe_webhook(request: Request, background_tasks: BackgroundTasks):
- """Stripe webhook endpoint to receive updates from Stripe's checkout
- operations. Send email after successful order confirmation.
- """
- main_logger.info("stripe-webhook => ...")
- try:
- event = None
- payload = await request.body()
- sig_header = request.headers["stripe-signature"]
- try:
- if os.getenv("ENV") == "production":
- endpoint_secret = os.getenv("STRIPE_ENDPOINT_SECRET")
- else:
- endpoint_secret = os.getenv("STRIPE_TEST_ENDPOINT_SECRET")
- event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
- except ValueError as e:
- main_logger.error(
- f"stripe-webhook error: Webhook error while parsing basic request. {e}"
- )
- raise HTTPException(status_code=400, detail="Stripe payment error.")
- except stripe.error.SignatureVerificationError as e:
- main_logger.error(
- f"stripe-webhook error: Webhook signature verification failed. {e}"
- )
- raise HTTPException(status_code=400, detail="Stripe payment error.")
- if event:
- checkout = event["data"]["object"]
- if "client_reference_id" in checkout:
- try:
- status_res = update_status_order(
- event["type"],
- checkout["client_reference_id"],
- settings.git_repo,
- settings.local_db.filepath,
- )
- if status_res is False:
- main_logger.error("stripe-webhook error: order could not be updated.")
- # Don't raise an exception, just log and continue
- #raise HTTPException(
- # status_code=400, detail="Order could not be updated."
- #)
- # We still want to try updating inventory and sending email
- except KeyError as e:
- main_logger.error(f"stripe-webhook error: Missing key in order data: {e}")
- # Continue processing - we want to still try to send the email
- except Exception as e:
- main_logger.error(f"stripe-webhook error: Unexpected error updating order: {e}")
- # Continue processing
- if event["type"] in [
- "checkout.session.completed",
- "checkout.session.async_payment_succeeded",
- ]:
- order_id = checkout["client_reference_id"]
-
- product_list = fetch_order_product_list(
- order_id, settings.git_repo, settings.local_db.filepath
- )
-
- if product_list:
- for product_info in product_list:
- # We need to read the product file and get the variation ID
- try:
- product = read_file(
- settings.git_repo,
- f"products/{product_info.filename}",
- settings.document_match,
- settings.block_types,
- )
-
- # Get the variation ID based on size and style
- product_variation_id = get_variation_id(product, product_info.size, product_info.style)
-
- update_product_inventory(
- product_info.filename,
- product_info.quantity,
- product_variation_id,
- settings,
- )
- except Exception as e:
- main_logger.error(f"stripe-webhook error: Failed to update inventory: {e}")
- # Continue processing other products
- # -- send email
- try:
- email_settings = {
- "git_repo": settings.git_repo,
- "document_match": settings.document_match,
- "block_types": settings.block_types,
- }
- # It's possible the order was just created and not all data is available yet
- # The prepare_email_order function should handle this gracefully
- try:
- order = prepare_email_order(order_id, email_settings)
- await send_email(email_settings, order, background_tasks)
- except TypeError as e:
- # Specifically catch the 'NoneType' object is not iterable error
- main_logger.warning(f"stripe-webhook warning: Email preparation issue, possibly due to timing: {e}")
- # Consider implementing a retry mechanism here if needed
- # For example, add the email task to a queue to try again later
- except Exception as e:
- main_logger.error(f"stripe-webhook error: Failed to send email: {e}")
- # Continue processing - we've already done inventory updates
- elif event["type"] == "checkout.session.async_payment_failed":
- main_logger.error(
- f"stripe-webhook error: Stripe payment error. {event['type']}"
- )
- raise HTTPException(status_code=400, detail="Stripe payment error.")
- else:
- main_logger.error(f"stripe-webhook error: Unhandled event type {event['type']}")
- raise HTTPException(status_code=400, detail="Stripe payment error.")
- except Exception as e:
- main_logger.error(f"stripe-webhook error: Unhandled exception: {e}")
- import traceback
- main_logger.error(traceback.format_exc())
- # Still return success to prevent Stripe from retrying the webhook
- # -- return 200
- return {"success": True}
- @app.post("/create-now-payments", response_class=RedirectResponse, status_code=303)
- async def create_now_payments(
- request: Request,
- order_id: str = Form(...),
- weight: int = Form(...),
- email: EmailStr = Form(...),
- first_name: str | None = Form(None),
- last_name: str = Form(...),
- address: str = Form(...),
- address_no: str = Form(...),
- address_extra: str | None = Form(None),
- postal_code: str = Form(...),
- city: str = Form(...),
- country: str = Form(...),
- phone_number: str = Form(None),
- note: str | None = Form(None),
- ):
- """NOWPayments checkout session. User selects with which currency to pay.
- Sort of a custom NOW Payments Invoice page.
- """
- # -- initial form-data validation
- form_data = await request.form()
- try:
- form = form_validation_checkout_session(form_data)
-
- except ValidationError as e:
- for error in e.errors():
- main_logger.error(f"form-validation-stripe-checkout-session => {error}")
- # -- check if order-id is unique
- orders_settings = {
- "git_repo": settings.git_repo,
- "document_match": settings.document_match,
- "block_types": settings.block_types,
- }
- if not is_order_id_open(orders_settings, form.order_id):
- main_logger.error("now-payments-checkout error: Order ID is wrong.")
- raise HTTPException(status_code=422, detail="Order ID is wrong.")
- shipping_info = {
- "email": form.email,
- "first_name": form.first_name,
- "last_name": form.last_name,
- "address": form.address,
- "address_no": form.address_no,
- "address_extra": form.address_extra,
- "postal_code": form.postal_code,
- "city": form.city,
- "country": form.country,
- "phone_number": form.phone_number,
- "note": form.note,
- }
- shipping_info = ShippingInfo(**shipping_info)
- session_data = request.session
- initialize_cart(session_data, settings.currency.default)
- if "cart" not in session_data and len(session_data["cart"].keys()) == 0:
- main_logger.error("now-payments-checkout error: Cart is empty.")
- raise HTTPException(status_code=403, detail="Cart is empty.")
- cart = CartSession(**request.session["cart"])
- data = {
- "price_amount": cart.meta.total,
- "price_currency": settings.currency.now_payments,
- "order_id": form.order_id,
- "ipn_callback_url": f"{request.base_url}now-webhook",
- "success_url": f"{request.base_url}checkout/success",
- "cancel_url": f"{request.base_url}checkout",
- }
- data = NowPaymentsInvoice(**data)
- payment_url, np_order_id = await generate_payment_link(data)
- product_list = create_product_list(settings, cart.items)
- # -- save transaction to local-db
- customer_order = prepare_customer_order_data(
- settings.git_repo,
- settings.document_match,
- settings.document_exclude,
- settings.block_types,
- np_order_id,
- product_list,
- "NOW Payments",
- data.order_id,
- cart,
- shipping_info,
- )
- save_shipping_info(customer_order, settings.git_repo, settings.local_db.filepath)
- # -- redirect to NOWPayments Checkout page
- return payment_url
- @app.post("/now-webhook")
- async def now_webhook(request: Request, background_tasks: BackgroundTasks):
- """NOW Payments IPN (Instant Payment Notification) webhook.
- Send email after successful payment confirmation.
- """
- main_logger.info("now-webhook...")
- payload = await request.json()
- sig_header = request.headers["x-nowpayments-sig"]
- if os.getenv("ENV") == "production":
- ipn_secret_key = os.getenv("NOWPAYMENTS_IPN")
- else:
- ipn_secret_key = os.getenv("NOWPAYMENTS_SANDBOX_IPN")
- is_verified = ipn_signature_check(ipn_secret_key, sig_header, payload)
- if is_verified:
- # check payment status, update order if
- # status has changed (finished, expired, failed)
- # send email if status: finished.
- event_type = payload["payment_status"]
- if event_type in ["finished", "expired", "failed"]:
- if "payment_id" in payload:
- order_id = payload["order_id"]
- status_res = update_status_order(
- event_type,
- order_id,
- settings.git_repo,
- settings.local_db.filepath,
- )
- if status_res is False:
- raise HTTPException(
- status_code=400, detail="Order could not be updated."
- )
- if event_type == "finished":
- product_list = fetch_order_product_list(
- order_id, settings.git_repo, settings.local_db.filepath
- )
- if product_list:
- for product_info in product_list:
- try:
- product = read_file(
- settings.git_repo,
- f"products/{product_info.filename}",
- settings.document_match,
- settings.block_types,
- )
-
- # Get the variation ID based on size and style
- product_variation_id = get_variation_id(product,
- product_info.size,
- product_info.style)
-
- update_product_inventory(
- product_info.filename,
- product_info.quantity,
- product_variation_id,
- settings,
- )
- except Exception as e:
- main_logger.error(f"now-webhook error: Failed to update inventory: {e}")
- # Continue processing other products
- # -- send email
- try:
- email_settings = {
- "git_repo": settings.git_repo,
- "document_match": settings.document_match,
- "block_types": settings.block_types,
- }
- try:
- order = prepare_email_order(order_id, email_settings)
- await send_email(email_settings, order, background_tasks)
- except TypeError as e:
- main_logger.warning(f"now-webhook warning: Email preparation issue: {e}")
- except Exception as e:
- main_logger.error(f"now-webhook error: Failed to send email: {e}")
- elif event_type == "failed":
- main_logger.error(
- f"(NowPayments) Unhandled event type {event_type}"
- )
- raise HTTPException(
- status_code=400, detail="NOWPayments payment error."
- )
- else:
- main_logger.error("(NowPayments) payment_id not in payload")
- raise HTTPException(
- status_code=400, detail="NOWPayments payment error."
- )
- @app.get("/checkout/success", response_class=HTMLResponse)
- async def checkout_success(request: Request):
- """View that confirms to the user that the checkout procedure was successful."""
- session_data = request.session
- # if there's no cart session, redirect the user to `/`
- if "cart" not in session_data:
- clear_cart(session_data, settings.currency.default)
- return RedirectResponse(f"{request.base_url}", status_code=303)
- cart = CartSession(**request.session["cart"])
- filename = "orders"
- orders = read_file(
- settings.git_repo, filename, settings.document_match, settings.block_types
- )
- customer_order = [
- order for order in orders.blocks if order.order_id == cart.meta.order_id
- ]
- # if cart.meta.order_id does not match any order, redirect the user to `/`
- if len(customer_order) == 0:
- clear_cart(session_data, settings.currency.default)
- return RedirectResponse(f"{request.base_url}", status_code=303)
- # -- save data and clear cart, return view
- data = {
- "title": "Checkout Success",
- "order_id": cart.meta.order_id,
- "customer_email": customer_order[0].shipping_info.email,
- }
- # -- prepare order to xlsx format
- product_list = fetch_order_product_list(
- cart.meta.order_id, settings.git_repo, settings.local_db.filepath
- )
- if not product_list:
- raise HTTPException(status_code=403, detail="Order checkout is missing information.")
- products_shipping_meta = []
- for product_info in product_list:
- product = read_file(settings.git_repo, f"products/{product_info.filename}", settings.document_match, settings.block_types)
- products_shipping_meta.append(
- ProductShippingMeta(
- weight=product.meta.weight,
- customs_tariff_code=product.meta.customs_tariff_code,
- origin_country_iso=product.meta.origin_country_iso,
- )
- )
- email_from = os.getenv("MAIL_FROM")
- export_order_to_xlsx(
- customer_order[0],
- products_shipping_meta,
- settings.order_upload.filepath,
- settings.currency.default,
- settings.git_repo,
- email_from,
- )
- clear_cart(session_data, settings.currency.default)
- cart = CartSession(**request.session["cart"])
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- return templates.TemplateResponse(
- request,
- "checkout-success.html",
- {"request": request, "site": site, "data": data, "cart": cart},
- )
- @app.get("/checkout/error", response_class=HTMLResponse)
- async def checkout_error(request: Request):
- """View for when the checkout procedure fails."""
- cart = CartSession(**request.session["cart"])
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- data = {
- "title": "Checkout",
- }
- return templates.TemplateResponse(
- request,
- "checkout-error.html",
- {"request": request, "site": site, "data": data, "cart": cart},
- )
- @app.post("/cart/clear")
- async def cart_clear(
- request: Request,
- js: bool = False,
- ):
- """Clear cart."""
- request.session.clear()
- return RedirectResponse(request.base_url, status_code=303)
- # -- set custom HTTP errors
- @app.exception_handler(RequestValidationError)
- async def validation_exception_handler(request, exc):
- """Handle invalid data exception by returning an HTML response
- rather than a JSON one.
- """
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- message = "We can't process your request."
- main_logger.error(exc)
- t = templates.TemplateResponse(
- request,
- "error.html",
- {
- "request": request,
- "site": site,
- "data": {
- "title": "Error",
- "error": message,
- "status_code": 422,
- },
- "cart": None,
- },
- )
- return HTMLResponse(content=t.body, status_code=422)
- @app.exception_handler(StarletteHTTPException)
- async def http_exception_handler(request, exc):
- """Custom handling of HTTP exception (400-500).
- Prepare an appropriate user-facing message and pass it
- to the error.html template.
- """
- message = ""
- main_logger.error(f"http-exception: error => {exc}")
- if exc.status_code == 404:
- message = "Nothing was found here."
- elif exc.status_code == 500:
- message = "Server error."
- elif 400 <= exc.status_code <= 499:
- message = "Something went wrong."
- if exc.detail:
- message = exc.detail
- site = read_file(
- settings.git_repo,
- "site",
- settings.document_match,
- settings.block_types,
- )
- data = {
- "title": "Error",
- "error": message,
- "status_code": exc.status_code,
- }
- t = templates.TemplateResponse(
- request,
- "error.html",
- {
- "request": request,
- "site": site,
- "data": data,
- "cart": None,
- },
- )
- return HTMLResponse(content=t.body, status_code=exc.status_code)
|