Skip to content

Checkout Blueprint

This Python module configures a Flask blueprint named checkout_bp for handling all checkout and payment-related processes in a web application. Below is an explanation of the blueprint, its functions, and endpoints:

Overview:

  • Blueprint Name: checkout
  • Purpose: Manages checkout, payment tokenization, finalizing sales, managing free trials, and processing user downgrades.
  • Modules Used:
    • Flask for routing and response handling.
    • Requests for making HTTP requests to external APIs.
    • Dotenv for environment variable management.

Endpoints and Descriptions:

Tokenize Card

  • Function: tokenize_card
  • Purpose: Handles the tokenization of credit card data using an external payment gateway API.
  • Endpoint: Not directly exposed; used internally within other routes.

Checkout

  • Endpoint: /checkout
  • Method: POST
  • Description: Initiates a checkout process by tokenizing card details and processing the sale via an external payment gateway.

Process Sale

  • Endpoint: /checkout/final
  • Method: POST
  • Description: Finalizes the payment process by handling the response from the payment gateway. It returns a web page snippet with JavaScript to communicate the payment status to the client-side.

Complete Sale

  • Endpoint: /sale-complete
  • Method: POST
  • Description: Post-sales processing that updates the database with transaction details, sends a payment confirmation email, and optionally sends a welcome email for new subscriptions.

Request Downgrade

  • Endpoint: /downgrade
  • Method: POST
  • Description: Allows users to request a downgrade of their subscription. Sends an email notification regarding the downgrade request.

Free Trial Activation

  • Endpoint: /free-trial
  • Method: POST
  • Description: Manages the activation of a free trial by tokenizing the provided credit card data and updating the user’s subscription tier.

Free Trial Feedback

  • Endpoint: /free-trial-feedback
  • Method: POST
  • Description: Handles feedback submission for users on a free trial, sending detailed feedback to an admin email.

Functions and Utilities:

  • Tokenization and Sale Requests: Functions like tokenize_card and process_sale interact with external APIs to manage credit card tokenization and payment processing.
  • Email Notifications: Utilizes email templates from email_helper to send formatted emails for various scenarios like payment confirmation, subscription upgrades, and downgrade requests.
  • Firebase Functions: Utilizes functions like get_current_user and update_supplier_tier to interact with Firebase for user authentication and database updates.

Security:

  • Most endpoints require user authentication, ensuring that only logged-in users can initiate payments or modify subscription details.

Error Handling:

  • Robust error handling mechanisms are in place to manage potential issues during external API interactions or internal processing steps.

This module plays a critical role in managing financial transactions, user subscriptions, and service-level changes, ensuring secure and efficient operations within the application.